Skip to content
This repository has been archived by the owner on Jan 6, 2023. It is now read-only.

Unify time format to every time input #11

Merged
merged 1 commit into from
Jan 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/libs/Dad.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,29 @@
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();
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);
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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`));
}
}
7 changes: 3 additions & 4 deletions src/libs/now.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
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;

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.');
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions src/utils/__tests__/unifyTimeFormat.spec.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
5 changes: 5 additions & 0 deletions src/utils/unifyTimeFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import suffix00 from './suffix00';

const unifyTimeFormat = (string = '') => suffix00(string.replace(' ', 'T'));

export default unifyTimeFormat;