scriptable 0.0.6 → 0.1.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.
Files changed (44) hide show
  1. package/.changeset/README.md +8 -0
  2. package/.changeset/config.json +11 -0
  3. package/.github/workflows/release.yml +62 -0
  4. package/CHANGELOG.md +14 -0
  5. package/README.md +185 -0
  6. package/dist/binary-ops.d.ts +2 -0
  7. package/dist/binary-ops.js +16 -0
  8. package/dist/builtin-functions.d.ts +3 -0
  9. package/dist/builtin-functions.js +151 -0
  10. package/dist/environment.d.ts +16 -0
  11. package/dist/environment.js +111 -0
  12. package/dist/evaluate.d.ts +5 -0
  13. package/dist/evaluate.js +232 -0
  14. package/dist/index.d.ts +13 -0
  15. package/dist/index.js +26 -0
  16. package/dist/logic-ops.d.ts +2 -0
  17. package/dist/logic-ops.js +5 -0
  18. package/package.json +22 -52
  19. package/scripts/release.js +127 -0
  20. package/src/__tests__/{break-out.js → break-out.test.ts} +3 -14
  21. package/src/__tests__/{builtin-functions-date.js → builtin-functions-date.test.ts} +30 -11
  22. package/src/__tests__/{builtin-functions.js → builtin-functions.test.ts} +2 -3
  23. package/src/__tests__/{expressions.js → expressions.test.ts} +2 -3
  24. package/src/__tests__/{functions.js → functions.test.ts} +7 -8
  25. package/src/__tests__/{logic.js → logic.test.ts} +2 -3
  26. package/src/__tests__/new-features.test.ts +158 -0
  27. package/src/__tests__/security.test.ts +399 -0
  28. package/src/__tests__/{variables.js → variables.test.ts} +3 -6
  29. package/src/binary-ops.ts +18 -0
  30. package/src/builtin-functions.ts +177 -0
  31. package/src/environment.ts +132 -0
  32. package/src/evaluate.ts +275 -0
  33. package/src/index.ts +42 -0
  34. package/src/logic-ops.ts +6 -0
  35. package/tsconfig.json +16 -0
  36. package/.npmignore +0 -1
  37. package/src/__tests__/index.js +0 -85
  38. package/src/binary-ops.js +0 -13
  39. package/src/builtin-functions.js +0 -58
  40. package/src/environment.js +0 -73
  41. package/src/evaluate.js +0 -174
  42. package/src/index.js +0 -26
  43. package/src/logic-ops.js +0 -6
  44. package/yarn.lock +0 -2632
@@ -0,0 +1,232 @@
1
+ import binaryOps from "./binary-ops.js";
2
+ import logicOps from "./logic-ops.js";
3
+ // Properties that enable sandbox escape (prototype chain → Function constructor)
4
+ // or prototype pollution. Blocked in both MemberExpression and deep path access.
5
+ const BLOCKED_PROPS = new Set([
6
+ "constructor",
7
+ "__proto__",
8
+ "prototype",
9
+ "__defineGetter__",
10
+ "__defineSetter__",
11
+ "__lookupGetter__",
12
+ "__lookupSetter__",
13
+ ]);
14
+ function assertSafeProperty(prop) {
15
+ if (BLOCKED_PROPS.has(prop)) {
16
+ throw new Error(`Access to property '${prop}' is not allowed`);
17
+ }
18
+ }
19
+ // Execution step counter — reset via resetStepCounter() before each run.
20
+ let _steps = 0;
21
+ let _maxSteps = 0;
22
+ export function resetStepCounter(maxSteps) {
23
+ _steps = 0;
24
+ _maxSteps = maxSteps;
25
+ }
26
+ function loc(exp) {
27
+ return exp.loc.start;
28
+ }
29
+ function makeFunc(params, body, env, functions) {
30
+ const scope = env.extend();
31
+ return (...args) => {
32
+ params.forEach((param, idx) => {
33
+ scope.def(param, idx < args.length ? args[idx] : false);
34
+ });
35
+ return evaluate(body, scope, functions);
36
+ };
37
+ }
38
+ const evals = {
39
+ ArrowFunctionExpression(exp, env, functions) {
40
+ const params = exp.params.map((param) => evaluate(param));
41
+ return makeFunc(params, exp.body, env, functions);
42
+ },
43
+ ArrayExpression(exp, env, functions) {
44
+ const result = [];
45
+ for (const el of exp.elements) {
46
+ if (el && el.type === "SpreadElement") {
47
+ const spread = evaluate(el.argument, env, functions);
48
+ result.push(...spread);
49
+ }
50
+ else if (el) {
51
+ result.push(evaluate(el, env, functions));
52
+ }
53
+ }
54
+ return result;
55
+ },
56
+ AssignmentExpression(exp, env, functions) {
57
+ const target = evaluate(exp.left);
58
+ return env.set(target, evaluate(exp.right, env, functions), loc(exp));
59
+ },
60
+ BinaryExpression(exp, env, functions) {
61
+ const left = evaluate(exp.left, env, functions);
62
+ const right = evaluate(exp.right, env, functions);
63
+ return binaryOps[exp.operator](left, right);
64
+ },
65
+ BlockStatement(exp, env, functions) {
66
+ const scope = env.extend();
67
+ let result;
68
+ for (const part of exp.body) {
69
+ result = evaluate(part, scope, functions);
70
+ if (part.type === "ReturnStatement") {
71
+ break;
72
+ }
73
+ }
74
+ return result;
75
+ },
76
+ CallExpression(exp, env, functions) {
77
+ const func = evaluate(exp.callee);
78
+ // Collect args, handling spread
79
+ const args = [];
80
+ for (const arg of exp.arguments) {
81
+ if (arg.type === "SpreadElement") {
82
+ const spread = evaluate(arg.argument, env, functions);
83
+ args.push(...spread);
84
+ }
85
+ else {
86
+ args.push(evaluate(arg, env, functions));
87
+ }
88
+ }
89
+ if (functions[func]) {
90
+ return functions[func](...args);
91
+ }
92
+ if (env.defined(func)) {
93
+ const customFunc = env.get(func, loc(exp));
94
+ if (typeof customFunc === "function") {
95
+ return customFunc(...args);
96
+ }
97
+ }
98
+ const l = loc(exp);
99
+ throw new Error(`${func} is not a function (${l.line}:${l.column})`);
100
+ },
101
+ ConditionalExpression(exp, env, functions) {
102
+ const test = evaluate(exp.test, env, functions);
103
+ return test
104
+ ? evaluate(exp.consequent, env, functions)
105
+ : evaluate(exp.alternate, env, functions);
106
+ },
107
+ ExpressionStatement(exp, env, functions) {
108
+ return evaluate(exp.expression, env, functions);
109
+ },
110
+ FunctionDeclaration(exp, env, functions) {
111
+ const name = evaluate(exp.id);
112
+ const params = exp.params.map((param) => evaluate(param));
113
+ env.set(name, makeFunc(params, exp.body, env, functions), loc(exp));
114
+ },
115
+ Identifier(exp, env) {
116
+ if (env) {
117
+ return env.get(exp.name, loc(exp));
118
+ }
119
+ return exp.name;
120
+ },
121
+ IfStatement(exp, env, functions) {
122
+ const test = evaluate(exp.test, env, functions);
123
+ if (test) {
124
+ return evaluate(exp.consequent, env, functions);
125
+ }
126
+ if (exp.alternate) {
127
+ return evaluate(exp.alternate, env, functions);
128
+ }
129
+ },
130
+ Literal(exp) {
131
+ return exp.value;
132
+ },
133
+ LogicalExpression(exp, env, functions) {
134
+ const left = evaluate(exp.left, env, functions);
135
+ const right = evaluate(exp.right, env, functions);
136
+ return logicOps[exp.operator](left, right);
137
+ },
138
+ MemberExpression(exp, env, functions) {
139
+ if (!env) {
140
+ const obj = evaluate(exp.object);
141
+ const prop = evaluate(exp.property);
142
+ assertSafeProperty(prop);
143
+ return `${obj}.${prop}`;
144
+ }
145
+ const obj = evaluate(exp.object, env, functions);
146
+ if (exp.computed) {
147
+ const prop = evaluate(exp.property, env, functions);
148
+ assertSafeProperty(String(prop));
149
+ return obj[prop];
150
+ }
151
+ const prop = evaluate(exp.property);
152
+ assertSafeProperty(prop);
153
+ return obj[prop];
154
+ },
155
+ ObjectExpression(exp, env, functions) {
156
+ const result = {};
157
+ for (const prop of exp.properties) {
158
+ if (prop.type === "SpreadElement") {
159
+ const spread = evaluate(prop.argument, env, functions);
160
+ Object.assign(result, spread);
161
+ }
162
+ else {
163
+ // Property — key can be Identifier or Literal
164
+ const key = prop.key.type === "Identifier" ? prop.key.name : prop.key.value;
165
+ result[key] = evaluate(prop.value, env, functions);
166
+ }
167
+ }
168
+ return result;
169
+ },
170
+ Program(exp, env, functions) {
171
+ let result;
172
+ for (const node of exp.body) {
173
+ result = evaluate(node, env, functions);
174
+ }
175
+ return result;
176
+ },
177
+ ReturnStatement(exp, env, functions) {
178
+ return evaluate(exp.argument, env, functions);
179
+ },
180
+ TemplateLiteral(exp, env, functions) {
181
+ let result = "";
182
+ for (let i = 0; i < exp.quasis.length; i++) {
183
+ result += exp.quasis[i].value.cooked;
184
+ if (i < exp.expressions.length) {
185
+ result += String(evaluate(exp.expressions[i], env, functions));
186
+ }
187
+ }
188
+ return result;
189
+ },
190
+ UnaryExpression(exp, env, functions) {
191
+ const arg = evaluate(exp.argument, env, functions);
192
+ switch (exp.operator) {
193
+ case "!":
194
+ return !arg;
195
+ case "-":
196
+ return -arg;
197
+ case "+":
198
+ return +arg;
199
+ case "typeof":
200
+ return typeof arg;
201
+ default:
202
+ throw new Error(`Unsupported unary operator: ${exp.operator}`);
203
+ }
204
+ },
205
+ VariableDeclaration(exp, env, functions) {
206
+ if (exp.kind === "var") {
207
+ const globalScope = env.getGlobalScope();
208
+ for (const dec of exp.declarations) {
209
+ evaluate(dec, globalScope, functions);
210
+ }
211
+ return;
212
+ }
213
+ for (const dec of exp.declarations) {
214
+ evaluate(dec, env, functions);
215
+ }
216
+ },
217
+ VariableDeclarator(exp, env, functions) {
218
+ const name = evaluate(exp.id);
219
+ const value = exp.init ? evaluate(exp.init, env, functions) : undefined;
220
+ return env.def(name, value);
221
+ },
222
+ };
223
+ function evaluate(exp, env, functions) {
224
+ if (_maxSteps > 0 && ++_steps > _maxSteps) {
225
+ throw new Error("Execution limit exceeded");
226
+ }
227
+ if (evals[exp.type]) {
228
+ return evals[exp.type](exp, env, functions);
229
+ }
230
+ throw new Error("Cannot evaluate " + exp.type);
231
+ }
232
+ export default evaluate;
@@ -0,0 +1,13 @@
1
+ import type { FunctionMap } from "./builtin-functions.js";
2
+ import Environment from "./environment.js";
3
+ export interface RunnerOptions {
4
+ functions?: FunctionMap;
5
+ maxSteps?: number;
6
+ }
7
+ export interface RunResult {
8
+ result: any;
9
+ env: Record<string, any>;
10
+ }
11
+ export default function runner({ functions, maxSteps }?: RunnerOptions): (code: string, env?: Record<string, any>, debug?: boolean) => RunResult;
12
+ export { Environment };
13
+ export type { FunctionMap };
package/dist/index.js ADDED
@@ -0,0 +1,26 @@
1
+ import * as acorn from "acorn";
2
+ import evaluate, { resetStepCounter } from "./evaluate.js";
3
+ import builtinFuncs from "./builtin-functions.js";
4
+ import Environment from "./environment.js";
5
+ function parse(code) {
6
+ return acorn.parse(code, {
7
+ ecmaVersion: "latest",
8
+ sourceType: "script",
9
+ locations: true,
10
+ });
11
+ }
12
+ const DEFAULT_MAX_STEPS = 100000;
13
+ export default function runner({ functions = builtinFuncs, maxSteps = DEFAULT_MAX_STEPS } = {}) {
14
+ return (code, env = {}, debug) => {
15
+ const ast = parse(code);
16
+ debug && console.log(JSON.stringify(ast.body, null, 2));
17
+ resetStepCounter(maxSteps);
18
+ const globalEnv = new Environment();
19
+ Object.keys(env).forEach((key) => {
20
+ globalEnv.def(key, env[key]);
21
+ });
22
+ const result = evaluate(ast, globalEnv, functions);
23
+ return { result, env: globalEnv.vars };
24
+ };
25
+ }
26
+ export { Environment };
@@ -0,0 +1,2 @@
1
+ declare const logicOps: Record<string, (l: unknown, r: unknown) => unknown>;
2
+ export default logicOps;
@@ -0,0 +1,5 @@
1
+ const logicOps = {
2
+ "||": (l, r) => l || r,
3
+ "&&": (l, r) => l && r,
4
+ };
5
+ export default logicOps;
package/package.json CHANGED
@@ -1,64 +1,34 @@
1
1
  {
2
2
  "name": "scriptable",
3
- "version": "0.0.6",
4
- "main": "src/index.js",
3
+ "version": "0.1.1",
4
+ "type": "module",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/index.d.ts",
10
+ "import": "./dist/index.js"
11
+ }
12
+ },
5
13
  "author": "jason@lecstor.com",
6
14
  "license": "MIT",
7
- "devDependencies": {
8
- "eslint": "^4.4.1",
9
- "eslint-config-prettier": "^2.3.0",
10
- "eslint-config-standard": "^10.2.1",
11
- "eslint-plugin-import": "^2.7.0",
12
- "eslint-plugin-node": "^5.1.1",
13
- "eslint-plugin-promise": "^3.5.0",
14
- "eslint-plugin-standard": "^3.0.1",
15
- "jest": "^20.0.4"
16
- },
17
15
  "repository": {
18
16
  "type": "git",
19
17
  "url": "https://github.com/lecstor/scriptable.git"
20
18
  },
21
- "scripts": {
22
- "test": "jest"
23
- },
24
19
  "dependencies": {
25
- "babylon": "^6.18.0",
26
- "lodash": "^4.17.4",
27
- "moment": "^2.18.1",
28
- "moment-timezone": "^0.5.13"
20
+ "acorn": "^8.14.1"
29
21
  },
30
- "eslintConfig": {
31
- "extends": [
32
- "standard",
33
- "prettier"
34
- ],
35
- "env": {
36
- "jest": true
37
- },
38
- "rules": {
39
- "curly": [
40
- "error",
41
- "all"
42
- ],
43
- "brace-style": [
44
- "error",
45
- "1tbs"
46
- ],
47
- "semi": [
48
- "error",
49
- "always",
50
- {
51
- "omitLastInOneLineBlock": true
52
- }
53
- ]
54
- }
22
+ "devDependencies": {
23
+ "@changesets/cli": "^2.30.0",
24
+ "typescript": "^5.7.0",
25
+ "vitest": "^3.1.0"
55
26
  },
56
- "jest": {
57
- "moduleFileExtensions": [
58
- "mjs",
59
- "js"
60
- ],
61
- "testEnvironment": "node",
62
- "verbose": true
27
+ "scripts": {
28
+ "build": "tsc",
29
+ "test": "vitest run",
30
+ "test:watch": "vitest",
31
+ "changeset": "changeset",
32
+ "release": "node scripts/release.js"
63
33
  }
64
- }
34
+ }
@@ -0,0 +1,127 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ * Release a new version end-to-end.
4
+ *
5
+ * Guards:
6
+ * 1. Refuses to run on a dirty tree.
7
+ * 2. Refuses to run off main.
8
+ * 3. Refuses to run if local main is behind origin/main (fetches first).
9
+ * 4. Refuses to run with no pending changesets.
10
+ *
11
+ * Steps:
12
+ * 5. Runs tests + build as a safety gate.
13
+ * 6. Consumes changesets to bump package.json + CHANGELOG.md.
14
+ * 7. Commits the result as "Version X.Y.Z".
15
+ * 8. Shows the commit and prompts for confirmation.
16
+ * 9. On confirm: pushes main, creates the vX.Y.Z annotated tag,
17
+ * pushes the tag. The tag push triggers .github/workflows/release.yml,
18
+ * which publishes to npm via OIDC (with provenance).
19
+ * 10. On abort: prints instructions to undo the version commit and exits 1.
20
+ *
21
+ * There is deliberately only one user-facing pnpm script (`pnpm release`) —
22
+ * the interactive prompt at step 8 is the abort window. Everything before
23
+ * the prompt is local-only and reversible with `git reset --hard HEAD^`.
24
+ */
25
+ import { execSync } from "node:child_process";
26
+ import { readFileSync, readdirSync } from "node:fs";
27
+ import { createInterface } from "node:readline/promises";
28
+ import { stdin, stdout } from "node:process";
29
+
30
+ function run(cmd) {
31
+ execSync(cmd, { stdio: "inherit" });
32
+ }
33
+
34
+ function capture(cmd) {
35
+ return execSync(cmd, { encoding: "utf-8" }).trim();
36
+ }
37
+
38
+ function fail(msg) {
39
+ console.error(`release: ${msg}`);
40
+ process.exit(1);
41
+ }
42
+
43
+ async function prompt(question) {
44
+ const rl = createInterface({ input: stdin, output: stdout });
45
+ try {
46
+ return (await rl.question(question)).trim().toLowerCase();
47
+ } finally {
48
+ rl.close();
49
+ }
50
+ }
51
+
52
+ // 1. Clean tree check.
53
+ if (capture("git status --porcelain")) {
54
+ fail("working tree is not clean — commit or stash first.");
55
+ }
56
+
57
+ // 2. On main?
58
+ const branch = capture("git rev-parse --abbrev-ref HEAD");
59
+ if (branch !== "main") {
60
+ fail(`not on main branch (currently on ${branch}).`);
61
+ }
62
+
63
+ // 3. Up-to-date check.
64
+ run("git fetch origin main");
65
+ const localMain = capture("git rev-parse main");
66
+ const remoteMain = capture("git rev-parse origin/main");
67
+ if (localMain !== remoteMain) {
68
+ const behind = capture(`git rev-list --count ${localMain}..${remoteMain}`);
69
+ if (behind !== "0") {
70
+ fail(`local main is ${behind} commit(s) behind origin/main — pull first.`);
71
+ }
72
+ }
73
+
74
+ // 4. Pending changeset check.
75
+ const pending = readdirSync(".changeset").filter(
76
+ (f) => f.endsWith(".md") && f.toLowerCase() !== "readme.md",
77
+ );
78
+ if (pending.length === 0) {
79
+ fail("no pending changesets — run `pnpm changeset` first.");
80
+ }
81
+ console.log(
82
+ `Found ${pending.length} pending changeset(s): ${pending.join(", ")}\n`,
83
+ );
84
+
85
+ // 5. Safety gate.
86
+ run("pnpm test");
87
+ run("pnpm build");
88
+
89
+ // 6. Consume changesets → bump package.json + CHANGELOG.md.
90
+ run("pnpm exec changeset version");
91
+
92
+ // 7. Commit. Stage only the files changeset version touches.
93
+ const { version } = JSON.parse(readFileSync("./package.json", "utf-8"));
94
+ run("git add package.json CHANGELOG.md .changeset");
95
+ run(`git commit -m "Version ${version}"`);
96
+
97
+ // 8. Show the commit and prompt.
98
+ console.log("");
99
+ run("git --no-pager show HEAD --stat");
100
+ console.log("");
101
+ console.log("-".repeat(60));
102
+ console.log(` About to release scriptable v${version}`);
103
+ console.log("-".repeat(60));
104
+ console.log(` Next: push main, create tag v${version}, push tag`);
105
+ console.log(" -> tag push triggers .github/workflows/release.yml");
106
+ console.log(" -> CI publishes to npm via OIDC (with provenance)");
107
+ console.log("");
108
+ console.log(" This is your last chance to abort. Once the tag is pushed,");
109
+ console.log(" publishing happens automatically.");
110
+ console.log("");
111
+ const answer = await prompt("Proceed? [y/N] ");
112
+
113
+ if (answer !== "y" && answer !== "yes") {
114
+ console.log("\nAborted. To undo the version commit locally:");
115
+ console.log(" git reset --hard HEAD^");
116
+ console.log(" # your .changeset files will come back from the reset");
117
+ process.exit(1);
118
+ }
119
+
120
+ // 9. Push main, tag, push tag.
121
+ run("git push origin main");
122
+ run("pnpm exec changeset tag");
123
+ run("git push --follow-tags");
124
+
125
+ console.log(`\nv${version} tag pushed.`);
126
+ console.log(` https://github.com/lecstor/scriptable/actions`);
127
+ console.log(` The release workflow will publish to npm shortly.`);
@@ -1,24 +1,20 @@
1
- const runner = require("../");
2
-
3
- const DEBUG = true;
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
4
3
 
5
4
  const run = runner();
6
5
 
7
6
  /*
8
7
  * https://blog.liftsecurity.io/2015/04/27/when-this-is-really-that/
9
- *
10
8
  */
11
9
 
12
10
  describe("try to get require", () => {
13
11
  it("this.constructor.constructor", () => {
14
- // https://gist.github.com/domenic/d15dfd8f06ae5d1109b0
15
12
  const code = `bar = this.constructor.constructor`;
16
13
  const env = {};
17
14
  expect(() => run(code, env)).toThrow("Cannot evaluate ThisExpression");
18
15
  });
19
16
 
20
17
  it("object.constructor", () => {
21
- // https://gist.github.com/domenic/d15dfd8f06ae5d1109b0
22
18
  const code = `
23
19
  bar = { any: "thing" };
24
20
  func = bar.constructor;
@@ -27,12 +23,11 @@ describe("try to get require", () => {
27
23
  `;
28
24
  const env = {};
29
25
  expect(() => run(code, env)).toThrow(
30
- new TypeError("func2 is not a function (5:16)")
26
+ "Access to property 'constructor' is not allowed"
31
27
  );
32
28
  });
33
29
 
34
30
  it("arguments.callee.caller", () => {
35
- // https://gist.github.com/bjoerge/2889278
36
31
  const code = `
37
32
  function getFunc () {
38
33
  return arguments.callee.caller.arguments.callee.caller.arguments.callee.caller.arguments.callee.arguments.callee.caller;
@@ -41,12 +36,6 @@ describe("try to get require", () => {
41
36
  func.call(func, "var fs = process.mainModule.require('fs');");
42
37
  `;
43
38
  const env = {};
44
- // try {
45
- // run(code, env, DEBUG);
46
- // } catch (err) {
47
- // console.error(err);
48
- // }
49
- // console.log(env.args);
50
39
  expect(() => run(code, env)).toThrow(
51
40
  new ReferenceError(`"arguments" is not defined (3:15)`)
52
41
  );
@@ -1,33 +1,32 @@
1
- const runner = require("../");
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
2
3
 
3
4
  const run = runner();
4
5
 
5
- const DEBUG = true;
6
-
7
6
  describe("builtin functions", () => {
8
7
  describe("date", () => {
9
- it("formats an iso8610 timestamp (+00:00 -> +00:00)", () => {
8
+ it("formats an iso8601 timestamp (+00:00 -> +00:00)", () => {
10
9
  const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z")`;
11
10
  const { result } = run(code);
12
11
  expect(result).toEqual("Aug 14 2017 11:08am UTC");
13
12
  });
14
13
 
15
- it("formats an iso8610 timestamp (+10:00 -> +00:00)", () => {
14
+ it("formats an iso8601 timestamp (+10:00 -> +00:00)", () => {
16
15
  const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z")`;
17
16
  const { result } = run(code);
18
17
  expect(result).toEqual("Aug 14 2017 1:08am UTC");
19
18
  });
20
19
 
21
- it("formats an iso8610 timestamp (+10:00 -> Australia/Brisbane)", () => {
20
+ it("formats an iso8601 timestamp (+10:00 -> Australia/Brisbane)", () => {
22
21
  const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
23
22
  const { result } = run(code);
24
- expect(result).toEqual("Aug 14 2017 11:08am AEST");
23
+ expect(result).toMatch(/^Aug 14 2017 11:08am (AEST|GMT\+10)$/);
25
24
  });
26
25
 
27
- it("formats an iso8610 timestamp (+00:00 -> Australia/Brisbane)", () => {
26
+ it("formats an iso8601 timestamp (+00:00 -> Australia/Brisbane)", () => {
28
27
  const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
29
28
  const { result } = run(code);
30
- expect(result).toEqual("Aug 14 2017 9:08pm AEST");
29
+ expect(result).toMatch(/^Aug 14 2017 9:08pm (AEST|GMT\+10)$/);
31
30
  });
32
31
 
33
32
  it("formats a Unix timestamp (+00:00 -> +00:00)", () => {
@@ -39,12 +38,12 @@ describe("builtin functions", () => {
39
38
  it("formats a Unix timestamp (+00:00 -> Australia/Brisbane)", () => {
40
39
  const code = `formatDate("1403454068850", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
41
40
  const { result } = run(code);
42
- expect(result).toEqual("Jun 23 2014 2:21am AEST");
41
+ expect(result).toMatch(/^Jun 23 2014 2:21am (AEST|GMT\+10)$/);
43
42
  });
44
43
  });
45
44
 
46
45
  describe("func that wraps formatDate", () => {
47
- it("wraps formatData", () => {
46
+ it("wraps formatDate", () => {
48
47
  const code = `
49
48
  Date = (date) => formatDate(date, "MMM D YYYY h:mma z", customer.tz);
50
49
  Date("2017-08-27T09:00:57.730+10:00");
@@ -53,4 +52,24 @@ describe("builtin functions", () => {
53
52
  expect(result).toEqual("Aug 26 2017 4:00pm PDT");
54
53
  });
55
54
  });
55
+
56
+ describe("additional format tokens", () => {
57
+ it("24-hour format", () => {
58
+ const code = `formatDate("2017-08-14T15:30:45+00:00", "HH:mm:ss", "UTC")`;
59
+ const { result } = run(code);
60
+ expect(result).toEqual("15:30:45");
61
+ });
62
+
63
+ it("full month name and padded day", () => {
64
+ const code = `formatDate("2017-01-05T10:00:00+00:00", "MMMM DD, YYYY")`;
65
+ const { result } = run(code);
66
+ expect(result).toEqual("January 05, 2017");
67
+ });
68
+
69
+ it("2-digit year", () => {
70
+ const code = `formatDate("2017-08-14T11:08:19+00:00", "MM/DD/YY")`;
71
+ const { result } = run(code);
72
+ expect(result).toEqual("08/14/17");
73
+ });
74
+ });
56
75
  });
@@ -1,9 +1,8 @@
1
- const runner = require("../");
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
2
3
 
3
4
  const run = runner();
4
5
 
5
- const DEBUG = true;
6
-
7
6
  describe("builtin functions", () => {
8
7
  it("concat", () => {
9
8
  const code = `concat(list, "i")`;
@@ -1,6 +1,5 @@
1
- const runner = require("../");
2
-
3
- const DEBUG = true;
1
+ import { describe, it, expect } from "vitest";
2
+ import runner from "../index.js";
4
3
 
5
4
  const run = runner();
6
5