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 150 151 152 153 154 155 156 157 158 159 160 | 1x 1x 1x 8x 8x 21x 8x 1x | const units = [ 'seconds', 'minutes', 'hours', 'days', 'months', 'years' ], util = require( './util' ), limits = [ 1, 60, 3600, 86400, 2592000, 31536000 ]; /** * Calculate the correct unit of timestamp * * @memberof module:mobile.startup/time * @instance * @param {number} timestampDelta * @return {{value: number, unit: string}} */ function timeAgo( timestampDelta ) { let 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 module:mobile.startup/time * @param {number} timestamp * @return {{value: number, unit: string}} */ function getTimeAgoDelta( timestamp ) { const currentTimestamp = Math.round( Date.now() / 1000 ); return timeAgo( currentTimestamp - timestamp ); } /** * Whether timestamp delta is less than a day old * * @instance * @memberof module:mobile.startup/time * @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? * * @instance * @memberof module:mobile.startup/time * @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. * * @instance * @memberof module:mobile.startup/time * @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 (deprecated) * @return {string} */ function getLastModifiedMessage( ts, username, gender, historyUrl ) { const linkAll = typeof historyUrl === 'undefined', 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'; const 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 ) ); } const lastEditedElement = linkAll ? util.parseHTML( '<strong>' ).attr( 'class', 'last-modified-text-accent' ) : util.parseHTML( '<a>' ).attr( 'href', historyUrl || '#' ); const usernameElement = linkAll ? util.parseHTML( '<span>' ).attr( 'class', 'last-modified-text-accent' ) : util.parseHTML( '<a>' ).attr( 'href', mw.util.getUrl( 'User:' + username ) ); args.push( lastEditedElement, // 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 ? usernameElement : '' ); return mw.message.apply( this, args ).parse(); } /** * Return a message relating to the registration date of the user * * @instance * @memberof module:mobile.startup/time * @param {string} ts timestamp * @param {string} [gender] of the last user editing this page * @return {string} */ function getRegistrationMessage( ts, gender ) { const 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' }; const args = []; gender = gender || 'unknown'; const 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 ) ); } const html = mw.message.apply( this, args ).parse(); return html; } /** * Utility library for relative time. * * @exports module:mobile.startup/time */ module.exports = { getLastModifiedMessage, getRegistrationMessage, timeAgo, getTimeAgoDelta, isNow, isRecent }; |