@discord/intl-ast 0.6.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Discord, Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,104 @@
1
+ export declare enum FormatJsNodeType {
2
+ Literal = 0,
3
+ Argument = 1,
4
+ Number = 2,
5
+ Date = 3,
6
+ Time = 4,
7
+ Select = 5,
8
+ Plural = 6,
9
+ Pound = 7,
10
+ Tag = 8
11
+ }
12
+ export type LiteralNode = [FormatJsNodeType.Literal, string];
13
+ export type ArgumentNode = [FormatJsNodeType.Argument, string];
14
+ export type NumberNode = [FormatJsNodeType.Number, string, string | undefined];
15
+ export type DateNode = [FormatJsNodeType.Date, string, string | undefined];
16
+ export type TimeNode = [FormatJsNodeType.Time, string, string | undefined];
17
+ export type SelectNode = [FormatJsNodeType.Select, string, Record<string, {
18
+ value: AstNode[];
19
+ }>];
20
+ export type PluralNode = [
21
+ FormatJsNodeType.Plural,
22
+ string,
23
+ Record<string, {
24
+ value: AstNode[];
25
+ }>,
26
+ number,
27
+ FormatJsPluralType
28
+ ];
29
+ export type PoundNode = [FormatJsNodeType.Pound];
30
+ export type TagNode = [FormatJsNodeType.Tag, string, AstNode[]];
31
+ export type AstNode = LiteralNode | ArgumentNode | NumberNode | DateNode | TimeNode | SelectNode | PluralNode | PoundNode | TagNode;
32
+ export declare enum AstNodeIndices {
33
+ Type = 0,
34
+ Value = 1,
35
+ Style = 2,
36
+ Options = 2,
37
+ Offset = 3,
38
+ PluralType = 4,
39
+ Children = 2
40
+ }
41
+ export interface FullFormatJsLiteral {
42
+ type: FormatJsNodeType.Literal;
43
+ value: string;
44
+ }
45
+ export interface FullFormatJsArgument {
46
+ type: FormatJsNodeType.Argument;
47
+ value: string;
48
+ }
49
+ export interface FullFormatJsNumber {
50
+ type: FormatJsNodeType.Number;
51
+ value: string;
52
+ style?: string;
53
+ }
54
+ export interface FullFormatJsDate {
55
+ type: FormatJsNodeType.Date;
56
+ value: string;
57
+ style?: string;
58
+ }
59
+ export interface FullFormatJsTime {
60
+ type: FormatJsNodeType.Time;
61
+ value: string;
62
+ style?: string;
63
+ }
64
+ export interface FullFormatJsSelect {
65
+ type: FormatJsNodeType.Select;
66
+ value: string;
67
+ options: Record<string, {
68
+ value: FullFormatJsNode[];
69
+ }>;
70
+ }
71
+ export type FormatJsPluralType = 'cardinal' | 'ordinal';
72
+ export interface FullFormatJsPlural {
73
+ type: FormatJsNodeType.Plural;
74
+ value: string;
75
+ options: Record<string, {
76
+ value: FullFormatJsNode[];
77
+ }>;
78
+ offset: number;
79
+ pluralType: FormatJsPluralType;
80
+ }
81
+ export interface FullFormatJsPound {
82
+ type: FormatJsNodeType.Pound;
83
+ }
84
+ export interface FullFormatJsTag {
85
+ type: FormatJsNodeType.Tag;
86
+ value: string;
87
+ children: FullFormatJsNode[];
88
+ }
89
+ export type FullFormatJsNode = FullFormatJsLiteral | FullFormatJsArgument | FullFormatJsNumber | FullFormatJsDate | FullFormatJsTime | FullFormatJsSelect | FullFormatJsPlural | FullFormatJsPound | FullFormatJsTag;
90
+ export declare const FORMAT_JS_POUND: FullFormatJsPound;
91
+ /**
92
+ * Hydrate the given keyless JSON into a FormatJS-compatible AST structure.
93
+ * The given object is reused as much as possible, meaning it cannot be safely
94
+ * used again after hydrating.
95
+ */
96
+ export declare function hydrateFormatJsAst(keyless: Array<Array<any>>): FullFormatJsNode[];
97
+ export declare function compressFormatJsToAst(node: FullFormatJsNode): AstNode;
98
+ export declare function compressFormatJsToAst(node: FullFormatJsNode[]): AstNode[];
99
+ /**
100
+ * Returns true if the given `node` is a compressed `AstNode` rather than a `FullFormatJsNode`. This
101
+ * also works for checking arrays of nodes, returning true if the nodes of the array are compressed.
102
+ */
103
+ export declare function isCompressedAst(node: AstNode | FullFormatJsNode): node is AstNode;
104
+ export declare function isCompressedAst(node: AstNode[] | FullFormatJsNode[]): node is AstNode[];
package/dist/index.js ADDED
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.FORMAT_JS_POUND = exports.AstNodeIndices = exports.FormatJsNodeType = void 0;
4
+ exports.hydrateFormatJsAst = hydrateFormatJsAst;
5
+ exports.compressFormatJsToAst = compressFormatJsToAst;
6
+ exports.isCompressedAst = isCompressedAst;
7
+ var FormatJsNodeType;
8
+ (function (FormatJsNodeType) {
9
+ FormatJsNodeType[FormatJsNodeType["Literal"] = 0] = "Literal";
10
+ FormatJsNodeType[FormatJsNodeType["Argument"] = 1] = "Argument";
11
+ FormatJsNodeType[FormatJsNodeType["Number"] = 2] = "Number";
12
+ FormatJsNodeType[FormatJsNodeType["Date"] = 3] = "Date";
13
+ FormatJsNodeType[FormatJsNodeType["Time"] = 4] = "Time";
14
+ FormatJsNodeType[FormatJsNodeType["Select"] = 5] = "Select";
15
+ FormatJsNodeType[FormatJsNodeType["Plural"] = 6] = "Plural";
16
+ FormatJsNodeType[FormatJsNodeType["Pound"] = 7] = "Pound";
17
+ FormatJsNodeType[FormatJsNodeType["Tag"] = 8] = "Tag";
18
+ })(FormatJsNodeType || (exports.FormatJsNodeType = FormatJsNodeType = {}));
19
+ var AstNodeIndices;
20
+ (function (AstNodeIndices) {
21
+ AstNodeIndices[AstNodeIndices["Type"] = 0] = "Type";
22
+ AstNodeIndices[AstNodeIndices["Value"] = 1] = "Value";
23
+ AstNodeIndices[AstNodeIndices["Style"] = 2] = "Style";
24
+ AstNodeIndices[AstNodeIndices["Options"] = 2] = "Options";
25
+ AstNodeIndices[AstNodeIndices["Offset"] = 3] = "Offset";
26
+ AstNodeIndices[AstNodeIndices["PluralType"] = 4] = "PluralType";
27
+ AstNodeIndices[AstNodeIndices["Children"] = 2] = "Children";
28
+ })(AstNodeIndices || (exports.AstNodeIndices = AstNodeIndices = {}));
29
+ //#endregion
30
+ //#region Hydration
31
+ //
32
+ // Utilities for converting compressed Keyless Json into the fully-typed FormatJS node structure.
33
+ // Using a static object here ensures there's only one allocation for it, which
34
+ // will likely appear in many, many messages. Freezing ensures that the shared
35
+ // object can't be accidentally modified if a consumer tries to transform the
36
+ // AST.
37
+ exports.FORMAT_JS_POUND = Object.freeze({ type: 7 });
38
+ function hydrateArray(elements) {
39
+ for (let i = 0; i < elements.length; i++) {
40
+ elements[i] = hydrateFormatJsAst(elements[i]);
41
+ }
42
+ }
43
+ function hydratePlural(keyless) {
44
+ const [type, value, options, offset, pluralType] = keyless;
45
+ // This tries to be efficient about updating the value for each option
46
+ // with a parsed version, reusing the same receiving object, and even
47
+ // the inner key within it, just replacing the end value with the
48
+ // parsed version.
49
+ // This saves multiple allocations compared to building a new object
50
+ // from scratch, either for the whole options object or for each value.
51
+ for (const key in options) {
52
+ hydrateArray(options[key].value);
53
+ }
54
+ // `pluralType` is technically only valid on `Plural` nodes, even
55
+ // though the structure is identical to `Select`.
56
+ if (type === FormatJsNodeType.Plural) {
57
+ return { type, value, options, offset, pluralType };
58
+ }
59
+ else {
60
+ return { type, value, options, offset };
61
+ }
62
+ }
63
+ function hydrateSingle(keyless) {
64
+ const [type] = keyless;
65
+ switch (type) {
66
+ case FormatJsNodeType.Literal:
67
+ case FormatJsNodeType.Argument:
68
+ return { type, value: keyless[1] };
69
+ case FormatJsNodeType.Number:
70
+ case FormatJsNodeType.Date:
71
+ case FormatJsNodeType.Time:
72
+ return { type, value: keyless[1], style: keyless[2] };
73
+ case FormatJsNodeType.Select:
74
+ case FormatJsNodeType.Plural:
75
+ return hydratePlural(keyless);
76
+ case FormatJsNodeType.Pound:
77
+ return exports.FORMAT_JS_POUND;
78
+ case FormatJsNodeType.Tag: {
79
+ const [type, value, children] = keyless;
80
+ hydrateArray(children);
81
+ return { type, value, children: children };
82
+ }
83
+ default:
84
+ throw new Error(`FormatJS keyless JSON encountered an unknown type: ${type}`);
85
+ }
86
+ }
87
+ 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])) {
91
+ hydrateArray(keyless);
92
+ return keyless;
93
+ }
94
+ else if (keyless.length === 0) {
95
+ // Some entries can be empty arrays, like an empty set of children, and those can
96
+ // be preserved.
97
+ return keyless;
98
+ }
99
+ return hydrateSingle(keyless);
100
+ }
101
+ function compressFormatJsToAst(node) {
102
+ if (Array.isArray(node)) {
103
+ return node.map((element) => compressFormatJsToAst(element));
104
+ }
105
+ console.log('compressing');
106
+ switch (node.type) {
107
+ case FormatJsNodeType.Literal:
108
+ case FormatJsNodeType.Argument:
109
+ return [node.type, node.value];
110
+ case FormatJsNodeType.Number:
111
+ case FormatJsNodeType.Date:
112
+ case FormatJsNodeType.Time:
113
+ return [node.type, node.value, node.style];
114
+ case FormatJsNodeType.Select: {
115
+ const reducedOptions = {};
116
+ for (const [name, option] of Object.entries(node.options)) {
117
+ reducedOptions[name] = { value: compressFormatJsToAst(option.value) };
118
+ }
119
+ return [node.type, node.value, reducedOptions];
120
+ }
121
+ case FormatJsNodeType.Plural: {
122
+ const reducedOptions = {};
123
+ for (const [name, option] of Object.entries(node.options)) {
124
+ reducedOptions[name] = { value: compressFormatJsToAst(option.value) };
125
+ }
126
+ return [node.type, node.value, reducedOptions, node.offset, node.pluralType];
127
+ }
128
+ case FormatJsNodeType.Pound:
129
+ return [node.type];
130
+ case FormatJsNodeType.Tag:
131
+ return [node.type, node.value, compressFormatJsToAst(node.children)];
132
+ }
133
+ }
134
+ function isCompressedAst(node) {
135
+ // Not an array at all means this is a singular fully-typed node.
136
+ if (!Array.isArray(node))
137
+ return false;
138
+ // Otherwise just check the first element. If it's an array, then the ast is compressed as
139
+ // keyless.
140
+ return Array.isArray(node[0]);
141
+ }
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "@discord/intl-ast",
3
+ "version": "0.6.1",
4
+ "license": "MIT",
5
+ "description": "Types and utilities for working with the ICU+Markdown AST format from @discord/intl",
6
+ "main": "./dist/index.js",
7
+ "exports": {
8
+ ".": {
9
+ "default": "./dist/index.js",
10
+ "types": "./dist/index.d.ts"
11
+ }
12
+ },
13
+ "files": [
14
+ "dist",
15
+ "src"
16
+ ],
17
+ "devDependencies": {
18
+ "typescript": "*"
19
+ },
20
+ "scripts": {
21
+ "build": "tsc",
22
+ "build:release": "tsc"
23
+ }
24
+ }