-
-
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.
fix: Add UpdateLocale plugin to update a locale's properties (#766)
- Loading branch information
Showing
22 changed files
with
236 additions
and
4 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
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
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
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,13 @@ | ||
export default (option, Dayjs, dayjs) => { | ||
dayjs.updateLocale = function (locale, customConfig) { | ||
const localeList = dayjs.Ls | ||
const localeConfig = localeList[locale] | ||
if (!localeConfig) return | ||
const customConfigKeys = customConfig ? Object.keys(customConfig) : [] | ||
customConfigKeys.forEach((c) => { | ||
localeConfig[c] = customConfig[c] | ||
}) | ||
return localeConfig // eslint-disable-line consistent-return | ||
} | ||
} | ||
|
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,71 @@ | ||
import MockDate from 'mockdate' | ||
import moment from 'moment' | ||
import dayjs from '../../src' | ||
import updateLocale from '../../src/plugin/updateLocale' | ||
import localizedFormat from '../../src/plugin/localizedFormat' | ||
import '../../src/locale/zh-cn' | ||
|
||
dayjs.extend(updateLocale) | ||
dayjs.extend(localizedFormat) | ||
|
||
beforeEach(() => { | ||
MockDate.set(new Date()) | ||
}) | ||
|
||
afterEach(() => { | ||
MockDate.reset() | ||
}) | ||
|
||
const newLocale = { | ||
months: new Array(12).fill('testMonth'), | ||
formats: { // formats for dayjs and longDateFormat for momentjs | ||
LT: '[testFormat]' | ||
}, | ||
longDateFormat: { | ||
LT: '[testFormat]' | ||
} | ||
} | ||
|
||
const formatString = 'MMMM LT' | ||
|
||
describe('Update locale', () => { | ||
it('Invalid argument', () => { | ||
const result = dayjs.updateLocale('InvalidLocaleName', {}) | ||
expect(result) | ||
.toEqual(undefined) | ||
expect(dayjs().format(formatString)) | ||
.toEqual(moment().format(formatString)) | ||
}) | ||
|
||
it('Return value', () => { | ||
const result1 = dayjs.updateLocale('en') | ||
expect(typeof result1).toEqual('object') | ||
const result2 = dayjs.updateLocale('en', {}) | ||
expect(typeof result2).toEqual('object') | ||
const result3 = dayjs.updateLocale('en', newLocale) | ||
expect(typeof result3).toEqual('object') | ||
}) | ||
|
||
it('Update build-in en locale', () => { | ||
moment.updateLocale('en', newLocale) | ||
dayjs.updateLocale('en', newLocale) | ||
|
||
expect(dayjs().format(formatString)) | ||
.toEqual('testMonth testFormat') | ||
|
||
expect(dayjs().format(formatString)) | ||
.toEqual(moment().format(formatString)) | ||
}) | ||
|
||
it('Update imported zh-cn locale', () => { | ||
moment.updateLocale('zh-cn', newLocale) | ||
dayjs.updateLocale('zh-cn', newLocale) | ||
dayjs.locale('zh-cn') | ||
moment.locale('zh-cn') | ||
expect(dayjs().format(formatString)) | ||
.toEqual('testMonth testFormat') | ||
|
||
expect(dayjs().format(formatString)) | ||
.toEqual(moment().format(formatString)) | ||
}) | ||
}) |
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,8 @@ | ||
import { PluginFunc } from 'dayjs' | ||
|
||
declare const plugin: PluginFunc | ||
export = plugin | ||
|
||
declare module 'dayjs' { | ||
export function updateLocale(localeName: String, customConfig: Object): any | ||
} |