@discord/intl-ast 0.6.2 → 0.7.1-rc.0

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,20 +9,16 @@ export declare enum FormatJsNodeType {
9
9
  Pound = 7,
10
10
  Tag = 8
11
11
  }
12
- export type LiteralNode = [FormatJsNodeType.Literal, 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];
16
16
  export type TimeNode = [FormatJsNodeType.Time, string, string | undefined];
17
- export type SelectNode = [FormatJsNodeType.Select, string, Record<string, {
18
- value: AstNode[];
19
- }>];
17
+ export type SelectNode = [FormatJsNodeType.Select, string, Record<string, AstNode[]>];
20
18
  export type PluralNode = [
21
19
  FormatJsNodeType.Plural,
22
20
  string,
23
- Record<string, {
24
- value: AstNode[];
25
- }>,
21
+ Record<string, AstNode[]>,
26
22
  number,
27
23
  FormatJsPluralType
28
24
  ];
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,7 +50,8 @@ 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
56
  // `pluralType` is technically only valid on `Plural` nodes, even
55
57
  // though the structure is identical to `Select`.
@@ -61,9 +63,11 @@ function hydratePlural(keyless) {
61
63
  }
62
64
  }
63
65
  function hydrateSingle(keyless) {
66
+ if (typeof keyless === 'string') {
67
+ return { type: 0, value: keyless };
68
+ }
64
69
  const [type] = keyless;
65
70
  switch (type) {
66
- case FormatJsNodeType.Literal:
67
71
  case FormatJsNodeType.Argument:
68
72
  return { type, value: keyless[1] };
69
73
  case FormatJsNodeType.Number:
@@ -85,26 +89,32 @@ function hydrateSingle(keyless) {
85
89
  }
86
90
  }
87
91
  function hydrateFormatJsAst(keyless) {
88
- // If the first element of the array is itself an array, then we have a list
89
- // of elements to parse rather than a single object.
90
- 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') {
91
95
  hydrateArray(keyless);
92
96
  return keyless;
93
97
  }
94
- else if (keyless.length === 0) {
98
+ if (keyless.length === 0) {
95
99
  // Some entries can be empty arrays, like an empty set of children, and those can
96
100
  // be preserved.
97
101
  return keyless;
98
102
  }
99
- 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;
100
110
  }
101
111
  function compressFormatJsToAst(node) {
102
112
  if (Array.isArray(node)) {
103
113
  return node.map((element) => compressFormatJsToAst(element));
104
114
  }
105
- console.log('compressing');
106
115
  switch (node.type) {
107
116
  case FormatJsNodeType.Literal:
117
+ return node.value;
108
118
  case FormatJsNodeType.Argument:
109
119
  return [node.type, node.value];
110
120
  case FormatJsNodeType.Number:
@@ -114,14 +124,14 @@ function compressFormatJsToAst(node) {
114
124
  case FormatJsNodeType.Select: {
115
125
  const reducedOptions = {};
116
126
  for (const [name, option] of Object.entries(node.options)) {
117
- reducedOptions[name] = { value: compressFormatJsToAst(option.value) };
127
+ reducedOptions[name] = compressFormatJsToAst(option.value);
118
128
  }
119
129
  return [node.type, node.value, reducedOptions];
120
130
  }
121
131
  case FormatJsNodeType.Plural: {
122
132
  const reducedOptions = {};
123
133
  for (const [name, option] of Object.entries(node.options)) {
124
- reducedOptions[name] = { value: compressFormatJsToAst(option.value) };
134
+ reducedOptions[name] = compressFormatJsToAst(option.value);
125
135
  }
126
136
  return [node.type, node.value, reducedOptions, node.offset, node.pluralType];
127
137
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@discord/intl-ast",
3
- "version": "0.6.2",
3
+ "version": "0.7.1-rc.0",
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",