@intlify/core-base 11.4.7 → 11.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,5 +1,5 @@
1
1
  /*!
2
- * core-base v11.4.7
2
+ * core-base v11.4.8
3
3
  * (c) 2026 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -736,7 +736,7 @@ function getWarnMessage(code, ...args) {
736
736
  * Intlify core-base version
737
737
  * @internal
738
738
  */
739
- const VERSION = '11.4.7';
739
+ const VERSION = '11.4.8';
740
740
  const NOT_REOSLVED = -1;
741
741
  const DEFAULT_LOCALE = 'en-US';
742
742
  const MISSING_RESOLVE_VALUE = '';
@@ -957,9 +957,68 @@ function isImplicitFallback(targetLocale, locales) {
957
957
  }
958
958
  /* eslint-enable @typescript-eslint/no-explicit-any */
959
959
 
960
+ function resolveFormatLocale(context, key, locale, formats, missingWarn, fallbackWarn, type) {
961
+ const { fallbackLocale, localeFallbacker, onWarn } = context;
962
+ const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
963
+ fallbackLocale, locale);
964
+ for (let i = 0; i < locales.length; i++) {
965
+ const targetLocale = locales[i];
966
+ const format = (formats[targetLocale] || {})[key];
967
+ if (shared.isPlainObject(format) && shared.isString(targetLocale)) {
968
+ return targetLocale;
969
+ }
970
+ handleMissing(context, key, targetLocale, missingWarn, type);
971
+ }
972
+ return null;
973
+ }
974
+ function getFormatterCacheKey(locale, key, overrides) {
975
+ let id = `${locale}__${key}`;
976
+ if (shared.isPlainObject(overrides) && !shared.isEmptyObject(overrides)) {
977
+ id = `${id}__${JSON.stringify(overrides)}`;
978
+ }
979
+ return id;
980
+ }
981
+ function clearFormatCache(formatters, locale, format) {
982
+ for (const key in format) {
983
+ const prefix = `${locale}__${key}`;
984
+ for (const id of formatters.keys()) {
985
+ if (id === prefix || id.startsWith(`${prefix}__`)) {
986
+ formatters.delete(id);
987
+ }
988
+ }
989
+ }
990
+ }
991
+ function parseFormatArgs(args, options, initialOverrides, optionsKeys) {
992
+ const [, arg2, arg3, arg4] = args;
993
+ let overrides = initialOverrides;
994
+ if (shared.isString(arg2)) {
995
+ options.key = arg2;
996
+ }
997
+ else if (shared.isPlainObject(arg2)) {
998
+ Object.keys(arg2).forEach(key => {
999
+ if (optionsKeys.includes(key)) {
1000
+ overrides[key] = arg2[key];
1001
+ }
1002
+ else {
1003
+ options[key] = arg2[key];
1004
+ }
1005
+ });
1006
+ }
1007
+ if (shared.isString(arg3)) {
1008
+ options.locale = arg3;
1009
+ }
1010
+ else if (shared.isPlainObject(arg3)) {
1011
+ overrides = arg3;
1012
+ }
1013
+ if (shared.isPlainObject(arg4)) {
1014
+ overrides = arg4;
1015
+ }
1016
+ return overrides;
1017
+ }
1018
+
960
1019
  // implementation of `datetime` function
961
1020
  function datetime(context, ...args) {
962
- const { datetimeFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
1021
+ const { datetimeFormats, unresolving, onWarn } = context;
963
1022
  const { __datetimeFormatters } = context;
964
1023
  if (!shared.isString(args[0]) && !shared.isDate(args[0]) && !shared.isNumber(args[0])) {
965
1024
  return MISSING_RESOLVE_VALUE;
@@ -968,39 +1027,21 @@ function datetime(context, ...args) {
968
1027
  const missingWarn = shared.isBoolean(options.missingWarn)
969
1028
  ? options.missingWarn
970
1029
  : context.missingWarn;
971
- shared.isBoolean(options.fallbackWarn)
1030
+ const fallbackWarn = shared.isBoolean(options.fallbackWarn)
972
1031
  ? options.fallbackWarn
973
1032
  : context.fallbackWarn;
974
1033
  const part = !!options.part;
975
1034
  const locale = getLocale(context, options);
976
- const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
977
- fallbackLocale, locale);
978
1035
  if (!shared.isString(key) || key === '') {
979
1036
  const formatter = new Intl.DateTimeFormat(locale.replace(/!/g, ''), overrides);
980
1037
  return !part ? formatter.format(value) : formatter.formatToParts(value);
981
1038
  }
982
- // resolve format
983
- let datetimeFormat = {};
984
- let targetLocale;
985
- let format = null;
986
- const type = 'datetime format';
987
- for (let i = 0; i < locales.length; i++) {
988
- targetLocale = locales[i];
989
- datetimeFormat =
990
- datetimeFormats[targetLocale] || {};
991
- format = datetimeFormat[key];
992
- if (shared.isPlainObject(format))
993
- break;
994
- handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
995
- }
996
- // checking format and target locale
997
- if (!shared.isPlainObject(format) || !shared.isString(targetLocale)) {
1039
+ const targetLocale = resolveFormatLocale(context, key, locale, datetimeFormats, missingWarn, fallbackWarn, 'datetime format');
1040
+ if (!shared.isString(targetLocale)) {
998
1041
  return unresolving ? NOT_REOSLVED : key;
999
1042
  }
1000
- let id = `${targetLocale}__${key}`;
1001
- if (!shared.isEmptyObject(overrides)) {
1002
- id = `${id}__${JSON.stringify(overrides)}`;
1003
- }
1043
+ const format = datetimeFormats[targetLocale][key];
1044
+ const id = getFormatterCacheKey(targetLocale, key, overrides);
1004
1045
  let formatter = __datetimeFormatters.get(id);
1005
1046
  if (!formatter) {
1006
1047
  formatter = new Intl.DateTimeFormat(targetLocale, shared.assign({}, format, overrides));
@@ -1033,9 +1074,9 @@ const DATETIME_FORMAT_OPTIONS_KEYS = [
1033
1074
  ];
1034
1075
  /** @internal */
1035
1076
  function parseDateTimeArgs(...args) {
1036
- const [arg1, arg2, arg3, arg4] = args;
1077
+ const [arg1] = args;
1037
1078
  const options = shared.create();
1038
- let overrides = shared.create();
1079
+ const initialOverrides = shared.create();
1039
1080
  let value;
1040
1081
  if (shared.isString(arg1)) {
1041
1082
  // Only allow ISO strings - other date formats are often supported,
@@ -1072,45 +1113,18 @@ function parseDateTimeArgs(...args) {
1072
1113
  else {
1073
1114
  throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
1074
1115
  }
1075
- if (shared.isString(arg2)) {
1076
- options.key = arg2;
1077
- }
1078
- else if (shared.isPlainObject(arg2)) {
1079
- Object.keys(arg2).forEach(key => {
1080
- if (DATETIME_FORMAT_OPTIONS_KEYS.includes(key)) {
1081
- overrides[key] = arg2[key];
1082
- }
1083
- else {
1084
- options[key] = arg2[key];
1085
- }
1086
- });
1087
- }
1088
- if (shared.isString(arg3)) {
1089
- options.locale = arg3;
1090
- }
1091
- else if (shared.isPlainObject(arg3)) {
1092
- overrides = arg3;
1093
- }
1094
- if (shared.isPlainObject(arg4)) {
1095
- overrides = arg4;
1096
- }
1116
+ const overrides = parseFormatArgs(args, options, initialOverrides, DATETIME_FORMAT_OPTIONS_KEYS);
1097
1117
  return [options.key || '', value, options, overrides];
1098
1118
  }
1099
1119
  /** @internal */
1100
1120
  function clearDateTimeFormat(ctx, locale, format) {
1101
1121
  const context = ctx;
1102
- for (const key in format) {
1103
- const id = `${locale}__${key}`;
1104
- if (!context.__datetimeFormatters.has(id)) {
1105
- continue;
1106
- }
1107
- context.__datetimeFormatters.delete(id);
1108
- }
1122
+ clearFormatCache(context.__datetimeFormatters, locale, format);
1109
1123
  }
1110
1124
 
1111
1125
  // implementation of `number` function
1112
1126
  function number(context, ...args) {
1113
- const { numberFormats, unresolving, fallbackLocale, onWarn, localeFallbacker } = context;
1127
+ const { numberFormats, unresolving, onWarn } = context;
1114
1128
  const { __numberFormatters } = context;
1115
1129
  if (!shared.isNumber(args[0])) {
1116
1130
  return MISSING_RESOLVE_VALUE;
@@ -1119,39 +1133,21 @@ function number(context, ...args) {
1119
1133
  const missingWarn = shared.isBoolean(options.missingWarn)
1120
1134
  ? options.missingWarn
1121
1135
  : context.missingWarn;
1122
- shared.isBoolean(options.fallbackWarn)
1136
+ const fallbackWarn = shared.isBoolean(options.fallbackWarn)
1123
1137
  ? options.fallbackWarn
1124
1138
  : context.fallbackWarn;
1125
1139
  const part = !!options.part;
1126
1140
  const locale = getLocale(context, options);
1127
- const locales = localeFallbacker(context, // eslint-disable-line @typescript-eslint/no-explicit-any
1128
- fallbackLocale, locale);
1129
1141
  if (!shared.isString(key) || key === '') {
1130
1142
  const formatter = new Intl.NumberFormat(locale.replace(/!/g, ''), overrides);
1131
1143
  return !part ? formatter.format(value) : formatter.formatToParts(value);
1132
1144
  }
1133
- // resolve format
1134
- let numberFormat = {};
1135
- let targetLocale;
1136
- let format = null;
1137
- const type = 'number format';
1138
- for (let i = 0; i < locales.length; i++) {
1139
- targetLocale = locales[i];
1140
- numberFormat =
1141
- numberFormats[targetLocale] || {};
1142
- format = numberFormat[key];
1143
- if (shared.isPlainObject(format))
1144
- break;
1145
- handleMissing(context, key, targetLocale, missingWarn, type); // eslint-disable-line @typescript-eslint/no-explicit-any
1146
- }
1147
- // checking format and target locale
1148
- if (!shared.isPlainObject(format) || !shared.isString(targetLocale)) {
1145
+ const targetLocale = resolveFormatLocale(context, key, locale, numberFormats, missingWarn, fallbackWarn, 'number format');
1146
+ if (!shared.isString(targetLocale)) {
1149
1147
  return unresolving ? NOT_REOSLVED : key;
1150
1148
  }
1151
- let id = `${targetLocale}__${key}`;
1152
- if (!shared.isEmptyObject(overrides)) {
1153
- id = `${id}__${JSON.stringify(overrides)}`;
1154
- }
1149
+ const format = numberFormats[targetLocale][key];
1150
+ const id = getFormatterCacheKey(targetLocale, key, overrides);
1155
1151
  let formatter = __numberFormatters.get(id);
1156
1152
  if (!formatter) {
1157
1153
  formatter = new Intl.NumberFormat(targetLocale, shared.assign({}, format, overrides));
@@ -1184,47 +1180,20 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
1184
1180
  ];
1185
1181
  /** @internal */
1186
1182
  function parseNumberArgs(...args) {
1187
- const [arg1, arg2, arg3, arg4] = args;
1183
+ const [arg1] = args;
1188
1184
  const options = shared.create();
1189
- let overrides = shared.create();
1185
+ const initialOverrides = shared.create();
1190
1186
  if (!shared.isNumber(arg1)) {
1191
1187
  throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
1192
1188
  }
1193
1189
  const value = arg1;
1194
- if (shared.isString(arg2)) {
1195
- options.key = arg2;
1196
- }
1197
- else if (shared.isPlainObject(arg2)) {
1198
- Object.keys(arg2).forEach(key => {
1199
- if (NUMBER_FORMAT_OPTIONS_KEYS.includes(key)) {
1200
- overrides[key] = arg2[key];
1201
- }
1202
- else {
1203
- options[key] = arg2[key];
1204
- }
1205
- });
1206
- }
1207
- if (shared.isString(arg3)) {
1208
- options.locale = arg3;
1209
- }
1210
- else if (shared.isPlainObject(arg3)) {
1211
- overrides = arg3;
1212
- }
1213
- if (shared.isPlainObject(arg4)) {
1214
- overrides = arg4;
1215
- }
1190
+ const overrides = parseFormatArgs(args, options, initialOverrides, NUMBER_FORMAT_OPTIONS_KEYS);
1216
1191
  return [options.key || '', value, options, overrides];
1217
1192
  }
1218
1193
  /** @internal */
1219
1194
  function clearNumberFormat(ctx, locale, format) {
1220
1195
  const context = ctx;
1221
- for (const key in format) {
1222
- const id = `${locale}__${key}`;
1223
- if (!context.__numberFormatters.has(id)) {
1224
- continue;
1225
- }
1226
- context.__numberFormatters.delete(id);
1227
- }
1196
+ clearFormatCache(context.__numberFormatters, locale, format);
1228
1197
  }
1229
1198
 
1230
1199
  const DEFAULT_MODIFIER = (str) => str;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/core-base",
3
- "version": "11.4.7",
3
+ "version": "11.4.8",
4
4
  "description": "@intlify/core-base",
5
5
  "keywords": [
6
6
  "core",
@@ -33,9 +33,9 @@
33
33
  "jsdelivr": "dist/core-base.global.js",
34
34
  "types": "dist/core-base.d.ts",
35
35
  "dependencies": {
36
- "@intlify/devtools-types": "11.4.7",
37
- "@intlify/message-compiler": "11.4.7",
38
- "@intlify/shared": "11.4.7"
36
+ "@intlify/devtools-types": "11.4.8",
37
+ "@intlify/message-compiler": "11.4.8",
38
+ "@intlify/shared": "11.4.8"
39
39
  },
40
40
  "engines": {
41
41
  "node": ">= 22"