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

Commit

Permalink
2.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
rwu823 committed Jan 11, 2018
2 parents ce3a2fc + dbc2a4d commit 4f2a37e
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@17media/dad",
"version": "2.0.0",
"version": "2.0.1",
"repository": "https://github.com/17media/dad.git",
"author": "Rocky Wu <[email protected]>",
"main": "dist/dad.js",
Expand Down
24 changes: 15 additions & 9 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 dateForamtRE = /^\d{4}-\d{2}-\d{2}([\sT]\d{2})?(:\d{2})?(:\d{2})?/;

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') {
if (!dateForamtRE.test(dateOrSec)) {
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`));
}
}
2 changes: 1 addition & 1 deletion src/libs/__tests__/Dad.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('Dad functionality', () => {

it('throw an error if date format is incorrect', () => {
expect(() => {
dad('2017/12/13');
dad('2017/13/32');
})
.toThrowError('Invaild date string format');
});
Expand Down
5 changes: 5 additions & 0 deletions src/libs/__tests__/now.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ describe('now functionality', () => {

expect(currentTime).toBe(DateGeneratedTime);
});

it('should throw an error when dad.now time format is invalid', () => {
process.argv = [null, null, '--foo', '17', '--dad.now=2017-12-12%adf2019:30', '--bar'];
expect(() => now()).toThrowError();
});
});
12 changes: 9 additions & 3 deletions src/libs/now.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
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.');
}

const isAuto = qs['--auto'] || qs.auto;

if (typeof startTime === 'undefined') {
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
13 changes: 13 additions & 0 deletions src/utils/__tests__/isValidDateFormat.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import isValidDateFormat from '../isValidDateFormat';

describe('`isValidDateFormat` Spec', () => {
it('return false if is invalid time format', () => {
const dataString = '2018-12-20%2019:00:00';
expect(isValidDateFormat(dataString)).toBe(false);
});

it('return true if is valid time format', () => {
const dataString = '2018-12-20 19:00:00';
expect(isValidDateFormat(dataString)).toBe(true);
});
});
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');
});
});
3 changes: 3 additions & 0 deletions src/utils/isValidDateFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const isValidDateFormat = dateString => !isNaN(+new Date(dateString));

export default isValidDateFormat;
2 changes: 1 addition & 1 deletion src/utils/qsParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const qsParser = (qstring = '') => {
const qs = qstring || (
isNode
? process.argv.slice(2).join('&')
: global.location.search.slice(1)
: decodeURIComponent(global.location.search.slice(1))
);

if (qs) {
Expand Down
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;

0 comments on commit 4f2a37e

Please sign in to comment.