scriptable 0.2.0 → 0.2.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.
@@ -1,75 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("builtin functions", () => {
7
- describe("date", () => {
8
- it("formats an iso8601 timestamp (+00:00 -> +00:00)", () => {
9
- const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z")`;
10
- const { result } = run(code);
11
- expect(result).toEqual("Aug 14 2017 11:08am UTC");
12
- });
13
-
14
- it("formats an iso8601 timestamp (+10:00 -> +00:00)", () => {
15
- const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z")`;
16
- const { result } = run(code);
17
- expect(result).toEqual("Aug 14 2017 1:08am UTC");
18
- });
19
-
20
- it("formats an iso8601 timestamp (+10:00 -> Australia/Brisbane)", () => {
21
- const code = `formatDate("2017-08-14T11:08:19+10:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
22
- const { result } = run(code);
23
- expect(result).toMatch(/^Aug 14 2017 11:08am (AEST|GMT\+10)$/);
24
- });
25
-
26
- it("formats an iso8601 timestamp (+00:00 -> Australia/Brisbane)", () => {
27
- const code = `formatDate("2017-08-14T11:08:19+00:00", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
28
- const { result } = run(code);
29
- expect(result).toMatch(/^Aug 14 2017 9:08pm (AEST|GMT\+10)$/);
30
- });
31
-
32
- it("formats a Unix timestamp (+00:00 -> +00:00)", () => {
33
- const code = `formatDate("1403454068850", "MMM D YYYY h:mma z")`;
34
- const { result } = run(code);
35
- expect(result).toEqual("Jun 22 2014 4:21pm UTC");
36
- });
37
-
38
- it("formats a Unix timestamp (+00:00 -> Australia/Brisbane)", () => {
39
- const code = `formatDate("1403454068850", "MMM D YYYY h:mma z", "Australia/Brisbane")`;
40
- const { result } = run(code);
41
- expect(result).toMatch(/^Jun 23 2014 2:21am (AEST|GMT\+10)$/);
42
- });
43
- });
44
-
45
- describe("func that wraps formatDate", () => {
46
- it("wraps formatDate", () => {
47
- const code = `
48
- Date = (date) => formatDate(date, "MMM D YYYY h:mma z", customer.tz);
49
- Date("2017-08-27T09:00:57.730+10:00");
50
- `;
51
- const { result } = run(code, { customer: { tz: "America/Los_Angeles" } });
52
- expect(result).toEqual("Aug 26 2017 4:00pm PDT");
53
- });
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
- });
75
- });
@@ -1,56 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("builtin functions", () => {
7
- it("concat", () => {
8
- const code = `concat(list, "i")`;
9
- const env = { list: ["me", "myself"] };
10
- const { result } = run(code, env);
11
- expect(result).toEqual(["me", "myself", "i"]);
12
- });
13
-
14
- it("push", () => {
15
- const code = `push(list, "i")`;
16
- const env = { list: ["me", "myself"] };
17
- const { result } = run(code, env);
18
- expect(result).toBeUndefined();
19
- expect(env.list).toEqual(["me", "myself", "i"]);
20
- });
21
-
22
- it("shift", () => {
23
- const code = `shift(list)`;
24
- const env = { list: ["me", "myself"] };
25
- const { result } = run(code, env);
26
- expect(result).toEqual("me");
27
- expect(env.list).toEqual(["myself"]);
28
- });
29
-
30
- it("numberFormatter - no args", () => {
31
- const code = `formatD = numberFormatter(); formatD(12340);`;
32
- const env = {};
33
- const { result } = run(code, env);
34
- expect(result).toEqual("12,340");
35
- });
36
-
37
- it("numberFormatter - with args", () => {
38
- const code = `
39
- formatD = numberFormatter("en-us", {
40
- minimumFractionDigits: 2,
41
- maximumFractionDigits: 2
42
- });
43
- formatD(12340);
44
- `;
45
- const env = {};
46
- const { result } = run(code, env);
47
- expect(result).toEqual("12,340.00");
48
- });
49
-
50
- it("formatDollars", () => {
51
- const code = `formatDollars(12340);`;
52
- const env = {};
53
- const { result } = run(code, env);
54
- expect(result).toEqual("12,340.00");
55
- });
56
- });
@@ -1,13 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("expressions", () => {
7
- it("6 + 3", () => {
8
- const code = `6 + 3`;
9
- const env = {};
10
- const { result } = run(code, env);
11
- expect(result).toEqual(9);
12
- });
13
- });
@@ -1,81 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("function", () => {
7
- it("defines a function", () => {
8
- const code = `
9
- function foo(bar){ return bar + 3; }
10
- baz = foo(7);
11
- `;
12
- const vars = {};
13
- const { env } = run(code, vars);
14
- expect(env.foo).toBeDefined();
15
- expect(env.baz).toEqual(10);
16
- });
17
-
18
- it("defines an arrow function", () => {
19
- const code = `
20
- foo = (bar) => { return bar + 3; }
21
- baz = foo(7);
22
- `;
23
- const vars = {};
24
- const { env } = run(code, vars);
25
- expect(env.foo).toBeDefined();
26
- expect(env.baz).toEqual(10);
27
- });
28
-
29
- it("has access to env", () => {
30
- const code = `
31
- greet = (name) => "hello " + name;
32
- greet(customer.name);
33
- `;
34
- const { result } = run(code, { customer: { name: "Jason" } });
35
- expect(result).toEqual("hello Jason");
36
- });
37
-
38
- it("can call a builtin with a function as arg", () => {
39
- const code = `
40
- nprices = map(prices, (price) => { price });
41
- `;
42
- const vars = { prices: [11, 22, 33] };
43
- const { env } = run(code, vars);
44
- expect(env).toEqual({
45
- prices: [11, 22, 33],
46
- nprices: [11, 22, 33],
47
- });
48
- });
49
-
50
- it("can call a builtin with a function as arg", () => {
51
- const code = `
52
- addTax = (price) => { return price + price / 10 };
53
- incTax = map(prices, (price) => { return addTax(price) });
54
- incTax2 = map(prices, (price) => addTax(price));
55
- incTax3 = map(prices, addTax);
56
- `;
57
- const vars = { prices: [10, 20, 30] };
58
- const { env } = run(code, vars);
59
- expect(env.incTax).toEqual([11, 22, 33]);
60
- expect(env.incTax2).toEqual([11, 22, 33]);
61
- expect(env.incTax3).toEqual([11, 22, 33]);
62
- });
63
-
64
- it("can use an array as a function arg", () => {
65
- const code = `
66
- getLength = (list) => list.length;
67
- getLength([1, 2, 3]);
68
- `;
69
- const { result } = run(code, {});
70
- expect(result).toEqual(3);
71
- });
72
-
73
- it("can use an object as a function arg", () => {
74
- const code = `
75
- getLength = (obj) => keys(obj).length;
76
- getLength({ one: 1, two: 2, three: 3 });
77
- `;
78
- const { result } = run(code, {});
79
- expect(result).toEqual(3);
80
- });
81
- });
@@ -1,54 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("logic", () => {
7
- describe("operators", () => {
8
- it("gt true", () => {
9
- const code = `6 > 3`;
10
- const env = {};
11
- const { result } = run(code, env);
12
- expect(result).toEqual(true);
13
- });
14
-
15
- it("gt false", () => {
16
- const code = `3 > 6`;
17
- const env = {};
18
- const { result } = run(code, env);
19
- expect(result).toEqual(false);
20
- });
21
- });
22
-
23
- describe("if", () => {
24
- it("if gt true", () => {
25
- const code = `if (6 > 3) { true }`;
26
- const env = {};
27
- const { result } = run(code, env);
28
- expect(result).toEqual(true);
29
- });
30
- });
31
-
32
- describe("||", () => {
33
- it("handles ||", () => {
34
- const code = `thing = who || "world"; thing`;
35
- const env = { who: "Me" };
36
- const { result } = run(code, env);
37
- expect(result).toEqual("Me");
38
- });
39
- });
40
- describe("&&", () => {
41
- it("handles && - true", () => {
42
- const code = `if (1 && 2) { thing = "true" }`;
43
- const vars = {};
44
- const { env } = run(code, vars);
45
- expect(env.thing).toEqual("true");
46
- });
47
- it("handles && - false", () => {
48
- const code = `if (0 && 2) { thing = "true" }`;
49
- const env = {};
50
- run(code, env);
51
- expect(env.thing).toBeUndefined();
52
- });
53
- });
54
- });
@@ -1,158 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import runner from "../index.js";
3
-
4
- const run = runner();
5
-
6
- describe("template literals", () => {
7
- it("evaluates a simple template literal", () => {
8
- const code = "result = `hello world`";
9
- const { env } = run(code, {});
10
- expect(env.result).toEqual("hello world");
11
- });
12
-
13
- it("evaluates template literal with expression", () => {
14
- const code = "name = \"Jason\"; result = `hello ${name}`";
15
- const { env } = run(code, {});
16
- expect(env.result).toEqual("hello Jason");
17
- });
18
-
19
- it("evaluates template literal with multiple expressions", () => {
20
- const code = "a = 1; b = 2; result = `${a} + ${b} = ${a + b}`";
21
- const { env } = run(code, {});
22
- expect(env.result).toEqual("1 + 2 = 3");
23
- });
24
- });
25
-
26
- describe("conditional (ternary) expression", () => {
27
- it("evaluates truthy ternary", () => {
28
- const code = `result = true ? "yes" : "no"`;
29
- const { env } = run(code, {});
30
- expect(env.result).toEqual("yes");
31
- });
32
-
33
- it("evaluates falsy ternary", () => {
34
- const code = `result = false ? "yes" : "no"`;
35
- const { env } = run(code, {});
36
- expect(env.result).toEqual("no");
37
- });
38
-
39
- it("evaluates ternary with complex test", () => {
40
- const code = `x = 5; result = x > 3 ? "big" : "small"`;
41
- const { env } = run(code, {});
42
- expect(env.result).toEqual("big");
43
- });
44
- });
45
-
46
- describe("spread element", () => {
47
- it("spreads in array literal", () => {
48
- const code = `
49
- a = [1, 2];
50
- b = [...a, 3, 4];
51
- `;
52
- const { env } = run(code, {});
53
- expect(env.b).toEqual([1, 2, 3, 4]);
54
- });
55
-
56
- it("spreads in function call", () => {
57
- const code = `
58
- args = [1, 2, 3];
59
- result = concat(args, ...args);
60
- `;
61
- const { env } = run(code, {});
62
- expect(env.result).toEqual([1, 2, 3, 1, 2, 3]);
63
- });
64
-
65
- it("spreads in object literal", () => {
66
- const code = `
67
- base = { a: 1, b: 2 };
68
- result = { ...base, c: 3 };
69
- `;
70
- const { env } = run(code, {});
71
- expect(env.result).toEqual({ a: 1, b: 2, c: 3 });
72
- });
73
- });
74
-
75
- describe("unary expressions", () => {
76
- it("negation", () => {
77
- const code = `result = !true`;
78
- const { env } = run(code, {});
79
- expect(env.result).toEqual(false);
80
- });
81
-
82
- it("unary minus", () => {
83
- const code = `result = -5`;
84
- const { env } = run(code, {});
85
- expect(env.result).toEqual(-5);
86
- });
87
-
88
- it("typeof", () => {
89
- const code = `result = typeof "hello"`;
90
- const { env } = run(code, {});
91
- expect(env.result).toEqual("string");
92
- });
93
- });
94
-
95
- describe("const/let declarations with expressions", () => {
96
- it("const with function call", () => {
97
- const code = `
98
- const items = concat([1, 2], 3);
99
- `;
100
- const { env } = run(code, {});
101
- expect(env.items).toEqual([1, 2, 3]);
102
- });
103
-
104
- it("let with binary expression", () => {
105
- const code = `
106
- let x = 5 + 3;
107
- `;
108
- const { env } = run(code, {});
109
- expect(env.x).toEqual(8);
110
- });
111
- });
112
-
113
- describe("if/else", () => {
114
- it("evaluates else branch", () => {
115
- const code = `
116
- if (false) {
117
- result = "yes";
118
- } else {
119
- result = "no";
120
- }
121
- `;
122
- const { env } = run(code, {});
123
- expect(env.result).toEqual("no");
124
- });
125
-
126
- it("evaluates else-if chain", () => {
127
- const code = `
128
- x = 2;
129
- if (x == 1) {
130
- result = "one";
131
- } else if (x == 2) {
132
- result = "two";
133
- } else {
134
- result = "other";
135
- }
136
- `;
137
- const { env } = run(code, {});
138
- expect(env.result).toEqual("two");
139
- });
140
- });
141
-
142
- describe("custom injected functions", () => {
143
- it("uses injected functions", () => {
144
- const run = runner({
145
- functions: {
146
- add: (a: number, b: number) => a + b,
147
- greet: (name: string) => `hello ${name}`,
148
- },
149
- });
150
- const code = `
151
- const sum = add(1, 2);
152
- const msg = greet("world");
153
- `;
154
- const { env } = run(code, {});
155
- expect(env.sum).toEqual(3);
156
- expect(env.msg).toEqual("hello world");
157
- });
158
- });