From 33ac6ac60f0c4b1e4d5dda7765916a4db265cd85 Mon Sep 17 00:00:00 2001 From: nanov Date: Wed, 6 Nov 2019 16:51:59 +0200 Subject: [PATCH 1/3] feat: implement date handling and formatting --- lib/utils.js | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/utils.js b/lib/utils.js index 3390ead..85e7431 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -1,18 +1,46 @@ '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) => { if (start && end) return { DateRange: { - Start: start, - End: end, + Start: date.toISODate(start), + End: date.toISODate(end), }, }; return {}; }; - module.exports = { dateRange, + date, }; From 2a17ff6056e7300c637c2337ec73ce15fa5d7528 Mon Sep 17 00:00:00 2001 From: nanov Date: Wed, 6 Nov 2019 16:52:20 +0200 Subject: [PATCH 2/3] chore: write tests for date range --- test/unit/utils.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 test/unit/utils.js diff --git a/test/unit/utils.js b/test/unit/utils.js new file mode 100644 index 0000000..19eaf72 --- /dev/null +++ b/test/unit/utils.js @@ -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' } }); + }); + }); + }); +}); From df9c33041184dd9f0e57c9245d361cef6d8a85e1 Mon Sep 17 00:00:00 2001 From: nanov Date: Wed, 6 Nov 2019 16:52:31 +0200 Subject: [PATCH 3/3] feat: drop moment dependency --- lib/BankLetter.js | 6 +++--- package.json | 1 - 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/BankLetter.js b/lib/BankLetter.js index 5c77a3b..8a7bae6 100644 --- a/lib/BankLetter.js +++ b/lib/BankLetter.js @@ -2,15 +2,15 @@ const fs = require('fs'); -const moment = require('moment'); const handlebars = require('handlebars'); const Crypto = require('./crypto/Crypto'); +const { date } = require('./utils.js'); // const BN = require('bn.js'); 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); diff --git a/package.json b/package.json index 3c08956..1abbeaa 100644 --- a/package.json +++ b/package.json @@ -33,7 +33,6 @@ "bn.js": "^5.0.0", "handlebars": "^4.4.3", "js2xmlparser": "^4.0.0", - "moment": "^2.24.0", "node-forge": "^0.9.1", "request": "^2.88.0", "uuid": "^3.3.3",