All files time.js

24.24% Statements 8/33
10% Branches 2/20
16.67% Functions 1/6
24.24% Lines 8/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 1501x 1x 1x                       8x 8x 21x   8x                                                                                                                                                                                                                                                     1x                
var units = [ 'seconds', 'minutes', 'hours', 'days', 'months', 'years' ],
	util = require( './util' ),
	limits = [ 1, 60, 3600, 86400, 2592000, 31536000 ];
 
/** @class time */
 
/**
 * Calculate the correct unit of timestamp
 * @memberof time
 * @instance
 * @param {number} timestampDelta
 * @return {{value: number, unit: string}}
 */
function timeAgo( timestampDelta ) {
	var i = 0;
	while ( i < limits.length && timestampDelta > limits[i + 1] ) {
		++i;
	}
	return {
		value: Math.round( timestampDelta / limits[i] ),
		unit: units[i]
	};
}
 
/**
 * Calculate the correct unit of timestamp delta
 * @memberof time
 * @instance
 * @param {number} timestamp
 * @return {{value: number, unit: string}}
 */
function getTimeAgoDelta( timestamp ) {
	var currentTimestamp = Math.round( new Date().getTime() / 1000 );
 
	return timeAgo( currentTimestamp - timestamp );
}
 
/**
 * Whether timestamp delta is less than a day old
 * @memberof time
 * @instance
 * @param {{value: number, unit: string}} delta Object of timestamp and its label
 * @return {boolean}
 */
function isRecent( delta ) {
	return [ 'seconds', 'minutes', 'hours' ].indexOf( delta.unit ) > -1;
}
 
/**
 * Is delta less than 10 seconds?
 * @memberof time
 * @instance
 * @param {{value: number, unit: string}} delta Object of timestamp and its label
 * @return {boolean}
 */
function isNow( delta ) {
	return delta.unit === 'seconds' && delta.value < 10;
}
 
/**
 * Return a message relating to the last modified relative time.
 * @memberof time
 * @instance
 * @param {number} ts timestamp
 * @param {string} username of the last user to modify the page
 * @param {string} [gender] of the last user to modify the page
 * @param {string} [historyUrl] url to the history page for the message, if omitted
 *  returns plain text string rather than html
 * @return {string}
 */
function getLastModifiedMessage( ts, username, gender, historyUrl ) {
	var delta, html,
		keys = {
			seconds: 'mobile-frontend-last-modified-with-user-seconds',
			minutes: 'mobile-frontend-last-modified-with-user-minutes',
			hours: 'mobile-frontend-last-modified-with-user-hours',
			days: 'mobile-frontend-last-modified-with-user-days',
			months: 'mobile-frontend-last-modified-with-user-months',
			years: 'mobile-frontend-last-modified-with-user-years'
		},
		args = [];
 
	gender = gender || 'unknown';
 
	delta = getTimeAgoDelta( ts );
	if ( isNow( delta ) ) {
		args.push( 'mobile-frontend-last-modified-with-user-just-now', gender, username );
	} else {
		args.push( keys[ delta.unit ], gender, username,
			mw.language.convertNumber( delta.value )
		);
	}
 
	args.push(
		historyUrl || '#',
		// Abuse PLURAL support to determine if the user is anonymous or not
		mw.language.convertNumber( username ? 1 : 0 ),
		// Our abuse of PLURAL support means we have to pass the relative URL
		// rather than construct it from a wikilink
		username ? mw.util.getUrl( 'User:' + username ) : ''
	);
	html = mw.message.apply( this, args ).parse();
	if ( historyUrl ) {
		return html;
	} else {
		return util.parseHTML( '<div>' ).html( html ).text();
	}
}
 
/**
 * Return a message relating to the registration date of the user
 * @memberof time
 * @instance
 * @param {string} ts timestamp
 * @param {string} [gender] of the last user editing this page
 * @return {string}
 */
function getRegistrationMessage( ts, gender ) {
	var delta, html,
		keys = {
			seconds: 'mobile-frontend-joined-seconds',
			minutes: 'mobile-frontend-joined-minutes',
			hours: 'mobile-frontend-joined-hours',
			days: 'mobile-frontend-joined-days',
			months: 'mobile-frontend-joined-months',
			years: 'mobile-frontend-joined-years'
		},
		args = [];
 
	gender = gender || 'unknown';
 
	delta = getTimeAgoDelta( parseInt( ts, 10 ) );
	if ( isNow( delta ) ) {
		args.push( 'mobile-frontend-joined-just-now', gender );
	} else {
		args.push( keys[ delta.unit ], gender, mw.language.convertNumber( delta.value ) );
	}
	html = mw.message.apply( this, args ).parse();
	return html;
}
 
module.exports = {
	getLastModifiedMessage: getLastModifiedMessage,
	getRegistrationMessage: getRegistrationMessage,
	timeAgo: timeAgo,
	getTimeAgoDelta: getTimeAgoDelta,
	isNow: isNow,
	isRecent: isRecent
};