From 5068f8293f9734568c381951febd393d27e6bcf0 Mon Sep 17 00:00:00 2001 From: gnituy18 Date: Thu, 11 Jan 2018 17:49:56 +0800 Subject: [PATCH] Unify time format to every time input --- src/libs/Dad.js | 20 +++++++++++++------- src/libs/now.js | 7 +++---- src/utils/__tests__/unifyTimeFormat.spec.js | 8 ++++++++ src/utils/unifyTimeFormat.js | 5 +++++ 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 src/utils/__tests__/unifyTimeFormat.spec.js create mode 100644 src/utils/unifyTimeFormat.js diff --git a/src/libs/Dad.js b/src/libs/Dad.js index c189c9c..50dfdd9 100644 --- a/src/libs/Dad.js +++ b/src/libs/Dad.js @@ -1,7 +1,7 @@ -import suffix00 from '../utils/suffix00'; import toSec from '../utils/toSec'; import secToDate from '../utils/secToDate'; import isValidDateFormat from '../utils/isValidDateFormat'; +import unifyTimeFormat from '../utils/unifyTimeFormat'; const wm = new WeakMap(); @@ -9,15 +9,21 @@ export default class Dad { /** * @param {String | Number} dateOrSec */ - constructor(dateOrSec) { - if (!/^(string|number)$/.test(typeof dateOrSec)) { + constructor(inputValue) { + if (!/^(string|number)$/.test(typeof inputValue)) { throw new Error('Invaild type. it should be `string` | `number`'); } - if (typeof dateOrSec === 'string') { + let dateOrSec; + + if (typeof inputValue === 'string') { + dateOrSec = unifyTimeFormat(inputValue); + if (!isValidDateFormat(dateOrSec)) { throw new Error('Invaild date string format, Please pass `YYYY-MM-DD HH:mm:ss`'); } + } else { + dateOrSec = inputValue; } const p = wm.set(this, {}).get(this); @@ -32,7 +38,7 @@ export default class Dad { return secToDate(9)(p.dateOrSec); } - return toSec(new Date(`${suffix00(p.dateOrSec)}+09:00`)); + return toSec(new Date(`${p.dateOrSec}+09:00`)); } get tw() { @@ -42,7 +48,7 @@ export default class Dad { return secToDate(8)(p.dateOrSec); } - return toSec(new Date(`${suffix00(p.dateOrSec)}+08:00`)); + return toSec(new Date(`${p.dateOrSec}+08:00`)); } get hk() { @@ -56,6 +62,6 @@ export default class Dad { return secToDate(7)(p.dateOrSec); } - return toSec(new Date(`${suffix00(p.dateOrSec)}+07:00`)); + return toSec(new Date(`${p.dateOrSec}+07:00`)); } } diff --git a/src/libs/now.js b/src/libs/now.js index 998214c..a7c789c 100644 --- a/src/libs/now.js +++ b/src/libs/now.js @@ -1,7 +1,7 @@ -import suffix00 from '../utils/suffix00'; import qsParser from '../utils/qsParser'; import toSec from '../utils/toSec'; import isValidDateFormat from '../utils/isValidDateFormat'; +import unifyTimeFormat from '../utils/unifyTimeFormat'; let startTime; @@ -9,8 +9,7 @@ const now = () => { const currentTime = Date.now(); const qs = qsParser(); - const dadNow = qs['dad.now'] || qs['--dad.now']; - + const dadNow = unifyTimeFormat(qs['dad.now'] || qs['--dad.now']); if (dadNow && !isValidDateFormat(dadNow)) { throw new Error('Invalid time format in dad.now.'); @@ -22,7 +21,7 @@ const now = () => { startTime = currentTime; } - const timeInterval = dadNow ? startTime - +new Date(suffix00(dadNow)) : 0; + const timeInterval = dadNow ? startTime - +new Date(dadNow) : 0; const timeDuration = dadNow && !isAuto ? currentTime - startTime : 0; const sec = toSec(currentTime - timeInterval - timeDuration); diff --git a/src/utils/__tests__/unifyTimeFormat.spec.js b/src/utils/__tests__/unifyTimeFormat.spec.js new file mode 100644 index 0000000..4c1f72e --- /dev/null +++ b/src/utils/__tests__/unifyTimeFormat.spec.js @@ -0,0 +1,8 @@ +import unifyTimeFormat from '../unifyTimeFormat'; + +describe('unifyTimeFormat spec', () => { + it('change unify time format', () => { + const dateString = '2018-01-01 19:00'; + expect(unifyTimeFormat(dateString)).toBe('2018-01-01T19:00:00'); + }); +}); diff --git a/src/utils/unifyTimeFormat.js b/src/utils/unifyTimeFormat.js new file mode 100644 index 0000000..e50e3af --- /dev/null +++ b/src/utils/unifyTimeFormat.js @@ -0,0 +1,5 @@ +import suffix00 from './suffix00'; + +const unifyTimeFormat = (string = '') => suffix00(string.replace(' ', 'T')); + +export default unifyTimeFormat;