bare-script 2.2.9 → 2.2.11

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/README.md CHANGED
@@ -83,7 +83,7 @@ console.log(await executeScriptAsync(script, {'fetchFn': fetch}));
83
83
  This outputs:
84
84
 
85
85
  ~~~
86
- The BareScript Library has 89 functions
86
+ The BareScript Library has 100 functions
87
87
  ~~~
88
88
 
89
89
 
package/lib/library.js CHANGED
@@ -257,15 +257,17 @@ export const scriptFunctions = {
257
257
  // $group: Datetime
258
258
  // $doc: Get the day of the month of a datetime
259
259
  // $arg datetime: The datetime
260
+ // $arg utc: Optional (default is false). If true, return the UTC day of the month.
260
261
  // $return: The day of the month
261
- 'datetimeDay': ([datetime]) => (datetime instanceof Date ? datetime.getDate() : null),
262
+ 'datetimeDay': ([datetime, utc = false]) => (datetime instanceof Date ? (utc ? datetime.getUTCDate() : datetime.getDate()) : null),
262
263
 
263
264
  // $function: datetimeHour
264
265
  // $group: Datetime
265
266
  // $doc: Get the hour of a datetime
266
267
  // $arg datetime: The datetime
268
+ // $arg utc: Optional (default is false). If true, return the UTC hour.
267
269
  // $return: The hour
268
- 'datetimeHour': ([datetime]) => (datetime instanceof Date ? datetime.getHours() : null),
270
+ 'datetimeHour': ([datetime, utc = false]) => (datetime instanceof Date ? (utc ? datetime.getUTCHours() : datetime.getHours()) : null),
269
271
 
270
272
  // $function: datetimeISOFormat
271
273
  // $group: Datetime
@@ -295,15 +297,21 @@ export const scriptFunctions = {
295
297
  // $group: Datetime
296
298
  // $doc: Get the number of minutes of a datetime
297
299
  // $arg datetime: The datetime
300
+ // $arg utc: Optional (default is false). If true, return the UTC minutes.
298
301
  // $return: The number of minutes
299
- 'datetimeMinute': ([datetime]) => (datetime instanceof Date ? datetime.getMinutes() : null),
302
+ 'datetimeMinute': ([datetime, utc = false]) => (
303
+ datetime instanceof Date ? (utc ? datetime.getUTCMinutes() : datetime.getMinutes()) : null
304
+ ),
300
305
 
301
306
  // $function: datetimeMonth
302
307
  // $group: Datetime
303
308
  // $doc: Get the number of the month (1-12) of a datetime
304
309
  // $arg datetime: The datetime
310
+ // $arg utc: Optional (default is false). If true, return the UTC month.
305
311
  // $return: The number of the month
306
- 'datetimeMonth': ([datetime]) => (datetime instanceof Date ? datetime.getMonth() + 1 : null),
312
+ 'datetimeMonth': ([datetime, utc = false]) => (
313
+ datetime instanceof Date ? (utc ? datetime.getUTCMonth() + 1 : datetime.getMonth() + 1) : null
314
+ ),
307
315
 
308
316
  // $function: datetimeNew
309
317
  // $group: Datetime
@@ -345,8 +353,11 @@ export const scriptFunctions = {
345
353
  // $group: Datetime
346
354
  // $doc: Get the number of seconds of a datetime
347
355
  // $arg datetime: The datetime
356
+ // $arg utc: Optional (default is false). If true, return the UTC seconds.
348
357
  // $return: The number of seconds
349
- 'datetimeSecond': ([datetime]) => (datetime instanceof Date ? datetime.getSeconds() : null),
358
+ 'datetimeSecond': ([datetime, utc = false]) => (
359
+ datetime instanceof Date ? (utc ? datetime.getUTCSeconds() : datetime.getSeconds()) : null
360
+ ),
350
361
 
351
362
  // $function: datetimeToday
352
363
  // $group: Datetime
@@ -361,8 +372,11 @@ export const scriptFunctions = {
361
372
  // $group: Datetime
362
373
  // $doc: Get the full year of a datetime
363
374
  // $arg datetime: The datetime
375
+ // $arg utc: Optional (default is false). If true, return the UTC year.
364
376
  // $return: The full year
365
- 'datetimeYear': ([datetime]) => (datetime instanceof Date ? datetime.getFullYear() : null),
377
+ 'datetimeYear': ([datetime, utc = false]) => (
378
+ datetime instanceof Date ? (utc ? datetime.getUTCFullYear() : datetime.getFullYear()) : null
379
+ ),
366
380
 
367
381
 
368
382
  //
package/lib/parser.js CHANGED
@@ -56,6 +56,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
56
56
  // Process each line
57
57
  const lineContinuation = [];
58
58
  let functionDef = null;
59
+ let functionLabelDefDepth = null;
59
60
  const labelDefs = [];
60
61
  let labelIndex = 0;
61
62
  let ixLine;
@@ -118,6 +119,7 @@ export function parseScript(scriptText, startLineNumber = 1) {
118
119
  }
119
120
 
120
121
  // Add the function definition statement
122
+ functionLabelDefDepth = labelDefs.length;
121
123
  functionDef = {
122
124
  'function': {
123
125
  'name': matchFunctionBegin.groups.name,
@@ -143,7 +145,17 @@ export function parseScript(scriptText, startLineNumber = 1) {
143
145
  if (functionDef === null) {
144
146
  throw new BareScriptParserError('No matching function definition', line, 1, startLineNumber + ixLine);
145
147
  }
148
+
149
+ // Check for un-matched label definitions
150
+ if (labelDefs.length > functionLabelDefDepth) {
151
+ const labelDef = labelDefs.pop();
152
+ const [defKey] = Object.keys(labelDef);
153
+ const def = labelDef[defKey];
154
+ throw new BareScriptParserError(`Missing end${defKey} statement`, def.line, 1, def.lineNumber);
155
+ }
156
+
146
157
  functionDef = null;
158
+ functionLabelDefDepth = null;
147
159
  continue;
148
160
  }
149
161
 
@@ -173,7 +185,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
173
185
  const matchIfElseIf = line.match(rScriptIfElseIf);
174
186
  if (matchIfElseIf !== null) {
175
187
  // Get the if-then definition
176
- const ifthen = (labelDefs.length > 0 ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
188
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
189
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
177
190
  if (ifthen === null) {
178
191
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
179
192
  }
@@ -204,7 +217,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
204
217
  const matchIfElse = line.match(rScriptIfElse);
205
218
  if (matchIfElse !== null) {
206
219
  // Get the if-then definition
207
- const ifthen = (labelDefs.length > 0 ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
220
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
221
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs[labelDefs.length - 1].if ?? null) : null);
208
222
  if (ifthen === null) {
209
223
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
210
224
  }
@@ -227,7 +241,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
227
241
  const matchIfEnd = line.match(rScriptIfEnd);
228
242
  if (matchIfEnd !== null) {
229
243
  // Pop the if-then definition
230
- const ifthen = (labelDefs.length > 0 ? (labelDefs.pop().if ?? null) : null);
244
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
245
+ const ifthen = (labelDefs.length > labelDefDepth ? (labelDefs.pop().if ?? null) : null);
231
246
  if (ifthen === null) {
232
247
  throw new BareScriptParserError('No matching if statement', line, 1, startLineNumber + ixLine);
233
248
  }
@@ -269,7 +284,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
269
284
  const matchWhileEnd = line.match(rScriptWhileEnd);
270
285
  if (matchWhileEnd !== null) {
271
286
  // Pop the while-do definition
272
- const whiledo = (labelDefs.length > 0 ? (labelDefs.pop().while ?? null) : null);
287
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
288
+ const whiledo = (labelDefs.length > labelDefDepth ? (labelDefs.pop().while ?? null) : null);
273
289
  if (whiledo === null) {
274
290
  throw new BareScriptParserError('No matching while statement', line, 1, startLineNumber + ixLine);
275
291
  }
@@ -319,7 +335,8 @@ export function parseScript(scriptText, startLineNumber = 1) {
319
335
  const matchForEnd = line.match(rScriptForEnd);
320
336
  if (matchForEnd !== null) {
321
337
  // Pop the foreach definition
322
- const foreach = (labelDefs.length > 0 ? (labelDefs.pop().for ?? null) : null);
338
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
339
+ const foreach = (labelDefs.length > labelDefDepth ? (labelDefs.pop().for ?? null) : null);
323
340
  if (foreach === null) {
324
341
  throw new BareScriptParserError('No matching for statement', line, 1, startLineNumber + ixLine);
325
342
  }
@@ -346,7 +363,9 @@ export function parseScript(scriptText, startLineNumber = 1) {
346
363
  const matchBreak = line.match(rScriptBreak);
347
364
  if (matchBreak !== null) {
348
365
  // Get the loop definition
349
- const labelDef = (labelDefs.length > 0 ? (labelDefs.findLast((def) => !('if' in def)) ?? null) : null);
366
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
367
+ const ixLabelDef = labelDefs.findLastIndex((def) => !('if' in def));
368
+ const labelDef = (ixLabelDef >= labelDefDepth ? labelDefs[ixLabelDef] : null);
350
369
  if (labelDef === null) {
351
370
  throw new BareScriptParserError('Break statement outside of loop', line, 1, startLineNumber + ixLine);
352
371
  }
@@ -362,7 +381,9 @@ export function parseScript(scriptText, startLineNumber = 1) {
362
381
  const matchContinue = line.match(rScriptContinue);
363
382
  if (matchContinue !== null) {
364
383
  // Get the loop definition
365
- const labelDef = (labelDefs.length > 0 ? (labelDefs.findLast((def) => !('if' in def)) ?? null) : null);
384
+ const labelDefDepth = (functionDef !== null ? functionLabelDefDepth : 0);
385
+ const ixLabelDef = labelDefs.findLastIndex((def) => !('if' in def));
386
+ const labelDef = (ixLabelDef >= labelDefDepth ? labelDefs[ixLabelDef] : null);
366
387
  if (labelDef === null) {
367
388
  throw new BareScriptParserError('Continue statement outside of loop', line, 1, startLineNumber + ixLine);
368
389
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bare-script",
4
- "version": "2.2.9",
4
+ "version": "2.2.11",
5
5
  "description": "BareScript; a lightweight scripting and expression language",
6
6
  "keywords": [
7
7
  "expression",
@@ -31,7 +31,7 @@
31
31
  },
32
32
  "devDependencies": {
33
33
  "c8": "~8.0",
34
- "eslint": "~8.50",
34
+ "eslint": "~8.52",
35
35
  "jsdoc": "~4.0"
36
36
  }
37
37
  }