From 508233cd370c81beb738419ce78898b0ad4686f8 Mon Sep 17 00:00:00 2001 From: iamkun <5992643+manre57@users.noreply.github.com> Date: Sat, 9 Jan 2021 15:49:09 +0800 Subject: [PATCH] fix: update devHelper to add dev warning setting locale before loading --- src/plugin/devHelper/index.js | 9 +++++++++ test/plugin/devHelper.test.js | 6 ++++++ 2 files changed, 15 insertions(+) diff --git a/src/plugin/devHelper/index.js b/src/plugin/devHelper/index.js index 5bace11..b4fb3c6 100644 --- a/src/plugin/devHelper/index.js +++ b/src/plugin/devHelper/index.js @@ -16,6 +16,15 @@ export default (o, c, d) => { } return oldParse.bind(this)(cfg) } + const oldLocale = d.locale + d.locale = function (preset, object, isLocal) { + if (typeof object === 'undefined' && typeof preset === 'string') { + if (!d.Ls[preset]) { + console.warn(`Guessing you may want to use locale ${preset}, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs`) + } + } + return oldLocale(preset, object, isLocal) + } } } diff --git a/test/plugin/devHelper.test.js b/test/plugin/devHelper.test.js index c0c6935..c69190a 100644 --- a/test/plugin/devHelper.test.js +++ b/test/plugin/devHelper.test.js @@ -32,3 +32,9 @@ it('Warning Enable customParseFormat plugin while passing the second format para dayjs('2020', 'YYYY') expect(consoleSpy).toHaveBeenCalledWith('To parse a date-time string like 2020 using the given format, you should enable customParseFormat plugin first. https://day.js.org/docs/en/parse/string-format') }) + +it('Warning: Setting locale before loading locale', () => { + const consoleSpy = jest.spyOn(console, 'warn') + dayjs.locale('zh-cn') + expect(consoleSpy).toHaveBeenCalledWith('Guessing you may want to use locale zh-cn, you have to load it before using it. https://day.js.org/docs/en/i18n/loading-into-nodejs') +})