scriptable 0.2.0 → 0.2.2
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 +21 -0
- package/dist/evaluate.js +4 -0
- package/package.json +5 -1
- package/.changeset/README.md +0 -8
- package/.changeset/config.json +0 -11
- package/.github/workflows/release.yml +0 -62
- package/CHANGELOG.md +0 -42
- package/SECURITY_FIXES.md +0 -81
- package/scripts/release.js +0 -127
- package/src/__tests__/audit-followups.test.ts +0 -206
- package/src/__tests__/break-out.test.ts +0 -43
- package/src/__tests__/builtin-functions-date.test.ts +0 -75
- package/src/__tests__/builtin-functions.test.ts +0 -56
- package/src/__tests__/expressions.test.ts +0 -13
- package/src/__tests__/functions.test.ts +0 -81
- package/src/__tests__/logic.test.ts +0 -54
- package/src/__tests__/new-features.test.ts +0 -158
- package/src/__tests__/security.test.ts +0 -790
- package/src/__tests__/semantic-fixes.test.ts +0 -171
- package/src/__tests__/variables.test.ts +0 -200
- package/src/binary-ops.ts +0 -18
- package/src/builtin-functions.ts +0 -177
- package/src/environment.ts +0 -192
- package/src/evaluate.ts +0 -414
- package/src/index.ts +0 -83
- package/tsconfig.json +0 -16
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import runner from "../index.js";
|
|
3
|
-
|
|
4
|
-
const run = runner();
|
|
5
|
-
|
|
6
|
-
// Regressions for three semantic/security bugs discovered in the v0.1.1 audit.
|
|
7
|
-
|
|
8
|
-
describe("functions lookup uses hasOwn (no Object.prototype walk)", () => {
|
|
9
|
-
it("`constructor()` does not resolve to Object via prototype chain", () => {
|
|
10
|
-
// Pre-fix: `functions['constructor']` inherited `Object` from Object.prototype,
|
|
11
|
-
// so this call succeeded and returned `{}`. Post-fix the builtin map is
|
|
12
|
-
// checked with hasOwn, so the call falls through to env lookup and throws.
|
|
13
|
-
expect(() => run(`constructor()`)).toThrow(
|
|
14
|
-
"Access to property 'constructor' is not allowed"
|
|
15
|
-
);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
it("`toString()` does not resolve to Object.prototype.toString", () => {
|
|
19
|
-
expect(() => run(`toString()`)).toThrow(
|
|
20
|
-
/is not a function|is not defined/
|
|
21
|
-
);
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
it("`valueOf()` does not resolve to Object.prototype.valueOf", () => {
|
|
25
|
-
expect(() => run(`valueOf()`)).toThrow(/is not a function|is not defined/);
|
|
26
|
-
});
|
|
27
|
-
|
|
28
|
-
it("`hasOwnProperty()` does not resolve to Object.prototype method", () => {
|
|
29
|
-
expect(() => run(`hasOwnProperty()`)).toThrow(
|
|
30
|
-
/is not a function|is not defined/
|
|
31
|
-
);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
it("real builtins still resolve", () => {
|
|
35
|
-
const { result } = run(`map([1, 2, 3], (x) => x * 2)`);
|
|
36
|
-
expect(result).toEqual([2, 4, 6]);
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
it("user-supplied functions map with Object.prototype inheritance is still safe", () => {
|
|
40
|
-
// A caller may pass a plain object literal. hasOwn defends against that
|
|
41
|
-
// too, not just the default builtins.
|
|
42
|
-
const run2 = runner({ functions: { add: (a: number, b: number) => a + b } });
|
|
43
|
-
expect(() => run2(`constructor()`)).toThrow(
|
|
44
|
-
/Access to property|is not a function|is not defined/
|
|
45
|
-
);
|
|
46
|
-
const { result } = run2(`add(2, 3)`);
|
|
47
|
-
expect(result).toEqual(5);
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe("LogicalExpression short-circuits", () => {
|
|
52
|
-
it("`false && side()` does not evaluate side effect", () => {
|
|
53
|
-
const code = `
|
|
54
|
-
count = 0;
|
|
55
|
-
side = () => { count = count + 1; return 1; };
|
|
56
|
-
result = false && side();
|
|
57
|
-
count;
|
|
58
|
-
`;
|
|
59
|
-
const { result } = run(code);
|
|
60
|
-
expect(result).toEqual(0);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
it("`true || side()` does not evaluate side effect", () => {
|
|
64
|
-
const code = `
|
|
65
|
-
count = 0;
|
|
66
|
-
side = () => { count = count + 1; return 1; };
|
|
67
|
-
result = true || side();
|
|
68
|
-
count;
|
|
69
|
-
`;
|
|
70
|
-
const { result } = run(code);
|
|
71
|
-
expect(result).toEqual(0);
|
|
72
|
-
});
|
|
73
|
-
|
|
74
|
-
it("`true && side()` does evaluate the right side", () => {
|
|
75
|
-
const code = `
|
|
76
|
-
count = 0;
|
|
77
|
-
side = () => { count = count + 1; return 42; };
|
|
78
|
-
result = true && side();
|
|
79
|
-
[result, count];
|
|
80
|
-
`;
|
|
81
|
-
const { result } = run(code);
|
|
82
|
-
expect(result).toEqual([42, 1]);
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
it("`null ?? value` returns value (and `0 ?? x` returns 0)", () => {
|
|
86
|
-
expect(run(`null ?? "fallback"`).result).toEqual("fallback");
|
|
87
|
-
expect(run(`0 ?? "fallback"`).result).toEqual(0);
|
|
88
|
-
expect(run(`"" ?? "fallback"`).result).toEqual("");
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
it("`x ?? side()` does not evaluate side effect when x is defined", () => {
|
|
92
|
-
const code = `
|
|
93
|
-
count = 0;
|
|
94
|
-
side = () => { count = count + 1; return "fallback"; };
|
|
95
|
-
result = "value" ?? side();
|
|
96
|
-
count;
|
|
97
|
-
`;
|
|
98
|
-
const { result } = run(code);
|
|
99
|
-
expect(result).toEqual(0);
|
|
100
|
-
});
|
|
101
|
-
});
|
|
102
|
-
|
|
103
|
-
describe("AssignmentExpression compound operators", () => {
|
|
104
|
-
it("`+=` adds", () => {
|
|
105
|
-
const { env } = run(`a = 10; a += 5;`);
|
|
106
|
-
expect(env.a).toEqual(15);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it("`-=` subtracts", () => {
|
|
110
|
-
const { env } = run(`a = 10; a -= 3;`);
|
|
111
|
-
expect(env.a).toEqual(7);
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
it("`*=` multiplies", () => {
|
|
115
|
-
const { env } = run(`a = 4; a *= 3;`);
|
|
116
|
-
expect(env.a).toEqual(12);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("`/=` divides", () => {
|
|
120
|
-
const { env } = run(`a = 12; a /= 4;`);
|
|
121
|
-
expect(env.a).toEqual(3);
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
it("`%=` modulo", () => {
|
|
125
|
-
const { env } = run(`a = 10; a %= 3;`);
|
|
126
|
-
expect(env.a).toEqual(1);
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
it("`+=` on strings concatenates", () => {
|
|
130
|
-
const { env } = run(`s = "hello"; s += " world";`);
|
|
131
|
-
expect(env.s).toEqual("hello world");
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
it("compound assignment works on nested paths", () => {
|
|
135
|
-
const { env } = run(`o = { n: 1 }; o.n += 10;`);
|
|
136
|
-
expect(env.o.n).toEqual(11);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
it("`||=` writes only when lhs is falsy", () => {
|
|
140
|
-
expect(run(`a = 0; a ||= 5; a;`).result).toEqual(5);
|
|
141
|
-
expect(run(`a = 7; a ||= 5; a;`).result).toEqual(7);
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it("`&&=` writes only when lhs is truthy", () => {
|
|
145
|
-
expect(run(`a = 0; a &&= 5; a;`).result).toEqual(0);
|
|
146
|
-
expect(run(`a = 7; a &&= 5; a;`).result).toEqual(5);
|
|
147
|
-
});
|
|
148
|
-
|
|
149
|
-
it("`??=` writes only when lhs is nullish", () => {
|
|
150
|
-
expect(run(`a = null; a ??= 5; a;`).result).toEqual(5);
|
|
151
|
-
expect(run(`a = 0; a ??= 5; a;`).result).toEqual(0);
|
|
152
|
-
});
|
|
153
|
-
|
|
154
|
-
it("`||=` does not evaluate rhs when lhs is truthy", () => {
|
|
155
|
-
const code = `
|
|
156
|
-
count = 0;
|
|
157
|
-
side = () => { count = count + 1; return 42; };
|
|
158
|
-
a = 7;
|
|
159
|
-
a ||= side();
|
|
160
|
-
[a, count];
|
|
161
|
-
`;
|
|
162
|
-
const { result } = run(code);
|
|
163
|
-
expect(result).toEqual([7, 0]);
|
|
164
|
-
});
|
|
165
|
-
|
|
166
|
-
it("compound assignment still blocks dangerous paths on the LHS", () => {
|
|
167
|
-
expect(() => run(`o = {}; o.__proto__ += 1`)).toThrow(
|
|
168
|
-
"Access to property '__proto__' is not allowed"
|
|
169
|
-
);
|
|
170
|
-
});
|
|
171
|
-
});
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import { describe, it, expect } from "vitest";
|
|
2
|
-
import runner from "../index.js";
|
|
3
|
-
|
|
4
|
-
const run = runner();
|
|
5
|
-
|
|
6
|
-
describe("define global vars", () => {
|
|
7
|
-
it(`foo`, () => {
|
|
8
|
-
const code = `foo`;
|
|
9
|
-
const vars = {};
|
|
10
|
-
expect(() => run(code, vars)).toThrow(
|
|
11
|
-
new ReferenceError(`"foo" is not defined (1:0)`)
|
|
12
|
-
);
|
|
13
|
-
});
|
|
14
|
-
it("let foo", () => {
|
|
15
|
-
const code = `let foo;`;
|
|
16
|
-
const vars = {};
|
|
17
|
-
const { result, env } = run(code, vars);
|
|
18
|
-
expect(result).toEqual(undefined);
|
|
19
|
-
expect(env).toEqual({ foo: undefined });
|
|
20
|
-
});
|
|
21
|
-
it("const foo", () => {
|
|
22
|
-
const code = `const foo;`;
|
|
23
|
-
const vars = {};
|
|
24
|
-
expect(() => run(code, vars)).toThrow(SyntaxError);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it(`foo = "bar"`, () => {
|
|
28
|
-
const code = `foo = "bar";`;
|
|
29
|
-
const vars = {};
|
|
30
|
-
const { result, env } = run(code, vars);
|
|
31
|
-
expect(result).toEqual("bar");
|
|
32
|
-
expect(env).toEqual({ foo: "bar" });
|
|
33
|
-
});
|
|
34
|
-
it(`var foo = "bar"`, () => {
|
|
35
|
-
const code = `var foo = "bar";`;
|
|
36
|
-
const vars = {};
|
|
37
|
-
const { result, env } = run(code, vars);
|
|
38
|
-
expect(result).toEqual(undefined);
|
|
39
|
-
expect(env).toEqual({ foo: "bar" });
|
|
40
|
-
});
|
|
41
|
-
it(`let foo = "bar"`, () => {
|
|
42
|
-
const code = `let foo = "bar";`;
|
|
43
|
-
const vars = {};
|
|
44
|
-
const { result, env } = run(code, vars);
|
|
45
|
-
expect(result).toEqual(undefined);
|
|
46
|
-
expect(env).toEqual({ foo: "bar" });
|
|
47
|
-
});
|
|
48
|
-
it(`const foo = "bar"`, () => {
|
|
49
|
-
const code = `const foo = "bar";`;
|
|
50
|
-
const vars = {};
|
|
51
|
-
const { result, env } = run(code, vars);
|
|
52
|
-
expect(result).toEqual(undefined);
|
|
53
|
-
expect(env).toEqual({ foo: "bar" });
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("define vars in scope", () => {
|
|
58
|
-
it(`foo = "foo"`, () => {
|
|
59
|
-
const code = `
|
|
60
|
-
if (true) {
|
|
61
|
-
foo = "foo";
|
|
62
|
-
}
|
|
63
|
-
`;
|
|
64
|
-
const vars = {};
|
|
65
|
-
const { result, env } = run(code, vars);
|
|
66
|
-
expect(result).toEqual("foo");
|
|
67
|
-
expect(env).toEqual({ foo: "foo" });
|
|
68
|
-
});
|
|
69
|
-
it(`var foo = "foo"`, () => {
|
|
70
|
-
const code = `
|
|
71
|
-
if (true) {
|
|
72
|
-
var foo = "foo";
|
|
73
|
-
}
|
|
74
|
-
`;
|
|
75
|
-
const vars = {};
|
|
76
|
-
const { result, env } = run(code, vars);
|
|
77
|
-
expect(result).toEqual(undefined);
|
|
78
|
-
expect(env).toEqual({ foo: "foo" });
|
|
79
|
-
});
|
|
80
|
-
it(`let foo = "foo"`, () => {
|
|
81
|
-
const code = `
|
|
82
|
-
if (true) {
|
|
83
|
-
let foo = "foo";
|
|
84
|
-
}
|
|
85
|
-
`;
|
|
86
|
-
const vars = {};
|
|
87
|
-
const { result, env } = run(code, vars);
|
|
88
|
-
expect(result).toEqual(undefined);
|
|
89
|
-
expect(env).toEqual({ foo: undefined });
|
|
90
|
-
});
|
|
91
|
-
it(`const foo = "foo"`, () => {
|
|
92
|
-
const code = `
|
|
93
|
-
if (true) {
|
|
94
|
-
const foo = "foo";
|
|
95
|
-
}
|
|
96
|
-
`;
|
|
97
|
-
const vars = {};
|
|
98
|
-
const { result, env } = run(code, vars);
|
|
99
|
-
expect(result).toEqual(undefined);
|
|
100
|
-
expect(env).toEqual({ foo: undefined });
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
|
|
104
|
-
describe("bare variables eval", () => {
|
|
105
|
-
it("evals a bare var", () => {
|
|
106
|
-
const code = `foo`;
|
|
107
|
-
const env = { foo: "bar" };
|
|
108
|
-
const { result } = run(code, env);
|
|
109
|
-
expect(result).toEqual("bar");
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it("evals a deep bare var", () => {
|
|
113
|
-
const code = `foo["bar"]`;
|
|
114
|
-
const env = { foo: { bar: "baz" } };
|
|
115
|
-
const { result } = run(code, env);
|
|
116
|
-
expect(result).toEqual("baz");
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it("evals an array index", () => {
|
|
120
|
-
const code = `foo[1]`;
|
|
121
|
-
const env = { foo: ["me", "myself", "i"] };
|
|
122
|
-
const { result } = run(code, env);
|
|
123
|
-
expect(result).toEqual("myself");
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("evals a deeper bare var", () => {
|
|
127
|
-
const code = `foo["bar"]["baz"]`;
|
|
128
|
-
const env = { foo: { bar: { baz: "fiz" } } };
|
|
129
|
-
const { result } = run(code, env);
|
|
130
|
-
expect(result).toEqual("fiz");
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
it("evals a dotted deep bare var", () => {
|
|
134
|
-
const code = `foo.bar`;
|
|
135
|
-
const env = { foo: { bar: "baz" } };
|
|
136
|
-
const { result } = run(code, env);
|
|
137
|
-
expect(result).toEqual("baz");
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
it("evals a deeper bare var", () => {
|
|
141
|
-
const code = `foo.bar.baz`;
|
|
142
|
-
const env = { foo: { bar: { baz: "fiz" } } };
|
|
143
|
-
const { result } = run(code, env);
|
|
144
|
-
expect(result).toEqual("fiz");
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
|
|
148
|
-
describe("assignment", () => {
|
|
149
|
-
it("assigns top level var", () => {
|
|
150
|
-
const code = `foo = "bar"`;
|
|
151
|
-
const vars = {};
|
|
152
|
-
const { env } = run(code, vars);
|
|
153
|
-
expect(env).toEqual({ foo: "bar" });
|
|
154
|
-
});
|
|
155
|
-
it("re-assigns third level var", () => {
|
|
156
|
-
const code = `foo.baz.fiz = "bar"`;
|
|
157
|
-
const vars = { foo: { baz: { fiz: "b00" } } };
|
|
158
|
-
const { env } = run(code, vars);
|
|
159
|
-
expect(env).toEqual({ foo: { baz: { fiz: "bar" } } });
|
|
160
|
-
});
|
|
161
|
-
|
|
162
|
-
it("modifies var from inbuilt function", () => {
|
|
163
|
-
const code = `forEach(foo, bar => { bar.baz = 2 })`;
|
|
164
|
-
const env = { foo: [{ baz: 1 }, { baz: 1 }] };
|
|
165
|
-
run(code, env);
|
|
166
|
-
expect(env).toEqual({ foo: [{ baz: 2 }, { baz: 2 }] });
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
// these probably shouldn't actually work..
|
|
170
|
-
it("assigns second level var", () => {
|
|
171
|
-
const code = `foo.baz = "bar"`;
|
|
172
|
-
const vars = {};
|
|
173
|
-
const { env } = run(code, vars);
|
|
174
|
-
expect(env).toEqual({ foo: { baz: "bar" } });
|
|
175
|
-
});
|
|
176
|
-
it("assigns third level var", () => {
|
|
177
|
-
const code = `foo.baz.fiz = "bar"`;
|
|
178
|
-
const vars = {};
|
|
179
|
-
const { env } = run(code, vars);
|
|
180
|
-
expect(env).toEqual({ foo: { baz: { fiz: "bar" } } });
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
|
|
184
|
-
describe("objects", () => {
|
|
185
|
-
it("creates an object", () => {
|
|
186
|
-
const code = `foo = { bar: "baz" };`;
|
|
187
|
-
const vars = {};
|
|
188
|
-
const { result, env } = run(code, vars);
|
|
189
|
-
expect(result).toEqual({ bar: "baz" });
|
|
190
|
-
expect(env).toEqual({ foo: { bar: "baz" } });
|
|
191
|
-
});
|
|
192
|
-
|
|
193
|
-
it("creates an array", () => {
|
|
194
|
-
const code = `foo = ["bar", "baz"];`;
|
|
195
|
-
const vars = {};
|
|
196
|
-
const { result, env } = run(code, vars);
|
|
197
|
-
expect(result).toEqual(["bar", "baz"]);
|
|
198
|
-
expect(env).toEqual({ foo: ["bar", "baz"] });
|
|
199
|
-
});
|
|
200
|
-
});
|
package/src/binary-ops.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
const binaryOps: Record<string, (l: unknown, r: unknown) => unknown> = {
|
|
2
|
-
"+": (l: any, r: any) => l + r,
|
|
3
|
-
"-": (l: any, r: any) => l - r,
|
|
4
|
-
"*": (l: any, r: any) => l * r,
|
|
5
|
-
"/": (l: any, r: any) => l / r,
|
|
6
|
-
"%": (l: any, r: any) => l % r,
|
|
7
|
-
|
|
8
|
-
">": (l: any, r: any) => l > r,
|
|
9
|
-
"<": (l: any, r: any) => l < r,
|
|
10
|
-
">=": (l: any, r: any) => l >= r,
|
|
11
|
-
"<=": (l: any, r: any) => l <= r,
|
|
12
|
-
"==": (l: any, r: any) => l === r,
|
|
13
|
-
"!=": (l: any, r: any) => l !== r,
|
|
14
|
-
"===": (l: any, r: any) => l === r,
|
|
15
|
-
"!==": (l: any, r: any) => l !== r,
|
|
16
|
-
};
|
|
17
|
-
|
|
18
|
-
export default binaryOps;
|
package/src/builtin-functions.ts
DELETED
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
export type FunctionMap = Record<string, (...args: any[]) => any>;
|
|
2
|
-
|
|
3
|
-
// ---------------------------------------------------------------------------
|
|
4
|
-
// Date helpers (Intl-based, no moment dependency)
|
|
5
|
-
// ---------------------------------------------------------------------------
|
|
6
|
-
|
|
7
|
-
function getDateParts(date: Date, tz: string) {
|
|
8
|
-
const fmt = (opts: Intl.DateTimeFormatOptions) =>
|
|
9
|
-
new Intl.DateTimeFormat("en-US", { timeZone: tz, ...opts });
|
|
10
|
-
|
|
11
|
-
const year = parseInt(fmt({ year: "numeric" }).format(date));
|
|
12
|
-
const month = parseInt(fmt({ month: "numeric" }).format(date));
|
|
13
|
-
const day = parseInt(fmt({ day: "numeric" }).format(date));
|
|
14
|
-
|
|
15
|
-
const h24Parts = fmt({
|
|
16
|
-
hour: "numeric",
|
|
17
|
-
minute: "numeric",
|
|
18
|
-
second: "numeric",
|
|
19
|
-
hourCycle: "h23",
|
|
20
|
-
}).formatToParts(date);
|
|
21
|
-
|
|
22
|
-
const part = (type: string) =>
|
|
23
|
-
h24Parts.find((p) => p.type === type)?.value || "0";
|
|
24
|
-
const hour24 = parseInt(part("hour"));
|
|
25
|
-
const minute = parseInt(part("minute"));
|
|
26
|
-
const second = parseInt(part("second"));
|
|
27
|
-
|
|
28
|
-
const hour12 = hour24 === 0 ? 12 : hour24 > 12 ? hour24 - 12 : hour24;
|
|
29
|
-
const ampm = hour24 >= 12 ? "pm" : "am";
|
|
30
|
-
|
|
31
|
-
const monthShort = fmt({ month: "short" }).format(date);
|
|
32
|
-
const monthLong = fmt({ month: "long" }).format(date);
|
|
33
|
-
|
|
34
|
-
const tzAbbr =
|
|
35
|
-
fmt({ timeZoneName: "short" })
|
|
36
|
-
.formatToParts(date)
|
|
37
|
-
.find((p) => p.type === "timeZoneName")?.value || "";
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
year, month, day,
|
|
41
|
-
hour12, hour24, minute, second,
|
|
42
|
-
ampm, monthShort, monthLong, tzAbbr,
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const FORMAT_TOKENS =
|
|
47
|
-
/YYYY|YY|MMMM|MMM|MM|M|DD|D|HH|H|hh|h|mm|m|ss|s|a|A|z/g;
|
|
48
|
-
|
|
49
|
-
function applyFormat(parts: ReturnType<typeof getDateParts>, format: string) {
|
|
50
|
-
return format.replace(FORMAT_TOKENS, (token) => {
|
|
51
|
-
switch (token) {
|
|
52
|
-
case "YYYY": return String(parts.year);
|
|
53
|
-
case "YY": return String(parts.year).slice(-2);
|
|
54
|
-
case "MMMM": return parts.monthLong;
|
|
55
|
-
case "MMM": return parts.monthShort;
|
|
56
|
-
case "MM": return String(parts.month).padStart(2, "0");
|
|
57
|
-
case "M": return String(parts.month);
|
|
58
|
-
case "DD": return String(parts.day).padStart(2, "0");
|
|
59
|
-
case "D": return String(parts.day);
|
|
60
|
-
case "HH": return String(parts.hour24).padStart(2, "0");
|
|
61
|
-
case "H": return String(parts.hour24);
|
|
62
|
-
case "hh": return String(parts.hour12).padStart(2, "0");
|
|
63
|
-
case "h": return String(parts.hour12);
|
|
64
|
-
case "mm": return String(parts.minute).padStart(2, "0");
|
|
65
|
-
case "m": return String(parts.minute);
|
|
66
|
-
case "ss": return String(parts.second).padStart(2, "0");
|
|
67
|
-
case "s": return String(parts.second);
|
|
68
|
-
case "a": return parts.ampm;
|
|
69
|
-
case "A": return parts.ampm.toUpperCase();
|
|
70
|
-
case "z": return parts.tzAbbr;
|
|
71
|
-
default: return token;
|
|
72
|
-
}
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
// ---------------------------------------------------------------------------
|
|
77
|
-
// Number helpers
|
|
78
|
-
// ---------------------------------------------------------------------------
|
|
79
|
-
|
|
80
|
-
function numberFormatter(...args: any[]) {
|
|
81
|
-
return new Intl.NumberFormat(...args).format;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
const functions: FunctionMap = {
|
|
85
|
-
push(...args: any[]) {
|
|
86
|
-
const [target, item] = args;
|
|
87
|
-
target.push(item);
|
|
88
|
-
},
|
|
89
|
-
|
|
90
|
-
shift(...args: any[]) {
|
|
91
|
-
const [target] = args;
|
|
92
|
-
return target.shift();
|
|
93
|
-
},
|
|
94
|
-
|
|
95
|
-
currentDate(tz?: string) {
|
|
96
|
-
return new Date();
|
|
97
|
-
},
|
|
98
|
-
|
|
99
|
-
formatDate(dateString: string, format: string, timezone?: string) {
|
|
100
|
-
const date = /^\d+$/.test(dateString)
|
|
101
|
-
? new Date(+dateString)
|
|
102
|
-
: new Date(dateString);
|
|
103
|
-
const tz = timezone || "UTC";
|
|
104
|
-
return applyFormat(getDateParts(date, tz), format);
|
|
105
|
-
},
|
|
106
|
-
|
|
107
|
-
numberFormatter,
|
|
108
|
-
|
|
109
|
-
formatDollars: numberFormatter("en-us", {
|
|
110
|
-
minimumFractionDigits: 2,
|
|
111
|
-
maximumFractionDigits: 2,
|
|
112
|
-
}),
|
|
113
|
-
|
|
114
|
-
// Array/collection operations (replaces lodash)
|
|
115
|
-
map(arr: any[], fn: (...args: any[]) => any) {
|
|
116
|
-
return arr.map(fn);
|
|
117
|
-
},
|
|
118
|
-
filter(arr: any[], fn: (...args: any[]) => any) {
|
|
119
|
-
return arr.filter(fn);
|
|
120
|
-
},
|
|
121
|
-
find(arr: any[], fn: (...args: any[]) => any) {
|
|
122
|
-
return arr.find(fn);
|
|
123
|
-
},
|
|
124
|
-
forEach(arr: any[], fn: (...args: any[]) => any) {
|
|
125
|
-
arr.forEach(fn);
|
|
126
|
-
},
|
|
127
|
-
reduce(arr: any[], fn: (...args: any[]) => any, initial?: any) {
|
|
128
|
-
return initial !== undefined ? arr.reduce(fn, initial) : arr.reduce(fn);
|
|
129
|
-
},
|
|
130
|
-
concat(arr: any[], ...items: any[]) {
|
|
131
|
-
return arr.concat(...items);
|
|
132
|
-
},
|
|
133
|
-
keys(obj: any) {
|
|
134
|
-
return Object.keys(obj);
|
|
135
|
-
},
|
|
136
|
-
values(obj: any) {
|
|
137
|
-
return Object.values(obj);
|
|
138
|
-
},
|
|
139
|
-
entries(obj: any) {
|
|
140
|
-
return Object.entries(obj);
|
|
141
|
-
},
|
|
142
|
-
assign(...args: any[]) {
|
|
143
|
-
return Object.assign({}, ...args);
|
|
144
|
-
},
|
|
145
|
-
join(arr: any[], sep: string) {
|
|
146
|
-
return arr.join(sep);
|
|
147
|
-
},
|
|
148
|
-
includes(arr: any[], item: any) {
|
|
149
|
-
return arr.includes(item);
|
|
150
|
-
},
|
|
151
|
-
slice(arr: any[], start?: number, end?: number) {
|
|
152
|
-
return arr.slice(start, end);
|
|
153
|
-
},
|
|
154
|
-
sort(arr: any[], fn?: (...args: any[]) => any) {
|
|
155
|
-
return [...arr].sort(fn);
|
|
156
|
-
},
|
|
157
|
-
reverse(arr: any[]) {
|
|
158
|
-
return [...arr].reverse();
|
|
159
|
-
},
|
|
160
|
-
flat(arr: any[], depth?: number) {
|
|
161
|
-
return arr.flat(depth);
|
|
162
|
-
},
|
|
163
|
-
flatMap(arr: any[], fn: (...args: any[]) => any) {
|
|
164
|
-
return arr.flatMap(fn);
|
|
165
|
-
},
|
|
166
|
-
some(arr: any[], fn: (...args: any[]) => any) {
|
|
167
|
-
return arr.some(fn);
|
|
168
|
-
},
|
|
169
|
-
every(arr: any[], fn: (...args: any[]) => any) {
|
|
170
|
-
return arr.every(fn);
|
|
171
|
-
},
|
|
172
|
-
indexOf(arr: any[], item: any) {
|
|
173
|
-
return arr.indexOf(item);
|
|
174
|
-
},
|
|
175
|
-
};
|
|
176
|
-
|
|
177
|
-
export default functions;
|