Skip to content

Commit

Permalink
refactor: update prettyUnit
Browse files Browse the repository at this point in the history
  • Loading branch information
iamkun committed May 31, 2018
1 parent e7afb35 commit fede35d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 13 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down
12 changes: 3 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 16 additions & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit fede35d

Please sign in to comment.