Skip to content

Commit 1311395

Browse files
committed
fix: fixed broken locale support
1 parent a86eb4a commit 1311395

File tree

7 files changed

+203
-65
lines changed

7 files changed

+203
-65
lines changed

.browserslistrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
extends @ladjs/browserslist-config
1+
defaults, not ie 11

.dist.babelrc

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
"presets": [
33
["@babel/env", {
44
"targets": {
5-
"browsers": [ "extends @ladjs/browserslist-config" ]
5+
"browsers": [ "defaults, not ie 11" ]
66
}
77
}]
8-
]
8+
],
9+
"sourceType": "script",
10+
"sourceMaps": "inline",
11+
"comments": false
912
}

.dist.eslintrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
"no-undef": "off",
1515
"no-empty": "off",
1616
"no-redeclare": "off",
17-
"no-control-regex": "off"
17+
"no-control-regex": "off",
18+
"no-useless-escape": "off"
1819
},
1920
"settings": {
2021
"polyfills": [

.lib.babelrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
["@babel/env", {
44
"targets": {
55
"node": "14",
6-
"browsers": [ "extends @ladjs/browserslist-config" ]
6+
"browsers": [ "defaults, not ie 11" ]
77
}
88
}]
99
],

package.json

-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
"@babel/preset-env": "^7.18.6",
1717
"@commitlint/cli": "^17.0.3",
1818
"@commitlint/config-conventional": "^17.0.3",
19-
"@ladjs/browserslist-config": "^1.0.0",
2019
"ava": "^4.3.0",
2120
"babelify": "^10.0.0",
2221
"browserify": "^17.0.0",

src/index.js

+178-59
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,185 @@
1-
const { debuglog } = require('util');
2-
31
const dayjs = require('dayjs');
4-
const locales = require('dayjs/locale.json');
5-
6-
const debug = debuglog('dayjs-with-plugins');
7-
8-
const plugins = [
9-
'advancedFormat',
10-
'arraySupport',
11-
'badMutable',
12-
'buddhistEra',
13-
'calendar',
14-
'customParseFormat',
15-
'dayOfYear',
16-
'devHelper',
17-
'duration',
18-
'isBetween',
19-
'isLeapYear',
20-
'isMoment',
21-
'isSameOrAfter',
22-
'isSameOrBefore',
23-
'isToday',
24-
'isTomorrow',
25-
'isYesterday',
26-
'isoWeek',
27-
'isoWeeksInYear',
28-
'localeData',
29-
'localizedFormat',
30-
'minMax',
31-
'objectSupport',
32-
'pluralGetSet',
33-
'preParsePostFormat',
34-
'quarterOfYear',
35-
'relativeTime',
36-
'timezone',
37-
'toArray',
38-
'toObject',
39-
'updateLocale',
40-
'utc',
41-
'weekOfYear',
42-
'weekYear',
43-
'weekday'
44-
];
452

46-
for (const plugin of plugins) {
47-
try {
48-
dayjs.extend(require(`dayjs/plugin/${plugin}`));
49-
debug(`added ${plugin} plugin to dayjs with dayjs.extend`);
50-
} catch (err) {
51-
debug(err);
52-
}
53-
}
3+
dayjs.extend(require('dayjs/plugin/advancedFormat'));
4+
dayjs.extend(require('dayjs/plugin/arraySupport'));
5+
dayjs.extend(require('dayjs/plugin/badMutable'));
6+
dayjs.extend(require('dayjs/plugin/buddhistEra'));
7+
dayjs.extend(require('dayjs/plugin/calendar'));
8+
dayjs.extend(require('dayjs/plugin/customParseFormat'));
9+
dayjs.extend(require('dayjs/plugin/dayOfYear'));
10+
dayjs.extend(require('dayjs/plugin/devHelper'));
11+
dayjs.extend(require('dayjs/plugin/duration'));
12+
dayjs.extend(require('dayjs/plugin/isBetween'));
13+
dayjs.extend(require('dayjs/plugin/isLeapYear'));
14+
dayjs.extend(require('dayjs/plugin/isMoment'));
15+
dayjs.extend(require('dayjs/plugin/isSameOrAfter'));
16+
dayjs.extend(require('dayjs/plugin/isSameOrBefore'));
17+
dayjs.extend(require('dayjs/plugin/isToday'));
18+
dayjs.extend(require('dayjs/plugin/isTomorrow'));
19+
dayjs.extend(require('dayjs/plugin/isYesterday'));
20+
dayjs.extend(require('dayjs/plugin/isoWeek'));
21+
dayjs.extend(require('dayjs/plugin/isoWeeksInYear'));
22+
dayjs.extend(require('dayjs/plugin/localeData'));
23+
dayjs.extend(require('dayjs/plugin/localizedFormat'));
24+
dayjs.extend(require('dayjs/plugin/minMax'));
25+
dayjs.extend(require('dayjs/plugin/objectSupport'));
26+
dayjs.extend(require('dayjs/plugin/pluralGetSet'));
27+
dayjs.extend(require('dayjs/plugin/preParsePostFormat'));
28+
dayjs.extend(require('dayjs/plugin/quarterOfYear'));
29+
dayjs.extend(require('dayjs/plugin/relativeTime'));
30+
dayjs.extend(require('dayjs/plugin/timezone'));
31+
dayjs.extend(require('dayjs/plugin/toArray'));
32+
dayjs.extend(require('dayjs/plugin/toObject'));
33+
dayjs.extend(require('dayjs/plugin/updateLocale'));
34+
dayjs.extend(require('dayjs/plugin/utc'));
35+
dayjs.extend(require('dayjs/plugin/weekOfYear'));
36+
dayjs.extend(require('dayjs/plugin/weekYear'));
37+
dayjs.extend(require('dayjs/plugin/weekday'));
5438

5539
// load locales
5640
// <https://github.com/iamkun/dayjs/issues/1041>
57-
for (const locale of locales) {
58-
try {
59-
dayjs.locale(require(`dayjs/locale/${locale}`), null, true);
60-
debug(`added ${locale} locale to dayjs with dayjs.locale`);
61-
} catch (err) {
62-
debug(err);
63-
}
64-
}
41+
dayjs.locale(require('dayjs/locale/af'), null, true);
42+
dayjs.locale(require('dayjs/locale/ar-dz'), null, true);
43+
dayjs.locale(require('dayjs/locale/am'), null, true);
44+
dayjs.locale(require('dayjs/locale/ar-iq'), null, true);
45+
dayjs.locale(require('dayjs/locale/ar-kw'), null, true);
46+
dayjs.locale(require('dayjs/locale/ar-ly'), null, true);
47+
dayjs.locale(require('dayjs/locale/ar-ma'), null, true);
48+
dayjs.locale(require('dayjs/locale/ar-sa'), null, true);
49+
dayjs.locale(require('dayjs/locale/ar-tn'), null, true);
50+
dayjs.locale(require('dayjs/locale/ar'), null, true);
51+
dayjs.locale(require('dayjs/locale/az'), null, true);
52+
dayjs.locale(require('dayjs/locale/be'), null, true);
53+
dayjs.locale(require('dayjs/locale/bg'), null, true);
54+
dayjs.locale(require('dayjs/locale/bm'), null, true);
55+
dayjs.locale(require('dayjs/locale/bi'), null, true);
56+
dayjs.locale(require('dayjs/locale/bn-bd'), null, true);
57+
dayjs.locale(require('dayjs/locale/bn'), null, true);
58+
dayjs.locale(require('dayjs/locale/bo'), null, true);
59+
dayjs.locale(require('dayjs/locale/br'), null, true);
60+
dayjs.locale(require('dayjs/locale/bs'), null, true);
61+
dayjs.locale(require('dayjs/locale/ca'), null, true);
62+
dayjs.locale(require('dayjs/locale/cs'), null, true);
63+
dayjs.locale(require('dayjs/locale/cv'), null, true);
64+
dayjs.locale(require('dayjs/locale/cy'), null, true);
65+
dayjs.locale(require('dayjs/locale/da'), null, true);
66+
dayjs.locale(require('dayjs/locale/de-at'), null, true);
67+
dayjs.locale(require('dayjs/locale/de-ch'), null, true);
68+
dayjs.locale(require('dayjs/locale/de'), null, true);
69+
dayjs.locale(require('dayjs/locale/dv'), null, true);
70+
dayjs.locale(require('dayjs/locale/el'), null, true);
71+
dayjs.locale(require('dayjs/locale/en-au'), null, true);
72+
dayjs.locale(require('dayjs/locale/en-ca'), null, true);
73+
dayjs.locale(require('dayjs/locale/en-gb'), null, true);
74+
dayjs.locale(require('dayjs/locale/en-ie'), null, true);
75+
dayjs.locale(require('dayjs/locale/en-il'), null, true);
76+
dayjs.locale(require('dayjs/locale/en-in'), null, true);
77+
dayjs.locale(require('dayjs/locale/en-nz'), null, true);
78+
dayjs.locale(require('dayjs/locale/en-sg'), null, true);
79+
dayjs.locale(require('dayjs/locale/en-tt'), null, true);
80+
dayjs.locale(require('dayjs/locale/eo'), null, true);
81+
dayjs.locale(require('dayjs/locale/en'), null, true);
82+
dayjs.locale(require('dayjs/locale/es-do'), null, true);
83+
dayjs.locale(require('dayjs/locale/es-mx'), null, true);
84+
dayjs.locale(require('dayjs/locale/es'), null, true);
85+
dayjs.locale(require('dayjs/locale/et'), null, true);
86+
dayjs.locale(require('dayjs/locale/eu'), null, true);
87+
dayjs.locale(require('dayjs/locale/fa'), null, true);
88+
dayjs.locale(require('dayjs/locale/fi'), null, true);
89+
dayjs.locale(require('dayjs/locale/fo'), null, true);
90+
dayjs.locale(require('dayjs/locale/fr-ca'), null, true);
91+
dayjs.locale(require('dayjs/locale/fr-ch'), null, true);
92+
dayjs.locale(require('dayjs/locale/fr'), null, true);
93+
dayjs.locale(require('dayjs/locale/fy'), null, true);
94+
dayjs.locale(require('dayjs/locale/ga'), null, true);
95+
dayjs.locale(require('dayjs/locale/gd'), null, true);
96+
dayjs.locale(require('dayjs/locale/gl'), null, true);
97+
dayjs.locale(require('dayjs/locale/gom-latn'), null, true);
98+
dayjs.locale(require('dayjs/locale/gu'), null, true);
99+
dayjs.locale(require('dayjs/locale/he'), null, true);
100+
dayjs.locale(require('dayjs/locale/hi'), null, true);
101+
dayjs.locale(require('dayjs/locale/hr'), null, true);
102+
dayjs.locale(require('dayjs/locale/ht'), null, true);
103+
dayjs.locale(require('dayjs/locale/hu'), null, true);
104+
dayjs.locale(require('dayjs/locale/hy-am'), null, true);
105+
dayjs.locale(require('dayjs/locale/id'), null, true);
106+
dayjs.locale(require('dayjs/locale/it-ch'), null, true);
107+
dayjs.locale(require('dayjs/locale/is'), null, true);
108+
dayjs.locale(require('dayjs/locale/ja'), null, true);
109+
dayjs.locale(require('dayjs/locale/it'), null, true);
110+
dayjs.locale(require('dayjs/locale/ka'), null, true);
111+
dayjs.locale(require('dayjs/locale/jv'), null, true);
112+
dayjs.locale(require('dayjs/locale/kk'), null, true);
113+
dayjs.locale(require('dayjs/locale/km'), null, true);
114+
dayjs.locale(require('dayjs/locale/ko'), null, true);
115+
dayjs.locale(require('dayjs/locale/kn'), null, true);
116+
dayjs.locale(require('dayjs/locale/ku'), null, true);
117+
dayjs.locale(require('dayjs/locale/ky'), null, true);
118+
dayjs.locale(require('dayjs/locale/lb'), null, true);
119+
dayjs.locale(require('dayjs/locale/lo'), null, true);
120+
dayjs.locale(require('dayjs/locale/lv'), null, true);
121+
dayjs.locale(require('dayjs/locale/lt'), null, true);
122+
dayjs.locale(require('dayjs/locale/me'), null, true);
123+
dayjs.locale(require('dayjs/locale/mi'), null, true);
124+
dayjs.locale(require('dayjs/locale/mk'), null, true);
125+
dayjs.locale(require('dayjs/locale/ml'), null, true);
126+
dayjs.locale(require('dayjs/locale/mn'), null, true);
127+
dayjs.locale(require('dayjs/locale/mr'), null, true);
128+
dayjs.locale(require('dayjs/locale/ms-my'), null, true);
129+
dayjs.locale(require('dayjs/locale/ms'), null, true);
130+
dayjs.locale(require('dayjs/locale/mt'), null, true);
131+
dayjs.locale(require('dayjs/locale/my'), null, true);
132+
dayjs.locale(require('dayjs/locale/nb'), null, true);
133+
dayjs.locale(require('dayjs/locale/ne'), null, true);
134+
dayjs.locale(require('dayjs/locale/nl-be'), null, true);
135+
dayjs.locale(require('dayjs/locale/nn'), null, true);
136+
dayjs.locale(require('dayjs/locale/nl'), null, true);
137+
dayjs.locale(require('dayjs/locale/pa-in'), null, true);
138+
dayjs.locale(require('dayjs/locale/oc-lnc'), null, true);
139+
dayjs.locale(require('dayjs/locale/pt-br'), null, true);
140+
dayjs.locale(require('dayjs/locale/pl'), null, true);
141+
dayjs.locale(require('dayjs/locale/pt'), null, true);
142+
dayjs.locale(require('dayjs/locale/rn'), null, true);
143+
dayjs.locale(require('dayjs/locale/ro'), null, true);
144+
dayjs.locale(require('dayjs/locale/ru'), null, true);
145+
dayjs.locale(require('dayjs/locale/es-us'), null, true);
146+
dayjs.locale(require('dayjs/locale/es-pr'), null, true);
147+
dayjs.locale(require('dayjs/locale/sd'), null, true);
148+
dayjs.locale(require('dayjs/locale/si'), null, true);
149+
dayjs.locale(require('dayjs/locale/sk'), null, true);
150+
dayjs.locale(require('dayjs/locale/sl'), null, true);
151+
dayjs.locale(require('dayjs/locale/sq'), null, true);
152+
dayjs.locale(require('dayjs/locale/sr-cyrl'), null, true);
153+
dayjs.locale(require('dayjs/locale/sr'), null, true);
154+
dayjs.locale(require('dayjs/locale/ss'), null, true);
155+
dayjs.locale(require('dayjs/locale/sv-fi'), null, true);
156+
dayjs.locale(require('dayjs/locale/sv'), null, true);
157+
dayjs.locale(require('dayjs/locale/sw'), null, true);
158+
dayjs.locale(require('dayjs/locale/te'), null, true);
159+
dayjs.locale(require('dayjs/locale/ta'), null, true);
160+
dayjs.locale(require('dayjs/locale/tet'), null, true);
161+
dayjs.locale(require('dayjs/locale/tg'), null, true);
162+
dayjs.locale(require('dayjs/locale/tk'), null, true);
163+
dayjs.locale(require('dayjs/locale/tl-ph'), null, true);
164+
dayjs.locale(require('dayjs/locale/tlh'), null, true);
165+
dayjs.locale(require('dayjs/locale/th'), null, true);
166+
dayjs.locale(require('dayjs/locale/tr'), null, true);
167+
dayjs.locale(require('dayjs/locale/tzl'), null, true);
168+
dayjs.locale(require('dayjs/locale/tzm-latn'), null, true);
169+
dayjs.locale(require('dayjs/locale/tzm'), null, true);
170+
dayjs.locale(require('dayjs/locale/ug-cn'), null, true);
171+
dayjs.locale(require('dayjs/locale/uk'), null, true);
172+
dayjs.locale(require('dayjs/locale/ur'), null, true);
173+
dayjs.locale(require('dayjs/locale/uz-latn'), null, true);
174+
dayjs.locale(require('dayjs/locale/uz'), null, true);
175+
dayjs.locale(require('dayjs/locale/vi'), null, true);
176+
dayjs.locale(require('dayjs/locale/x-pseudo'), null, true);
177+
dayjs.locale(require('dayjs/locale/yo'), null, true);
178+
dayjs.locale(require('dayjs/locale/zh-cn'), null, true);
179+
dayjs.locale(require('dayjs/locale/zh-hk'), null, true);
180+
dayjs.locale(require('dayjs/locale/zh-tw'), null, true);
181+
dayjs.locale(require('dayjs/locale/zh'), null, true);
182+
dayjs.locale(require('dayjs/locale/rw'), null, true);
183+
dayjs.locale(require('dayjs/locale/se'), null, true);
65184

66185
module.exports = dayjs;

test/test.js

+16
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,19 @@ const dayjs = require('..');
55
test('exports dayjs function', (t) => {
66
t.true(typeof dayjs === 'function');
77
});
8+
9+
test('locale works for en', (t) => {
10+
t.is(dayjs().add(5, 'minute').fromNow(true), '5 minutes');
11+
});
12+
13+
test('locale works for es', (t) => {
14+
t.is(dayjs().add(5, 'minute').locale('es').fromNow(true), '5 minutos');
15+
});
16+
17+
test('locale works for en -> es -> en -> es -> es', (t) => {
18+
t.is(dayjs().add(5, 'minute').fromNow(true), '5 minutes');
19+
t.is(dayjs().add(5, 'minute').locale('es').fromNow(true), '5 minutos');
20+
t.is(dayjs().add(5, 'minute').fromNow(true), '5 minutes');
21+
t.is(dayjs().add(5, 'minute').locale('es').fromNow(true), '5 minutos');
22+
t.is(dayjs().add(5, 'minute').locale('es').fromNow(true), '5 minutos');
23+
});

0 commit comments

Comments
 (0)