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

Commit

Permalink
2.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rwu823 committed Feb 26, 2018
2 parents 5f5c4ac + bb421c6 commit e12bcd9
Show file tree
Hide file tree
Showing 9 changed files with 433 additions and 324 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
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
{
"name": "@17media/dad",
"version": "2.1.0",
"version": "2.2.0",
"repository": "https://github.com/17media/dad.git",
"author": "Rocky Wu <rocky@17.media>",
"main": "dist/dad.js",
"license": "MIT",
"devDependencies": {
"@17media/eslint-config-17media": "^1.2.0",
"@types/jest": "^22.0.1",
"@types/jest": "^22.1.1",
"babel-cli": "^6.26.0",
"babel-eslint": "^8.2.1",
"babel-preset-env": "^1.6.1",
"eslint": "^4.15.0",
"eslint": "^4.16.0",
"eslint-plugin-import": "^2.8.0",
"eslint-plugin-react": "^7.5.1",
"eslint-plugin-react": "^7.6.1",
"gulp": "^3.9.1",
"gulp-rename": "^1.2.2",
"gulp-size": "^3.0.0",
"gulp-uglify-es": "^0.2.0",
"jest": "^22.0.4",
"pump": "^2.0.0",
"rollup": "^0.53.4",
"gulp-uglify-es": "^1.0.0",
"jest": "^22.1.4",
"pump": "^2.0.1",
"rollup": "^0.55.1",
"rollup-plugin-babel": "^3.0.3",
"sh-exec": "^0.1.1"
},
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;
Loading

0 comments on commit e12bcd9

Please sign in to comment.