Comparing version 0.0.1 to 0.0.2
@@ -1,4 +0,5 @@ | ||
function timeSince(timestamp, compareDate, timeChunk) { | ||
function timeSince(timestamp, compareDate, timeChunk, maxUnit) { | ||
var now = compareDate === undefined ? +new Date() : compareDate; | ||
var remaining = (timeChunk !== undefined) ? timeChunk : now - timestamp; | ||
maxUnit = maxUnit || "year"; | ||
var string = ""; | ||
@@ -17,3 +18,2 @@ var separator = ", "; | ||
var levels = [ | ||
{ plural: "years", singular: "year", ms: milli_per_year }, | ||
@@ -28,4 +28,6 @@ { plural: "months", singular: "month", ms: milli_per_month }, | ||
var crossedThreshold = false; | ||
for (var i=0; i < levels.length; i++) { | ||
if ( remaining < levels[i].ms ) continue; | ||
if ( maxUnit === levels[i].singular ) crossedThreshold = true; | ||
if ( remaining < levels[i].ms || !crossedThreshold ) continue; | ||
level++; | ||
@@ -32,0 +34,0 @@ var num = Math.floor( remaining / levels[i].ms ); |
{ | ||
"name": "bormat", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "a bunch of boring utility functions for formatting time and numbers, mostly.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,6 +17,23 @@ var assert = require('assert'); | ||
// describe("a timeSince formatter", function() { | ||
// | ||
// }); | ||
describe("a timeSince formatter", function() { | ||
it("should calculate the time since a given unix timestamp", function() { | ||
var now = +new Date(); | ||
var timestamp = now - 176400000; // 2 days and 1 hour | ||
assert.equal( bormat.timeSince(timestamp), "2 days, 1 hour", "timeSince did not work with one parameter" ) | ||
}); | ||
it("should be able to take a compare date in lieu of assuming the current timestamp", function() { | ||
var compare = +new Date() - 172800000; // 2 days from now | ||
var timestamp = compare - 3602000; // 1 hour and 2 seconds before compare | ||
assert.equal( bormat.timeSince(timestamp, compare), "1 hour, 2 seconds", "timeSince did not work with a provided compare date"); | ||
}); | ||
it("should be able to specify a max unit size", function() { | ||
var timestamp = +new Date() - 2419200000 - 604800000; | ||
assert.equal( bormat.timeSince(timestamp), "1 month, 1 week", "timeSince did not work with control test for max unit"); | ||
assert.equal( bormat.timeSince(timestamp, undefined, undefined, "day"), "35 days", "timeSince did not work with a max unit specified"); | ||
}); | ||
}); | ||
}) |
4482
76