vague-time
Advanced tools
Comparing version 2.1.0 to 2.2.0
{ | ||
"name": "vague-time", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"main": "./src/vagueTime.js", | ||
@@ -5,0 +5,0 @@ "ignore": [ |
{ | ||
"name": "vague-time", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A tiny library that formats precise time differences as a vague/fuzzy time.", | ||
@@ -5,0 +5,0 @@ "repo": "philbooth/vagueTime.js", |
{ | ||
"name": "vague-time", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "A tiny library that formats precise time differences as a vague/fuzzy time.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/philbooth/vagueTime.js", |
@@ -37,5 +37,5 @@ # vagueTime.js | ||
3.3 kb unminified with comments, | ||
1.1 kb minified or | ||
0.6 kb minified+gzipped. | ||
3.6 kb unminified with comments, | ||
1.2 kb minified or | ||
671 bytes minified+gzipped. | ||
@@ -77,2 +77,3 @@ ## What doesn't it do? | ||
* [countdown.js](http://countdownjs.org/) | ||
* [javascript-time-ago](https://github.com/halt-hammerzeit/javascript-time-ago) | ||
@@ -79,0 +80,0 @@ ## How do I install it? |
@@ -41,3 +41,3 @@ /** | ||
function getVagueTime (options) { | ||
var now, units, diff, action, fallback, time, value, plural; | ||
var now, units, diff, action, key, value, threshold, plural; | ||
@@ -47,31 +47,41 @@ now = Date.now(); | ||
diff = normaliseTime(options.from, units, now) - normaliseTime(options.to, units, now); | ||
plural = 's'; | ||
if (diff >= 0) { | ||
action = past; | ||
fallback = 'just now'; | ||
} else { | ||
diff = -diff; | ||
action = future; | ||
fallback = 'soon'; | ||
} | ||
for (time in times) { | ||
if (times.hasOwnProperty(time) && diff >= times[time]) { | ||
value = Math.floor(diff / times[time]); | ||
switch (value) { | ||
case 1: | ||
value = time === 'hour' ? 'an' : 'a'; | ||
plural = ''; | ||
break; | ||
case 2: | ||
value = 'a couple of'; | ||
default: | ||
plural = 's' | ||
for (key in times) { | ||
if (times.hasOwnProperty(key)) { | ||
value = times[key]; | ||
threshold = value / 4; | ||
if (diff < value / 2 - threshold) { | ||
continue; | ||
} | ||
return action(value, time + plural); | ||
if (diff < value - threshold) { | ||
value = 'half ' + singleValue(key); | ||
plural = ''; | ||
} else { | ||
value = Math.round(diff / value); | ||
switch (value) { | ||
case 1: | ||
value = singleValue(key); | ||
plural = ''; | ||
break; | ||
case 2: | ||
value = 'a couple of'; | ||
} | ||
} | ||
return action(value, key + plural); | ||
} | ||
} | ||
return fallback; | ||
return action.f; | ||
} | ||
@@ -122,2 +132,3 @@ | ||
} | ||
past.f = 'just now'; | ||
@@ -127,3 +138,8 @@ function future (value, units) { | ||
} | ||
future.f = 'soon'; | ||
function singleValue (time) { | ||
return time === 'hour' ? 'an' : 'a'; | ||
} | ||
function exportFunctions () { | ||
@@ -130,0 +146,0 @@ if (typeof define === 'function' && define.amd) { |
@@ -1,1 +0,1 @@ | ||
!function(e){"use strict";function n(e){var n,r,u,a,c,d,m,p;n=Date.now(),r=t(e.units),u=o(e.from,r,n)-o(e.to,r,n),u>=0?(a=i,c="just now"):(u=-u,a=f,c="soon");for(d in s)if(s.hasOwnProperty(d)&&u>=s[d]){switch(m=Math.floor(u/s[d])){case 1:m="hour"===d?"an":"a",p="";break;case 2:m="a couple of";default:p="s"}return a(m,d+p)}return c}function t(e){if("undefined"==typeof e)return"ms";if("s"===e||"ms"===e)return e;throw new Error("Invalid units")}function o(e,n,t){if("undefined"==typeof e)return t;if("string"==typeof e&&(e=parseInt(e,10)),r(e)&&u(e))throw new Error("Invalid time");return"number"==typeof e&&"s"===n&&(e*=1e3),e}function r(e){return"[object Date]"!==Object.prototype.toString.call(e)||isNaN(e.getTime())}function u(e){return"number"!=typeof e||isNaN(e)}function i(e,n){return e+" "+n+" ago"}function f(e,n){return"in "+e+" "+n}function a(){"function"==typeof define&&define.amd?define("vagueTime",["exports"],function(e){e.get=c.get}):"undefined"!=typeof module&&module&&module.exports?module.exports=c:e.vagueTime=c}var s,c;s={year:315576e5,month:26298e5,week:6048e5,day:864e5,hour:36e5,minute:6e4},c={get:n},a()}(this); | ||
!function(e){"use strict";function n(e){var n,o,i,s,d,m,p,l;n=Date.now(),o=t(e.units),i=r(e.from,o,n)-r(e.to,o,n),l="s",i>=0?s=u:(i=-i,s=f);for(d in c)if(c.hasOwnProperty(d)){if(m=c[d],p=m/4,m/2-p>i)continue;if(m-p>i)m="half "+a(d),l="";else switch(m=Math.round(i/m)){case 1:m=a(d),l="";break;case 2:m="a couple of"}return s(m,d+l)}return s.f}function t(e){if("undefined"==typeof e)return"ms";if("s"===e||"ms"===e)return e;throw new Error("Invalid units")}function r(e,n,t){if("undefined"==typeof e)return t;if("string"==typeof e&&(e=parseInt(e,10)),o(e)&&i(e))throw new Error("Invalid time");return"number"==typeof e&&"s"===n&&(e*=1e3),e}function o(e){return"[object Date]"!==Object.prototype.toString.call(e)||isNaN(e.getTime())}function i(e){return"number"!=typeof e||isNaN(e)}function u(e,n){return e+" "+n+" ago"}function f(e,n){return"in "+e+" "+n}function a(e){return"hour"===e?"an":"a"}function s(){"function"==typeof define&&define.amd?define("vagueTime",["exports"],function(e){e.get=d.get}):"undefined"!=typeof module&&module&&module.exports?module.exports=d:e.vagueTime=d}var c,d;c={year:315576e5,month:26298e5,week:6048e5,day:864e5,hour:36e5,minute:6e4},d={get:n},s(),u.f="just now",f.f="soon"}(this); |
@@ -41,3 +41,3 @@ /*globals require, chai */ | ||
test('get throws when from is bad string', function () { | ||
test('get throws when `from` is bad string', function () { | ||
assert.throws(function () { | ||
@@ -52,3 +52,3 @@ vagueTime.get({ | ||
test('get throws when until is bad string', function () { | ||
test('get throws when `to` is bad string', function () { | ||
assert.throws(function () { | ||
@@ -63,3 +63,3 @@ vagueTime.get({ | ||
test('get throws when units is bad string', function () { | ||
test('get throws when `units` is bad string', function () { | ||
assert.throws(function () { | ||
@@ -74,5 +74,13 @@ vagueTime.get({ | ||
test('get returns just now when time is 1 second ago', function () { | ||
test('get returns "just now" when time is identical', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567890, | ||
units: 's' | ||
}), 'just now'); | ||
}); | ||
test('get returns "just now" when time is 1 second ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567889, | ||
@@ -83,6 +91,6 @@ units: 's' | ||
test('get returns just now when time is 59 seconds ago', function () { | ||
test('get returns "just now" when time is 14 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567831, | ||
to: 1234567876, | ||
units: 's' | ||
@@ -92,214 +100,238 @@ }), 'just now'); | ||
test('get returns a minute ago when time is 60 seconds ago', function () { | ||
test('get returns "half a minute ago" when time is 15 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567830, | ||
to: 1234567875, | ||
units: 's' | ||
}), 'half a minute ago'); | ||
}); | ||
test('get returns "half a minute ago" when time is 44 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567846, | ||
units: 's' | ||
}), 'half a minute ago'); | ||
}); | ||
test('get returns "a minute ago" when time is 45 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567845, | ||
units: 's' | ||
}), 'a minute ago'); | ||
}); | ||
test('get returns 59 minutes ago when time is 3,599 seconds ago', function () { | ||
test('get returns "a minute ago" when time is 89 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234564291, | ||
to: 1234567801, | ||
units: 's' | ||
}), '59 minutes ago'); | ||
}), 'a minute ago'); | ||
}); | ||
test('get returns an hour ago when time is 3,600 seconds ago', function () { | ||
test('get returns "a couple of minutes ago" when time is 90 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234564290, | ||
to: 1234567800, | ||
units: 's' | ||
}), 'an hour ago'); | ||
}), 'a couple of minutes ago'); | ||
}); | ||
test('get returns 23 hours ago when time is 86,399 seconds ago', function () { | ||
test('get returns "a couple of minutes ago" when time is 149 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234481491, | ||
to: 1234567741, | ||
units: 's' | ||
}), '23 hours ago'); | ||
}), 'a couple of minutes ago'); | ||
}); | ||
test('get returns a day ago when time is 86,400 seconds ago', function () { | ||
test('get returns "3 minutes ago" when time is 150 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234481490, | ||
to: 1234567740, | ||
units: 's' | ||
}), 'a day ago'); | ||
}), '3 minutes ago'); | ||
}); | ||
test('get returns 6 days ago when time is 604,799 seconds ago', function () { | ||
test('get returns "14 minutes ago" when time is 869 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1233963091, | ||
to: 1234567021, | ||
units: 's' | ||
}), '6 days ago'); | ||
}), '14 minutes ago'); | ||
}); | ||
test('get returns a week ago when time is 604,800 seconds ago', function () { | ||
test('get returns "15 minutes ago" when time is 870 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1233963090, | ||
to: 1234567020, | ||
units: 's' | ||
}), 'a week ago'); | ||
}), '15 minutes ago'); | ||
}); | ||
test('get returns 4 weeks ago when time is 2,629,799 seconds ago', function () { | ||
test('get returns "15 minutes ago" when time is 899 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1231938091, | ||
to: 1234566991, | ||
units: 's' | ||
}), '4 weeks ago'); | ||
}), '15 minutes ago'); | ||
}); | ||
test('get returns a month ago when time is 2,629,800 seconds ago', function () { | ||
test('get returns "half an hour ago" when time is 900 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1231938090, | ||
to: 1234566990, | ||
units: 's' | ||
}), 'a month ago'); | ||
}), 'half an hour ago'); | ||
}); | ||
test('get returns 11 months ago when time is 31,557,599 seconds ago', function () { | ||
test('get returns "half an hour ago" when time is 2,699 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1203010291, | ||
to: 1234565191, | ||
units: 's' | ||
}), '11 months ago'); | ||
}), 'half an hour ago'); | ||
}); | ||
test('get returns a year ago when time is 31,557,600 seconds ago', function () { | ||
test('get returns "an hour ago" when time is 2,700 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1203010290, | ||
to: 1234565190, | ||
units: 's' | ||
}), 'a year ago'); | ||
}), 'an hour ago'); | ||
}); | ||
test('get returns a couple of years ago when time is 63,115,200 seconds ago', function () { | ||
test('get returns "an hour ago" when time is 5,399 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1171452690, | ||
to: 1234562491, | ||
units: 's' | ||
}), 'a couple of years ago'); | ||
}), 'an hour ago'); | ||
}); | ||
test('get returns just now when time is 59,000 milliseconds ago', function () { | ||
test('get returns "a couple of hours ago" when time is 5,400 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234567831000, | ||
units: 'ms' | ||
}), 'just now'); | ||
from: 1234567890, | ||
to: 1234562490, | ||
units: 's' | ||
}), 'a couple of hours ago'); | ||
}); | ||
test('get returns a minute ago when time is 60,000 milliseconds ago', function () { | ||
test('get returns "a couple of hours ago" when time is 8,999 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234567830000, | ||
units: 'ms' | ||
}), 'a minute ago'); | ||
from: 1234567890, | ||
to: 1234558891, | ||
units: 's' | ||
}), 'a couple of hours ago'); | ||
}); | ||
test('get returns 59 minutes ago when time is 3,599,000 milliseconds ago', function () { | ||
test('get returns "3 hours ago" when time is 9,000 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234564291000, | ||
units: 'ms' | ||
}), '59 minutes ago'); | ||
from: 1234567890, | ||
to: 1234558890, | ||
units: 's' | ||
}), '3 hours ago'); | ||
}); | ||
test('get returns an hour ago when time is 3,600,000 milliseconds ago', function () { | ||
test('get returns "6 hours ago" when time is 21,599 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234564290000, | ||
units: 'ms' | ||
}), 'an hour ago'); | ||
from: 1234567890, | ||
to: 1234546291, | ||
units: 's' | ||
}), '6 hours ago'); | ||
}); | ||
test('get returns 23 hours ago when time is 86,399,000 milliseconds ago', function () { | ||
test('get returns "half a day ago" when time is 21,600 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234481491000, | ||
units: 'ms' | ||
}), '23 hours ago'); | ||
from: 1234567890, | ||
to: 1234546290, | ||
units: 's' | ||
}), 'half a day ago'); | ||
}); | ||
test('get returns a day ago when time is 86,400,000 milliseconds ago', function () { | ||
test('get returns "half a day ago" when time is 64,799 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1234481490000, | ||
units: 'ms' | ||
from: 1234567890, | ||
to: 1234503091, | ||
units: 's' | ||
}), 'half a day ago'); | ||
}); | ||
test('get returns "a day ago" when time is 64,800 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234503090, | ||
units: 's' | ||
}), 'a day ago'); | ||
}); | ||
test('get returns 6 days ago when time is 604,799,000 milliseconds ago', function () { | ||
test('get returns "a day ago" when time is 129,599 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1233963091000, | ||
units: 'ms' | ||
}), '6 days ago'); | ||
from: 1234567890, | ||
to: 1234438291, | ||
units: 's' | ||
}), 'a day ago'); | ||
}); | ||
test('get returns a week ago when time is 604,800,000 milliseconds ago', function () { | ||
test('get returns "a couple of days ago" when time is 129,600 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1233963090000, | ||
units: 'ms' | ||
}), 'a week ago'); | ||
from: 1234567890, | ||
to: 1234438290, | ||
units: 's' | ||
}), 'a couple of days ago'); | ||
}); | ||
test('get returns 4 weeks ago when time is 2,629,799,000 milliseconds ago', function () { | ||
test('get returns "a week ago" when time is 604,800 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1231938091000, | ||
units: 'ms' | ||
}), '4 weeks ago'); | ||
from: 1234567890, | ||
to: 1233963090, | ||
units: 's' | ||
}), 'a week ago'); | ||
}); | ||
test('get returns a month ago when time is 2,629,800,000 milliseconds ago', function () { | ||
test('get returns "a month ago" when time is 2,629,800 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1231938090000, | ||
units: 'ms' | ||
from: 1234567890, | ||
to: 1231938090, | ||
units: 's' | ||
}), 'a month ago'); | ||
}); | ||
test('get returns 11 months ago when time is 31,557,599,000 milliseconds ago', function () { | ||
test('get returns "a year ago" when time is 31,557,600 seconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1203010291000, | ||
units: 'ms' | ||
}), '11 months ago'); | ||
from: 1234567890, | ||
to: 1203010290, | ||
units: 's' | ||
}), 'a year ago'); | ||
}); | ||
test('get returns a year ago when time is 31,557,600,000 milliseconds ago', function () { | ||
test('get returns "just now" when time is 14,999 milliseconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1203010290000, | ||
from: 14999, | ||
to: 0, | ||
units: 'ms' | ||
}), 'a year ago'); | ||
}), 'just now'); | ||
}); | ||
test('get returns a couple of years ago when time is 63,115,200,000 milliseconds ago', function () { | ||
test('get returns "half a minute ago" when time is 15,000 milliseconds ago', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890000, | ||
to: 1171452690000, | ||
from: 15000, | ||
to: 0, | ||
units: 'ms' | ||
}), 'a couple of years ago'); | ||
}), 'half a minute ago'); | ||
}); | ||
test('get returns just now when times are equal', function () { | ||
test('get returns "soon" when time is in 1 second', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567890, | ||
from: 0, | ||
to: 1, | ||
units: 's' | ||
}), 'just now'); | ||
}), 'soon'); | ||
}); | ||
test('get returns soon when time is 59 seconds ahead', function () { | ||
test('get returns "soon" when time is in 14 seconds', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567949, | ||
from: 0, | ||
to: 14, | ||
units: 's' | ||
@@ -309,8 +341,8 @@ }), 'soon'); | ||
test('get returns in a minute when time is 60 seconds ahead', function () { | ||
test('get returns "in half a minute" when time is 15 seconds ahead', function () { | ||
assert.equal(vagueTime.get({ | ||
from: 1234567890, | ||
to: 1234567950, | ||
from: 0, | ||
to: 15, | ||
units: 's' | ||
}), 'in a minute'); | ||
}), 'in half a minute'); | ||
}); | ||
@@ -335,3 +367,3 @@ | ||
assert.equal(vagueTime.get({ | ||
from: new Date(2012, 11, 16, 23, 59, 59), | ||
from: new Date(2012, 11, 15, 23, 59, 59), | ||
to: new Date(2012, 11, 15), | ||
@@ -342,3 +374,3 @@ units: 's' | ||
test('units defaults to milliseconds', function () { | ||
test('`units` defaults to milliseconds', function () { | ||
assert.equal(vagueTime.get({ | ||
@@ -350,3 +382,3 @@ from: 60000, | ||
test('from defaults to just now', function () { | ||
test('`from` defaults to just now', function () { | ||
assert.include(vagueTime.get({ | ||
@@ -358,3 +390,3 @@ to: Date.now(), | ||
test('until defaults to just now', function () { | ||
test('`to` defaults to just now', function () { | ||
assert.include(vagueTime.get({ | ||
@@ -361,0 +393,0 @@ to: Date.now(), |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26371
492
212