diff --git a/package.json b/package.json index 1404f2166..b47add5ed 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,7 @@ "build": "cross-env BABEL_ENV=build node build && npm run size", "sauce": "npx karma start karma.sauce.conf.js", "test:sauce": "npm run sauce -- 0 && npm run sauce -- 1 && npm run sauce -- 2 && npm run sauce -- 3", - "size": "size-limit" + "size": "size-limit && gzip-size dayjs.min.js" }, "pre-commit": [ "lint" @@ -66,6 +66,7 @@ "eslint-config-airbnb-base": "^12.1.0", "eslint-plugin-import": "^2.10.0", "eslint-plugin-jest": "^21.15.0", + "gzip-size-cli": "^2.1.0", "jasmine-core": "^2.99.1", "jest": "^22.4.3", "karma": "^2.0.2", diff --git a/src/index.js b/src/index.js index 38f903f47..d86dd056e 100644 --- a/src/index.js +++ b/src/index.js @@ -156,8 +156,8 @@ class Dayjs { startOf(units, startOf) { // startOf -> endOf const isStartOf = !Utils.isUndefined(startOf) ? startOf : true const unit = Utils.prettyUnit(units) - const instanceFactory = (d, m, y = this.$y) => { - const ins = wrapper(new Date(y, m, d), this) + const instanceFactory = (d, m) => { + const ins = wrapper(new Date(this.$y, m, d), this) return isStartOf ? ins : ins.endOf(C.D) } const instanceFactorySet = (method, slice) => { @@ -171,13 +171,13 @@ class Dayjs { switch (unit) { case C.Y: return isStartOf ? instanceFactory(1, 0) : - instanceFactory(31, 11, this.$y) + instanceFactory(31, 11) case C.M: return isStartOf ? instanceFactory(1, this.$M) : - instanceFactory(0, this.$M + 1, this.$y) + instanceFactory(0, this.$M + 1) case C.W: return isStartOf ? instanceFactory(this.$D - this.$W, this.$M) : - instanceFactory(this.$D + (6 - this.$W), this.$M, this.$y) + instanceFactory(this.$D + (6 - this.$W), this.$M) case C.D: case C.DATE: return instanceFactorySet('setHours', 0) @@ -234,37 +234,31 @@ class Dayjs { add(number, units) { number = Number(number) // eslint-disable-line no-param-reassign - // units === 'ms' hard code here, will update in next release - const unit = (units && (units.length === 1 || units === 'ms')) ? units : Utils.prettyUnit(units) + const unit = Utils.prettyUnit(units) const instanceFactory = (u, n) => { const date = this.set(C.DATE, 1).set(u, n + number) return date.set(C.DATE, Math.min(this.$D, date.daysInMonth())) } - if (['M', C.M].indexOf(unit) > -1) { + if (unit === C.M) { return instanceFactory(C.M, this.$M) } - if (['y', C.Y].indexOf(unit) > -1) { + if (unit === C.Y) { return instanceFactory(C.Y, this.$y) } let step switch (unit) { - case 'm': case C.MIN: step = C.MILLISECONDS_A_MINUTE break - case 'h': case C.H: step = C.MILLISECONDS_A_HOUR break - case 'd': case C.D: step = C.MILLISECONDS_A_DAY break - case 'w': case C.W: step = C.MILLISECONDS_A_WEEK break - case 's': case C.S: step = C.MILLISECONDS_A_SECOND break diff --git a/src/utils.js b/src/utils.js index 692012706..f993ae9e4 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,3 +1,5 @@ +import * as C from './constant' + const padStart = (string, length, pad) => { const s = String(string) if (!s || s.length >= length) return string @@ -22,9 +24,21 @@ const monthDiff = (a, b) => { const absFloor = n => (n < 0 ? Math.ceil(n) || 0 : Math.floor(n)) -const prettyUnit = u => (u && String(u).toLowerCase().replace(/s$/, '')) +const prettyUnit = (u) => { + const special = { + M: C.M, + y: C.Y, + w: C.W, + d: C.D, + h: C.H, + m: C.MIN, + s: C.S, + ms: C.MS + } + return special[u] || String(u || '').toLowerCase().replace(/s$/, '') +} -const isUndefined = s => s === void 0 // eslint-disable-line no-void +const isUndefined = s => s === undefined export default { padStart, diff --git a/test/utils.test.js b/test/utils.test.js index e70623e79..5a4dbaa50 100644 --- a/test/utils.test.js +++ b/test/utils.test.js @@ -10,7 +10,7 @@ it('PrettyUnit', () => { expect(prettyUnit('Days')).toBe('day') expect(prettyUnit('days')).toBe('day') expect(prettyUnit('day')).toBe('day') - expect(prettyUnit()).toBe(undefined) + expect(prettyUnit()).toBe('') }) it('PadZoneStr', () => {