bare-script 3.0.7 → 3.0.9

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/library.js CHANGED
@@ -342,7 +342,7 @@ const arraySortArgs = valueArgsModel([
342
342
  // $group: Data
343
343
  // $doc: Aggregate a data array
344
344
  // $arg data: The data array
345
- // $arg aggregation: The [aggregation model](model.html#var.vName='Aggregation')
345
+ // $arg aggregation: The [aggregation model](https://craigahobbs.github.io/bare-script/library/model.html#var.vName='Aggregation')
346
346
  // $return: The aggregated data array
347
347
  function dataAggregate(args) {
348
348
  const [data, aggregation] = valueArgsValidate(dataAggregateArgs, args);
@@ -1256,7 +1256,8 @@ const rRegexEscape = /[.*+?^${}()|[\]\\]/g;
1256
1256
  // $doc: Find the first match of a regular expression in a string
1257
1257
  // $arg regex: The regular expression
1258
1258
  // $arg string: The string
1259
- // $return: The [match object](model.html#var.vName='RegexMatch'), or null if no matches are found
1259
+ // $return: The [match object](https://craigahobbs.github.io/bare-script/library/model.html#var.vName='RegexMatch'),
1260
+ // $return: or null if no matches are found
1260
1261
  function regexMatch(args) {
1261
1262
  const [regex, string] = valueArgsValidate(regexMatchArgs, args);
1262
1263
  const match = string.match(regex);
@@ -1274,13 +1275,10 @@ const regexMatchArgs = valueArgsModel([
1274
1275
  // $doc: Find all matches of regular expression in a string
1275
1276
  // $arg regex: The regular expression
1276
1277
  // $arg string: The string
1277
- // $return: The array of [match objects](model.html#var.vName='RegexMatch')
1278
+ // $return: The array of [match objects](https://craigahobbs.github.io/bare-script/library/model.html#var.vName='RegexMatch')
1278
1279
  function regexMatchAll(args) {
1279
1280
  const [regex, string] = valueArgsValidate(regexMatchAllArgs, args);
1280
-
1281
- // Re-compile the regex with the "g" flag, if necessary
1282
- const regexGlobal = (regex.flags.indexOf('g') !== -1 ? regex : new RegExp(regex.source, `${regex.flags}g`));
1283
-
1281
+ const regexGlobal = regexEnsureGlobal(regex);
1284
1282
  return Array.from(string.matchAll(regexGlobal)).map((match) => regexMatchGroups(match));
1285
1283
  }
1286
1284
 
@@ -1290,6 +1288,33 @@ const regexMatchAllArgs = valueArgsModel([
1290
1288
  ]);
1291
1289
 
1292
1290
 
1291
+ // Helper te re-compile regex with the global flag and cache
1292
+ function regexEnsureGlobal(regex) {
1293
+ let regexGlobal = regex;
1294
+ const {source, flags} = regex;
1295
+ if (flags.indexOf('g') === -1) {
1296
+ let sourceCache = regexGlobalCache[source] ?? null;
1297
+ if (sourceCache === null) {
1298
+ sourceCache = {};
1299
+ regexGlobalCache[source] = sourceCache;
1300
+ }
1301
+ let flagsCache = sourceCache[flags] ?? null;
1302
+ if (flagsCache === null) {
1303
+ flagsCache = {};
1304
+ sourceCache[flags] = flagsCache;
1305
+ }
1306
+ regexGlobal = flagsCache[flags] ?? null;
1307
+ if (regexGlobal === null) {
1308
+ regexGlobal = new RegExp(source, `${flags}g`);
1309
+ flagsCache[flags] = regexGlobal;
1310
+ }
1311
+ }
1312
+ return regexGlobal;
1313
+ }
1314
+
1315
+ const regexGlobalCache = {};
1316
+
1317
+
1293
1318
  // Helper function to create a match model from a metch object
1294
1319
  function regexMatchGroups(match) {
1295
1320
  const groups = {};
@@ -1368,10 +1393,7 @@ const regexNewArgs = valueArgsModel([
1368
1393
  // $return: The updated string
1369
1394
  function regexReplace(args) {
1370
1395
  const [regex, string, substr] = valueArgsValidate(regexReplaceArgs, args);
1371
-
1372
- // Re-compile the regex with the "g" flag, if necessary
1373
- const regexGlobal = (regex.flags.indexOf('g') !== -1 ? regex : new RegExp(regex.source, `${regex.flags}g`));
1374
-
1396
+ const regexGlobal = regexEnsureGlobal(regex);
1375
1397
  return string.replaceAll(regexGlobal, substr);
1376
1398
  }
1377
1399
 
@@ -1787,8 +1809,10 @@ function systemCompare([left = null, right = null]) {
1787
1809
  // $function: systemFetch
1788
1810
  // $group: System
1789
1811
  // $doc: Retrieve a URL resource
1790
- // $arg url: The resource URL, [request model](model.html#var.vName='SystemFetchRequest'), or array of URL and
1791
- // $arg url: [request model](model.html#var.vName='SystemFetchRequest')
1812
+ // $arg url: The resource URL,
1813
+ // $arg url: [request model](https://craigahobbs.github.io/bare-script/library/model.html#var.vName='SystemFetchRequest'),
1814
+ // $arg url: or array of URL and
1815
+ // $arg url: [request model](https://craigahobbs.github.io/bare-script/library/model.html#var.vName='SystemFetchRequest')
1792
1816
  // $return: The response string or array of strings; null if an error occurred
1793
1817
  async function systemFetch([url = null], options) {
1794
1818
  // Options
@@ -1823,6 +1847,7 @@ async function systemFetch([url = null], options) {
1823
1847
  const fetchURL = urlFn !== null ? urlFn(request.url) : request.url;
1824
1848
  const fetchOptions = {};
1825
1849
  if ((request.body ?? null) !== null) {
1850
+ fetchOptions.method = 'POST';
1826
1851
  fetchOptions.body = request.body;
1827
1852
  }
1828
1853
  if ((request.headers ?? null) !== null) {
package/lib/parser.js CHANGED
@@ -589,7 +589,7 @@ function parseUnaryExpression(exprText) {
589
589
  if (matchFunctionOpen !== null) {
590
590
  let argText = exprText.slice(matchFunctionOpen[0].length);
591
591
  const args = [];
592
- while (true) { // eslint-disable-line no-constant-condition
592
+ while (true) {
593
593
  // Function close?
594
594
  const matchFunctionClose = argText.match(rExprFunctionClose);
595
595
  if (matchFunctionClose !== null) {
@@ -139,7 +139,7 @@ async function executeScriptHelperAsync(statements, options, locals) {
139
139
  }
140
140
 
141
141
  // Parse the include script
142
- let scriptModel = null;
142
+ let scriptModel;
143
143
  try {
144
144
  scriptModel = parseScript(scriptText);
145
145
  } catch (error) {
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "type": "module",
3
3
  "name": "bare-script",
4
- "version": "3.0.7",
4
+ "version": "3.0.9",
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": "~9.1",
34
- "eslint": "~8.57",
34
+ "eslint": "~9.1",
35
35
  "jsdoc": "~4.0"
36
36
  }
37
37
  }