@intlify/core-base 9.14.1 → 9.14.2

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 v9.14.1
2
+ * core-base v9.14.2
3
3
  * (c) 2024 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
@@ -241,7 +241,7 @@ function resolveWithKeyValue(obj, path) {
241
241
  *
242
242
  * @VueI18nGeneral
243
243
  */
244
- function resolveValue(obj, path) {
244
+ function resolveValue$1(obj, path) {
245
245
  // check object
246
246
  if (!shared.isObject(obj)) {
247
247
  return null;
@@ -334,7 +334,7 @@ function createMessageContext(options = {}) {
334
334
  const _list = options.list || [];
335
335
  const list = (index) => _list[index];
336
336
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
337
- const _named = options.named || {};
337
+ const _named = options.named || shared.create();
338
338
  shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
339
339
  const named = (key) => _named[key];
340
340
  function message(key) {
@@ -401,7 +401,7 @@ function createMessageContext(options = {}) {
401
401
  ["type" /* HelperNameMap.TYPE */]: type,
402
402
  ["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
403
403
  ["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
404
- ["values" /* HelperNameMap.VALUES */]: shared.assign({}, _list, _named)
404
+ ["values" /* HelperNameMap.VALUES */]: shared.assign(shared.create(), _list, _named)
405
405
  };
406
406
  return ctx;
407
407
  }
@@ -635,7 +635,7 @@ function appendItemToChain(chain, target, blocks) {
635
635
  * Intlify core-base version
636
636
  * @internal
637
637
  */
638
- const VERSION = '9.14.1';
638
+ const VERSION = '9.14.2';
639
639
  const NOT_REOSLVED = -1;
640
640
  const DEFAULT_LOCALE = 'en-US';
641
641
  const MISSING_RESOLVE_VALUE = '';
@@ -725,17 +725,17 @@ function createCoreContext(options = {}) {
725
725
  : _locale;
726
726
  const messages = shared.isPlainObject(options.messages)
727
727
  ? options.messages
728
- : { [_locale]: {} };
728
+ : createResources(_locale);
729
729
  const datetimeFormats = shared.isPlainObject(options.datetimeFormats)
730
730
  ? options.datetimeFormats
731
- : { [_locale]: {} }
731
+ : createResources(_locale)
732
732
  ;
733
733
  const numberFormats = shared.isPlainObject(options.numberFormats)
734
734
  ? options.numberFormats
735
- : { [_locale]: {} }
735
+ : createResources(_locale)
736
736
  ;
737
- const modifiers = shared.assign({}, options.modifiers || {}, getDefaultLinkedModifiers());
738
- const pluralRules = options.pluralRules || {};
737
+ const modifiers = shared.assign(shared.create(), options.modifiers, getDefaultLinkedModifiers());
738
+ const pluralRules = options.pluralRules || shared.create();
739
739
  const missing = shared.isFunction(options.missing) ? options.missing : null;
740
740
  const missingWarn = shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
741
741
  ? options.missingWarn
@@ -809,6 +809,7 @@ function createCoreContext(options = {}) {
809
809
  }
810
810
  return context;
811
811
  }
812
+ const createResources = (locale) => ({ [locale]: shared.create() });
812
813
  /** @internal */
813
814
  function isTranslateFallbackWarn(fallback, key) {
814
815
  return fallback instanceof RegExp ? fallback.test(key) : fallback;
@@ -860,10 +861,14 @@ function format(ast) {
860
861
  return msg;
861
862
  }
862
863
  function formatParts(ctx, ast) {
863
- const body = ast.b || ast.body;
864
- if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
864
+ const body = resolveBody(ast);
865
+ if (body == null) {
866
+ throw createUnhandleNodeError(0 /* NodeTypes.Resource */);
867
+ }
868
+ const type = resolveType(body);
869
+ if (type === 1 /* NodeTypes.Plural */) {
865
870
  const plural = body;
866
- const cases = plural.c || plural.cases;
871
+ const cases = resolveCases(plural);
867
872
  return ctx.plural(cases.reduce((messages, c) => [
868
873
  ...messages,
869
874
  formatMessageParts(ctx, c)
@@ -873,63 +878,132 @@ function formatParts(ctx, ast) {
873
878
  return formatMessageParts(ctx, body);
874
879
  }
875
880
  }
881
+ const PROPS_BODY = ['b', 'body'];
882
+ function resolveBody(node) {
883
+ return resolveProps(node, PROPS_BODY);
884
+ }
885
+ const PROPS_CASES = ['c', 'cases'];
886
+ function resolveCases(node) {
887
+ return resolveProps(node, PROPS_CASES, []);
888
+ }
876
889
  function formatMessageParts(ctx, node) {
877
- const _static = node.s || node.static;
878
- if (_static) {
890
+ const static_ = resolveStatic(node);
891
+ if (static_ != null) {
879
892
  return ctx.type === 'text'
880
- ? _static
881
- : ctx.normalize([_static]);
893
+ ? static_
894
+ : ctx.normalize([static_]);
882
895
  }
883
896
  else {
884
- const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
897
+ const messages = resolveItems(node).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
885
898
  return ctx.normalize(messages);
886
899
  }
887
900
  }
901
+ const PROPS_STATIC = ['s', 'static'];
902
+ function resolveStatic(node) {
903
+ return resolveProps(node, PROPS_STATIC);
904
+ }
905
+ const PROPS_ITEMS = ['i', 'items'];
906
+ function resolveItems(node) {
907
+ return resolveProps(node, PROPS_ITEMS, []);
908
+ }
888
909
  function formatMessagePart(ctx, node) {
889
- const type = node.t || node.type;
910
+ const type = resolveType(node);
890
911
  switch (type) {
891
912
  case 3 /* NodeTypes.Text */: {
892
- const text = node;
893
- return (text.v || text.value);
913
+ return resolveValue(node, type);
894
914
  }
895
915
  case 9 /* NodeTypes.Literal */: {
896
- const literal = node;
897
- return (literal.v || literal.value);
916
+ return resolveValue(node, type);
898
917
  }
899
918
  case 4 /* NodeTypes.Named */: {
900
919
  const named = node;
901
- return ctx.interpolate(ctx.named(named.k || named.key));
920
+ if (shared.hasOwn(named, 'k') && named.k) {
921
+ return ctx.interpolate(ctx.named(named.k));
922
+ }
923
+ if (shared.hasOwn(named, 'key') && named.key) {
924
+ return ctx.interpolate(ctx.named(named.key));
925
+ }
926
+ throw createUnhandleNodeError(type);
902
927
  }
903
928
  case 5 /* NodeTypes.List */: {
904
929
  const list = node;
905
- return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
930
+ if (shared.hasOwn(list, 'i') && shared.isNumber(list.i)) {
931
+ return ctx.interpolate(ctx.list(list.i));
932
+ }
933
+ if (shared.hasOwn(list, 'index') && shared.isNumber(list.index)) {
934
+ return ctx.interpolate(ctx.list(list.index));
935
+ }
936
+ throw createUnhandleNodeError(type);
906
937
  }
907
938
  case 6 /* NodeTypes.Linked */: {
908
939
  const linked = node;
909
- const modifier = linked.m || linked.modifier;
910
- return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
940
+ const modifier = resolveLinkedModifier(linked);
941
+ const key = resolveLinkedKey(linked);
942
+ return ctx.linked(formatMessagePart(ctx, key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
911
943
  }
912
944
  case 7 /* NodeTypes.LinkedKey */: {
913
- const linkedKey = node;
914
- return (linkedKey.v || linkedKey.value);
945
+ return resolveValue(node, type);
915
946
  }
916
947
  case 8 /* NodeTypes.LinkedModifier */: {
917
- const linkedModifier = node;
918
- return (linkedModifier.v || linkedModifier.value);
948
+ return resolveValue(node, type);
919
949
  }
920
950
  default:
921
- throw new Error(`unhandled node type on format message part: ${type}`);
951
+ throw new Error(`unhandled node on format message part: ${type}`);
952
+ }
953
+ }
954
+ const PROPS_TYPE = ['t', 'type'];
955
+ function resolveType(node) {
956
+ return resolveProps(node, PROPS_TYPE);
957
+ }
958
+ const PROPS_VALUE = ['v', 'value'];
959
+ function resolveValue(node, type) {
960
+ const resolved = resolveProps(node, PROPS_VALUE);
961
+ if (resolved) {
962
+ return resolved;
922
963
  }
964
+ else {
965
+ throw createUnhandleNodeError(type);
966
+ }
967
+ }
968
+ const PROPS_MODIFIER = ['m', 'modifier'];
969
+ function resolveLinkedModifier(node) {
970
+ return resolveProps(node, PROPS_MODIFIER);
971
+ }
972
+ const PROPS_KEY = ['k', 'key'];
973
+ function resolveLinkedKey(node) {
974
+ const resolved = resolveProps(node, PROPS_KEY);
975
+ if (resolved) {
976
+ return resolved;
977
+ }
978
+ else {
979
+ throw createUnhandleNodeError(6 /* NodeTypes.Linked */);
980
+ }
981
+ }
982
+ function resolveProps(node, props, defaultValue) {
983
+ for (let i = 0; i < props.length; i++) {
984
+ const prop = props[i];
985
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
986
+ if (shared.hasOwn(node, prop) && node[prop] != null) {
987
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
988
+ return node[prop];
989
+ }
990
+ }
991
+ return defaultValue;
992
+ }
993
+ function createUnhandleNodeError(type) {
994
+ return new Error(`unhandled node type: ${type}`);
923
995
  }
924
996
 
925
997
  const defaultOnCacheKey = (message) => message;
926
- let compileCache = Object.create(null);
998
+ let compileCache = shared.create();
927
999
  function clearCompileCache() {
928
- compileCache = Object.create(null);
1000
+ compileCache = shared.create();
1001
+ }
1002
+ function isMessageAST(val) {
1003
+ return (shared.isObject(val) &&
1004
+ resolveType(val) === 0 &&
1005
+ (shared.hasOwn(val, 'b') || shared.hasOwn(val, 'body')));
929
1006
  }
930
- const isMessageAST = (val) => shared.isObject(val) &&
931
- (val.t === 0 || val.type === 0) &&
932
- ('b' in val || 'body' in val);
933
1007
  function baseCompile(message, options = {}) {
934
1008
  // error detecting on compile
935
1009
  let detectError = false;
@@ -1047,7 +1121,7 @@ function translate(context, ...args) {
1047
1121
  : [
1048
1122
  key,
1049
1123
  locale,
1050
- messages[locale] || {}
1124
+ messages[locale] || shared.create()
1051
1125
  ];
1052
1126
  // NOTE:
1053
1127
  // Fix to work around `ssrTransfrom` bug in Vite.
@@ -1112,14 +1186,14 @@ function escapeParams(options) {
1112
1186
  function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
1113
1187
  const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
1114
1188
  const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
1115
- let message = {};
1189
+ let message = shared.create();
1116
1190
  let targetLocale;
1117
1191
  let format = null;
1118
1192
  const type = 'translate';
1119
1193
  for (let i = 0; i < locales.length; i++) {
1120
1194
  targetLocale = locales[i];
1121
1195
  message =
1122
- messages[targetLocale] || {};
1196
+ messages[targetLocale] || shared.create();
1123
1197
  if ((format = resolveValue(message, key)) === null) {
1124
1198
  // if null, resolve with object key path
1125
1199
  format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -1164,7 +1238,7 @@ function evaluateMessage(context, msg, msgCtx) {
1164
1238
  /** @internal */
1165
1239
  function parseTranslateArgs(...args) {
1166
1240
  const [arg1, arg2, arg3] = args;
1167
- const options = {};
1241
+ const options = shared.create();
1168
1242
  if (!shared.isString(arg1) &&
1169
1243
  !shared.isNumber(arg1) &&
1170
1244
  !isMessageFunction(arg1) &&
@@ -1335,8 +1409,8 @@ const DATETIME_FORMAT_OPTIONS_KEYS = [
1335
1409
  /** @internal */
1336
1410
  function parseDateTimeArgs(...args) {
1337
1411
  const [arg1, arg2, arg3, arg4] = args;
1338
- const options = {};
1339
- let overrides = {};
1412
+ const options = shared.create();
1413
+ let overrides = shared.create();
1340
1414
  let value;
1341
1415
  if (shared.isString(arg1)) {
1342
1416
  // Only allow ISO strings - other date formats are often supported,
@@ -1482,8 +1556,8 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
1482
1556
  /** @internal */
1483
1557
  function parseNumberArgs(...args) {
1484
1558
  const [arg1, arg2, arg3, arg4] = args;
1485
- const options = {};
1486
- let overrides = {};
1559
+ const options = shared.create();
1560
+ let overrides = shared.create();
1487
1561
  if (!shared.isNumber(arg1)) {
1488
1562
  throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
1489
1563
  }
@@ -1568,7 +1642,7 @@ exports.registerLocaleFallbacker = registerLocaleFallbacker;
1568
1642
  exports.registerMessageCompiler = registerMessageCompiler;
1569
1643
  exports.registerMessageResolver = registerMessageResolver;
1570
1644
  exports.resolveLocale = resolveLocale;
1571
- exports.resolveValue = resolveValue;
1645
+ exports.resolveValue = resolveValue$1;
1572
1646
  exports.resolveWithKeyValue = resolveWithKeyValue;
1573
1647
  exports.setAdditionalMeta = setAdditionalMeta;
1574
1648
  exports.setDevToolsHook = setDevToolsHook;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/core-base",
3
- "version": "9.14.1",
3
+ "version": "9.14.2",
4
4
  "description": "@intlify/core-base",
5
5
  "keywords": [
6
6
  "core",
@@ -33,12 +33,12 @@
33
33
  "jsdelivr": "dist/core-base.global.js",
34
34
  "types": "dist/core-base.d.ts",
35
35
  "dependencies": {
36
- "@intlify/shared": "9.14.1",
37
- "@intlify/message-compiler": "9.14.1"
36
+ "@intlify/message-compiler": "9.14.2",
37
+ "@intlify/shared": "9.14.2"
38
38
  },
39
39
  "devDependencies": {
40
- "@intlify/devtools-if": "9.14.1",
41
- "@intlify/vue-devtools": "9.14.1"
40
+ "@intlify/vue-devtools": "9.14.2",
41
+ "@intlify/devtools-if": "9.14.2"
42
42
  },
43
43
  "engines": {
44
44
  "node": ">= 16"