-
-
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 #1299 from iamkun/dev
D2M
- Loading branch information
Showing
14 changed files
with
431 additions
and
10 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
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
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,47 @@ | ||
// Plugin template from https://day.js.org/docs/en/plugin/plugin | ||
export default (option, dayjsClass) => { | ||
const oldParse = dayjsClass.prototype.parse | ||
dayjsClass.prototype.parse = function (cfg) { | ||
if (typeof cfg.date === 'string') { | ||
const locale = this.$locale() | ||
cfg.date = | ||
locale && locale.preparse ? locale.preparse(cfg.date) : cfg.date | ||
} | ||
// original parse result | ||
return oldParse.bind(this)(cfg) | ||
} | ||
|
||
// // overriding existing API | ||
// // e.g. extend dayjs().format() | ||
const oldFormat = dayjsClass.prototype.format | ||
dayjsClass.prototype.format = function (...args) { | ||
// original format result | ||
const result = oldFormat.call(this, ...args) | ||
// return modified result | ||
const locale = this.$locale() | ||
return locale && locale.postformat ? locale.postformat(result) : result | ||
} | ||
|
||
const oldFromTo = dayjsClass.prototype.fromToBase | ||
|
||
if (oldFromTo) { | ||
dayjsClass.prototype.fromToBase = function ( | ||
input, | ||
withoutSuffix, | ||
instance, | ||
isFrom | ||
) { | ||
const locale = this.$locale() || instance.$locale() | ||
|
||
// original format result | ||
return oldFromTo.call( | ||
this, | ||
input, | ||
withoutSuffix, | ||
instance, | ||
isFrom, | ||
locale && locale.postformat | ||
) | ||
} | ||
} | ||
} |
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,52 @@ | ||
import moment from 'moment' | ||
import MockDate from 'mockdate' | ||
import dayjs from '../../src' | ||
import relativeTime from '../../src/plugin/relativeTime' | ||
import preParsePostFormat from '../../src/plugin/preParsePostFormat' | ||
import localeData from '../../src/plugin/localeData' | ||
import '../../src/locale/ar' | ||
|
||
dayjs.extend(localeData) | ||
dayjs.extend(relativeTime) | ||
dayjs.extend(preParsePostFormat) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
it('Format Month with locale function', () => { | ||
for (let i = 0; i <= 7; i += 1) { | ||
const dayjsAR = dayjs().locale('ar').add(i, 'day') | ||
const momentAR = moment().locale('ar').add(i, 'day') | ||
const testFormat1 = 'DD MMMM YYYY MMM' | ||
const testFormat2 = 'MMMM' | ||
const testFormat3 = 'MMM' | ||
expect(dayjsAR.format(testFormat1)).toEqual(momentAR.format(testFormat1)) | ||
expect(dayjsAR.format(testFormat2)).toEqual(momentAR.format(testFormat2)) | ||
expect(dayjsAR.format(testFormat3)).toEqual(momentAR.format(testFormat3)) | ||
} | ||
}) | ||
|
||
it('Preparse with locale function', () => { | ||
for (let i = 0; i <= 7; i += 1) { | ||
dayjs.locale('ar') | ||
const momentAR = moment().locale('ar').add(i, 'day') | ||
expect(dayjs(momentAR.format()).format()).toEqual(momentAR.format()) | ||
} | ||
}) | ||
|
||
it('RelativeTime: Time from X gets formatted', () => { | ||
const T = [ | ||
[44.4, 'second', 'منذ ثانية واحدة'] | ||
] | ||
|
||
T.forEach((t) => { | ||
dayjs.locale('ar') | ||
expect(dayjs().from(dayjs().add(t[0], t[1]))) | ||
.toBe(t[2]) | ||
}) | ||
}) |
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,45 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import relativeTime from '../../src/plugin/relativeTime' | ||
import '../../src/locale/ja' | ||
|
||
dayjs.extend(relativeTime) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
it('Finnish locale relative time in past and future', () => { | ||
const cases = [ | ||
[1, 'd', '1日後'], | ||
[-1, 'd', '1日前'], | ||
[2, 'd', '2日後'], | ||
[-2, 'd', '2日前'], | ||
[10, 'd', '10日後'], | ||
[-10, 'd', '10日前'], | ||
[6, 'm', '6分後'], | ||
[-6, 'm', '6分前'], | ||
[5, 'h', '5時間後'], | ||
[-5, 'h', '5時間前'], | ||
[3, 'M', '3ヶ月後'], | ||
[-3, 'M', '3ヶ月前'], | ||
[4, 'y', '4年後'], | ||
[-4, 'y', '4年前'] | ||
] | ||
cases.forEach((c) => { | ||
expect(dayjs().add(c[0], c[1]).locale('ja').fromNow()) | ||
.toBe(c[2]) | ||
expect(dayjs().add(c[0], c[1]).locale('ja').fromNow()) | ||
.toBe(moment().add(c[0], c[1]).locale('ja').fromNow()) | ||
}) | ||
expect(dayjs().add(-10, 'd').locale('ja').fromNow(true)) | ||
.toBe('10日') | ||
expect(dayjs().add(-10, 'd').locale('ja').fromNow(true)) | ||
.toBe(moment().add(-10, 'd').locale('ja').fromNow(true)) | ||
}) | ||
|
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
Oops, something went wrong.