@jplorg/jpl 1.0.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.
Files changed (215) hide show
  1. package/README.md +34 -0
  2. package/lib/applyDefaults.js +34 -0
  3. package/lib/builtins/funcContains.js +25 -0
  4. package/lib/builtins/funcEndsWith.js +21 -0
  5. package/lib/builtins/funcError.js +11 -0
  6. package/lib/builtins/funcFromJSON.js +26 -0
  7. package/lib/builtins/funcHas.js +24 -0
  8. package/lib/builtins/funcIn.js +24 -0
  9. package/lib/builtins/funcKeys.js +20 -0
  10. package/lib/builtins/funcLength.js +24 -0
  11. package/lib/builtins/funcNow.js +10 -0
  12. package/lib/builtins/funcStartsWith.js +21 -0
  13. package/lib/builtins/funcToJSON.js +10 -0
  14. package/lib/builtins/funcToNumber.js +26 -0
  15. package/lib/builtins/funcToString.js +10 -0
  16. package/lib/builtins/funcTrim.js +17 -0
  17. package/lib/builtins/funcTrimEnd.js +17 -0
  18. package/lib/builtins/funcTrimStart.js +17 -0
  19. package/lib/builtins/funcType.js +10 -0
  20. package/lib/builtins/funcVoid.js +10 -0
  21. package/lib/builtins/index.js +158 -0
  22. package/lib/builtins/internals.js +8 -0
  23. package/lib/builtins/math.js +40 -0
  24. package/lib/builtins/native.js +164 -0
  25. package/lib/createSingleton.js +14 -0
  26. package/lib/index.js +80 -0
  27. package/lib/interpreter/index.js +57 -0
  28. package/lib/interpreter/parse.js +2377 -0
  29. package/lib/interpreter/parseFunction.js +24 -0
  30. package/lib/interpreter/system.js +10 -0
  31. package/lib/interpreter/util.js +349 -0
  32. package/lib/library/apply.js +142 -0
  33. package/lib/library/definition.js +9 -0
  34. package/lib/library/errors/adaptErrors.js +34 -0
  35. package/lib/library/errors/enclosure.js +16 -0
  36. package/lib/library/errors/error.js +18 -0
  37. package/lib/library/errors/execution.js +20 -0
  38. package/lib/library/errors/fatal.js +15 -0
  39. package/lib/library/errors/runtime.js +78 -0
  40. package/lib/library/errors/syntax.js +15 -0
  41. package/lib/library/functions.js +82 -0
  42. package/lib/library/index.js +336 -0
  43. package/lib/library/mux.js +106 -0
  44. package/lib/library/ops/index.js +60 -0
  45. package/lib/library/ops/op.js +180 -0
  46. package/lib/library/ops/opa.js +41 -0
  47. package/lib/library/ops/opc.js +59 -0
  48. package/lib/library/ops/opm.js +50 -0
  49. package/lib/library/ops/opu.js +77 -0
  50. package/lib/library/runtimeScope.js +38 -0
  51. package/lib/library/runtimeSignal.js +72 -0
  52. package/lib/library/types.js +305 -0
  53. package/lib/program/index.js +72 -0
  54. package/lib/program/ops/index.js +61 -0
  55. package/lib/program/ops/opAccess/index.js +55 -0
  56. package/lib/program/ops/opAccess/opaField.js +50 -0
  57. package/lib/program/ops/opAccess/opaFunction.js +50 -0
  58. package/lib/program/ops/opAccess/opaIter.js +31 -0
  59. package/lib/program/ops/opAccess/opaSlice.js +44 -0
  60. package/lib/program/ops/opAnd.js +30 -0
  61. package/lib/program/ops/opArrayConstructor.js +20 -0
  62. package/lib/program/ops/opAssignment/index.js +90 -0
  63. package/lib/program/ops/opAssignment/opaAssignField.js +125 -0
  64. package/lib/program/ops/opAssignment/opaAssignIter.js +69 -0
  65. package/lib/program/ops/opAssignment/opaAssignSlice.js +99 -0
  66. package/lib/program/ops/opAssignment/opuAddition.js +31 -0
  67. package/lib/program/ops/opAssignment/opuDivision.js +31 -0
  68. package/lib/program/ops/opAssignment/opuMultiplication.js +31 -0
  69. package/lib/program/ops/opAssignment/opuNullCoalescence.js +25 -0
  70. package/lib/program/ops/opAssignment/opuRemainder.js +31 -0
  71. package/lib/program/ops/opAssignment/opuSet.js +19 -0
  72. package/lib/program/ops/opAssignment/opuSubtraction.js +31 -0
  73. package/lib/program/ops/opAssignment/opuUpdate.js +19 -0
  74. package/lib/program/ops/opCalculation/index.js +57 -0
  75. package/lib/program/ops/opCalculation/opmAddition.js +49 -0
  76. package/lib/program/ops/opCalculation/opmDivision.js +42 -0
  77. package/lib/program/ops/opCalculation/opmMultiplication.js +40 -0
  78. package/lib/program/ops/opCalculation/opmRemainder.js +34 -0
  79. package/lib/program/ops/opCalculation/opmSubtraction.js +53 -0
  80. package/lib/program/ops/opComparison/index.js +59 -0
  81. package/lib/program/ops/opComparison/opcEqual.js +19 -0
  82. package/lib/program/ops/opComparison/opcGreater.js +19 -0
  83. package/lib/program/ops/opComparison/opcGreaterEqual.js +19 -0
  84. package/lib/program/ops/opComparison/opcLess.js +19 -0
  85. package/lib/program/ops/opComparison/opcLessEqual.js +19 -0
  86. package/lib/program/ops/opComparison/opcUnequal.js +19 -0
  87. package/lib/program/ops/opConstant.js +18 -0
  88. package/lib/program/ops/opConstantFalse.js +16 -0
  89. package/lib/program/ops/opConstantNull.js +16 -0
  90. package/lib/program/ops/opConstantTrue.js +16 -0
  91. package/lib/program/ops/opFunctionDefinition.js +21 -0
  92. package/lib/program/ops/opIf.js +38 -0
  93. package/lib/program/ops/opInterpolatedString.js +24 -0
  94. package/lib/program/ops/opNegation.js +25 -0
  95. package/lib/program/ops/opNot.js +16 -0
  96. package/lib/program/ops/opNullCoalescence.js +34 -0
  97. package/lib/program/ops/opNumber.js +18 -0
  98. package/lib/program/ops/opObjectConstructor.js +37 -0
  99. package/lib/program/ops/opOr.js +30 -0
  100. package/lib/program/ops/opOutputConcat.js +19 -0
  101. package/lib/program/ops/opString.js +18 -0
  102. package/lib/program/ops/opTry.js +37 -0
  103. package/lib/program/ops/opVariable.js +20 -0
  104. package/lib/program/ops/opVariableDefinition.js +24 -0
  105. package/lib/program/ops/opVoid.js +16 -0
  106. package/lib/program/ops/utils.js +35 -0
  107. package/lib/runtime/index.js +258 -0
  108. package/package.json +67 -0
  109. package/repl.js +212 -0
  110. package/src/applyDefaults.js +35 -0
  111. package/src/builtins/funcContains.js +27 -0
  112. package/src/builtins/funcEndsWith.js +21 -0
  113. package/src/builtins/funcError.js +7 -0
  114. package/src/builtins/funcFromJSON.js +24 -0
  115. package/src/builtins/funcHas.js +25 -0
  116. package/src/builtins/funcIn.js +25 -0
  117. package/src/builtins/funcKeys.js +20 -0
  118. package/src/builtins/funcLength.js +26 -0
  119. package/src/builtins/funcNow.js +5 -0
  120. package/src/builtins/funcStartsWith.js +21 -0
  121. package/src/builtins/funcToJSON.js +5 -0
  122. package/src/builtins/funcToNumber.js +25 -0
  123. package/src/builtins/funcToString.js +5 -0
  124. package/src/builtins/funcTrim.js +16 -0
  125. package/src/builtins/funcTrimEnd.js +16 -0
  126. package/src/builtins/funcTrimStart.js +16 -0
  127. package/src/builtins/funcType.js +5 -0
  128. package/src/builtins/funcVoid.js +5 -0
  129. package/src/builtins/index.js +145 -0
  130. package/src/builtins/internals.js +9 -0
  131. package/src/builtins/math.js +44 -0
  132. package/src/builtins/native.js +19 -0
  133. package/src/createSingleton.js +9 -0
  134. package/src/index.js +44 -0
  135. package/src/interpreter/index.js +48 -0
  136. package/src/interpreter/parse.js +1473 -0
  137. package/src/interpreter/parseFunction.js +20 -0
  138. package/src/interpreter/system.js +5 -0
  139. package/src/interpreter/util.js +237 -0
  140. package/src/library/apply.js +135 -0
  141. package/src/library/definition.js +3 -0
  142. package/src/library/errors/adaptErrors.js +28 -0
  143. package/src/library/errors/enclosure.js +11 -0
  144. package/src/library/errors/error.js +14 -0
  145. package/src/library/errors/execution.js +15 -0
  146. package/src/library/errors/fatal.js +10 -0
  147. package/src/library/errors/runtime.js +71 -0
  148. package/src/library/errors/syntax.js +10 -0
  149. package/src/library/functions.js +73 -0
  150. package/src/library/index.js +41 -0
  151. package/src/library/mux.js +98 -0
  152. package/src/library/ops/index.js +5 -0
  153. package/src/library/ops/op.js +174 -0
  154. package/src/library/ops/opa.js +35 -0
  155. package/src/library/ops/opc.js +53 -0
  156. package/src/library/ops/opm.js +44 -0
  157. package/src/library/ops/opu.js +71 -0
  158. package/src/library/runtimeScope.js +35 -0
  159. package/src/library/runtimeSignal.js +67 -0
  160. package/src/library/types.js +299 -0
  161. package/src/program/index.js +80 -0
  162. package/src/program/ops/index.js +82 -0
  163. package/src/program/ops/opAccess/index.js +55 -0
  164. package/src/program/ops/opAccess/opaField.js +58 -0
  165. package/src/program/ops/opAccess/opaFunction.js +52 -0
  166. package/src/program/ops/opAccess/opaIter.js +31 -0
  167. package/src/program/ops/opAccess/opaSlice.js +58 -0
  168. package/src/program/ops/opAnd.js +32 -0
  169. package/src/program/ops/opArrayConstructor.js +16 -0
  170. package/src/program/ops/opAssignment/index.js +102 -0
  171. package/src/program/ops/opAssignment/opaAssignField.js +156 -0
  172. package/src/program/ops/opAssignment/opaAssignIter.js +74 -0
  173. package/src/program/ops/opAssignment/opaAssignSlice.js +157 -0
  174. package/src/program/ops/opAssignment/opuAddition.js +29 -0
  175. package/src/program/ops/opAssignment/opuDivision.js +29 -0
  176. package/src/program/ops/opAssignment/opuMultiplication.js +29 -0
  177. package/src/program/ops/opAssignment/opuNullCoalescence.js +26 -0
  178. package/src/program/ops/opAssignment/opuRemainder.js +29 -0
  179. package/src/program/ops/opAssignment/opuSet.js +15 -0
  180. package/src/program/ops/opAssignment/opuSubtraction.js +29 -0
  181. package/src/program/ops/opAssignment/opuUpdate.js +15 -0
  182. package/src/program/ops/opCalculation/index.js +64 -0
  183. package/src/program/ops/opCalculation/opmAddition.js +63 -0
  184. package/src/program/ops/opCalculation/opmDivision.js +47 -0
  185. package/src/program/ops/opCalculation/opmMultiplication.js +52 -0
  186. package/src/program/ops/opCalculation/opmRemainder.js +49 -0
  187. package/src/program/ops/opCalculation/opmSubtraction.js +72 -0
  188. package/src/program/ops/opComparison/index.js +67 -0
  189. package/src/program/ops/opComparison/opcEqual.js +17 -0
  190. package/src/program/ops/opComparison/opcGreater.js +17 -0
  191. package/src/program/ops/opComparison/opcGreaterEqual.js +17 -0
  192. package/src/program/ops/opComparison/opcLess.js +17 -0
  193. package/src/program/ops/opComparison/opcLessEqual.js +17 -0
  194. package/src/program/ops/opComparison/opcUnequal.js +17 -0
  195. package/src/program/ops/opConstant.js +13 -0
  196. package/src/program/ops/opConstantFalse.js +11 -0
  197. package/src/program/ops/opConstantNull.js +11 -0
  198. package/src/program/ops/opConstantTrue.js +11 -0
  199. package/src/program/ops/opFunctionDefinition.js +17 -0
  200. package/src/program/ops/opIf.js +44 -0
  201. package/src/program/ops/opInterpolatedString.js +27 -0
  202. package/src/program/ops/opNegation.js +26 -0
  203. package/src/program/ops/opNot.js +11 -0
  204. package/src/program/ops/opNullCoalescence.js +36 -0
  205. package/src/program/ops/opNumber.js +13 -0
  206. package/src/program/ops/opObjectConstructor.js +40 -0
  207. package/src/program/ops/opOr.js +32 -0
  208. package/src/program/ops/opOutputConcat.js +17 -0
  209. package/src/program/ops/opString.js +13 -0
  210. package/src/program/ops/opTry.js +38 -0
  211. package/src/program/ops/opVariable.js +17 -0
  212. package/src/program/ops/opVariableDefinition.js +18 -0
  213. package/src/program/ops/opVoid.js +11 -0
  214. package/src/program/ops/utils.js +19 -0
  215. package/src/runtime/index.js +309 -0
package/repl.js ADDED
@@ -0,0 +1,212 @@
1
+ #!/usr/bin/env node
2
+
3
+ const fs = require('fs');
4
+ const os = require('os');
5
+ const path = require('path');
6
+ const readline = require('readline');
7
+ const { default: jpl } = require('@jplorg/jpl');
8
+ const pkg = require('@jplorg/jpl/package.json');
9
+
10
+ const replKeys = [':', '!'];
11
+ const defaultReplKey = replKeys[0];
12
+
13
+ const homeDir = os.homedir();
14
+ const historyFile = homeDir ? path.join(homeDir, '.jpl_repl_history') : undefined;
15
+
16
+ const rl = readline.createInterface({
17
+ input: process.stdin,
18
+ output: process.stdout,
19
+ history: readHistory(),
20
+ historySize: 50,
21
+ removeHistoryDuplicates: true,
22
+ prompt: '> ',
23
+ });
24
+
25
+ function readHistory() {
26
+ if (!historyFile) return [];
27
+ try {
28
+ return fs
29
+ .readFileSync(historyFile)
30
+ .toString()
31
+ .split(/\r?\n|\r/)
32
+ .filter(Boolean)
33
+ .reverse();
34
+ } catch (err) {
35
+ return [];
36
+ }
37
+ }
38
+
39
+ if (historyFile) {
40
+ rl.on('history', (history) => {
41
+ try {
42
+ fs.writeFileSync(historyFile, history.filter(Boolean).reverse().join('\n') + '\n', {
43
+ mode: 0o600,
44
+ });
45
+ } catch (err) {
46
+ // ignore
47
+ }
48
+ });
49
+ }
50
+
51
+ rl.on('close', () => {
52
+ process.exit(0);
53
+ });
54
+
55
+ rl.on('SIGINT', () => {
56
+ if (rl.cursor === 0) return process.exit(0);
57
+ process.stdout.write(`\nTo exit, press Ctrl+C again or type ${defaultReplKey}e`);
58
+ rl.clearLine();
59
+ rl.prompt();
60
+ return undefined;
61
+ });
62
+
63
+ let keep;
64
+ let inputs;
65
+ let measureTime;
66
+
67
+ main();
68
+
69
+ async function main() {
70
+ console.log(`Welcome to JPL v${pkg.version}.`);
71
+ console.log(`Type "${defaultReplKey}h" for more information.\n`);
72
+
73
+ const options = await jpl.getOptions();
74
+ options.runtime.vars.exit = jpl.nativeFunction(() => {
75
+ process.exit(0);
76
+ });
77
+ options.runtime.vars.clear = jpl.nativeFunction(() => {
78
+ console.clear();
79
+ return [];
80
+ });
81
+
82
+ rl.prompt();
83
+
84
+ // eslint-disable-next-line no-restricted-syntax
85
+ for await (const line of rl) {
86
+ rl.pause();
87
+ await handle(line);
88
+ rl.resume();
89
+ }
90
+ }
91
+
92
+ async function handle(input) {
93
+ if (!keep || inputs.length === 0) inputs = [null];
94
+
95
+ let line = input;
96
+ const t = line.trimStart();
97
+
98
+ if (!t) {
99
+ rl.prompt();
100
+ return undefined;
101
+ }
102
+
103
+ if (replKeys.some((replKey) => t.startsWith(replKey))) {
104
+ const command = (t[1] ?? ' ').toLowerCase();
105
+
106
+ line = t.substring(2);
107
+
108
+ switch (command) {
109
+ case 'h':
110
+ printHelp();
111
+ break;
112
+
113
+ case 'e':
114
+ case 'q':
115
+ return process.exit(0);
116
+
117
+ case 'c':
118
+ console.clear();
119
+ break;
120
+
121
+ case 'k':
122
+ keep = parseBool(line, !keep) ?? keep;
123
+ break;
124
+
125
+ case 't':
126
+ measureTime = parseBool(line, !measureTime) ?? measureTime;
127
+ break;
128
+
129
+ case 'i':
130
+ try {
131
+ console.log(JSON.stringify((await jpl.parse(line)).definition, null, 2));
132
+ } catch (err) {
133
+ if (jpl.JPLSyntaxError.is(err)) console.log(`${err.name ?? 'JPLError'}: ${err.message}`);
134
+ else console.log(err.stack);
135
+ }
136
+ break;
137
+
138
+ case ' ':
139
+ console.log('Error: missing REPL command\n');
140
+ printHelp();
141
+ break;
142
+
143
+ default:
144
+ console.log(`Error: unrecognized REPL command ${defaultReplKey}${command}\n`);
145
+ printHelp();
146
+ }
147
+ } else {
148
+ try {
149
+ const program = await jpl.parse(line);
150
+ let before;
151
+ let diff;
152
+ if (measureTime) before = Date.now();
153
+ inputs = await program.run(inputs);
154
+ if (measureTime) diff = Date.now() - before;
155
+ console.log(inputs.map((output) => JSON.stringify(output, null, 2)).join(', '));
156
+ if (measureTime) console.log(`-> took ${diff / 1000}s`);
157
+ } catch (err) {
158
+ if (jpl.JPLSyntaxError.is(err) || jpl.JPLExecutionError.is(err))
159
+ console.log(`${err.name ?? 'JPLError'}: ${err.message}`);
160
+ else console.log(err.stack);
161
+ }
162
+ }
163
+
164
+ rl.prompt();
165
+ return undefined;
166
+ }
167
+
168
+ function parseBool(input, defaultValue) {
169
+ const b = input.trim().toLowerCase();
170
+ let v;
171
+ if (b.length === 0) v = defaultValue;
172
+ else if (b === 'on' || ['true', 'yes', 'enabled'].some((e) => e.startsWith(b))) v = true;
173
+ else if (b === 'off' || ['false', 'no', 'disabled'].some((e) => e.startsWith(b))) v = false;
174
+ if (typeof v === 'boolean') {
175
+ console.log(` -> ${v ? 'on' : 'off'}`);
176
+ return v;
177
+ }
178
+ console.log(`Error: invalid boolean ${b}`);
179
+ return null;
180
+ }
181
+
182
+ function printBool(value) {
183
+ return `boolean (${value ? 'on' : 'off'})`;
184
+ }
185
+
186
+ function printHelp() {
187
+ const commands = [
188
+ ['c', '', 'Clear the console screen'],
189
+ ['e', '', 'Exit the REPL'],
190
+ ['h', '', 'Print this help message'],
191
+ ['i', 'program', 'Interpret the specified program without executing it'],
192
+ [
193
+ 'k',
194
+ printBool(keep),
195
+ 'Set whether program output should be kept as input for the next program',
196
+ ],
197
+ ['t', printBool(measureTime), 'Set whether execution time should be measured'],
198
+ ['q', '', 'Exit the REPL'],
199
+ ];
200
+ const aLen = commands.reduce((sum, [, a]) => Math.max(sum, a.length), 0);
201
+
202
+ console.log(`JPL v${pkg.version} REPL reference\n`);
203
+ console.log(
204
+ `The following synonymous tokens may be used to precede a command: ${replKeys.join('')}\n`,
205
+ );
206
+
207
+ commands.forEach(([c, a, d]) =>
208
+ console.log(`${defaultReplKey}${c} ${a}${' '.repeat(aLen - a.length + 3)}${d}`),
209
+ );
210
+
211
+ console.log('\nPress Ctrl+C to abort current expression, Ctrl+D to exit the REPL');
212
+ }
@@ -0,0 +1,35 @@
1
+ /** Apply the specified defaults to the config */
2
+ function applyDefaults(config, defaults, ...strictPaths) {
3
+ const iter = (c, d, s) => {
4
+ if (Array.isArray(c)) return c;
5
+
6
+ if (typeof c === 'object') {
7
+ if (c === null || Array.isArray(d) || typeof d !== 'object' || d === null) return c;
8
+ const result = { ...c };
9
+ const isStrict = s.some((entry) => entry.length === 0);
10
+ Object.keys({ ...c, ...d }).forEach((key) => {
11
+ if (isStrict) {
12
+ if (!Object.hasOwn(result, key)) result[key] = d[key];
13
+ } else {
14
+ result[key] = iter(
15
+ result[key],
16
+ d[key],
17
+ s.filter((entry) => entry[0] === key).map((entry) => entry.slice(1)),
18
+ );
19
+ }
20
+ if (result[key] === undefined) delete result[key];
21
+ });
22
+ return result;
23
+ }
24
+
25
+ return c === undefined ? d : c;
26
+ };
27
+
28
+ return iter(
29
+ config,
30
+ defaults,
31
+ strictPaths.map((path) => (typeof path === 'string' ? path.split('.') : path)),
32
+ );
33
+ }
34
+
35
+ export default applyDefaults;
@@ -0,0 +1,27 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input, arg0) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ const token = runtime.unwrapValue(arg0 ?? null);
8
+ const tt = runtime.type(token);
9
+
10
+ switch (t) {
11
+ case 'array':
12
+ return next(value.some((item) => runtime.equals(item, token)));
13
+
14
+ case 'string':
15
+ if (tt === 'string') return next(value.includes(token));
16
+ break;
17
+
18
+ case 'object':
19
+ return next(Object.values(value).some((item) => runtime.equals(item, token)));
20
+
21
+ default:
22
+ }
23
+
24
+ throw new JPLTypeError('%s (%*<100v) cannot contain %s (%*<100v)', t, value, tt, token);
25
+ }
26
+
27
+ export default builtin;
@@ -0,0 +1,21 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input, arg0) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ const token = runtime.unwrapValue(arg0 ?? null);
8
+ const tt = runtime.type(token);
9
+
10
+ switch (t) {
11
+ case 'string':
12
+ if (tt === 'string') return next(value.endsWith(token));
13
+ break;
14
+
15
+ default:
16
+ }
17
+
18
+ throw new JPLTypeError('%s (%*<100v) cannot end with %s (%*<100v)', t, value, tt, token);
19
+ }
20
+
21
+ export default builtin;
@@ -0,0 +1,7 @@
1
+ import { JPLRuntimeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input) {
4
+ throw new JPLRuntimeError(input);
5
+ }
6
+
7
+ export default builtin;
@@ -0,0 +1,24 @@
1
+ import { JPLRuntimeError, JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ switch (t) {
8
+ case 'string': {
9
+ let result;
10
+ try {
11
+ result = JSON.parse(value);
12
+ } catch (err) {
13
+ throw new JPLRuntimeError(err.message);
14
+ }
15
+ return next(result);
16
+ }
17
+
18
+ default:
19
+ }
20
+
21
+ throw new JPLTypeError('%s (%*<100v) cannot be parsed as JSON', t, value);
22
+ }
23
+
24
+ export default builtin;
@@ -0,0 +1,25 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input, arg0) {
4
+ const value = runtime.unwrapValue(input);
5
+ const tv = runtime.type(value);
6
+
7
+ const key = runtime.unwrapValue(arg0 ?? null);
8
+ const tk = runtime.type(key);
9
+
10
+ switch (tv) {
11
+ case 'array':
12
+ if (tk === 'number') return next(key >= 0 && key < value.length);
13
+ break;
14
+
15
+ case 'object':
16
+ if (tk === 'string') return next(Object.hasOwn(value, key));
17
+ break;
18
+
19
+ default:
20
+ }
21
+
22
+ throw new JPLTypeError('%s (%*<100v) cannot have %s (%*<100v) as key', tv, value, tk, key);
23
+ }
24
+
25
+ export default builtin;
@@ -0,0 +1,25 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input, arg0) {
4
+ const key = runtime.unwrapValue(input);
5
+ const tk = runtime.type(key);
6
+
7
+ const value = runtime.unwrapValue(arg0 ?? null);
8
+ const tv = runtime.type(value);
9
+
10
+ switch (tv) {
11
+ case 'array':
12
+ if (tk === 'number') return next(key >= 0 && key < value.length);
13
+ break;
14
+
15
+ case 'object':
16
+ if (tk === 'string') return next(Object.hasOwn(value, key));
17
+ break;
18
+
19
+ default:
20
+ }
21
+
22
+ throw new JPLTypeError('%s (%*<100v) cannot have %s (%*<100v) as key', tv, value, tk, key);
23
+ }
24
+
25
+ export default builtin;
@@ -0,0 +1,20 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ switch (t) {
8
+ case 'array':
9
+ return next(value.map((_, i) => i));
10
+
11
+ case 'object':
12
+ return next(Object.keys(value));
13
+
14
+ default:
15
+ }
16
+
17
+ throw new JPLTypeError('%s (%*<100v) has no keys', t, value);
18
+ }
19
+
20
+ export default builtin;
@@ -0,0 +1,26 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ switch (t) {
8
+ case 'null':
9
+ return next(0);
10
+
11
+ case 'array':
12
+ return next(value.length);
13
+
14
+ case 'string':
15
+ return next([...value].length);
16
+
17
+ case 'object':
18
+ return next(Object.keys(value).length);
19
+
20
+ default:
21
+ }
22
+
23
+ throw new JPLTypeError('%s (%*<100v) has no length', t, value);
24
+ }
25
+
26
+ export default builtin;
@@ -0,0 +1,5 @@
1
+ function builtin(runtime, signal, next) {
2
+ return next(Date.now());
3
+ }
4
+
5
+ export default builtin;
@@ -0,0 +1,21 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input, arg0) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ const token = runtime.unwrapValue(arg0 ?? null);
8
+ const tt = runtime.type(token);
9
+
10
+ switch (t) {
11
+ case 'string':
12
+ if (tt === 'string') return next(value.startsWith(token));
13
+ break;
14
+
15
+ default:
16
+ }
17
+
18
+ throw new JPLTypeError('%s (%*<100v) cannot start with %s (%*<100v)', t, value, tt, token);
19
+ }
20
+
21
+ export default builtin;
@@ -0,0 +1,5 @@
1
+ function builtin(runtime, signal, next, input) {
2
+ return next(runtime.stringifyJSON(input, false));
3
+ }
4
+
5
+ export default builtin;
@@ -0,0 +1,25 @@
1
+ import { JPLTypeConversionError, JPLTypeError } from '../library';
2
+
3
+ function builtin(runtime, signal, next, input) {
4
+ const value = runtime.unwrapValue(input);
5
+ const t = runtime.type(value);
6
+
7
+ switch (t) {
8
+ case 'number':
9
+ return next(value);
10
+
11
+ case 'string': {
12
+ const result = runtime.normalizeValue(+value);
13
+ if (value === '' || runtime.type(result) !== 'number') {
14
+ throw new JPLTypeConversionError('%s (%*<100v) does not contain a valid number', t, value);
15
+ }
16
+ return next(result);
17
+ }
18
+
19
+ default:
20
+ }
21
+
22
+ throw new JPLTypeError('%s (%*<100v) cannot be parsed as a number', t, value);
23
+ }
24
+
25
+ export default builtin;
@@ -0,0 +1,5 @@
1
+ function builtin(runtime, signal, next, input) {
2
+ return next(runtime.stringifyJSON(input, true));
3
+ }
4
+
5
+ export default builtin;
@@ -0,0 +1,16 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ async function builtin(runtime, signal, next, input) {
4
+ const t = runtime.type(input);
5
+
6
+ switch (t) {
7
+ case 'string':
8
+ return next(await runtime.alterValue(input, (value) => value.trim()));
9
+
10
+ default:
11
+ }
12
+
13
+ throw new JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
14
+ }
15
+
16
+ export default builtin;
@@ -0,0 +1,16 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ async function builtin(runtime, signal, next, input) {
4
+ const t = runtime.type(input);
5
+
6
+ switch (t) {
7
+ case 'string':
8
+ return next(await runtime.alterValue(input, (value) => value.trimEnd()));
9
+
10
+ default:
11
+ }
12
+
13
+ throw new JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
14
+ }
15
+
16
+ export default builtin;
@@ -0,0 +1,16 @@
1
+ import { JPLTypeError } from '../library';
2
+
3
+ async function builtin(runtime, signal, next, input) {
4
+ const t = runtime.type(input);
5
+
6
+ switch (t) {
7
+ case 'string':
8
+ return next(await runtime.alterValue(input, (value) => value.trimStart()));
9
+
10
+ default:
11
+ }
12
+
13
+ throw new JPLTypeError('%s (%*<100v) cannot be trimmed', t, runtime.unwrapValue(input));
14
+ }
15
+
16
+ export default builtin;
@@ -0,0 +1,5 @@
1
+ function builtin(runtime, signal, next, input) {
2
+ return next(runtime.type(input));
3
+ }
4
+
5
+ export default builtin;
@@ -0,0 +1,5 @@
1
+ function builtin() {
2
+ return [];
3
+ }
4
+
5
+ export default builtin;