bare-script 3.0.7 → 3.0.8

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.
Files changed (2) hide show
  1. package/lib/library.js +29 -8
  2. package/package.json +1 -1
package/lib/library.js CHANGED
@@ -1277,10 +1277,7 @@ const regexMatchArgs = valueArgsModel([
1277
1277
  // $return: The array of [match objects](model.html#var.vName='RegexMatch')
1278
1278
  function regexMatchAll(args) {
1279
1279
  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
-
1280
+ const regexGlobal = regexEnsureGlobal(regex);
1284
1281
  return Array.from(string.matchAll(regexGlobal)).map((match) => regexMatchGroups(match));
1285
1282
  }
1286
1283
 
@@ -1290,6 +1287,33 @@ const regexMatchAllArgs = valueArgsModel([
1290
1287
  ]);
1291
1288
 
1292
1289
 
1290
+ // Helper te re-compile regex with the global flag and cache
1291
+ function regexEnsureGlobal(regex) {
1292
+ let regexGlobal = regex;
1293
+ const {source, flags} = regex;
1294
+ if (flags.indexOf('g') === -1) {
1295
+ let sourceCache = regexGlobalCache[source] ?? null;
1296
+ if (sourceCache === null) {
1297
+ sourceCache = {};
1298
+ regexGlobalCache[source] = sourceCache;
1299
+ }
1300
+ let flagsCache = sourceCache[flags] ?? null;
1301
+ if (flagsCache === null) {
1302
+ flagsCache = {};
1303
+ sourceCache[flags] = flagsCache;
1304
+ }
1305
+ regexGlobal = flagsCache[flags] ?? null;
1306
+ if (regexGlobal === null) {
1307
+ regexGlobal = new RegExp(source, `${flags}g`);
1308
+ flagsCache[flags] = regexGlobal;
1309
+ }
1310
+ }
1311
+ return regexGlobal;
1312
+ }
1313
+
1314
+ const regexGlobalCache = {};
1315
+
1316
+
1293
1317
  // Helper function to create a match model from a metch object
1294
1318
  function regexMatchGroups(match) {
1295
1319
  const groups = {};
@@ -1368,10 +1392,7 @@ const regexNewArgs = valueArgsModel([
1368
1392
  // $return: The updated string
1369
1393
  function regexReplace(args) {
1370
1394
  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
-
1395
+ const regexGlobal = regexEnsureGlobal(regex);
1375
1396
  return string.replaceAll(regexGlobal, substr);
1376
1397
  }
1377
1398
 
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.8",
5
5
  "description": "BareScript; a lightweight scripting and expression language",
6
6
  "keywords": [
7
7
  "expression",