Skip to content

Commit 29db149

Browse files
committed
fix: fix clone
1 parent 5ad890e commit 29db149

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,8 @@
4040
"rollup": "^0.57.1",
4141
"rollup-plugin-babel": "^4.0.0-beta.4",
4242
"rollup-plugin-uglify": "^3.0.0"
43+
},
44+
"dependencies": {
45+
"lodash": "^4.17.5"
4346
}
4447
}

src/index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cloneDeep } from 'lodash'
12
import * as Constant from './constant'
23

34
const padStart = (string, length, pad) => {
@@ -83,6 +84,9 @@ class Dayjs {
8384
case 'month':
8485
this.$date.setMonth(int)
8586
break
87+
case 'year':
88+
this.$date.setFullYear(int)
89+
break
8690
default:
8791
break
8892
}
@@ -179,7 +183,7 @@ class Dayjs {
179183
}
180184

181185
clone() {
182-
return Object.assign(Object.create(this), this)
186+
return cloneDeep(this)
183187
}
184188
}
185189

test/parse.test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,17 @@ test('String Other', () => {
2727
global.console.warn = jest.genMockFunction()// moment.js otherString will throw warn
2828
expect(dayjs('otherString').toString().toLowerCase()).toBe(moment('otherString').toString().toLowerCase())
2929
})
30+
31+
it('Clone not affect each other', () => {
32+
const base = dayjs()
33+
const year = base.year()
34+
const another = base.clone()
35+
another.set('year', year + 1)
36+
expect(another.unix() - base.unix()).toBe(31536000)
37+
})
38+
39+
it('Clone with same value', () => {
40+
const base = dayjs('20170101')
41+
const another = base.clone()
42+
expect(base.toString()).toBe(another.toString())
43+
})

0 commit comments

Comments
 (0)