Skip to content

Commit

Permalink
Merge pull request #125 from xx45/feature/iamkun
Browse files Browse the repository at this point in the history
fix: fix add millisecond
  • Loading branch information
iamkun authored May 10, 2018
2 parents a544174 + 12c35df commit e00b943
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
9 changes: 8 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dayjs",
"version": "0.0.0-development",
"description": "",
"description": "2KB immutable date library alternative to Moment.js with the same modern API ",
"main": "dist/dayjs.min.js",
"types": "index.d.ts",
"scripts": {
Expand All @@ -23,6 +23,13 @@
"src/*"
]
},
"keywords": [
"dayjs",
"date",
"time",
"immutable",
"moment"
],
"author": "iamkun",
"license": "MIT",
"repository": {
Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,8 @@ class Dayjs {

add(number, units) {
number = Number(number) // eslint-disable-line no-param-reassign
const unit = (units && units.length === 1) ? units : Utils.prettyUnit(units)
// units === 'ms' hard code here, will update in next release
const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units)
if (['M', C.M].indexOf(unit) > -1) {
let date = this.set(C.DATE, 1).set(C.M, this.$M + number)
date = date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
Expand All @@ -202,8 +203,12 @@ class Dayjs {
case C.W:
step = C.MILLISECONDS_A_WEEK
break
default: // s seconds
case 's':
case C.S:
step = C.MILLISECONDS_A_SECOND
break
default: // ms
step = 1
}
const nextTimeStamp = this.valueOf() + (number * step)
return new Dayjs(nextTimeStamp)
Expand Down
2 changes: 2 additions & 0 deletions test/manipulate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ describe('StartOf EndOf', () => {


it('Add Time days', () => {
expect(dayjs().add(1, 'ms').valueOf()).toBe(moment().add(1, 'ms').valueOf())
expect(dayjs().add(1, 'milliseconds').valueOf()).toBe(moment().add(1, 'milliseconds').valueOf())
expect(dayjs().add(1, 's').valueOf()).toBe(moment().add(1, 's').valueOf())
expect(dayjs().add(1, 'seconds').valueOf()).toBe(moment().add(1, 'seconds').valueOf())
expect(dayjs().add(1, 'm').valueOf()).toBe(moment().add(1, 'm').valueOf())
Expand Down

0 comments on commit e00b943

Please sign in to comment.