-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from schiem/plugin
Added a plugin system.
- Loading branch information
Showing
4 changed files
with
120 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) | ||
}) |