@intlify/core-base 10.0.4 → 10.0.6

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,6 +1,6 @@
1
1
  /*!
2
- * core-base v10.0.4
3
- * (c) 2024 kazuya kawaguchi
2
+ * core-base v10.0.6
3
+ * (c) 2025 kazuya kawaguchi
4
4
  * Released under the MIT License.
5
5
  */
6
6
  'use strict';
@@ -13,10 +13,14 @@ function format(ast) {
13
13
  return msg;
14
14
  }
15
15
  function formatParts(ctx, ast) {
16
- const body = ast.b || ast.body;
17
- if ((body.t || body.type) === 1 /* NodeTypes.Plural */) {
16
+ const body = resolveBody(ast);
17
+ if (body == null) {
18
+ throw createUnhandleNodeError(0 /* NodeTypes.Resource */);
19
+ }
20
+ const type = resolveType(body);
21
+ if (type === 1 /* NodeTypes.Plural */) {
18
22
  const plural = body;
19
- const cases = plural.c || plural.cases;
23
+ const cases = resolveCases(plural);
20
24
  return ctx.plural(cases.reduce((messages, c) => [
21
25
  ...messages,
22
26
  formatMessageParts(ctx, c)
@@ -26,63 +30,132 @@ function formatParts(ctx, ast) {
26
30
  return formatMessageParts(ctx, body);
27
31
  }
28
32
  }
33
+ const PROPS_BODY = ['b', 'body'];
34
+ function resolveBody(node) {
35
+ return resolveProps(node, PROPS_BODY);
36
+ }
37
+ const PROPS_CASES = ['c', 'cases'];
38
+ function resolveCases(node) {
39
+ return resolveProps(node, PROPS_CASES, []);
40
+ }
29
41
  function formatMessageParts(ctx, node) {
30
- const _static = node.s || node.static;
31
- if (_static != null) {
42
+ const static_ = resolveStatic(node);
43
+ if (static_ != null) {
32
44
  return ctx.type === 'text'
33
- ? _static
34
- : ctx.normalize([_static]);
45
+ ? static_
46
+ : ctx.normalize([static_]);
35
47
  }
36
48
  else {
37
- const messages = (node.i || node.items).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
49
+ const messages = resolveItems(node).reduce((acm, c) => [...acm, formatMessagePart(ctx, c)], []);
38
50
  return ctx.normalize(messages);
39
51
  }
40
52
  }
53
+ const PROPS_STATIC = ['s', 'static'];
54
+ function resolveStatic(node) {
55
+ return resolveProps(node, PROPS_STATIC);
56
+ }
57
+ const PROPS_ITEMS = ['i', 'items'];
58
+ function resolveItems(node) {
59
+ return resolveProps(node, PROPS_ITEMS, []);
60
+ }
41
61
  function formatMessagePart(ctx, node) {
42
- const type = node.t || node.type;
62
+ const type = resolveType(node);
43
63
  switch (type) {
44
64
  case 3 /* NodeTypes.Text */: {
45
- const text = node;
46
- return (text.v || text.value);
65
+ return resolveValue$1(node, type);
47
66
  }
48
67
  case 9 /* NodeTypes.Literal */: {
49
- const literal = node;
50
- return (literal.v || literal.value);
68
+ return resolveValue$1(node, type);
51
69
  }
52
70
  case 4 /* NodeTypes.Named */: {
53
71
  const named = node;
54
- return ctx.interpolate(ctx.named(named.k || named.key));
72
+ if (shared.hasOwn(named, 'k') && named.k) {
73
+ return ctx.interpolate(ctx.named(named.k));
74
+ }
75
+ if (shared.hasOwn(named, 'key') && named.key) {
76
+ return ctx.interpolate(ctx.named(named.key));
77
+ }
78
+ throw createUnhandleNodeError(type);
55
79
  }
56
80
  case 5 /* NodeTypes.List */: {
57
81
  const list = node;
58
- return ctx.interpolate(ctx.list(list.i != null ? list.i : list.index));
82
+ if (shared.hasOwn(list, 'i') && shared.isNumber(list.i)) {
83
+ return ctx.interpolate(ctx.list(list.i));
84
+ }
85
+ if (shared.hasOwn(list, 'index') && shared.isNumber(list.index)) {
86
+ return ctx.interpolate(ctx.list(list.index));
87
+ }
88
+ throw createUnhandleNodeError(type);
59
89
  }
60
90
  case 6 /* NodeTypes.Linked */: {
61
91
  const linked = node;
62
- const modifier = linked.m || linked.modifier;
63
- return ctx.linked(formatMessagePart(ctx, linked.k || linked.key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
92
+ const modifier = resolveLinkedModifier(linked);
93
+ const key = resolveLinkedKey(linked);
94
+ return ctx.linked(formatMessagePart(ctx, key), modifier ? formatMessagePart(ctx, modifier) : undefined, ctx.type);
64
95
  }
65
96
  case 7 /* NodeTypes.LinkedKey */: {
66
- const linkedKey = node;
67
- return (linkedKey.v || linkedKey.value);
97
+ return resolveValue$1(node, type);
68
98
  }
69
99
  case 8 /* NodeTypes.LinkedModifier */: {
70
- const linkedModifier = node;
71
- return (linkedModifier.v || linkedModifier.value);
100
+ return resolveValue$1(node, type);
72
101
  }
73
102
  default:
74
- throw new Error(`unhandled node type on format message part: ${type}`);
103
+ throw new Error(`unhandled node on format message part: ${type}`);
104
+ }
105
+ }
106
+ const PROPS_TYPE = ['t', 'type'];
107
+ function resolveType(node) {
108
+ return resolveProps(node, PROPS_TYPE);
109
+ }
110
+ const PROPS_VALUE = ['v', 'value'];
111
+ function resolveValue$1(node, type) {
112
+ const resolved = resolveProps(node, PROPS_VALUE);
113
+ if (resolved) {
114
+ return resolved;
75
115
  }
116
+ else {
117
+ throw createUnhandleNodeError(type);
118
+ }
119
+ }
120
+ const PROPS_MODIFIER = ['m', 'modifier'];
121
+ function resolveLinkedModifier(node) {
122
+ return resolveProps(node, PROPS_MODIFIER);
123
+ }
124
+ const PROPS_KEY = ['k', 'key'];
125
+ function resolveLinkedKey(node) {
126
+ const resolved = resolveProps(node, PROPS_KEY);
127
+ if (resolved) {
128
+ return resolved;
129
+ }
130
+ else {
131
+ throw createUnhandleNodeError(6 /* NodeTypes.Linked */);
132
+ }
133
+ }
134
+ function resolveProps(node, props, defaultValue) {
135
+ for (let i = 0; i < props.length; i++) {
136
+ const prop = props[i];
137
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
138
+ if (shared.hasOwn(node, prop) && node[prop] != null) {
139
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
140
+ return node[prop];
141
+ }
142
+ }
143
+ return defaultValue;
144
+ }
145
+ function createUnhandleNodeError(type) {
146
+ return new Error(`unhandled node type: ${type}`);
76
147
  }
77
148
 
78
149
  const defaultOnCacheKey = (message) => message;
79
- let compileCache = Object.create(null);
150
+ let compileCache = shared.create();
80
151
  function clearCompileCache() {
81
- compileCache = Object.create(null);
152
+ compileCache = shared.create();
153
+ }
154
+ function isMessageAST(val) {
155
+ return (shared.isObject(val) &&
156
+ resolveType(val) === 0 &&
157
+ (shared.hasOwn(val, 'b') || shared.hasOwn(val, 'body')));
82
158
  }
83
- const isMessageAST = (val) => shared.isObject(val) &&
84
- (val.t === 0 || val.type === 0) &&
85
- ('b' in val || 'body' in val);
86
159
  function baseCompile(message, options = {}) {
87
160
  // error detecting on compile
88
161
  let detectError = false;
@@ -632,7 +705,7 @@ function getWarnMessage(code, ...args) {
632
705
  * Intlify core-base version
633
706
  * @internal
634
707
  */
635
- const VERSION = '10.0.4';
708
+ const VERSION = '10.0.6';
636
709
  const NOT_REOSLVED = -1;
637
710
  const DEFAULT_LOCALE = 'en-US';
638
711
  const MISSING_RESOLVE_VALUE = '';
@@ -722,17 +795,17 @@ function createCoreContext(options = {}) {
722
795
  : _locale;
723
796
  const messages = shared.isPlainObject(options.messages)
724
797
  ? options.messages
725
- : { [_locale]: {} };
798
+ : createResources(_locale);
726
799
  const datetimeFormats = shared.isPlainObject(options.datetimeFormats)
727
800
  ? options.datetimeFormats
728
- : { [_locale]: {} }
801
+ : createResources(_locale)
729
802
  ;
730
803
  const numberFormats = shared.isPlainObject(options.numberFormats)
731
804
  ? options.numberFormats
732
- : { [_locale]: {} }
805
+ : createResources(_locale)
733
806
  ;
734
- const modifiers = shared.assign({}, options.modifiers || {}, getDefaultLinkedModifiers());
735
- const pluralRules = options.pluralRules || {};
807
+ const modifiers = shared.assign(shared.create(), options.modifiers, getDefaultLinkedModifiers());
808
+ const pluralRules = options.pluralRules || shared.create();
736
809
  const missing = shared.isFunction(options.missing) ? options.missing : null;
737
810
  const missingWarn = shared.isBoolean(options.missingWarn) || shared.isRegExp(options.missingWarn)
738
811
  ? options.missingWarn
@@ -806,6 +879,7 @@ function createCoreContext(options = {}) {
806
879
  }
807
880
  return context;
808
881
  }
882
+ const createResources = (locale) => ({ [locale]: shared.create() });
809
883
  /** @internal */
810
884
  function isTranslateFallbackWarn(fallback, key) {
811
885
  return fallback instanceof RegExp ? fallback.test(key) : fallback;
@@ -925,8 +999,8 @@ const DATETIME_FORMAT_OPTIONS_KEYS = [
925
999
  /** @internal */
926
1000
  function parseDateTimeArgs(...args) {
927
1001
  const [arg1, arg2, arg3, arg4] = args;
928
- const options = {};
929
- let overrides = {};
1002
+ const options = shared.create();
1003
+ let overrides = shared.create();
930
1004
  let value;
931
1005
  if (shared.isString(arg1)) {
932
1006
  // Only allow ISO strings - other date formats are often supported,
@@ -1072,8 +1146,8 @@ const NUMBER_FORMAT_OPTIONS_KEYS = [
1072
1146
  /** @internal */
1073
1147
  function parseNumberArgs(...args) {
1074
1148
  const [arg1, arg2, arg3, arg4] = args;
1075
- const options = {};
1076
- let overrides = {};
1149
+ const options = shared.create();
1150
+ let overrides = shared.create();
1077
1151
  if (!shared.isNumber(arg1)) {
1078
1152
  throw createCoreError(CoreErrorCodes.INVALID_ARGUMENT);
1079
1153
  }
@@ -1172,7 +1246,7 @@ function createMessageContext(options = {}) {
1172
1246
  const _list = options.list || [];
1173
1247
  const list = (index) => _list[index];
1174
1248
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
1175
- const _named = options.named || {};
1249
+ const _named = options.named || shared.create();
1176
1250
  shared.isNumber(options.pluralIndex) && normalizeNamed(pluralIndex, _named);
1177
1251
  const named = (key) => _named[key];
1178
1252
  function message(key, useLinked) {
@@ -1239,7 +1313,7 @@ function createMessageContext(options = {}) {
1239
1313
  ["type" /* HelperNameMap.TYPE */]: type,
1240
1314
  ["interpolate" /* HelperNameMap.INTERPOLATE */]: interpolate,
1241
1315
  ["normalize" /* HelperNameMap.NORMALIZE */]: normalize,
1242
- ["values" /* HelperNameMap.VALUES */]: shared.assign({}, _list, _named)
1316
+ ["values" /* HelperNameMap.VALUES */]: shared.assign(shared.create(), _list, _named)
1243
1317
  };
1244
1318
  return ctx;
1245
1319
  }
@@ -1281,7 +1355,7 @@ function translate(context, ...args) {
1281
1355
  : [
1282
1356
  key,
1283
1357
  locale,
1284
- messages[locale] || {}
1358
+ messages[locale] || shared.create()
1285
1359
  ];
1286
1360
  // NOTE:
1287
1361
  // Fix to work around `ssrTransfrom` bug in Vite.
@@ -1346,14 +1420,14 @@ function escapeParams(options) {
1346
1420
  function resolveMessageFormat(context, key, locale, fallbackLocale, fallbackWarn, missingWarn) {
1347
1421
  const { messages, onWarn, messageResolver: resolveValue, localeFallbacker } = context;
1348
1422
  const locales = localeFallbacker(context, fallbackLocale, locale); // eslint-disable-line @typescript-eslint/no-explicit-any
1349
- let message = {};
1423
+ let message = shared.create();
1350
1424
  let targetLocale;
1351
1425
  let format = null;
1352
1426
  const type = 'translate';
1353
1427
  for (let i = 0; i < locales.length; i++) {
1354
1428
  targetLocale = locales[i];
1355
1429
  message =
1356
- messages[targetLocale] || {};
1430
+ messages[targetLocale] || shared.create();
1357
1431
  if ((format = resolveValue(message, key)) === null) {
1358
1432
  // if null, resolve with object key path
1359
1433
  format = message[key]; // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -1398,7 +1472,7 @@ function evaluateMessage(context, msg, msgCtx) {
1398
1472
  /** @internal */
1399
1473
  function parseTranslateArgs(...args) {
1400
1474
  const [arg1, arg2, arg3] = args;
1401
- const options = {};
1475
+ const options = shared.create();
1402
1476
  if (!shared.isString(arg1) &&
1403
1477
  !shared.isNumber(arg1) &&
1404
1478
  !isMessageFunction(arg1) &&
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@intlify/core-base",
3
- "version": "10.0.4",
3
+ "version": "10.0.6",
4
4
  "description": "@intlify/core-base",
5
5
  "keywords": [
6
6
  "core",
@@ -33,11 +33,11 @@
33
33
  "jsdelivr": "dist/core-base.global.js",
34
34
  "types": "dist/core-base.d.ts",
35
35
  "dependencies": {
36
- "@intlify/message-compiler": "10.0.4",
37
- "@intlify/shared": "10.0.4"
36
+ "@intlify/message-compiler": "10.0.6",
37
+ "@intlify/shared": "10.0.6"
38
38
  },
39
39
  "devDependencies": {
40
- "@intlify/devtools-types": "10.0.4"
40
+ "@intlify/devtools-types": "10.0.6"
41
41
  },
42
42
  "engines": {
43
43
  "node": ">= 16"