Skip to content

Commit

Permalink
feat(format): add and subtract
Browse files Browse the repository at this point in the history
add time and sbtract time
  • Loading branch information
iamkun committed Apr 12, 2018
1 parent 09719f4 commit 6d529db
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const SECONDS_A_MINUTE = 60
export const SECONDS_A_HOUR = SECONDS_A_MINUTE * 60
export const SECONDS_A_DAY = SECONDS_A_HOUR * 24
export const SECONDS_A_WEEK = SECONDS_A_DAY * 7
32 changes: 32 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import * as Constant from './constant'

class Dayjs {
constructor(config) {
this.utc = false
Expand Down Expand Up @@ -49,6 +51,36 @@ class Dayjs {
}
}

add(number, string) {
let step
switch (string) {
case 'm':
case 'minutes':
step = Constant.SECONDS_A_MINUTE
break
case 'h':
case 'hours':
step = Constant.SECONDS_A_HOUR
break
case 'd':
case 'days':
step = Constant.SECONDS_A_DAY
break
case 'w':
case 'weeks':
step = Constant.SECONDS_A_WEEK
break
default: // s seconds
step = 1
}
const nextTimeStamp = this.unix() + (number * step)
return new Dayjs(nextTimeStamp * 1000)
}

subtract(number, string) {
return this.add(number * -1, string)
}

format(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') {
const weeks = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
const { date } = this
Expand Down
16 changes: 16 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,19 @@ test('StartOf Other', () => {
expect(dayjs().startOf('otherString').unix()).toBe(moment().startOf('otherString').unix())
})

test('Add Time days', () => {
expect(dayjs().add(1, 's').unix()).toBe(moment().add(1, 's').unix())
expect(dayjs().add(1, 'seconds').unix()).toBe(moment().add(1, 'seconds').unix())
expect(dayjs().add(1, 'm').unix()).toBe(moment().add(1, 'm').unix())
expect(dayjs().add(1, 'minutes').unix()).toBe(moment().add(1, 'minutes').unix())
expect(dayjs().add(1, 'h').unix()).toBe(moment().add(1, 'h').unix())
expect(dayjs().add(1, 'hours').unix()).toBe(moment().add(1, 'hours').unix())
expect(dayjs().add(1, 'w').unix()).toBe(moment().add(1, 'w').unix())
expect(dayjs().add(1, 'weeks').unix()).toBe(moment().add(1, 'weeks').unix())
expect(dayjs().add(1, 'd').unix()).toBe(moment().add(1, 'd').unix())
expect(dayjs().add(1, 'days').unix()).toBe(moment().add(1, 'days').unix())
})

test('Subtract Time days', () => {
expect(dayjs().subtract(1, 'days').unix()).toBe(moment().subtract(1, 'days').unix())
})
5 changes: 5 additions & 0 deletions test/parse.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ test('String 20130208', () => {
expect(dayjs('20130208').unix()).toBe(moment('20130208').unix())
})

test('String timestamp 1523520536000 ms', () => {
const timestamp = 1523520536000
expect(dayjs(timestamp).unix()).toBe(moment(timestamp).unix())
})

test('String Other', () => {
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
Expand Down

0 comments on commit 6d529db

Please sign in to comment.