Merge pull request #20 from eCollect/feat/drop-moment

Drop moment dependency
This commit is contained in:
Dimitar Nanov 2019-11-06 16:55:15 +02:00 committed by GitHub
commit d21d89fb36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 63 additions and 7 deletions

View File

@ -2,15 +2,15 @@
const fs = require('fs'); const fs = require('fs');
const moment = require('moment');
const handlebars = require('handlebars'); const handlebars = require('handlebars');
const Crypto = require('./crypto/Crypto'); const Crypto = require('./crypto/Crypto');
const { date } = require('./utils.js');
// const BN = require('bn.js'); // const BN = require('bn.js');
const registerHelpers = () => { const registerHelpers = () => {
handlebars.registerHelper('today', () => moment().format('DD.MM.YYYY')); handlebars.registerHelper('today', () => date.toISODate(Date.now(), false));
handlebars.registerHelper('now', () => moment().format('HH:mm:ss')); handlebars.registerHelper('now', () => date.toISOTime(Date.now(), false));
handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 8); handlebars.registerHelper('keyExponentBits', k => Buffer.byteLength(k.e()) * 8);

View File

@ -1,18 +1,46 @@
'use strict'; 'use strict';
const prefixNumber = (n) => {
if (n < 10)
return `0${n}`;
return n.toString();
};
const date = {
getDateObject(d = Date.now()) {
const dateObject = new Date(d);
// eslint-disable-next-line no-restricted-globals
if (isNaN(dateObject))
throw new Error(`${d} is invalid date.`);
return dateObject;
},
toISODate(d = Date.now(), utc = true) {
const t = date.getDateObject(d);
if (utc)
return `${t.getUTCFullYear()}-${prefixNumber(t.getUTCMonth() + 1)}-${prefixNumber(t.getUTCDate())}`;
return `${t.getFullYear()}-${prefixNumber(t.getMonth() + 1)}-${prefixNumber(t.getDate())}`;
},
toISOTime(d = Date.now(), utc = true) {
const t = date.getDateObject(d);
if (utc)
return `${prefixNumber(t.getUTCHours())}:${prefixNumber(t.getUTCMinutes())}:${prefixNumber(t.getUTCSeconds())}`;
return `${prefixNumber(t.getHours())}:${prefixNumber(t.getMinutes())}:${prefixNumber(t.getSeconds())}`;
},
};
const dateRange = (start, end) => { const dateRange = (start, end) => {
if (start && end) if (start && end)
return { return {
DateRange: { DateRange: {
Start: start, Start: date.toISODate(start),
End: end, End: date.toISODate(end),
}, },
}; };
return {}; return {};
}; };
module.exports = { module.exports = {
dateRange, dateRange,
date,
}; };

View File

@ -33,7 +33,6 @@
"bn.js": "^5.0.0", "bn.js": "^5.0.0",
"handlebars": "^4.4.3", "handlebars": "^4.4.3",
"js2xmlparser": "^4.0.0", "js2xmlparser": "^4.0.0",
"moment": "^2.24.0",
"node-forge": "^0.9.1", "node-forge": "^0.9.1",
"request": "^2.88.0", "request": "^2.88.0",
"uuid": "^3.3.3", "uuid": "^3.3.3",

29
test/unit/utils.js Normal file
View File

@ -0,0 +1,29 @@
'use strict';
/* eslint-env node, mocha */
const { assert } = require('chai');
const utils = require('../../lib/utils');
describe('utils', () => {
describe('dateRange', () => {
describe('dateRange', () => {
it('should generate empty object with partial parameters', () => {
assert.isEmpty(utils.dateRange());
});
it('should throw with invalid date', () => {
assert.throws(() => utils.dateRange('2018-15-15', '2018-20-20'));
});
it('should work for valid string input', () => {
assert.containsAllDeepKeys(utils.dateRange('2018-01-15', '2018-01-20'), { DateRange: { Start: '2018-01-15', End: '2018-01-20' } });
});
it('should work for Date string input', () => {
assert.containsAllDeepKeys(utils.dateRange(new Date('2018-01-15'), new Date('2018-01-20')), { DateRange: { Start: '2018-01-15', End: '2018-01-20' } });
});
it('should work for timestamp string input', () => {
assert.containsAllDeepKeys(utils.dateRange(new Date('2018-01-15').getTime(), new Date('2018-01-20').getTime()), { DateRange: { Start: '2018-01-15', End: '2018-01-20' } });
});
});
});
});