@elaraai/east 0.0.1-beta.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/LICENSE.md +682 -0
- package/README.md +276 -0
- package/dist/src/analyze.d.ts +95 -0
- package/dist/src/analyze.d.ts.map +1 -0
- package/dist/src/analyze.js +1110 -0
- package/dist/src/analyze.js.map +1 -0
- package/dist/src/ast.d.ts +263 -0
- package/dist/src/ast.d.ts.map +1 -0
- package/dist/src/ast.js +151 -0
- package/dist/src/ast.js.map +1 -0
- package/dist/src/ast_to_ir.d.ts +24 -0
- package/dist/src/ast_to_ir.d.ts.map +1 -0
- package/dist/src/ast_to_ir.js +834 -0
- package/dist/src/ast_to_ir.js.map +1 -0
- package/dist/src/builtins.d.ts +18 -0
- package/dist/src/builtins.d.ts.map +1 -0
- package/dist/src/builtins.js +1105 -0
- package/dist/src/builtins.js.map +1 -0
- package/dist/src/comparison.d.ts +28 -0
- package/dist/src/comparison.d.ts.map +1 -0
- package/dist/src/comparison.js +1017 -0
- package/dist/src/comparison.js.map +1 -0
- package/dist/src/compile.d.ts +22 -0
- package/dist/src/compile.d.ts.map +1 -0
- package/dist/src/compile.js +3260 -0
- package/dist/src/compile.js.map +1 -0
- package/dist/src/containers/ref.d.ts +106 -0
- package/dist/src/containers/ref.d.ts.map +1 -0
- package/dist/src/containers/ref.js +100 -0
- package/dist/src/containers/ref.js.map +1 -0
- package/dist/src/containers/sortedmap.d.ts +165 -0
- package/dist/src/containers/sortedmap.d.ts.map +1 -0
- package/dist/src/containers/sortedmap.js +237 -0
- package/dist/src/containers/sortedmap.js.map +1 -0
- package/dist/src/containers/sortedset.d.ts +185 -0
- package/dist/src/containers/sortedset.d.ts.map +1 -0
- package/dist/src/containers/sortedset.js +312 -0
- package/dist/src/containers/sortedset.js.map +1 -0
- package/dist/src/containers/variant.d.ts +131 -0
- package/dist/src/containers/variant.d.ts.map +1 -0
- package/dist/src/containers/variant.js +68 -0
- package/dist/src/containers/variant.js.map +1 -0
- package/dist/src/datetime_format/parse.d.ts +50 -0
- package/dist/src/datetime_format/parse.d.ts.map +1 -0
- package/dist/src/datetime_format/parse.js +908 -0
- package/dist/src/datetime_format/parse.js.map +1 -0
- package/dist/src/datetime_format/print.d.ts +35 -0
- package/dist/src/datetime_format/print.d.ts.map +1 -0
- package/dist/src/datetime_format/print.js +157 -0
- package/dist/src/datetime_format/print.js.map +1 -0
- package/dist/src/datetime_format/tokenize.d.ts +76 -0
- package/dist/src/datetime_format/tokenize.d.ts.map +1 -0
- package/dist/src/datetime_format/tokenize.js +271 -0
- package/dist/src/datetime_format/tokenize.js.map +1 -0
- package/dist/src/datetime_format/types.d.ts +99 -0
- package/dist/src/datetime_format/types.d.ts.map +1 -0
- package/dist/src/datetime_format/types.js +103 -0
- package/dist/src/datetime_format/types.js.map +1 -0
- package/dist/src/datetime_format/validate.d.ts +51 -0
- package/dist/src/datetime_format/validate.d.ts.map +1 -0
- package/dist/src/datetime_format/validate.js +208 -0
- package/dist/src/datetime_format/validate.js.map +1 -0
- package/dist/src/default.d.ts +21 -0
- package/dist/src/default.d.ts.map +1 -0
- package/dist/src/default.js +82 -0
- package/dist/src/default.js.map +1 -0
- package/dist/src/eastir.d.ts +33 -0
- package/dist/src/eastir.d.ts.map +1 -0
- package/dist/src/eastir.js +92 -0
- package/dist/src/eastir.js.map +1 -0
- package/dist/src/error.d.ts +13 -0
- package/dist/src/error.d.ts.map +1 -0
- package/dist/src/error.js +8 -0
- package/dist/src/error.js.map +1 -0
- package/dist/src/expr/array.d.ts +1711 -0
- package/dist/src/expr/array.d.ts.map +1 -0
- package/dist/src/expr/array.js +1805 -0
- package/dist/src/expr/array.js.map +1 -0
- package/dist/src/expr/ast.d.ts +17 -0
- package/dist/src/expr/ast.d.ts.map +1 -0
- package/dist/src/expr/ast.js +302 -0
- package/dist/src/expr/ast.js.map +1 -0
- package/dist/src/expr/blob.d.ts +141 -0
- package/dist/src/expr/blob.d.ts.map +1 -0
- package/dist/src/expr/blob.js +198 -0
- package/dist/src/expr/blob.js.map +1 -0
- package/dist/src/expr/block.d.ts +201 -0
- package/dist/src/expr/block.d.ts.map +1 -0
- package/dist/src/expr/block.js +1505 -0
- package/dist/src/expr/block.js.map +1 -0
- package/dist/src/expr/boolean.d.ts +207 -0
- package/dist/src/expr/boolean.d.ts.map +1 -0
- package/dist/src/expr/boolean.js +261 -0
- package/dist/src/expr/boolean.js.map +1 -0
- package/dist/src/expr/datetime.d.ts +544 -0
- package/dist/src/expr/datetime.d.ts.map +1 -0
- package/dist/src/expr/datetime.js +980 -0
- package/dist/src/expr/datetime.js.map +1 -0
- package/dist/src/expr/dict.d.ts +1242 -0
- package/dist/src/expr/dict.d.ts.map +1 -0
- package/dist/src/expr/dict.js +1492 -0
- package/dist/src/expr/dict.js.map +1 -0
- package/dist/src/expr/expr.d.ts +95 -0
- package/dist/src/expr/expr.d.ts.map +1 -0
- package/dist/src/expr/expr.js +171 -0
- package/dist/src/expr/expr.js.map +1 -0
- package/dist/src/expr/float.d.ts +357 -0
- package/dist/src/expr/float.d.ts.map +1 -0
- package/dist/src/expr/float.js +637 -0
- package/dist/src/expr/float.js.map +1 -0
- package/dist/src/expr/function.d.ts +46 -0
- package/dist/src/expr/function.d.ts.map +1 -0
- package/dist/src/expr/function.js +58 -0
- package/dist/src/expr/function.js.map +1 -0
- package/dist/src/expr/index.d.ts +450 -0
- package/dist/src/expr/index.d.ts.map +1 -0
- package/dist/src/expr/index.js +423 -0
- package/dist/src/expr/index.js.map +1 -0
- package/dist/src/expr/integer.d.ts +256 -0
- package/dist/src/expr/integer.d.ts.map +1 -0
- package/dist/src/expr/integer.js +311 -0
- package/dist/src/expr/integer.js.map +1 -0
- package/dist/src/expr/libs/array.d.ts +106 -0
- package/dist/src/expr/libs/array.d.ts.map +1 -0
- package/dist/src/expr/libs/array.js +140 -0
- package/dist/src/expr/libs/array.js.map +1 -0
- package/dist/src/expr/libs/blob.d.ts +42 -0
- package/dist/src/expr/libs/blob.d.ts.map +1 -0
- package/dist/src/expr/libs/blob.js +70 -0
- package/dist/src/expr/libs/blob.js.map +1 -0
- package/dist/src/expr/libs/datetime.d.ts +479 -0
- package/dist/src/expr/libs/datetime.d.ts.map +1 -0
- package/dist/src/expr/libs/datetime.js +624 -0
- package/dist/src/expr/libs/datetime.js.map +1 -0
- package/dist/src/expr/libs/dict.d.ts +66 -0
- package/dist/src/expr/libs/dict.d.ts.map +1 -0
- package/dist/src/expr/libs/dict.js +77 -0
- package/dist/src/expr/libs/dict.js.map +1 -0
- package/dist/src/expr/libs/float.d.ts +299 -0
- package/dist/src/expr/libs/float.d.ts.map +1 -0
- package/dist/src/expr/libs/float.js +564 -0
- package/dist/src/expr/libs/float.js.map +1 -0
- package/dist/src/expr/libs/integer.d.ts +228 -0
- package/dist/src/expr/libs/integer.d.ts.map +1 -0
- package/dist/src/expr/libs/integer.js +398 -0
- package/dist/src/expr/libs/integer.js.map +1 -0
- package/dist/src/expr/libs/set.d.ts +59 -0
- package/dist/src/expr/libs/set.d.ts.map +1 -0
- package/dist/src/expr/libs/set.js +69 -0
- package/dist/src/expr/libs/set.js.map +1 -0
- package/dist/src/expr/libs/string.d.ts +71 -0
- package/dist/src/expr/libs/string.d.ts.map +1 -0
- package/dist/src/expr/libs/string.js +75 -0
- package/dist/src/expr/libs/string.js.map +1 -0
- package/dist/src/expr/never.d.ts +15 -0
- package/dist/src/expr/never.d.ts.map +1 -0
- package/dist/src/expr/never.js +12 -0
- package/dist/src/expr/never.js.map +1 -0
- package/dist/src/expr/null.d.ts +15 -0
- package/dist/src/expr/null.d.ts.map +1 -0
- package/dist/src/expr/null.js +12 -0
- package/dist/src/expr/null.js.map +1 -0
- package/dist/src/expr/ref.d.ts +103 -0
- package/dist/src/expr/ref.d.ts.map +1 -0
- package/dist/src/expr/ref.js +131 -0
- package/dist/src/expr/ref.js.map +1 -0
- package/dist/src/expr/regex_validation.d.ts +25 -0
- package/dist/src/expr/regex_validation.d.ts.map +1 -0
- package/dist/src/expr/regex_validation.js +130 -0
- package/dist/src/expr/regex_validation.js.map +1 -0
- package/dist/src/expr/set.d.ts +1071 -0
- package/dist/src/expr/set.d.ts.map +1 -0
- package/dist/src/expr/set.js +1137 -0
- package/dist/src/expr/set.js.map +1 -0
- package/dist/src/expr/string.d.ts +414 -0
- package/dist/src/expr/string.d.ts.map +1 -0
- package/dist/src/expr/string.js +683 -0
- package/dist/src/expr/string.js.map +1 -0
- package/dist/src/expr/struct.d.ts +48 -0
- package/dist/src/expr/struct.d.ts.map +1 -0
- package/dist/src/expr/struct.js +65 -0
- package/dist/src/expr/struct.js.map +1 -0
- package/dist/src/expr/types.d.ts +68 -0
- package/dist/src/expr/types.d.ts.map +1 -0
- package/dist/src/expr/types.js +6 -0
- package/dist/src/expr/types.js.map +1 -0
- package/dist/src/expr/variant.d.ts +137 -0
- package/dist/src/expr/variant.d.ts.map +1 -0
- package/dist/src/expr/variant.js +105 -0
- package/dist/src/expr/variant.js.map +1 -0
- package/dist/src/fuzz.d.ts +80 -0
- package/dist/src/fuzz.d.ts.map +1 -0
- package/dist/src/fuzz.js +300 -0
- package/dist/src/fuzz.js.map +1 -0
- package/dist/src/index.d.ts +21 -0
- package/dist/src/index.d.ts.map +1 -0
- package/dist/src/index.js +21 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/internal.d.ts +36 -0
- package/dist/src/internal.d.ts.map +1 -0
- package/dist/src/internal.js +11 -0
- package/dist/src/internal.js.map +1 -0
- package/dist/src/ir.d.ts +1571 -0
- package/dist/src/ir.d.ts.map +1 -0
- package/dist/src/ir.js +56 -0
- package/dist/src/ir.js.map +1 -0
- package/dist/src/location.d.ts +48 -0
- package/dist/src/location.d.ts.map +1 -0
- package/dist/src/location.js +62 -0
- package/dist/src/location.js.map +1 -0
- package/dist/src/platform.d.ts +21 -0
- package/dist/src/platform.d.ts.map +1 -0
- package/dist/src/platform.js +8 -0
- package/dist/src/platform.js.map +1 -0
- package/dist/src/serialization/beast.d.ts +39 -0
- package/dist/src/serialization/beast.d.ts.map +1 -0
- package/dist/src/serialization/beast.js +555 -0
- package/dist/src/serialization/beast.js.map +1 -0
- package/dist/src/serialization/beast2-stream.d.ts +38 -0
- package/dist/src/serialization/beast2-stream.d.ts.map +1 -0
- package/dist/src/serialization/beast2-stream.js +665 -0
- package/dist/src/serialization/beast2-stream.js.map +1 -0
- package/dist/src/serialization/beast2.d.ts +41 -0
- package/dist/src/serialization/beast2.d.ts.map +1 -0
- package/dist/src/serialization/beast2.js +489 -0
- package/dist/src/serialization/beast2.js.map +1 -0
- package/dist/src/serialization/binary-utils.d.ts +151 -0
- package/dist/src/serialization/binary-utils.d.ts.map +1 -0
- package/dist/src/serialization/binary-utils.js +929 -0
- package/dist/src/serialization/binary-utils.js.map +1 -0
- package/dist/src/serialization/east.d.ts +84 -0
- package/dist/src/serialization/east.d.ts.map +1 -0
- package/dist/src/serialization/east.js +1802 -0
- package/dist/src/serialization/east.js.map +1 -0
- package/dist/src/serialization/index.d.ts +11 -0
- package/dist/src/serialization/index.d.ts.map +1 -0
- package/dist/src/serialization/index.js +12 -0
- package/dist/src/serialization/index.js.map +1 -0
- package/dist/src/serialization/json.d.ts +36 -0
- package/dist/src/serialization/json.d.ts.map +1 -0
- package/dist/src/serialization/json.js +849 -0
- package/dist/src/serialization/json.js.map +1 -0
- package/dist/src/type_of_type.d.ts +115 -0
- package/dist/src/type_of_type.d.ts.map +1 -0
- package/dist/src/type_of_type.js +362 -0
- package/dist/src/type_of_type.js.map +1 -0
- package/dist/src/types.d.ts +648 -0
- package/dist/src/types.d.ts.map +1 -0
- package/dist/src/types.js +1631 -0
- package/dist/src/types.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
import { get_location } from "../location.js";
|
|
2
|
+
import { ArrayType, BlobType, BooleanType, IntegerType, isDataType, StringType } from "../types.js";
|
|
3
|
+
import { valueOrExprToAstTyped } from "./ast.js";
|
|
4
|
+
import { AstSymbol, Expr, FactorySymbol } from "./expr.js";
|
|
5
|
+
import { validateCrossPlatformCompatible } from "./regex_validation.js";
|
|
6
|
+
/**
|
|
7
|
+
* Expression representing string values and operations.
|
|
8
|
+
*
|
|
9
|
+
* StringExpr provides methods for string manipulation, parsing, searching (including regex),
|
|
10
|
+
* splitting, formatting, and encoding. Strings are immutable Unicode text values.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```ts
|
|
14
|
+
* // String manipulation
|
|
15
|
+
* const formatName = East.function([StringType, StringType], StringType, ($, first, last) => {
|
|
16
|
+
* const full = first.concat(" ").concat(last);
|
|
17
|
+
* $.return(full.upperCase());
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // String parsing and validation
|
|
21
|
+
* const extractNumber = East.function([StringType], StringType, ($, text) => {
|
|
22
|
+
* const trimmed = text.trim();
|
|
23
|
+
* $.if(trimmed.contains(/^\d+$/), $ => $.return(trimmed));
|
|
24
|
+
* $.return("0");
|
|
25
|
+
* });
|
|
26
|
+
*
|
|
27
|
+
* // String splitting and joining
|
|
28
|
+
* const processCsv = East.function([StringType], ArrayType(StringType), ($, csv) => {
|
|
29
|
+
* $.return(csv.split(","));
|
|
30
|
+
* });
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
export class StringExpr extends Expr {
|
|
34
|
+
constructor(ast, createExpr) {
|
|
35
|
+
super(StringType, ast, createExpr);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Parses a value of the given type from the string using East's canonical text format.
|
|
39
|
+
*
|
|
40
|
+
* @param type - The East type to parse the string as
|
|
41
|
+
* @returns An expression of the specified type
|
|
42
|
+
*
|
|
43
|
+
* @remarks The string must be formatted as produced by `East.print()`. For example, strings must be
|
|
44
|
+
* double-quoted, arrays use `[...]`, structs use `(...)`. This is NOT JSON parsing.
|
|
45
|
+
*
|
|
46
|
+
* @throws East runtime error if the string is not a valid representation of the type
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* ```ts
|
|
50
|
+
* const parseInteger = East.function([StringType], IntegerType, ($, text) => {
|
|
51
|
+
* $.return(text.parse(IntegerType));
|
|
52
|
+
* });
|
|
53
|
+
* const compiled = East.compile(parseInteger.toIR(), []);
|
|
54
|
+
* compiled("42"); // 42n
|
|
55
|
+
* compiled("-100"); // -100n
|
|
56
|
+
* // compiled("3.14") would throw error (not an integer format)
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
parse(type) {
|
|
60
|
+
if (!isDataType(type)) {
|
|
61
|
+
throw new Error(`parse expected a DataType, but got: ${type}`);
|
|
62
|
+
}
|
|
63
|
+
return this[FactorySymbol]({
|
|
64
|
+
ast_type: "Builtin",
|
|
65
|
+
type: type,
|
|
66
|
+
location: get_location(2),
|
|
67
|
+
builtin: "Parse",
|
|
68
|
+
type_parameters: [type],
|
|
69
|
+
arguments: [this[AstSymbol]],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Parses a value of the given type from JSON using East's canonical JSON format.
|
|
74
|
+
*
|
|
75
|
+
* @param type - The East type to parse the JSON as
|
|
76
|
+
* @returns An expression of the specified type
|
|
77
|
+
*
|
|
78
|
+
* @throws East runtime error if the JSON is invalid or doesn't match the type
|
|
79
|
+
*
|
|
80
|
+
* @example
|
|
81
|
+
* ```ts
|
|
82
|
+
* const parseJsonArray = East.function([StringType], ArrayType(IntegerType), ($, json) => {
|
|
83
|
+
* $.return(json.parseJson(ArrayType(IntegerType)));
|
|
84
|
+
* });
|
|
85
|
+
* const compiled = East.compile(parseJsonArray.toIR(), []);
|
|
86
|
+
* compiled("[1, 2, 3]"); // [1n, 2n, 3n]
|
|
87
|
+
* compiled("[]"); // []
|
|
88
|
+
* // compiled("{\"a\": 1}") would throw error (not an array)
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
parseJson(type) {
|
|
92
|
+
return Expr.fromAst({
|
|
93
|
+
ast_type: "Builtin",
|
|
94
|
+
type: type,
|
|
95
|
+
builtin: "StringParseJSON",
|
|
96
|
+
location: { filename: "stdlib", line: 1, column: 1 },
|
|
97
|
+
type_parameters: [type],
|
|
98
|
+
arguments: [this[AstSymbol]],
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Concatenates two strings together (str + other).
|
|
103
|
+
*
|
|
104
|
+
* @param string - The string to append
|
|
105
|
+
* @returns A StringExpr representing the concatenated result
|
|
106
|
+
*
|
|
107
|
+
* @example
|
|
108
|
+
* ```ts
|
|
109
|
+
* const joinStrings = East.function([StringType, StringType], StringType, ($, a, b) => {
|
|
110
|
+
* $.return(a.concat(b));
|
|
111
|
+
* });
|
|
112
|
+
* const compiled = East.compile(joinStrings.toIR(), []);
|
|
113
|
+
* compiled("Hello, ", "World!"); // "Hello, World!"
|
|
114
|
+
* compiled("foo", "bar"); // "foobar"
|
|
115
|
+
* compiled("", "test"); // "test"
|
|
116
|
+
* ```
|
|
117
|
+
*/
|
|
118
|
+
concat(string) {
|
|
119
|
+
const stringAst = valueOrExprToAstTyped(string, StringType);
|
|
120
|
+
return this[FactorySymbol]({
|
|
121
|
+
ast_type: "Builtin",
|
|
122
|
+
type: StringType,
|
|
123
|
+
location: get_location(2),
|
|
124
|
+
builtin: "StringConcat",
|
|
125
|
+
type_parameters: [],
|
|
126
|
+
arguments: [this[AstSymbol], stringAst],
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Repeats the string a specified number of times.
|
|
131
|
+
*
|
|
132
|
+
* @param count - The number of times to repeat the string
|
|
133
|
+
* @returns A StringExpr representing the repeated string
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* const repeatString = East.function([StringType, IntegerType], StringType, ($, str, n) => {
|
|
138
|
+
* $.return(str.repeat(n));
|
|
139
|
+
* });
|
|
140
|
+
* const compiled = East.compile(repeatString.toIR(), []);
|
|
141
|
+
* compiled("abc", 3n); // "abcabcabc"
|
|
142
|
+
* compiled("x", 5n); // "xxxxx"
|
|
143
|
+
* compiled("test", 0n); // ""
|
|
144
|
+
* compiled("hi", 1n); // "hi"
|
|
145
|
+
* ```
|
|
146
|
+
*/
|
|
147
|
+
repeat(count) {
|
|
148
|
+
const countAst = valueOrExprToAstTyped(count, IntegerType);
|
|
149
|
+
return this[FactorySymbol]({
|
|
150
|
+
ast_type: "Builtin",
|
|
151
|
+
type: StringType,
|
|
152
|
+
location: get_location(2),
|
|
153
|
+
builtin: "StringRepeat",
|
|
154
|
+
type_parameters: [],
|
|
155
|
+
arguments: [this[AstSymbol], countAst],
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Returns the length of the string in Unicode code points.
|
|
160
|
+
*
|
|
161
|
+
* @returns An IntegerExpr representing the string length
|
|
162
|
+
*
|
|
163
|
+
* @example
|
|
164
|
+
* ```ts
|
|
165
|
+
* const getLength = East.function([StringType], IntegerType, ($, str) => {
|
|
166
|
+
* $.return(str.length());
|
|
167
|
+
* });
|
|
168
|
+
* const compiled = East.compile(getLength.toIR(), []);
|
|
169
|
+
* compiled("hello"); // 5n
|
|
170
|
+
* compiled(""); // 0n
|
|
171
|
+
* compiled("😀🎉"); // 2n (2 emoji code points)
|
|
172
|
+
* ```
|
|
173
|
+
*/
|
|
174
|
+
length() {
|
|
175
|
+
return this[FactorySymbol]({
|
|
176
|
+
ast_type: "Builtin",
|
|
177
|
+
type: IntegerType,
|
|
178
|
+
location: get_location(2),
|
|
179
|
+
builtin: "StringLength",
|
|
180
|
+
type_parameters: [],
|
|
181
|
+
arguments: [this[AstSymbol]],
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Extracts a substring from the string between two indices.
|
|
186
|
+
*
|
|
187
|
+
* @param from - The starting index (inclusive)
|
|
188
|
+
* @param to - The ending index (exclusive)
|
|
189
|
+
* @returns A StringExpr containing the substring
|
|
190
|
+
*
|
|
191
|
+
* @remarks Indices are zero-based. Negative indices not supported.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```ts
|
|
195
|
+
* const substr = East.function([StringType, IntegerType, IntegerType], StringType, ($, str, start, end) => {
|
|
196
|
+
* $.return(str.substring(start, end));
|
|
197
|
+
* });
|
|
198
|
+
* const compiled = East.compile(substr.toIR(), []);
|
|
199
|
+
* compiled("hello world", 0n, 5n); // "hello"
|
|
200
|
+
* compiled("hello world", 6n, 11n); // "world"
|
|
201
|
+
* compiled("test", 1n, 3n); // "es"
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
substring(from, to) {
|
|
205
|
+
const fromAst = valueOrExprToAstTyped(from, IntegerType);
|
|
206
|
+
const toAst = valueOrExprToAstTyped(to, IntegerType);
|
|
207
|
+
return this[FactorySymbol]({
|
|
208
|
+
ast_type: "Builtin",
|
|
209
|
+
type: StringType,
|
|
210
|
+
location: get_location(2),
|
|
211
|
+
builtin: "StringSubstring",
|
|
212
|
+
type_parameters: [],
|
|
213
|
+
arguments: [this[AstSymbol], fromAst, toAst],
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Converts the string to uppercase.
|
|
218
|
+
*
|
|
219
|
+
* @returns A StringExpr with all characters converted to uppercase
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```ts
|
|
223
|
+
* const toUpper = East.function([StringType], StringType, ($, str) => {
|
|
224
|
+
* $.return(str.upperCase());
|
|
225
|
+
* });
|
|
226
|
+
* const compiled = East.compile(toUpper.toIR(), []);
|
|
227
|
+
* compiled("hello"); // "HELLO"
|
|
228
|
+
* compiled("Hello World"); // "HELLO WORLD"
|
|
229
|
+
* compiled("test123"); // "TEST123"
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
upperCase() {
|
|
233
|
+
return this[FactorySymbol]({
|
|
234
|
+
ast_type: "Builtin",
|
|
235
|
+
type: StringType,
|
|
236
|
+
location: get_location(2),
|
|
237
|
+
builtin: "StringUpperCase",
|
|
238
|
+
type_parameters: [],
|
|
239
|
+
arguments: [this[AstSymbol]],
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Converts the string to lowercase.
|
|
244
|
+
*
|
|
245
|
+
* @returns A StringExpr with all characters converted to lowercase
|
|
246
|
+
*
|
|
247
|
+
* @example
|
|
248
|
+
* ```ts
|
|
249
|
+
* const toLower = East.function([StringType], StringType, ($, str) => {
|
|
250
|
+
* $.return(str.lowerCase());
|
|
251
|
+
* });
|
|
252
|
+
* const compiled = East.compile(toLower.toIR(), []);
|
|
253
|
+
* compiled("HELLO"); // "hello"
|
|
254
|
+
* compiled("Hello World"); // "hello world"
|
|
255
|
+
* compiled("TEST123"); // "test123"
|
|
256
|
+
* ```
|
|
257
|
+
*/
|
|
258
|
+
lowerCase() {
|
|
259
|
+
return this[FactorySymbol]({
|
|
260
|
+
ast_type: "Builtin",
|
|
261
|
+
type: StringType,
|
|
262
|
+
location: get_location(2),
|
|
263
|
+
builtin: "StringLowerCase",
|
|
264
|
+
type_parameters: [],
|
|
265
|
+
arguments: [this[AstSymbol]],
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Splits the string into an array of substrings using a delimiter.
|
|
270
|
+
*
|
|
271
|
+
* @param delimiter - The string to split on
|
|
272
|
+
* @returns An ArrayExpr of StringType containing the split parts
|
|
273
|
+
*
|
|
274
|
+
* @example
|
|
275
|
+
* ```ts
|
|
276
|
+
* const splitString = East.function([StringType, StringType], ArrayType(StringType), ($, str, delim) => {
|
|
277
|
+
* $.return(str.split(delim));
|
|
278
|
+
* });
|
|
279
|
+
* const compiled = East.compile(splitString.toIR(), []);
|
|
280
|
+
* compiled("a,b,c", ","); // ["a", "b", "c"]
|
|
281
|
+
* compiled("hello world", " "); // ["hello", "world"]
|
|
282
|
+
* compiled("one", ","); // ["one"]
|
|
283
|
+
* compiled("a,,b", ","); // ["a", "", "b"]
|
|
284
|
+
* ```
|
|
285
|
+
*/
|
|
286
|
+
split(delimiter) {
|
|
287
|
+
const delimiterAst = valueOrExprToAstTyped(delimiter, StringType);
|
|
288
|
+
return this[FactorySymbol]({
|
|
289
|
+
ast_type: "Builtin",
|
|
290
|
+
type: ArrayType(StringType),
|
|
291
|
+
location: get_location(2),
|
|
292
|
+
builtin: "StringSplit",
|
|
293
|
+
type_parameters: [],
|
|
294
|
+
arguments: [this[AstSymbol], delimiterAst],
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Removes whitespace from both the beginning and end of the string.
|
|
299
|
+
*
|
|
300
|
+
* @returns A StringExpr with whitespace trimmed
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* ```ts
|
|
304
|
+
* const trimString = East.function([StringType], StringType, ($, str) => {
|
|
305
|
+
* $.return(str.trim());
|
|
306
|
+
* });
|
|
307
|
+
* const compiled = East.compile(trimString.toIR(), []);
|
|
308
|
+
* compiled(" hello "); // "hello"
|
|
309
|
+
* compiled("test\n"); // "test"
|
|
310
|
+
* compiled(" "); // ""
|
|
311
|
+
* compiled("no-space"); // "no-space"
|
|
312
|
+
* ```
|
|
313
|
+
*/
|
|
314
|
+
trim() {
|
|
315
|
+
return this[FactorySymbol]({
|
|
316
|
+
ast_type: "Builtin",
|
|
317
|
+
type: StringType,
|
|
318
|
+
location: get_location(2),
|
|
319
|
+
builtin: "StringTrim",
|
|
320
|
+
type_parameters: [],
|
|
321
|
+
arguments: [this[AstSymbol]],
|
|
322
|
+
});
|
|
323
|
+
}
|
|
324
|
+
/**
|
|
325
|
+
* Removes whitespace from the beginning of the string.
|
|
326
|
+
*
|
|
327
|
+
* @returns A StringExpr with leading whitespace removed
|
|
328
|
+
*
|
|
329
|
+
* @example
|
|
330
|
+
* ```ts
|
|
331
|
+
* const trimLeft = East.function([StringType], StringType, ($, str) => {
|
|
332
|
+
* $.return(str.trimStart());
|
|
333
|
+
* });
|
|
334
|
+
* const compiled = East.compile(trimLeft.toIR(), []);
|
|
335
|
+
* compiled(" hello"); // "hello"
|
|
336
|
+
* compiled("\ttest"); // "test"
|
|
337
|
+
* compiled(" hello "); // "hello "
|
|
338
|
+
* ```
|
|
339
|
+
*/
|
|
340
|
+
trimStart() {
|
|
341
|
+
return this[FactorySymbol]({
|
|
342
|
+
ast_type: "Builtin",
|
|
343
|
+
type: StringType,
|
|
344
|
+
location: get_location(2),
|
|
345
|
+
builtin: "StringTrimStart",
|
|
346
|
+
type_parameters: [],
|
|
347
|
+
arguments: [this[AstSymbol]],
|
|
348
|
+
});
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Removes whitespace from the end of the string.
|
|
352
|
+
*
|
|
353
|
+
* @returns A StringExpr with trailing whitespace removed
|
|
354
|
+
*
|
|
355
|
+
* @example
|
|
356
|
+
* ```ts
|
|
357
|
+
* const trimRight = East.function([StringType], StringType, ($, str) => {
|
|
358
|
+
* $.return(str.trimEnd());
|
|
359
|
+
* });
|
|
360
|
+
* const compiled = East.compile(trimRight.toIR(), []);
|
|
361
|
+
* compiled("hello "); // "hello"
|
|
362
|
+
* compiled("test\n"); // "test"
|
|
363
|
+
* compiled(" hello "); // " hello"
|
|
364
|
+
* ```
|
|
365
|
+
*/
|
|
366
|
+
trimEnd() {
|
|
367
|
+
return this[FactorySymbol]({
|
|
368
|
+
ast_type: "Builtin",
|
|
369
|
+
type: StringType,
|
|
370
|
+
location: get_location(2),
|
|
371
|
+
builtin: "StringTrimEnd",
|
|
372
|
+
type_parameters: [],
|
|
373
|
+
arguments: [this[AstSymbol]],
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
/**
|
|
377
|
+
* Tests if the string starts with a specified prefix.
|
|
378
|
+
*
|
|
379
|
+
* @param prefix - The prefix to test for
|
|
380
|
+
* @returns A BooleanExpr that is true if the string starts with the prefix
|
|
381
|
+
*
|
|
382
|
+
* @example
|
|
383
|
+
* ```ts
|
|
384
|
+
* const checkPrefix = East.function([StringType, StringType], BooleanType, ($, str, prefix) => {
|
|
385
|
+
* $.return(str.startsWith(prefix));
|
|
386
|
+
* });
|
|
387
|
+
* const compiled = East.compile(checkPrefix.toIR(), []);
|
|
388
|
+
* compiled("hello world", "hello"); // true
|
|
389
|
+
* compiled("test", "te"); // true
|
|
390
|
+
* compiled("hello", "world"); // false
|
|
391
|
+
* compiled("", ""); // true
|
|
392
|
+
* ```
|
|
393
|
+
*/
|
|
394
|
+
startsWith(prefix) {
|
|
395
|
+
const prefixAst = valueOrExprToAstTyped(prefix, StringType);
|
|
396
|
+
return this[FactorySymbol]({
|
|
397
|
+
ast_type: "Builtin",
|
|
398
|
+
type: BooleanType,
|
|
399
|
+
location: get_location(2),
|
|
400
|
+
builtin: "StringStartsWith",
|
|
401
|
+
type_parameters: [],
|
|
402
|
+
arguments: [this[AstSymbol], prefixAst],
|
|
403
|
+
});
|
|
404
|
+
}
|
|
405
|
+
/**
|
|
406
|
+
* Tests if the string ends with a specified suffix.
|
|
407
|
+
*
|
|
408
|
+
* @param suffix - The suffix to test for
|
|
409
|
+
* @returns A BooleanExpr that is true if the string ends with the suffix
|
|
410
|
+
*
|
|
411
|
+
* @example
|
|
412
|
+
* ```ts
|
|
413
|
+
* const checkSuffix = East.function([StringType, StringType], BooleanType, ($, str, suffix) => {
|
|
414
|
+
* $.return(str.endsWith(suffix));
|
|
415
|
+
* });
|
|
416
|
+
* const compiled = East.compile(checkSuffix.toIR(), []);
|
|
417
|
+
* compiled("hello world", "world"); // true
|
|
418
|
+
* compiled("test", "st"); // true
|
|
419
|
+
* compiled("hello", "world"); // false
|
|
420
|
+
* compiled("", ""); // true
|
|
421
|
+
* ```
|
|
422
|
+
*/
|
|
423
|
+
endsWith(suffix) {
|
|
424
|
+
const suffixAst = valueOrExprToAstTyped(suffix, StringType);
|
|
425
|
+
return this[FactorySymbol]({
|
|
426
|
+
ast_type: "Builtin",
|
|
427
|
+
type: BooleanType,
|
|
428
|
+
location: get_location(2),
|
|
429
|
+
builtin: "StringEndsWith",
|
|
430
|
+
type_parameters: [],
|
|
431
|
+
arguments: [this[AstSymbol], suffixAst],
|
|
432
|
+
});
|
|
433
|
+
}
|
|
434
|
+
/**
|
|
435
|
+
* Tests if the string contains a substring or matches a regular expression.
|
|
436
|
+
*
|
|
437
|
+
* @param substringOrRegex - The substring or RegExp pattern to search for
|
|
438
|
+
* @returns A BooleanExpr that is true if the string contains the substring or matches the regex
|
|
439
|
+
*
|
|
440
|
+
* @example
|
|
441
|
+
* ```ts
|
|
442
|
+
* const hasSubstring = East.function([StringType, StringType], BooleanType, ($, str, substr) => {
|
|
443
|
+
* $.return(str.contains(substr));
|
|
444
|
+
* });
|
|
445
|
+
* const compiled = East.compile(hasSubstring.toIR(), []);
|
|
446
|
+
* compiled("hello world", "world"); // true
|
|
447
|
+
* compiled("test", "xyz"); // false
|
|
448
|
+
*
|
|
449
|
+
* // With regex
|
|
450
|
+
* const hasDigits = East.function([StringType], BooleanType, ($, str) => {
|
|
451
|
+
* $.return(str.contains(/\d+/));
|
|
452
|
+
* });
|
|
453
|
+
* compiled = East.compile(hasDigits.toIR(), []);
|
|
454
|
+
* compiled("test123"); // true
|
|
455
|
+
* compiled("test"); // false
|
|
456
|
+
* ```
|
|
457
|
+
*/
|
|
458
|
+
contains(substringOrRegex) {
|
|
459
|
+
if (substringOrRegex instanceof RegExp) {
|
|
460
|
+
const regexObj = substringOrRegex;
|
|
461
|
+
const validation = validateCrossPlatformCompatible(regexObj);
|
|
462
|
+
if (!validation.isValid) {
|
|
463
|
+
console.warn(`RegExp pattern may not be portable across backends: ${regexObj.source}`);
|
|
464
|
+
for (const error of validation.errors) {
|
|
465
|
+
console.warn(` Error: ${error}`);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
if (validation.warnings.length > 0) {
|
|
469
|
+
console.warn(`RegExp pattern has portability warnings: ${regexObj.source}`);
|
|
470
|
+
for (const warning of validation.warnings) {
|
|
471
|
+
console.warn(` Warning: ${warning}`);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
const patternAst = valueOrExprToAstTyped(regexObj.source, StringType);
|
|
475
|
+
const flagsAst = valueOrExprToAstTyped(regexObj.flags, StringType);
|
|
476
|
+
return this[FactorySymbol]({
|
|
477
|
+
ast_type: "Builtin",
|
|
478
|
+
type: BooleanType,
|
|
479
|
+
location: get_location(2),
|
|
480
|
+
builtin: "RegexContains",
|
|
481
|
+
type_parameters: [],
|
|
482
|
+
arguments: [this[AstSymbol], patternAst, flagsAst],
|
|
483
|
+
});
|
|
484
|
+
}
|
|
485
|
+
else {
|
|
486
|
+
const substringAst = valueOrExprToAstTyped(substringOrRegex, StringType);
|
|
487
|
+
return this[FactorySymbol]({
|
|
488
|
+
ast_type: "Builtin",
|
|
489
|
+
type: BooleanType,
|
|
490
|
+
location: get_location(2),
|
|
491
|
+
builtin: "StringContains",
|
|
492
|
+
type_parameters: [],
|
|
493
|
+
arguments: [this[AstSymbol], substringAst],
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
/**
|
|
498
|
+
* Finds the index of the first occurrence of a substring or regex match.
|
|
499
|
+
*
|
|
500
|
+
* @param substringOrRegex - The substring or RegExp pattern to search for
|
|
501
|
+
* @returns An IntegerExpr representing the zero-based index, or -1 if not found
|
|
502
|
+
*
|
|
503
|
+
* @example
|
|
504
|
+
* ```ts
|
|
505
|
+
* const findIndex = East.function([StringType, StringType], IntegerType, ($, str, substr) => {
|
|
506
|
+
* $.return(str.indexOf(substr));
|
|
507
|
+
* });
|
|
508
|
+
* const compiled = East.compile(findIndex.toIR(), []);
|
|
509
|
+
* compiled("hello world", "world"); // 6n
|
|
510
|
+
* compiled("test", "t"); // 0n
|
|
511
|
+
* compiled("hello", "xyz"); // -1n
|
|
512
|
+
*
|
|
513
|
+
* // With regex
|
|
514
|
+
* const findDigit = East.function([StringType], IntegerType, ($, str) => {
|
|
515
|
+
* $.return(str.indexOf(/\d/));
|
|
516
|
+
* });
|
|
517
|
+
* compiled = East.compile(findDigit.toIR(), []);
|
|
518
|
+
* compiled("abc123"); // 3n
|
|
519
|
+
* ```
|
|
520
|
+
*/
|
|
521
|
+
indexOf(substringOrRegex) {
|
|
522
|
+
if (substringOrRegex instanceof RegExp) {
|
|
523
|
+
const regexObj = substringOrRegex;
|
|
524
|
+
const validation = validateCrossPlatformCompatible(regexObj);
|
|
525
|
+
if (!validation.isValid) {
|
|
526
|
+
console.warn(`RegExp pattern may not be portable across backends: ${regexObj.source}`);
|
|
527
|
+
for (const error of validation.errors) {
|
|
528
|
+
console.warn(` Error: ${error}`);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
if (validation.warnings.length > 0) {
|
|
532
|
+
console.warn(`RegExp pattern has portability warnings: ${regexObj.source}`);
|
|
533
|
+
for (const warning of validation.warnings) {
|
|
534
|
+
console.warn(` Warning: ${warning}`);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
const patternAst = valueOrExprToAstTyped(regexObj.source, StringType);
|
|
538
|
+
const flagsAst = valueOrExprToAstTyped(regexObj.flags, StringType);
|
|
539
|
+
return this[FactorySymbol]({
|
|
540
|
+
ast_type: "Builtin",
|
|
541
|
+
type: IntegerType,
|
|
542
|
+
location: get_location(2),
|
|
543
|
+
builtin: "RegexIndexOf",
|
|
544
|
+
type_parameters: [],
|
|
545
|
+
arguments: [this[AstSymbol], patternAst, flagsAst],
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
else {
|
|
549
|
+
const substringAst = valueOrExprToAstTyped(substringOrRegex, StringType);
|
|
550
|
+
return this[FactorySymbol]({
|
|
551
|
+
ast_type: "Builtin",
|
|
552
|
+
type: IntegerType,
|
|
553
|
+
location: get_location(2),
|
|
554
|
+
builtin: "StringIndexOf",
|
|
555
|
+
type_parameters: [],
|
|
556
|
+
arguments: [this[AstSymbol], substringAst],
|
|
557
|
+
});
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
/**
|
|
561
|
+
* Replaces all occurrences of a substring or regex pattern with a replacement string.
|
|
562
|
+
*
|
|
563
|
+
* @param searchValue - The substring or RegExp pattern to search for
|
|
564
|
+
* @param replaceValue - The replacement string (can include regex capture groups like $1, $2)
|
|
565
|
+
* @returns A StringExpr with all occurrences replaced
|
|
566
|
+
*
|
|
567
|
+
* @example
|
|
568
|
+
* ```ts
|
|
569
|
+
* const replaceAll = East.function([StringType, StringType, StringType], StringType, ($, str, search, replace) => {
|
|
570
|
+
* $.return(str.replace(search, replace));
|
|
571
|
+
* });
|
|
572
|
+
* const compiled = East.compile(replaceAll.toIR(), []);
|
|
573
|
+
* compiled("hello hello", "hello", "hi"); // "hi hi"
|
|
574
|
+
* compiled("test_test_value", "test", "prod"); // "prod_prod_value"
|
|
575
|
+
*
|
|
576
|
+
* // With regex and capture groups
|
|
577
|
+
* const swapWords = East.function([StringType], StringType, ($, str) => {
|
|
578
|
+
* $.return(str.replace(/(\w+) (\w+)/, "$2 $1"));
|
|
579
|
+
* });
|
|
580
|
+
* compiled = East.compile(swapWords.toIR(), []);
|
|
581
|
+
* compiled("hello world"); // "world hello"
|
|
582
|
+
* ```
|
|
583
|
+
*/
|
|
584
|
+
replace(searchValue, replaceValue) {
|
|
585
|
+
if (searchValue instanceof RegExp) {
|
|
586
|
+
const regexObj = searchValue;
|
|
587
|
+
// Strip 'g' flag before validation since we handle replaceAll semantics in the backend
|
|
588
|
+
const flagsWithoutG = regexObj.flags.replace(/g/g, '');
|
|
589
|
+
const regexForValidation = new RegExp(regexObj.source, flagsWithoutG);
|
|
590
|
+
const validation = validateCrossPlatformCompatible(regexForValidation);
|
|
591
|
+
if (!validation.isValid) {
|
|
592
|
+
console.warn(`RegExp pattern may not be portable across backends: ${regexObj.source}`);
|
|
593
|
+
for (const error of validation.errors) {
|
|
594
|
+
console.warn(` Error: ${error}`);
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
if (validation.warnings.length > 0) {
|
|
598
|
+
console.warn(`RegExp pattern has portability warnings: ${regexObj.source}`);
|
|
599
|
+
for (const warning of validation.warnings) {
|
|
600
|
+
console.warn(` Warning: ${warning}`);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const patternAst = valueOrExprToAstTyped(regexObj.source, StringType);
|
|
604
|
+
const flagsAst = valueOrExprToAstTyped(regexObj.flags, StringType);
|
|
605
|
+
const replaceAst = valueOrExprToAstTyped(replaceValue, StringType);
|
|
606
|
+
return this[FactorySymbol]({
|
|
607
|
+
ast_type: "Builtin",
|
|
608
|
+
type: StringType,
|
|
609
|
+
location: get_location(2),
|
|
610
|
+
builtin: "RegexReplace",
|
|
611
|
+
type_parameters: [],
|
|
612
|
+
arguments: [this[AstSymbol], patternAst, flagsAst, replaceAst],
|
|
613
|
+
});
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
const searchAst = valueOrExprToAstTyped(searchValue, StringType);
|
|
617
|
+
const replaceAst = valueOrExprToAstTyped(replaceValue, StringType);
|
|
618
|
+
return this[FactorySymbol]({
|
|
619
|
+
ast_type: "Builtin",
|
|
620
|
+
type: StringType,
|
|
621
|
+
location: get_location(2),
|
|
622
|
+
builtin: "StringReplace",
|
|
623
|
+
type_parameters: [],
|
|
624
|
+
arguments: [this[AstSymbol], searchAst, replaceAst],
|
|
625
|
+
});
|
|
626
|
+
}
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Encodes the string as UTF-8 bytes, returning a Blob.
|
|
630
|
+
*
|
|
631
|
+
* @returns A BlobExpr containing the UTF-8 encoded bytes
|
|
632
|
+
*
|
|
633
|
+
* @remarks No byte-order mark (BOM) is added. UTF-8 is a superset of ASCII.
|
|
634
|
+
*
|
|
635
|
+
* @example
|
|
636
|
+
* ```ts
|
|
637
|
+
* const encodeUtf8 = East.function([StringType], BlobType, ($, str) => {
|
|
638
|
+
* $.return(str.encodeUtf8());
|
|
639
|
+
* });
|
|
640
|
+
* const compiled = East.compile(encodeUtf8.toIR(), []);
|
|
641
|
+
* compiled("hello"); // Blob with UTF-8 bytes
|
|
642
|
+
* compiled("😀"); // Blob with UTF-8 encoded emoji
|
|
643
|
+
* ```
|
|
644
|
+
*/
|
|
645
|
+
encodeUtf8() {
|
|
646
|
+
return this[FactorySymbol]({
|
|
647
|
+
ast_type: "Builtin",
|
|
648
|
+
type: BlobType,
|
|
649
|
+
location: get_location(2),
|
|
650
|
+
builtin: "StringEncodeUtf8",
|
|
651
|
+
type_parameters: [],
|
|
652
|
+
arguments: [this[AstSymbol]],
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Encodes the string as UTF-16 bytes, returning a Blob.
|
|
657
|
+
*
|
|
658
|
+
* @returns A BlobExpr containing the UTF-16 encoded bytes
|
|
659
|
+
*
|
|
660
|
+
* @remarks Always uses little-endian with a byte-order mark (BOM). UTF-16 is common on Windows.
|
|
661
|
+
*
|
|
662
|
+
* @example
|
|
663
|
+
* ```ts
|
|
664
|
+
* const encodeUtf16 = East.function([StringType], BlobType, ($, str) => {
|
|
665
|
+
* $.return(str.encodeUtf16());
|
|
666
|
+
* });
|
|
667
|
+
* const compiled = East.compile(encodeUtf16.toIR(), []);
|
|
668
|
+
* compiled("hello"); // Blob with UTF-16 LE bytes + BOM
|
|
669
|
+
* compiled("😀"); // Blob with UTF-16 encoded emoji
|
|
670
|
+
* ```
|
|
671
|
+
*/
|
|
672
|
+
encodeUtf16() {
|
|
673
|
+
return this[FactorySymbol]({
|
|
674
|
+
ast_type: "Builtin",
|
|
675
|
+
type: BlobType,
|
|
676
|
+
location: get_location(2),
|
|
677
|
+
builtin: "StringEncodeUtf16",
|
|
678
|
+
type_parameters: [],
|
|
679
|
+
arguments: [this[AstSymbol]],
|
|
680
|
+
});
|
|
681
|
+
}
|
|
682
|
+
}
|
|
683
|
+
//# sourceMappingURL=string.js.map
|