mirror of
https://github.com/node-ebics/node-ebics-client.git
synced 2024-11-22 14:12:07 +00:00
Merge pull request #20 from eCollect/feat/drop-moment
Drop moment dependency
This commit is contained in:
commit
d21d89fb36
@ -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);
|
||||||
|
|
||||||
|
34
lib/utils.js
34
lib/utils.js
@ -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,
|
||||||
};
|
};
|
||||||
|
@ -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
29
test/unit/utils.js
Normal 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' } });
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user