bare-script 2.2.15 → 2.2.17
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/lib/data.js +13 -2
- package/lib/library.js +3 -5
- package/lib/parser.js +7 -7
- package/lib/runtime.js +5 -3
- package/package.json +1 -1
package/lib/data.js
CHANGED
|
@@ -376,6 +376,9 @@ enum AggregationFunction
|
|
|
376
376
|
# The least of the measure's values
|
|
377
377
|
min
|
|
378
378
|
|
|
379
|
+
# The standard deviation of the measure's values
|
|
380
|
+
stddev
|
|
381
|
+
|
|
379
382
|
# The sum of the measure's values
|
|
380
383
|
sum
|
|
381
384
|
`);
|
|
@@ -434,7 +437,9 @@ export function aggregateData(data, aggregation) {
|
|
|
434
437
|
if (!(field in aggregateRow)) {
|
|
435
438
|
aggregateRow[field] = [];
|
|
436
439
|
}
|
|
437
|
-
|
|
440
|
+
if (value !== null) {
|
|
441
|
+
aggregateRow[field].push(value);
|
|
442
|
+
}
|
|
438
443
|
}
|
|
439
444
|
}
|
|
440
445
|
|
|
@@ -445,7 +450,9 @@ export function aggregateData(data, aggregation) {
|
|
|
445
450
|
const field = measure.name ?? measure.field;
|
|
446
451
|
const func = measure.function;
|
|
447
452
|
const measureValues = aggregateRow[field];
|
|
448
|
-
if (
|
|
453
|
+
if (!measureValues.length) {
|
|
454
|
+
aggregateRow[field] = null;
|
|
455
|
+
} else if (func === 'count') {
|
|
449
456
|
aggregateRow[field] = measureValues.length;
|
|
450
457
|
} else if (func === 'max') {
|
|
451
458
|
aggregateRow[field] = measureValues.reduce((max, val) => (val > max ? val : max));
|
|
@@ -453,7 +460,11 @@ export function aggregateData(data, aggregation) {
|
|
|
453
460
|
aggregateRow[field] = measureValues.reduce((min, val) => (val < min ? val : min));
|
|
454
461
|
} else if (func === 'sum') {
|
|
455
462
|
aggregateRow[field] = measureValues.reduce((sum, val) => sum + val, 0);
|
|
463
|
+
} else if (func === 'stddev') {
|
|
464
|
+
const average = measureValues.reduce((sum, val) => sum + val, 0) / measureValues.length;
|
|
465
|
+
aggregateRow[field] = Math.sqrt(measureValues.reduce((sum, val) => sum + (val - average) ** 2, 0) / measureValues.length);
|
|
456
466
|
} else {
|
|
467
|
+
// func === 'average'
|
|
457
468
|
aggregateRow[field] = measureValues.reduce((sum, val) => sum + val, 0) / measureValues.length;
|
|
458
469
|
}
|
|
459
470
|
}
|
package/lib/library.js
CHANGED
|
@@ -682,7 +682,7 @@ export const scriptFunctions = {
|
|
|
682
682
|
// $doc: Escape a string for use in a regular expression
|
|
683
683
|
// $arg string: The string to escape
|
|
684
684
|
// $return: The escaped string
|
|
685
|
-
'regexEscape': ([string]) => (typeof string === 'string' ? string.replace(
|
|
685
|
+
'regexEscape': ([string]) => (typeof string === 'string' ? string.replace(rRegexEscape, '\\$&') : null),
|
|
686
686
|
|
|
687
687
|
// $function: regexMatch
|
|
688
688
|
// $group: Regex
|
|
@@ -1055,7 +1055,7 @@ export const scriptFunctions = {
|
|
|
1055
1055
|
|
|
1056
1056
|
|
|
1057
1057
|
// Regex escape regular expression
|
|
1058
|
-
const
|
|
1058
|
+
const rRegexEscape = /[.*+?^${}()|[\]\\]/g;
|
|
1059
1059
|
|
|
1060
1060
|
|
|
1061
1061
|
// Fixed-number trim regular expression
|
|
@@ -1108,8 +1108,6 @@ export const expressionFunctionMap = {
|
|
|
1108
1108
|
'today': 'datetimeToday',
|
|
1109
1109
|
'trim': 'stringTrim',
|
|
1110
1110
|
'upper': 'stringUpper',
|
|
1111
|
-
'urlEncode': 'urlEncode',
|
|
1112
|
-
'urlEncodeComponent': 'urlEncodeComponent',
|
|
1113
1111
|
'year': 'datetimeYear'
|
|
1114
1112
|
};
|
|
1115
1113
|
|
|
@@ -1117,4 +1115,4 @@ export const expressionFunctionMap = {
|
|
|
1117
1115
|
// The built-in expression functions
|
|
1118
1116
|
export const expressionFunctions = Object.fromEntries(Object.entries(expressionFunctionMap).map(
|
|
1119
1117
|
([exprFnName, scriptFnName]) => [exprFnName, scriptFunctions[scriptFnName]]
|
|
1120
|
-
)
|
|
1118
|
+
));
|
package/lib/parser.js
CHANGED
|
@@ -63,6 +63,11 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
63
63
|
for (const [ixLinePart, linePart] of lines.entries()) {
|
|
64
64
|
const statements = (functionDef !== null ? functionDef.function.statements : script.statements);
|
|
65
65
|
|
|
66
|
+
// Comment?
|
|
67
|
+
if (linePart.match(rScriptComment) !== null) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
|
|
66
71
|
// Set the line index
|
|
67
72
|
const isContinued = (lineContinuation.length !== 0);
|
|
68
73
|
if (!isContinued) {
|
|
@@ -72,7 +77,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
72
77
|
// Line continuation?
|
|
73
78
|
const linePartNoContinuation = linePart.replace(rScriptContinuation, '');
|
|
74
79
|
if (linePart !== linePartNoContinuation) {
|
|
75
|
-
lineContinuation.push(
|
|
80
|
+
lineContinuation.push(isContinued ? linePartNoContinuation.trim() : linePartNoContinuation.trimEnd());
|
|
76
81
|
continue;
|
|
77
82
|
} else if (isContinued) {
|
|
78
83
|
lineContinuation.push(linePartNoContinuation.trim());
|
|
@@ -87,11 +92,6 @@ export function parseScript(scriptText, startLineNumber = 1) {
|
|
|
87
92
|
line = linePart;
|
|
88
93
|
}
|
|
89
94
|
|
|
90
|
-
// Comment?
|
|
91
|
-
if (line.match(rScriptComment) !== null) {
|
|
92
|
-
continue;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
95
|
// Assignment?
|
|
96
96
|
const matchAssignment = line.match(rScriptAssignment);
|
|
97
97
|
if (matchAssignment !== null) {
|
|
@@ -709,7 +709,7 @@ export class BareScriptParserError extends Error {
|
|
|
709
709
|
let lineError = line;
|
|
710
710
|
let lineColumn = columnNumber;
|
|
711
711
|
if (line.length > lineLengthMax) {
|
|
712
|
-
const lineLeft = columnNumber - 1 -
|
|
712
|
+
const lineLeft = columnNumber - 1 - lineLengthMax / 2;
|
|
713
713
|
const lineRight = lineLeft + lineLengthMax;
|
|
714
714
|
if (lineLeft < 0) {
|
|
715
715
|
lineError = line.slice(0, lineLengthMax) + lineSuffix;
|
package/lib/runtime.js
CHANGED
|
@@ -193,8 +193,10 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
193
193
|
}
|
|
194
194
|
|
|
195
195
|
// Get the local or global variable value or null if undefined
|
|
196
|
-
let varValue
|
|
197
|
-
if (
|
|
196
|
+
let varValue;
|
|
197
|
+
if (locals !== null && expr.variable in locals) {
|
|
198
|
+
varValue = locals[expr.variable];
|
|
199
|
+
} else {
|
|
198
200
|
varValue = (globals !== null ? (globals[expr.variable] ?? null) : null);
|
|
199
201
|
}
|
|
200
202
|
return varValue;
|
|
@@ -312,7 +314,7 @@ export function evaluateExpression(expr, options = null, locals = null, builtins
|
|
|
312
314
|
*/
|
|
313
315
|
export class BareScriptRuntimeError extends Error {
|
|
314
316
|
/**
|
|
315
|
-
* Create a BareScript
|
|
317
|
+
* Create a BareScript runtime error
|
|
316
318
|
*
|
|
317
319
|
* @param {string} message - The runtime error message
|
|
318
320
|
*/
|