vague-time
Advanced tools
Comparing version 0.2.1 to 0.2.3
{ | ||
"name": "vague-time", | ||
"version": "0.2.1", | ||
"version": "0.2.3", | ||
"author": "Phil Booth <pmbooth@gmail.com>", | ||
"description": "a small javascript library for formatting time differences as a vague time, e.g. 'just now' or '3 months ago'", | ||
"description": "A small javascript library for formatting time differences as a vague time, e.g. 'just now' or '3 months ago'.", | ||
"contributors": { | ||
@@ -7,0 +7,0 @@ "name": "Phil Booth", |
@@ -35,3 +35,3 @@ # vagueTime.js | ||
``` | ||
<script type="text/javascript" src=".../vagueTime/src/vagueTime.min.js"></script> | ||
<script type="text/javascript" src=".../vagueTime.js/src/vagueTime.min.js"></script> | ||
``` | ||
@@ -41,14 +41,16 @@ | ||
vagueTime.js exports a single public function, `get`, which returns | ||
a vague time string, such as 'just now' or '3 months ago', based on | ||
the argument(s) that you pass it. | ||
vagueTime.js exports a single public function, `get`, | ||
which returns a vague time string, | ||
such as 'just now' or '3 months ago', | ||
based on the argument(s) that you pass it. | ||
The arguments are passed as properties on a single options object. | ||
The property `from` is a timestamp denoting the point in time that | ||
you would like to convert to a vague time. The optional property | ||
`until` is a timestamp denoting the reference point from which you | ||
want to calculate the vague time difference, or it defaults to | ||
`Date.now()` if undefined. The optional property `units` is a | ||
string denoting the units that the `from` and `until` timestamps | ||
are specified in, either `'s'` for seconds or `'ms'` for milliseconds, | ||
The required property `from` is a timestamp, | ||
denoting the point in time that you would like to convert to a vague time. | ||
The optional property `until` is a timestamp, | ||
denoting the reference point from which you want to calculate the vague time difference, | ||
defaulting to `Date.now()` if undefined. | ||
The optional property `units` is a string, | ||
denoting the units that the `from` and `until` timestamps are specified in, | ||
either `'s'` for seconds or `'ms'` for milliseconds, | ||
defaulting to `'s'` if undefined. | ||
@@ -60,10 +62,17 @@ | ||
vagueTime.get({ | ||
from: 1171452690, | ||
until: 1234567890 | ||
}); // returns '2 years ago' | ||
from: 0, | ||
until: 60 | ||
}); // returns '1 minute ago' | ||
vagueTime.get({ | ||
from: 1231938091, | ||
until: 1234567890 | ||
}); // returns '4 weeks ago' | ||
from: 0, | ||
until: 7200 | ||
}); // returns '2 hours ago' | ||
vagueTime.get({ | ||
from: 0, | ||
until: 345600 | ||
}); // returns '4 days ago' | ||
vagueTime.get({ | ||
from: Date.now(), | ||
@@ -78,17 +87,21 @@ units: 'ms' | ||
The build environment relies on [Node.js][node], [NPM], [Jake], [JSHint], | ||
[Mocha], [Chai] and [UglifyJS]. Assuming that you already have Node.js | ||
and NPM set up, you just need to run `npm install` to install all of the | ||
dependencies as listed in `package.json`. | ||
The build environment relies on | ||
[Node.js][node], | ||
[NPM], | ||
[Jake], | ||
[JSHint], | ||
[Mocha], | ||
[Chai] and | ||
[UglifyJS]. | ||
Assuming that you already have Node.js and NPM set up, | ||
you just need to run `npm install` | ||
to install all of the dependencies as listed in `package.json`. | ||
### Unit tests | ||
The unit tests are in `test/vagueTime.js`. You can run them with the | ||
command `npm test` or `jake test`. | ||
The unit tests are in `test/vagueTime.js`. | ||
You can run them with the command `npm test` or `jake test`. | ||
[ci-image]: https://secure.travis-ci.org/philbooth/vagueTime.js.png?branch=master | ||
[ci-status]: http://travis-ci.org/#!/philbooth/vagueTime.js | ||
[onejs]: https://github.com/azer/onejs | ||
[browserify]: https://github.com/substack/node-browserify | ||
[ender]: http://ender.no.de/ | ||
[node]: http://nodejs.org/ | ||
@@ -95,0 +108,0 @@ [npm]: https://npmjs.org/ |
@@ -33,6 +33,5 @@ /** | ||
* based on a precise timestamp and an optional reference | ||
* timestamp. If the reference timestamp is not provided, | ||
* a default value of `Date.now()` is used instead. | ||
* timestamp. | ||
* | ||
* @param from {number} The timestamp to convert, in seconds. | ||
* @param from {number} The timestamp to convert to vague time. | ||
* @param [until] {number} The optional reference timestamp from | ||
@@ -54,6 +53,3 @@ * which to calculate the vague time, | ||
for (time in times) { | ||
if ( | ||
times.hasOwnProperty(time) && | ||
difference >= times[time] | ||
) { | ||
if (times.hasOwnProperty(time) && difference >= times[time]) { | ||
vagueTime = Math.floor(difference / times[time]); | ||
@@ -72,7 +68,7 @@ return vagueTime + ' ' + pluraliseNoun(time, vagueTime) + ' ago'; | ||
if (units !== 's' && units !== 'ms') { | ||
throw new Error('Invalid units'); | ||
if (units === 's' || units === 'ms') { | ||
return units; | ||
} | ||
return units; | ||
throw new Error('Invalid units'); | ||
} | ||
@@ -79,0 +75,0 @@ |
@@ -1,1 +0,1 @@ | ||
(function(){"use strict";function t(t){var s,o,u=n(t.units),a=r(t.from,u),f=r(t.until,u,Date.now()),l=f-a;for(s in e)if(e.hasOwnProperty(s)&&l>=e[s])return o=Math.floor(l/e[s]),o+" "+i(s,o)+" ago";return"just now"}function n(e){if(typeof e=="undefined")return"s";if(e!=="s"&&e!=="ms")throw new Error("Invalid units");return e}function r(e,t,n){if(typeof e=="undefined"&&typeof n=="number")return n;typeof e=="string"&&(e=parseInt(e,10));if(typeof e!="number"||isNaN(e))throw new Error("Invalid timestamp");return t==="s"?e*1e3:e}function i(e,t){return e+(t>1?"s":"")}var e={year:315576e5,month:26298e5,week:6048e5,day:864e5,hour:36e5,minute:6e4};typeof exports=="undefined"?window.vagueTime={get:t}:exports.get=t})(); | ||
(function(){"use strict";function t(t){var s,o,u=n(t.units),a=r(t.from,u),f=r(t.until,u,Date.now()),l=f-a;for(s in e)if(e.hasOwnProperty(s)&&l>=e[s])return o=Math.floor(l/e[s]),o+" "+i(s,o)+" ago";return"just now"}function n(e){if(typeof e=="undefined")return"s";if(e==="s"||e==="ms")return e;throw new Error("Invalid units")}function r(e,t,n){if(typeof e=="undefined"&&typeof n=="number")return n;typeof e=="string"&&(e=parseInt(e,10));if(typeof e!="number"||isNaN(e))throw new Error("Invalid timestamp");return t==="s"?e*1e3:e}function i(e,t){return e+(t>1?"s":"")}var e={year:315576e5,month:26298e5,week:6048e5,day:864e5,hour:36e5,minute:6e4};typeof exports=="undefined"?window.vagueTime={get:t}:exports.get=t})(); |
@@ -6,8 +6,10 @@ /*globals require, exports */ | ||
var assert = require('chai').assert; | ||
var assert = require('chai').assert, | ||
modulePath = '../src/vagueTime'; | ||
suite('vagueTime', function () { | ||
test('require does not throw', function () { | ||
assert.doesNotThrow(function () { | ||
require('../src/vagueTime'); | ||
require(modulePath); | ||
}); | ||
@@ -17,3 +19,3 @@ }); | ||
test('require returns object', function () { | ||
assert.isObject(require('../src/vagueTime')); | ||
assert.isObject(require(modulePath)); | ||
}); | ||
@@ -25,3 +27,3 @@ | ||
setup(function () { | ||
vagueTime = require('../src/vagueTime'); | ||
vagueTime = require(modulePath); | ||
}); | ||
@@ -28,0 +30,0 @@ |
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
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
24381
12
489
111
3