Commit 1e5787f1 by Chavee Issariyapat

initial commit

parents
node_modules
const ms = require('./ms');
// new Date('xxxx-xx-xx xx:xx:xx') assuem the input time is of a local timezone
// if want to specify timezone use --> new Date('xxxx-xx-xx xx:xx:xx+0:00')
module.exports.parse = function(tm) {
let out;
out = new Date(tm).getTime();
if (typeof(out)=='number' && !isNaN(out)) return out;
else {
let dur = ms(tm);
if (dur !== undefined) {
if (dur > 0) return Date.now() - dur; // 1h is the same as -1h
else return Date.now() + dur;
}
else {
return undefined;
}
}
}
\ No newline at end of file
/**
* ms module originally taken from
* https://github.com/vercel/ms/edit/master/index.js
*/
var s = 1000;
var m = s * 60;
var h = m * 60;
var d = h * 24;
var w = d * 7;
var y = d * 365.25;
/**
* Parse or format the given `val`.
*
* Options:
*
* - `long` verbose formatting [false]
*
* @param {String|Number} val
* @param {Object} [options]
* @throws {Error} throw an error if val is not a non-empty string or a number
* @return {String|Number}
* @api public
*/
module.exports = function (val, options) {
options = options || {};
var type = typeof val;
if (type === 'string' && val.length > 0) {
return parse(val);
} else if (type === 'number' && isFinite(val)) {
return options.long ? fmtLong(val) : fmtShort(val);
}
throw new Error(
'val is not a non-empty string or a valid number. val=' +
JSON.stringify(val)
);
};
function calulate_month(n) {
/* for future version */
return undefined;
}
/**
* Parse the given `str` and return milliseconds.
*
* @param {String} str
* @return {Number}
* @api private
*/
function parse(str) {
str = String(str);
if (str.length > 100) {
return;
}
var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y|months?|mos?|mths?)?$/i.exec(
str
);
//console.log({match})
if (!match) {
return;
}
var n = parseFloat(match[1]);
var type = (match[2] || 'ms').toLowerCase();
switch (type) {
case 'years':
case 'year':
case 'yrs':
case 'yr':
case 'y':
return n * y;
case 'weeks':
case 'week':
case 'w':
return n * w;
case 'days':
case 'day':
case 'd':
return n * d;
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
return n * h;
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
return n * m;
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
return n * s;
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
return n;
case 'months':
case 'month':
case 'mos':
case 'mo':
case 'mths':
case 'mth':
return calulate_month(match[1]);
default:
return undefined;
}
}
/**
* Short format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtShort(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return Math.round(ms / d) + 'd';
}
if (msAbs >= h) {
return Math.round(ms / h) + 'h';
}
if (msAbs >= m) {
return Math.round(ms / m) + 'm';
}
if (msAbs >= s) {
return Math.round(ms / s) + 's';
}
return ms + 'ms';
}
/**
* Long format for `ms`.
*
* @param {Number} ms
* @return {String}
* @api private
*/
function fmtLong(ms) {
var msAbs = Math.abs(ms);
if (msAbs >= d) {
return plural(ms, msAbs, d, 'day');
}
if (msAbs >= h) {
return plural(ms, msAbs, h, 'hour');
}
if (msAbs >= m) {
return plural(ms, msAbs, m, 'minute');
}
if (msAbs >= s) {
return plural(ms, msAbs, s, 'second');
}
return ms + ' ms';
}
/**
* Pluralization helper.
*/
function plural(ms, msAbs, n, name) {
var isPlural = msAbs >= n * 1.5;
return Math.round(ms / n) + ' ' + name + (isPlural ? 's' : '');
}
{
"name": "nexpietime",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha spec --require spec/helpers/chai.js --reporter spec --exit"
},
"repository": {
"type": "git",
"url": "https://dev.nexpie.com/npm/nexpietime"
},
"author": "",
"license": "ISC",
"dependencies": {
},
"devDependencies": {
"chai": "^4.2.0",
"chai-each": "0.0.1",
"chai-things": "^0.2.0",
"sinon": "^7.3.1"
}
}
var chai = require('chai');
chai.use(require('chai-things'));
chai.use(require('chai-each'));
chai.config.includeStack = false;
global.expect = chai.expect;
global.AssertionError = chai.AssertionError;
global.Assertion = chai.Assertion;
global.assert = chai.assert;
describe("nexpietime", function() {
const nexpietime = require('../index.js');
before(function(done) {
done();
});
it("should accept time in string format 2021-08-12 08:30:00", function(done) {
expect(nexpietime.parse('2021-08-12 08:30:00+7:00')).to.be.above(0);
done();
});
it("should take unix timestamp and return the same value", function(done) {
expect(nexpietime.parse(1628731800000)).to.equal(1628731800000);
done();
});
it("should return timestamp", function(done) {
expect(nexpietime.parse('2021-08-12 08:30:00+7:00')).to.equal(1628731800000);
expect(nexpietime.parse('1970-01-01 07:00:00+7:00')).to.equal(0);
expect(nexpietime.parse('1970-01-01 07:00:00+7')).to.equal(0);
done();
});
it("should accept relative time string 1h as 1 hour ago", function(done) {
expect(nexpietime.parse('1h')).to.equal(Date.now() - 60*60*1000);
expect(nexpietime.parse('-1h')).to.equal(Date.now() - 60*60*1000);
done();
});
it("should accept relative time string 1 h as 1 hour ago", function(done) {
expect(nexpietime.parse('1 h')).to.equal(Date.now() - 60*60*1000);
expect(nexpietime.parse('-1 h')).to.equal(Date.now() - 60*60*1000);
done();
});
it("should accept relative time string 0 h as now", function(done) {
expect(nexpietime.parse('0 h')).to.equal(Date.now());
expect(nexpietime.parse('-0 h')).to.equal(Date.now());
done();
});
it("should accept relative time string 1.5hrs as last 1.5 hour ago", function(done) {
expect(nexpietime.parse('1.5hrs')).to.equal(Date.now() - 1.5*60*60*1000);
expect(nexpietime.parse('-1.5hrs')).to.equal(Date.now() - 1.5*60*60*1000);
done();
});
it("should accept relative time string 2hours as 2 hours ago", function(done) {
expect(nexpietime.parse('2hours')).to.equal(Date.now() - 2*60*60*1000);
expect(nexpietime.parse('-2hours')).to.equal(Date.now() - 2*60*60*1000);
done();
});
it("should accept relative time string 1m as 1 minute ago", function(done) {
expect(nexpietime.parse('1m')).to.equal(Date.now() - 1*60*1000);
expect(nexpietime.parse('-1m')).to.equal(Date.now() - 1*60*1000);
done();
});
it("should accept relative time string 1s as 1 second ago", function(done) {
expect(nexpietime.parse('1s')).to.equal(Date.now() - 1*1000);
expect(nexpietime.parse('-1s')).to.equal(Date.now() - 1*1000);
done();
});
it("should accept relative time string 1d as 1 day ago", function(done) {
expect(nexpietime.parse('1d')).to.equal(Date.now() - 24*60*60*1000);
expect(nexpietime.parse('-1d')).to.equal(Date.now() - 24*60*60*1000);
done();
});
it("should accept relative time string 1d as 1 day ago", function(done) {
expect(nexpietime.parse('1d')).to.equal(Date.now() - 24*60*60*1000);
expect(nexpietime.parse('-1d')).to.equal(Date.now() - 24*60*60*1000);
done();
});
// it("should accept relative time string 1month as 1 month ago", function(done) {
// expect(nexpietime.parse('1month')).to.equal(Date.now() - 24*60*60*1000);
// expect(nexpietime.parse('-1month')).to.equal(Date.now() - 24*60*60*1000);
// done();
// });
// it("should accept relative time string 2month as 1 month ago", function(done) {
// expect(nexpietime.parse('2months')).to.equal(Date.now() - 24*60*60*1000);
// expect(nexpietime.parse('-2months')).to.equal(Date.now() - 24*60*60*1000);
// done();
// });
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment