Skip to content

Commit

Permalink
Merge pull request #92 from schiem/plugin
Browse files Browse the repository at this point in the history
Added a plugin system.
  • Loading branch information
iamkun authored May 7, 2018
2 parents ab67853 + 4f9eb23 commit b36bdcf
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
31 changes: 31 additions & 0 deletions plugins/advancedFormat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
let advFormat
export default advFormat = Dayjs => {
const oldFormat = Dayjs.prototype.format
Dayjs.prototype.format = function(formatStr = 'YYYY-MM-DDTHH:mm:ssZ') {
let oldResult = oldFormat.bind(this)(formatStr)

const suffixes = ['th', 'st', 'nd', 'rd']

return oldResult.replace(/Q|ddd|X|x|k{1,2}|S/g, (match) => {
switch (match) {
case 'Q':
return Math.ceil((this.$M + 1) / 3)
case 'ddd': {
const digits = this.Utils().padStart(String(this.$D), 2, '0').slice(-2)
const ls = parseInt(digits[1], 10)
return this.$D + suffixes[digits[0] === '1' || ls > 3 ? 0 : ls]
}
case 'k':
return this.$H === 0 ? 24 : this.$H
case 'kk':
return this.Utils().padStart(String(this.$H === 0 ? 24 : this.$H), 2, '0')
case 'X':
return Math.floor(this.$d.getTime() / 1000)
case 'x':
return this.$d.getTime()
default: //'S'
return this.Utils().padStart(String(this.$ms), 3, '0')
}
})
}
}
40 changes: 39 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export class Dayjs {
this.$L = locale || eng
}

// eslint-disable-next-line class-methods-use-this
Utils() {
return Utils
}

isValid() {
return !(this.$d.toString() === 'Invalid Date')
}
Expand Down Expand Up @@ -227,6 +232,7 @@ export class Dayjs {
return this.add(number * -1, string)
}


format(formatStr = this.$format, L = {}) {
const weeks = L.WEEKDAYS || this.$L.WEEKDAYS
const months = L.MONTHS || this.$L.MONTHS
Expand Down Expand Up @@ -275,8 +281,10 @@ export class Dayjs {
return Utils.padStart(this.$s, 2, '0')
case 'Z':
return `${this.$zoneStr.slice(0, -2)}:00`
default: // 'ZZ'
case 'ZZ':
return this.$zoneStr
default:
return match
}
})
}
Expand Down Expand Up @@ -368,4 +376,34 @@ export class Dayjs {
}
}



const dayjs = config => new Dayjs(config)
const applyExtend = (proto, factory) => {
factory.extend = (plugin, isNew = false) => { // eslint-disable-line no-param-reassign
// Return a new subclass instead of the original
if (isNew) {
// Extend the class
class PluginDayjs extends proto {}

// Apply the plugin
plugin(PluginDayjs)

// Make a new factory
const pluginFactory = config => new PluginDayjs(config)

// Apply this method, so the subclass can have more plugins
applyExtend(PluginDayjs, pluginFactory)
return pluginFactory
}

// Apply the plugin
plugin(Dayjs)
// Return the factory so it can be chained
return factory
}
}
applyExtend(Dayjs, dayjs)


export default dayjs
3 changes: 2 additions & 1 deletion test/display.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@ it('Format Minute m mm', () => {
expect(dayjs().format('mm')).toBe(moment().format('mm'))
})

it('Format Second s sss', () => {
it('Format Second s ss', () => {
expect(dayjs().format('s')).toBe(moment().format('s'))
expect(dayjs().format('ss')).toBe(moment().format('ss'))
})


it('Format Time Zone ZZ', () => {
MockDate.set(new Date('2018-05-02T23:00:00.000'), 60 * 8)
expect(dayjs().format('Z')).toBe(moment().format('Z'))
Expand Down
48 changes: 48 additions & 0 deletions test/plugin.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import moment from 'moment'
import MockDate from 'mockdate'
import dayjs from '../src'
import advFormat from '../plugins/advancedFormat'

dayjs.extend(advFormat)
const extendedDayjs = dayjs.extend(advFormat, true)

beforeEach(() => {
MockDate.set(new Date())
})

afterEach(() => {
MockDate.reset()
})

it('Format Quarter Q', () => {
expect(dayjs().format('Q')).toBe(moment().format('Q'))
expect(extendedDayjs().format('Q')).toBe(dayjs().format('Q'))
})

it('Format Timestamp X x', () => {
expect(dayjs().format('X')).toBe(moment().format('X'))
expect(dayjs().format('x')).toBe(moment().format('x'))

expect(extendedDayjs().format('X')).toBe(dayjs().format('X'))
expect(extendedDayjs().format('x')).toBe(dayjs().format('x'))
})

it('Format Day of Month ddd 1 - 31', () => {
expect(dayjs().format('ddd')).toBe(moment().format('Do'))

expect(extendedDayjs().format('ddd')).toBe(dayjs().format('ddd'))
})

it('Format Hour k kk 24-hour 1 - 24', () => {
expect(dayjs().format('k')).toBe(moment().format('k'))
expect(dayjs().format('kk')).toBe(moment().format('kk'))

expect(extendedDayjs().format('k')).toBe(dayjs().format('k'))
expect(extendedDayjs().format('kk')).toBe(dayjs().format('kk'))
})

it('Format Second S', () => {
expect(dayjs().format('S')).toBe(moment().format('SSS'))

expect(extendedDayjs().format('S')).toBe(dayjs().format('S'))
})

0 comments on commit b36bdcf

Please sign in to comment.