Skip to content

Commit ebcb6d5

Browse files
authoredMay 31, 2018
fix(): bugfix, utils update and locale file update
chore bugfix && utils update Merge pull request #214 from iamkun/feature/iamkun
2 parents 2f9034e + fede35d commit ebcb6d5

File tree

4 files changed

+27
-18
lines changed

4 files changed

+27
-18
lines changed
 

‎package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"build": "cross-env BABEL_ENV=build node build && npm run size",
1111
"sauce": "npx karma start karma.sauce.conf.js",
1212
"test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2 && npm run sauce -- 3",
13-
"size": "size-limit"
13+
"size": "size-limit && gzip-size dayjs.min.js"
1414
},
1515
"pre-commit": [
1616
"lint"
@@ -66,6 +66,7 @@
6666
"eslint-config-airbnb-base": "^12.1.0",
6767
"eslint-plugin-import": "^2.10.0",
6868
"eslint-plugin-jest": "^21.15.0",
69+
"gzip-size-cli": "^2.1.0",
6970
"jasmine-core": "^2.99.1",
7071
"jest": "^22.4.3",
7172
"karma": "^2.0.2",

‎src/index.js

+8-14
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ class Dayjs {
156156
startOf(units, startOf) { // startOf -> endOf
157157
const isStartOf = !Utils.isUndefined(startOf) ? startOf : true
158158
const unit = Utils.prettyUnit(units)
159-
const instanceFactory = (d, m, y = this.$y) => {
160-
const ins = wrapper(new Date(y, m, d), this)
159+
const instanceFactory = (d, m) => {
160+
const ins = wrapper(new Date(this.$y, m, d), this)
161161
return isStartOf ? ins : ins.endOf(C.D)
162162
}
163163
const instanceFactorySet = (method, slice) => {
@@ -171,13 +171,13 @@ class Dayjs {
171171
switch (unit) {
172172
case C.Y:
173173
return isStartOf ? instanceFactory(1, 0) :
174-
instanceFactory(31, 11, this.$y)
174+
instanceFactory(31, 11)
175175
case C.M:
176176
return isStartOf ? instanceFactory(1, this.$M) :
177-
instanceFactory(0, this.$M + 1, this.$y)
177+
instanceFactory(0, this.$M + 1)
178178
case C.W:
179179
return isStartOf ? instanceFactory(this.$D - this.$W, this.$M) :
180-
instanceFactory(this.$D + (6 - this.$W), this.$M, this.$y)
180+
instanceFactory(this.$D + (6 - this.$W), this.$M)
181181
case C.D:
182182
case C.DATE:
183183
return instanceFactorySet('setHours', 0)
@@ -234,37 +234,31 @@ class Dayjs {
234234

235235
add(number, units) {
236236
number = Number(number) // eslint-disable-line no-param-reassign
237-
// units === 'ms' hard code here, will update in next release
238-
const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units)
237+
const unit = Utils.prettyUnit(units)
239238
const instanceFactory = (u, n) => {
240239
const date = this.set(C.DATE, 1).set(u, n + number)
241240
return date.set(C.DATE, Math.min(this.$D, date.daysInMonth()))
242241
}
243-
if (['M', C.M].indexOf(unit) > -1) {
242+
if (unit === C.M) {
244243
return instanceFactory(C.M, this.$M)
245244
}
246-
if (['y', C.Y].indexOf(unit) > -1) {
245+
if (unit === C.Y) {
247246
return instanceFactory(C.Y, this.$y)
248247
}
249248
let step
250249
switch (unit) {
251-
case 'm':
252250
case C.MIN:
253251
step = C.MILLISECONDS_A_MINUTE
254252
break
255-
case 'h':
256253
case C.H:
257254
step = C.MILLISECONDS_A_HOUR
258255
break
259-
case 'd':
260256
case C.D:
261257
step = C.MILLISECONDS_A_DAY
262258
break
263-
case 'w':
264259
case C.W:
265260
step = C.MILLISECONDS_A_WEEK
266261
break
267-
case 's':
268262
case C.S:
269263
step = C.MILLISECONDS_A_SECOND
270264
break

‎src/utils.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import * as C from './constant'
2+
13
const padStart = (string, length, pad) => {
24
const s = String(string)
35
if (!s || s.length >= length) return string
@@ -22,9 +24,21 @@ const monthDiff = (a, b) => {
2224

2325
const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n))
2426

25-
const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, ''))
27+
const prettyUnit = (u) => {
28+
const special = {
29+
M: C.M,
30+
y: C.Y,
31+
w: C.W,
32+
d: C.D,
33+
h: C.H,
34+
m: C.MIN,
35+
s: C.S,
36+
ms: C.MS
37+
}
38+
return special[u] || String(u || '').toLowerCase().replace(/s$/, '')
39+
}
2640

27-
const isUndefined = s => s === void 0 // eslint-disable-line no-void
41+
const isUndefined = s => s === undefined
2842

2943
export default {
3044
padStart,

‎test/utils.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ it('PrettyUnit', () => {
1010
expect(prettyUnit('Days')).toBe('day')
1111
expect(prettyUnit('days')).toBe('day')
1212
expect(prettyUnit('day')).toBe('day')
13-
expect(prettyUnit()).toBe(undefined)
13+
expect(prettyUnit()).toBe('')
1414
})
1515

1616
it('PadZoneStr', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.