Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore bugfix && utils update #214

Merged
merged 2 commits into from
May 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 8 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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)
Expand Down 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