@nshiab/simple-data-analysis-core 2.1.0 → 2.1.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/esm/class/SimpleTable.d.ts +16 -2
- package/esm/class/SimpleTable.d.ts.map +1 -1
- package/esm/class/SimpleTable.js +15 -1
- package/esm/helpers/normalizeCacheCode.d.ts +8 -0
- package/esm/helpers/normalizeCacheCode.d.ts.map +1 -0
- package/esm/helpers/normalizeCacheCode.js +331 -0
- package/esm/helpers/serializeCacheInputs.d.ts.map +1 -1
- package/esm/helpers/serializeCacheInputs.js +2 -1
- package/esm/methods/cache.d.ts.map +1 -1
- package/esm/methods/cache.js +2 -1
- package/esm/methods/trim.d.ts +1 -1
- package/esm/methods/trim.d.ts.map +1 -1
- package/esm/methods/trim.js +17 -8
- package/llm.md +19 -6
- package/llms.txt +1 -1
- package/package.json +1 -1
- package/script/class/SimpleTable.d.ts +16 -2
- package/script/class/SimpleTable.d.ts.map +1 -1
- package/script/class/SimpleTable.js +15 -1
- package/script/helpers/normalizeCacheCode.d.ts +8 -0
- package/script/helpers/normalizeCacheCode.d.ts.map +1 -0
- package/script/helpers/normalizeCacheCode.js +334 -0
- package/script/helpers/serializeCacheInputs.d.ts.map +1 -1
- package/script/helpers/serializeCacheInputs.js +2 -1
- package/script/methods/cache.d.ts.map +1 -1
- package/script/methods/cache.js +2 -1
- package/script/methods/trim.d.ts +1 -1
- package/script/methods/trim.d.ts.map +1 -1
- package/script/methods/trim.js +17 -8
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = normalizeCacheCode;
|
|
4
|
+
const lineTerminator = /[\r\n\u2028\u2029]/u;
|
|
5
|
+
const word = /^[$_\p{ID_Start}][$\u200c\u200d\p{ID_Continue}]*/u;
|
|
6
|
+
const number = /^(?:0[xX][\da-fA-F_]+n?|0[bB][01_]+n?|0[oO][0-7_]+n?|(?:\d[\d_]*(?:\.[\d_]*)?|\.[\d_]+)(?:[eE][+-]?[\d_]+)?n?)/;
|
|
7
|
+
const punctuation = /^(?:>>>=|===|!==|\*\*=|&&=|\|\|=|\?\?=|>>>|<<=|>>=|=>|==|!=|<=|>=|\+\+|--|&&|\|\||\?\?|\?\.(?!\d)|\*\*|<<|>>|\+=|-=|\*=|%=|&=|\|=|\^=|\.\.\.|[{}()[\].,;:?~!+*%<>=&|^\-#])/;
|
|
8
|
+
const expressionPrefixes = new Set([
|
|
9
|
+
"(",
|
|
10
|
+
"[",
|
|
11
|
+
"{",
|
|
12
|
+
",",
|
|
13
|
+
";",
|
|
14
|
+
":",
|
|
15
|
+
"?",
|
|
16
|
+
"=",
|
|
17
|
+
"=>",
|
|
18
|
+
"!",
|
|
19
|
+
"~",
|
|
20
|
+
"+",
|
|
21
|
+
"-",
|
|
22
|
+
"*",
|
|
23
|
+
"**",
|
|
24
|
+
"%",
|
|
25
|
+
"&",
|
|
26
|
+
"|",
|
|
27
|
+
"^",
|
|
28
|
+
"&&",
|
|
29
|
+
"||",
|
|
30
|
+
"??",
|
|
31
|
+
"==",
|
|
32
|
+
"===",
|
|
33
|
+
"!=",
|
|
34
|
+
"!==",
|
|
35
|
+
"<",
|
|
36
|
+
">",
|
|
37
|
+
"<=",
|
|
38
|
+
">=",
|
|
39
|
+
"<<",
|
|
40
|
+
">>",
|
|
41
|
+
">>>",
|
|
42
|
+
"+=",
|
|
43
|
+
"-=",
|
|
44
|
+
"*=",
|
|
45
|
+
"**=",
|
|
46
|
+
"%=",
|
|
47
|
+
"&=",
|
|
48
|
+
"|=",
|
|
49
|
+
"^=",
|
|
50
|
+
"&&=",
|
|
51
|
+
"||=",
|
|
52
|
+
"??=",
|
|
53
|
+
"<<=",
|
|
54
|
+
">>=",
|
|
55
|
+
">>>=",
|
|
56
|
+
"/",
|
|
57
|
+
"/=",
|
|
58
|
+
]);
|
|
59
|
+
const expressionKeywords = new Set([
|
|
60
|
+
"return",
|
|
61
|
+
"throw",
|
|
62
|
+
"case",
|
|
63
|
+
"delete",
|
|
64
|
+
"void",
|
|
65
|
+
"typeof",
|
|
66
|
+
"new",
|
|
67
|
+
"in",
|
|
68
|
+
"instanceof",
|
|
69
|
+
"yield",
|
|
70
|
+
"await",
|
|
71
|
+
"else",
|
|
72
|
+
"do",
|
|
73
|
+
]);
|
|
74
|
+
const restrictedLineBreaks = new Set([
|
|
75
|
+
"return",
|
|
76
|
+
"throw",
|
|
77
|
+
"break",
|
|
78
|
+
"continue",
|
|
79
|
+
"yield",
|
|
80
|
+
"async",
|
|
81
|
+
"++",
|
|
82
|
+
"--",
|
|
83
|
+
]);
|
|
84
|
+
/**
|
|
85
|
+
* Normalizes comments, whitespace, and known optional punctuation without
|
|
86
|
+
* dependencies or executing user code. Literal contents and token boundaries
|
|
87
|
+
* stay intact. Potentially significant line breaks are retained; ambiguous
|
|
88
|
+
* slash syntax falls back to exact source instead of risking a stale cache hit.
|
|
89
|
+
*/
|
|
90
|
+
function normalizeCacheCode(source) {
|
|
91
|
+
let position = 0;
|
|
92
|
+
let uncertain = false;
|
|
93
|
+
function quoted(quote) {
|
|
94
|
+
const start = position++;
|
|
95
|
+
while (position < source.length) {
|
|
96
|
+
const character = source[position++];
|
|
97
|
+
if (character === "\\") {
|
|
98
|
+
position++;
|
|
99
|
+
}
|
|
100
|
+
else if (character === quote) {
|
|
101
|
+
return source.slice(start, position);
|
|
102
|
+
}
|
|
103
|
+
else if (lineTerminator.test(character)) {
|
|
104
|
+
return undefined;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function template() {
|
|
109
|
+
const parts = [];
|
|
110
|
+
let start = ++position;
|
|
111
|
+
while (position < source.length) {
|
|
112
|
+
if (source[position] === "\\") {
|
|
113
|
+
position += 2;
|
|
114
|
+
}
|
|
115
|
+
else if (source[position] === "`") {
|
|
116
|
+
parts.push(source.slice(start, position++));
|
|
117
|
+
return JSON.stringify(parts);
|
|
118
|
+
}
|
|
119
|
+
else if (source.startsWith("${", position)) {
|
|
120
|
+
parts.push(source.slice(start, position));
|
|
121
|
+
position += 2;
|
|
122
|
+
const tokens = scan(true);
|
|
123
|
+
if (uncertain)
|
|
124
|
+
return undefined;
|
|
125
|
+
parts.push(serialize(tokens));
|
|
126
|
+
start = position;
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
position++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
function regex() {
|
|
134
|
+
const start = position++;
|
|
135
|
+
let inClass = false;
|
|
136
|
+
while (position < source.length) {
|
|
137
|
+
const character = source[position++];
|
|
138
|
+
if (lineTerminator.test(character))
|
|
139
|
+
return undefined;
|
|
140
|
+
if (character === "\\") {
|
|
141
|
+
position++;
|
|
142
|
+
}
|
|
143
|
+
else if (character === "[") {
|
|
144
|
+
// Nested character classes depend on the v flag. Leave these opaque.
|
|
145
|
+
if (inClass)
|
|
146
|
+
return undefined;
|
|
147
|
+
inClass = true;
|
|
148
|
+
}
|
|
149
|
+
else if (character === "]") {
|
|
150
|
+
inClass = false;
|
|
151
|
+
}
|
|
152
|
+
else if (character === "/" && !inClass) {
|
|
153
|
+
const flags = source.slice(position).match(word)?.[0] ?? "";
|
|
154
|
+
position += flags.length;
|
|
155
|
+
return source.slice(start, position);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
function scan(interpolation = false) {
|
|
160
|
+
const tokens = [];
|
|
161
|
+
let braces = 0;
|
|
162
|
+
let lineBreak = false;
|
|
163
|
+
while (position < source.length) {
|
|
164
|
+
const rest = source.slice(position);
|
|
165
|
+
const character = source[position];
|
|
166
|
+
if (/\s/u.test(character)) {
|
|
167
|
+
lineBreak ||= lineTerminator.test(character);
|
|
168
|
+
position++;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
if (rest.startsWith("//")) {
|
|
172
|
+
while (position < source.length && !lineTerminator.test(source[position])) {
|
|
173
|
+
position++;
|
|
174
|
+
}
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
if (rest.startsWith("/*")) {
|
|
178
|
+
const end = source.indexOf("*/", position + 2);
|
|
179
|
+
if (end === -1)
|
|
180
|
+
break;
|
|
181
|
+
lineBreak ||= lineTerminator.test(source.slice(position, end + 2));
|
|
182
|
+
position = end + 2;
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
if (interpolation && character === "}" && braces === 0) {
|
|
186
|
+
position++;
|
|
187
|
+
return tokens;
|
|
188
|
+
}
|
|
189
|
+
// Legacy HTML comments and native function text are not ordinary tokens.
|
|
190
|
+
if (rest.startsWith("<!--") || rest.startsWith("-->") ||
|
|
191
|
+
rest.startsWith("[native code]"))
|
|
192
|
+
break;
|
|
193
|
+
const previous = tokens.at(-1);
|
|
194
|
+
let kind = "punctuation";
|
|
195
|
+
let text;
|
|
196
|
+
if (character === "'" || character === '"') {
|
|
197
|
+
kind = "literal";
|
|
198
|
+
text = quoted(character);
|
|
199
|
+
}
|
|
200
|
+
else if (character === "`") {
|
|
201
|
+
kind = "template";
|
|
202
|
+
text = template();
|
|
203
|
+
}
|
|
204
|
+
else if (character === "/") {
|
|
205
|
+
const property = [".", "?."].includes(tokens.at(-2)?.text ?? "");
|
|
206
|
+
if (!previous ||
|
|
207
|
+
(previous.kind === "punctuation" &&
|
|
208
|
+
expressionPrefixes.has(previous.text)) ||
|
|
209
|
+
(previous.kind === "word" && !property &&
|
|
210
|
+
expressionKeywords.has(previous.text))) {
|
|
211
|
+
kind = "literal";
|
|
212
|
+
text = regex();
|
|
213
|
+
}
|
|
214
|
+
else if (previous.kind === "literal" || previous.kind === "template") {
|
|
215
|
+
text = rest.startsWith("/=") ? "/=" : "/";
|
|
216
|
+
position += text.length;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
// After a closing block/parenthesis or contextual keyword, deciding
|
|
220
|
+
// between division and a regex can require a full JavaScript parser.
|
|
221
|
+
break;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
text = rest.match(word)?.[0];
|
|
226
|
+
if (text !== undefined) {
|
|
227
|
+
kind = "word";
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
text = rest.match(number)?.[0];
|
|
231
|
+
if (text !== undefined)
|
|
232
|
+
kind = "literal";
|
|
233
|
+
else
|
|
234
|
+
text = rest.match(punctuation)?.[0];
|
|
235
|
+
}
|
|
236
|
+
if (text !== undefined)
|
|
237
|
+
position += text.length;
|
|
238
|
+
}
|
|
239
|
+
if (text === undefined) {
|
|
240
|
+
uncertain = true;
|
|
241
|
+
break;
|
|
242
|
+
}
|
|
243
|
+
if (kind === "punctuation") {
|
|
244
|
+
if (text === "{")
|
|
245
|
+
braces++;
|
|
246
|
+
if (text === "}")
|
|
247
|
+
braces--;
|
|
248
|
+
}
|
|
249
|
+
tokens.push({ kind, text, lineBreak });
|
|
250
|
+
lineBreak = false;
|
|
251
|
+
}
|
|
252
|
+
if (position < source.length || interpolation)
|
|
253
|
+
uncertain = true;
|
|
254
|
+
return tokens;
|
|
255
|
+
}
|
|
256
|
+
const tokens = scan();
|
|
257
|
+
return uncertain
|
|
258
|
+
? JSON.stringify(["source", source])
|
|
259
|
+
: JSON.stringify(["tokens", serialize(tokens)]);
|
|
260
|
+
}
|
|
261
|
+
function serialize(tokens) {
|
|
262
|
+
tokens = normalizePunctuation(tokens);
|
|
263
|
+
return JSON.stringify(tokens.map((token, index) => {
|
|
264
|
+
const previous = tokens[index - 1];
|
|
265
|
+
// Only discard newlines where continuation is certain. Keeping other
|
|
266
|
+
// newlines permits conservative misses rather than unsafe cache hits.
|
|
267
|
+
const significant = token.lineBreak && previous !== undefined && (restrictedLineBreaks.has(previous.text) ||
|
|
268
|
+
["++", "--"].includes(token.text) ||
|
|
269
|
+
!((previous.kind === "punctuation" &&
|
|
270
|
+
(expressionPrefixes.has(previous.text) ||
|
|
271
|
+
[".", "?.", "}"].includes(previous.text))) ||
|
|
272
|
+
(token.kind === "punctuation" &&
|
|
273
|
+
([")", "]", "}", ",", ";", ".", "?."].includes(token.text) ||
|
|
274
|
+
(expressionPrefixes.has(token.text) &&
|
|
275
|
+
!["(", "[", "{", "!", "~"].includes(token.text))))));
|
|
276
|
+
return [token.kind, token.text, significant];
|
|
277
|
+
}));
|
|
278
|
+
}
|
|
279
|
+
function normalizePunctuation(input) {
|
|
280
|
+
const tokens = input.map((token) => ({ ...token }));
|
|
281
|
+
const result = [];
|
|
282
|
+
const brackets = [];
|
|
283
|
+
const controls = new Set(["if", "while", "for", "with", "switch", "catch"]);
|
|
284
|
+
let previousControl = false;
|
|
285
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
286
|
+
const token = tokens[i];
|
|
287
|
+
const next = tokens[i + 1];
|
|
288
|
+
const previous = result.at(-1);
|
|
289
|
+
if (token.kind === "literal" && /^["']/.test(token.text) &&
|
|
290
|
+
!token.text.includes("\\")) {
|
|
291
|
+
token.text = JSON.stringify(token.text.slice(1, -1));
|
|
292
|
+
}
|
|
293
|
+
// A single arrow parameter has the same binding with or without parentheses.
|
|
294
|
+
if (token.text === "(" && tokens[i + 1]?.kind === "word" &&
|
|
295
|
+
tokens[i + 2]?.text === ")" && tokens[i + 3]?.text === "=>") {
|
|
296
|
+
result.push({ ...tokens[i + 1], lineBreak: token.lineBreak });
|
|
297
|
+
i += 2;
|
|
298
|
+
previousControl = false;
|
|
299
|
+
continue;
|
|
300
|
+
}
|
|
301
|
+
// Retain commas representing array holes, including the final hole.
|
|
302
|
+
if (token.text === "," && [")", "]", "}"].includes(next?.text ?? "") &&
|
|
303
|
+
previous && ![",", "[", "("].includes(previous.text))
|
|
304
|
+
continue;
|
|
305
|
+
const expressionEnd = previous && (previous.kind !== "punctuation" || previous.text === "]" ||
|
|
306
|
+
(previous.text === ")" && !previousControl));
|
|
307
|
+
if (token.text === ";" && brackets.at(-1)?.text === "{" && expressionEnd &&
|
|
308
|
+
!restrictedLineBreaks.has(previous.text)) {
|
|
309
|
+
if (next?.text === "}")
|
|
310
|
+
continue;
|
|
311
|
+
// A call followed by an identifier starts a new statement. Preserve that
|
|
312
|
+
// boundary as a newline so explicit and automatically inserted semicolons
|
|
313
|
+
// agree. Never do this in a for header or after a control header.
|
|
314
|
+
if (previous.text === ")" && next?.kind === "word" &&
|
|
315
|
+
!["in", "instanceof", "of"].includes(next.text)) {
|
|
316
|
+
next.lineBreak = true;
|
|
317
|
+
continue;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
previousControl = false;
|
|
321
|
+
if (["(", "[", "{"].includes(token.text)) {
|
|
322
|
+
brackets.push({
|
|
323
|
+
text: token.text,
|
|
324
|
+
control: token.text === "(" && (controls.has(previous?.text ?? "") ||
|
|
325
|
+
(previous?.text === "await" && result.at(-2)?.text === "for")),
|
|
326
|
+
});
|
|
327
|
+
}
|
|
328
|
+
else if ([")", "]", "}"].includes(token.text)) {
|
|
329
|
+
previousControl = brackets.pop()?.control ?? false;
|
|
330
|
+
}
|
|
331
|
+
result.push(token);
|
|
332
|
+
}
|
|
333
|
+
return result;
|
|
334
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"serializeCacheInputs.d.ts","sourceRoot":"","sources":["../../src/helpers/serializeCacheInputs.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"serializeCacheInputs.d.ts","sourceRoot":"","sources":["../../src/helpers/serializeCacheInputs.ts"],"names":[],"mappings":"AAIA;;;GAGG;AACH,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAC1C,MAAM,EAAE,SAAS,OAAO,EAAE,GACzB,MAAM,CASR;AAED,+EAA+E;AAC/E,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAIrE"}
|
|
@@ -7,6 +7,7 @@ exports.default = serializeCacheInputs;
|
|
|
7
7
|
exports.getCacheInputLabel = getCacheInputLabel;
|
|
8
8
|
const SimpleTable_js_1 = __importDefault(require("../class/SimpleTable.js"));
|
|
9
9
|
const tableGeneration_js_1 = require("./tableGeneration.js");
|
|
10
|
+
const normalizeCacheCode_js_1 = __importDefault(require("./normalizeCacheCode.js"));
|
|
10
11
|
/**
|
|
11
12
|
* Converts cache inputs to a deterministic, type-tagged representation.
|
|
12
13
|
* SimpleTable values are represented by their name and current generation.
|
|
@@ -41,7 +42,7 @@ function serializeValue(value, path, ancestors) {
|
|
|
41
42
|
case "function":
|
|
42
43
|
return JSON.stringify([
|
|
43
44
|
"function",
|
|
44
|
-
Function.prototype.toString.call(value),
|
|
45
|
+
(0, normalizeCacheCode_js_1.default)(Function.prototype.toString.call(value)),
|
|
45
46
|
]);
|
|
46
47
|
case "symbol":
|
|
47
48
|
throw new TypeError(`cache() ${path} cannot be a symbol.`);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/methods/cache.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"cache.d.ts","sourceRoot":"","sources":["../../src/methods/cache.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AA6DvD,wBAA8B,KAAK,CAAC,KAAK,SAAS,WAAW,EAC3D,KAAK,EAAE,KAAK,EACZ,OAAO,EAAE,CAAC,KAAK,EAAE,KAAK,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,EAC/C,OAAO,GAAE;IACP,GAAG,CAAC,EAAE,MAAM,CAAC;IACb,MAAM,CAAC,EAAE,SAAS,OAAO,EAAE,CAAC;IAC5B,OAAO,CAAC,EAAE,OAAO,CAAC;CACd,iBA4LP"}
|
package/script/methods/cache.js
CHANGED
|
@@ -41,6 +41,7 @@ const node_fs_1 = require("node:fs");
|
|
|
41
41
|
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
42
42
|
const flushAllTables_js_1 = __importDefault(require("../helpers/flushAllTables.js"));
|
|
43
43
|
const prettyDuration_js_1 = __importDefault(require("../helpers/prettyDuration.js"));
|
|
44
|
+
const normalizeCacheCode_js_1 = __importDefault(require("../helpers/normalizeCacheCode.js"));
|
|
44
45
|
const cacheDiagnostics_js_1 = require("../helpers/cacheDiagnostics.js");
|
|
45
46
|
const serializeCacheInputs_js_1 = __importStar(require("../helpers/serializeCacheInputs.js"));
|
|
46
47
|
const tableGeneration_js_1 = require("../helpers/tableGeneration.js");
|
|
@@ -66,7 +67,7 @@ async function cache(table, compute, options = {}) {
|
|
|
66
67
|
if ((0, node_fs_1.existsSync)(cacheSourcesPath)) {
|
|
67
68
|
cacheSources = JSON.parse((0, node_fs_1.readFileSync)(cacheSourcesPath, "utf-8"));
|
|
68
69
|
}
|
|
69
|
-
const functionBody = compute.toString();
|
|
70
|
+
const functionBody = (0, normalizeCacheCode_js_1.default)(compute.toString());
|
|
70
71
|
const entryGeneration = (0, tableGeneration_js_1.peekTableGeneration)(table) ?? null;
|
|
71
72
|
const inputs = options.inputs?.filter((input) => input !== table) ?? [];
|
|
72
73
|
const hasInputs = inputs.length > 0;
|
package/script/methods/trim.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type SimpleTable from "../class/SimpleTable.js";
|
|
2
|
-
export default function trim(simpleTable: SimpleTable, columns: string | string[], options?: {
|
|
2
|
+
export default function trim(simpleTable: SimpleTable, columns: "all" | string | string[], options?: {
|
|
3
3
|
character?: string;
|
|
4
4
|
side?: "left" | "right" | "both";
|
|
5
5
|
}): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"trim.d.ts","sourceRoot":"","sources":["../../src/methods/trim.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"trim.d.ts","sourceRoot":"","sources":["../../src/methods/trim.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,WAAW,MAAM,yBAAyB,CAAC;AAGvD,MAAM,CAAC,OAAO,UAAU,IAAI,CAC1B,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,KAAK,GAAG,MAAM,GAAG,MAAM,EAAE,EAClC,OAAO,GAAE;IACP,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;CAC7B,QAgDP"}
|
package/script/methods/trim.js
CHANGED
|
@@ -8,7 +8,9 @@ const quoteIdentifier_js_1 = __importDefault(require("../helpers/quoteIdentifier
|
|
|
8
8
|
const stringToArray_js_1 = __importDefault(require("../helpers/stringToArray.js"));
|
|
9
9
|
const queueOp_js_1 = __importDefault(require("../helpers/queueOp.js"));
|
|
10
10
|
function trim(simpleTable, columns, options = {}) {
|
|
11
|
-
const cols = (0, stringToArray_js_1.default)(columns);
|
|
11
|
+
const cols = columns === "all" ? null : (0, stringToArray_js_1.default)(columns);
|
|
12
|
+
const resolveColumns = (schema) => cols ??
|
|
13
|
+
Object.keys(schema).filter((column) => schema[column] === "VARCHAR");
|
|
12
14
|
const side = options.side ?? "both";
|
|
13
15
|
const fn = side === "left"
|
|
14
16
|
? "LTRIM"
|
|
@@ -20,17 +22,24 @@ function trim(simpleTable, columns, options = {}) {
|
|
|
20
22
|
if (fn === null) {
|
|
21
23
|
throw new Error(`trim() options.side must be "left", "right", or "both". Received ${JSON.stringify(options.side)}.`);
|
|
22
24
|
}
|
|
23
|
-
const
|
|
25
|
+
const character = options.character;
|
|
26
|
+
const specialCharacter = typeof character === "string" ? ", ?" : "";
|
|
24
27
|
(0, queueOp_js_1.default)(simpleTable, {
|
|
25
28
|
kind: "fusable",
|
|
26
29
|
method: "trim()",
|
|
27
30
|
parameters: { columns, options },
|
|
28
|
-
values: typeof
|
|
29
|
-
?
|
|
31
|
+
values: typeof character === "string"
|
|
32
|
+
? (schema) => resolveColumns(schema).map(() => character)
|
|
30
33
|
: undefined,
|
|
31
|
-
needsSchema:
|
|
32
|
-
buildSelect: (input) =>
|
|
33
|
-
|
|
34
|
-
|
|
34
|
+
needsSchema: columns === "all",
|
|
35
|
+
buildSelect: (input, schema) => {
|
|
36
|
+
const selectedColumns = resolveColumns(schema);
|
|
37
|
+
if (columns === "all" && selectedColumns.length === 0) {
|
|
38
|
+
return `SELECT * FROM ${input}`;
|
|
39
|
+
}
|
|
40
|
+
return `SELECT * REPLACE (${selectedColumns
|
|
41
|
+
.map((c) => `${fn}(${(0, quoteIdentifier_js_1.default)(c)}${specialCharacter}) AS ${(0, quoteIdentifier_js_1.default)(c)}`)
|
|
42
|
+
.join(", ")}) FROM ${input}`;
|
|
43
|
+
},
|
|
35
44
|
});
|
|
36
45
|
}
|