@discord/intl-ast 0.7.0 → 0.7.1

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.
package/dist/index.d.ts CHANGED
@@ -9,7 +9,7 @@ export declare enum FormatJsNodeType {
9
9
  Pound = 7,
10
10
  Tag = 8
11
11
  }
12
- export type LiteralNode = [string];
12
+ export type LiteralNode = string;
13
13
  export type ArgumentNode = [FormatJsNodeType.Argument, string];
14
14
  export type NumberNode = [FormatJsNodeType.Number, string, string | undefined];
15
15
  export type DateNode = [FormatJsNodeType.Date, string, string | undefined];
package/dist/index.js CHANGED
@@ -37,8 +37,9 @@ var AstNodeIndices;
37
37
  exports.FORMAT_JS_POUND = Object.freeze({ type: 7 });
38
38
  function hydrateArray(elements) {
39
39
  for (let i = 0; i < elements.length; i++) {
40
- elements[i] = hydrateFormatJsAst(elements[i]);
40
+ elements[i] = hydrateSingle(elements[i]);
41
41
  }
42
+ return true;
42
43
  }
43
44
  function hydratePlural(keyless) {
44
45
  const [type, value, options, offset, pluralType] = keyless;
@@ -49,29 +50,24 @@ function hydratePlural(keyless) {
49
50
  // This saves multiple allocations compared to building a new object
50
51
  // from scratch, either for the whole options object or for each value.
51
52
  for (const key in options) {
52
- hydrateArray(options[key].value);
53
+ hydrateArray(options[key]);
54
+ options[key] = { value: options[key] };
53
55
  }
54
- const valueOptions = options.map((option) => ({ value: option }));
55
56
  // `pluralType` is technically only valid on `Plural` nodes, even
56
57
  // though the structure is identical to `Select`.
57
58
  if (type === FormatJsNodeType.Plural) {
58
- return {
59
- type,
60
- value,
61
- options,
62
- offset: valueOptions,
63
- pluralType,
64
- };
59
+ return { type, value, options, offset, pluralType };
65
60
  }
66
61
  else {
67
- return { type, value, options: valueOptions, offset };
62
+ return { type, value, options, offset };
68
63
  }
69
64
  }
70
65
  function hydrateSingle(keyless) {
66
+ if (typeof keyless === 'string') {
67
+ return { type: 0, value: keyless };
68
+ }
71
69
  const [type] = keyless;
72
70
  switch (type) {
73
- case FormatJsNodeType.Literal:
74
- return { type: 0, value: keyless[0] };
75
71
  case FormatJsNodeType.Argument:
76
72
  return { type, value: keyless[1] };
77
73
  case FormatJsNodeType.Number:
@@ -93,18 +89,24 @@ function hydrateSingle(keyless) {
93
89
  }
94
90
  }
95
91
  function hydrateFormatJsAst(keyless) {
96
- // If the first element of the array is itself an array, then we have a list
97
- // of elements to parse rather than a single object.
98
- if (Array.isArray(keyless[0])) {
92
+ // If the first element of the array is a string, then we _do_ have an array of elements, but the
93
+ // first one is a literal node.
94
+ if (typeof keyless[0] === 'string') {
99
95
  hydrateArray(keyless);
100
96
  return keyless;
101
97
  }
102
- else if (keyless.length === 0) {
98
+ if (keyless.length === 0) {
103
99
  // Some entries can be empty arrays, like an empty set of children, and those can
104
100
  // be preserved.
105
101
  return keyless;
106
102
  }
107
- return hydrateSingle(keyless);
103
+ // If the first element is otherwise not an array, then it must be a
104
+ // type identifier for a single node.
105
+ if (!Array.isArray(keyless[0])) {
106
+ return hydrateSingle(keyless);
107
+ }
108
+ hydrateArray(keyless);
109
+ return keyless;
108
110
  }
109
111
  function compressFormatJsToAst(node) {
110
112
  if (Array.isArray(node)) {
@@ -112,7 +114,7 @@ function compressFormatJsToAst(node) {
112
114
  }
113
115
  switch (node.type) {
114
116
  case FormatJsNodeType.Literal:
115
- return [node.value];
117
+ return node.value;
116
118
  case FormatJsNodeType.Argument:
117
119
  return [node.type, node.value];
118
120
  case FormatJsNodeType.Number:
@@ -143,7 +145,7 @@ function isCompressedAst(node) {
143
145
  // Not an array at all means this is a singular fully-typed node.
144
146
  if (!Array.isArray(node))
145
147
  return false;
146
- // Otherwise just check the first element. If it's an array, then the ast is compressed as
147
- // keyless.
148
- return Array.isArray(node[0]);
148
+ // Otherwise just check the first element. If it's an array or a direct string, then the ast is
149
+ // compressed as keyless.
150
+ return Array.isArray(node[0]) || typeof node[0] === 'string';
149
151
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl-ast",
3
- "version": "0.7.0",
3
+ "version": "0.7.1",
4
4
  "license": "MIT",
5
5
  "description": "Types and utilities for working with the ICU+Markdown AST format from @discord/intl",
6
6
  "main": "./dist/index.js",