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

Commit

Permalink
Add new API format (#15)
Browse files Browse the repository at this point in the history
* disable `no-debugger`

* Add `format`

* Update README

* Add ISO Date format
  • Loading branch information
rwu823 authored Feb 26, 2018
1 parent c28a804 commit 307e74b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"jest": true
},
"rules": {
"no-debugger": "off",
"no-unused-expressions": [
2, { "allowTaggedTemplates": true }
]
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ import { tw } from '@17media/dad'
tw('2017-12-12')
// 1513036800

tw(1513036800)
tw(1514736000)
// {
// year: 2018,
// month: 0,
Expand All @@ -56,6 +56,8 @@ tw(1513036800)
// seconds: 0,
// day: 1
// }

tw(1514736000).format('YYYY/MM/DD hh:mm:ss') // 2018/01/01 00:00:00
```

## Valid date format
Expand Down
6 changes: 3 additions & 3 deletions src/libs/__tests__/Dad.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('Dad functionality', () => {
it('if input is second number', () => {
const CST0101 = new Date('2018-01-01 00:00:00+08:00') / 1000;

expect(tw(CST0101)).toEqual({
expect(tw(CST0101)).toMatchObject({
year: 2018,
month: 0,
date: 1,
Expand All @@ -53,7 +53,7 @@ describe('Dad functionality', () => {
seconds: 0,
day: 1,
});
expect(ja(CST0101)).toEqual({
expect(ja(CST0101)).toMatchObject({
year: 2018,
month: 0,
date: 1,
Expand All @@ -62,7 +62,7 @@ describe('Dad functionality', () => {
seconds: 0,
day: 1,
});
expect(indo(CST0101)).toEqual({
expect(indo(CST0101)).toMatchObject({
year: 2017,
month: 11,
date: 31,
Expand Down
13 changes: 13 additions & 0 deletions src/utils/__tests__/prefix0.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import prefix0 from '../prefix0';

describe('Test `prefix0` Spec', () => {
it('should be prefixed', () => {
expect(prefix0(9)).toBe('09');
expect(prefix0('9')).toBe('09');
});

it('shouldn\'t be prefixed ', () => {
expect(prefix0(10)).toBe('10');
expect(prefix0('10')).toBe('10');
});
});
24 changes: 22 additions & 2 deletions src/utils/__tests__/secToDate.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('`secToDate` Spec', () => {

expect(
secToDate(8)(s)
).toEqual({
).toMatchObject({
year: 2018,
month: 0,
date: 4,
Expand All @@ -22,7 +22,7 @@ describe('`secToDate` Spec', () => {

expect(
secToDate(9)(s)
).toEqual({
).toMatchObject({
year: 2018,
month: 0,
date: 4,
Expand All @@ -32,4 +32,24 @@ describe('`secToDate` Spec', () => {
day: 4,
});
});

it('test format feature', () => {
const s = new Date('2018-01-04 08:00:00+08:00') / 1000;

expect(
secToDate(9)(s).format('YYYY/MM/DD hh:mm:ss')
).toBe('2018/01/04 09:00:00');

expect(
secToDate(9)(s).format('D-M-YY h:m:s YMD')
).toBe('4-1-18 9:0:0 YMD');
});

it('test ISO date string', () => {
const s = new Date('2018-01-04 08:00:00+08:00') / 1000;

expect(
secToDate(9)(s).ISO
).toBe('2018-01-04T09:00:00');
});
});
11 changes: 11 additions & 0 deletions src/utils/prefix0.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const prefix0 = (stringOfNumber) => {
const n = String(stringOfNumber);

if (n.length < 2) {
return `0${n}`;
}

return n;
};

export default prefix0;
19 changes: 18 additions & 1 deletion src/utils/secToDate.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import prefix0 from './prefix0';

/**
* @param {Number} tz
* @return {Date}
Expand All @@ -7,7 +9,7 @@ const secToDate = tz => (sec = 0) => {

D.setTime(+D + (tz * 60 * 60 * 1000));

return {
const date = {
year: D.getUTCFullYear(),
month: D.getUTCMonth(),
date: D.getUTCDate(),
Expand All @@ -16,6 +18,21 @@ const secToDate = tz => (sec = 0) => {
seconds: D.getUTCSeconds(),
day: D.getUTCDay(),
};

date.format = (str = '') => str
.replace(/[yY]+/, Y => String(date.year).slice(0 - Y.length))
.replace(/M+/, (MM) => {
const mm = date.month + 1;
return (MM.length > 1 ? prefix0(mm) : mm);
})
.replace(/D+/, DD => (DD.length > 1 ? prefix0(date.date) : date.date))
.replace(/h+/, h => (h.length > 1 ? prefix0(date.hours) : date.hours))
.replace(/m+/, h => (h.length > 1 ? prefix0(date.minutes) : date.minutes))
.replace(/s+/, h => (h.length > 1 ? prefix0(date.seconds) : date.seconds));

date.ISO = date.format('YYYY-MM-DDThh:mm:ss');

return date;
};

export default secToDate;

0 comments on commit 307e74b

Please sign in to comment.