@rkmodules/rules 0.0.61 → 0.0.62

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/dist/index.cjs.js CHANGED
@@ -239,6 +239,9 @@ function isSingleTon(value) {
239
239
  }
240
240
  /**
241
241
  * turns a value or array of values into a tree
242
+ * - a single value becomes a tree with one branch "0" and one item
243
+ * - an array of values becomes a tree with one branch "0" and the items in the array
244
+ * - a tree is returned as is
242
245
  */
243
246
  function broadCast(value) {
244
247
  if (Array.isArray(value)) {
@@ -403,30 +406,42 @@ function toArray(a) {
403
406
  * @param b
404
407
  * @returns
405
408
  */
406
- function mergeTrees(a, b) {
407
- var e_1, _a;
408
- var _b;
409
- var out = __assign({}, a);
410
- try {
411
- for (var _c = __values(Object.entries(b)), _d = _c.next(); !_d.done; _d = _c.next()) {
412
- var _e = __read(_d.value, 2), k = _e[0], arr = _e[1];
413
- out[k] = ((_b = out[k]) !== null && _b !== void 0 ? _b : []).concat(arr);
414
- }
409
+ function mergeTrees() {
410
+ var others = [];
411
+ for (var _i = 0; _i < arguments.length; _i++) {
412
+ others[_i] = arguments[_i];
415
413
  }
416
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
417
- finally {
414
+ var a = others.shift() || { 0: [] };
415
+ var out = __assign({}, a);
416
+ others.forEach(function (b) {
417
+ var e_1, _a;
418
+ var _b;
418
419
  try {
419
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
420
+ for (var _c = __values(Object.entries(b)), _d = _c.next(); !_d.done; _d = _c.next()) {
421
+ var _e = __read(_d.value, 2), k = _e[0], arr = _e[1];
422
+ out[k] = ((_b = out[k]) !== null && _b !== void 0 ? _b : []).concat(arr);
423
+ }
420
424
  }
421
- finally { if (e_1) throw e_1.error; }
422
- }
425
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
426
+ finally {
427
+ try {
428
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
429
+ }
430
+ finally { if (e_1) throw e_1.error; }
431
+ }
432
+ });
423
433
  return out;
424
434
  }
425
435
 
426
436
  function isReference(value) {
427
- return (typeof value === "string" &&
428
- value.startsWith("<") &&
429
- value.endsWith(">"));
437
+ return (typeof value === "string" && !!value.match(/^<([\w\.]+)>$/)
438
+ // value.startsWith("<") &&
439
+ // value.endsWith(">")
440
+ );
441
+ }
442
+ function hasReference(value) {
443
+ var _a;
444
+ return typeof value === "string" && !!((_a = value.match) === null || _a === void 0 ? void 0 : _a.call(value, /<(.*?)>/g));
430
445
  }
431
446
  function parseReference(ref) {
432
447
  return ref.slice(1, -1);
@@ -446,29 +461,35 @@ function getValue$1(obj, path) {
446
461
  return value;
447
462
  }
448
463
  function interpolateValue(value, scope) {
464
+ // interpolating arrays
449
465
  if (Array.isArray(value)) {
450
466
  if (!value.length)
451
- return broadCast([]);
452
- if (!value.every(isTree)) {
453
- // interpolate to an array of trees
454
- var mappedValue_1 = value.map(function (v) { return interpolateValue(v, scope); });
455
- if (!mappedValue_1.every(isTree)) {
467
+ return [];
468
+ if (value.every(isTree)) {
469
+ // array of trees: merge the trees into one
470
+ var tree = mergeTrees.apply(void 0, __spreadArray([], __read(value), false));
471
+ // then interpolate each value in the tree in case there are references
472
+ return mapTree(tree, function (v) { return interpolateValue(v, scope); });
473
+ }
474
+ else {
475
+ // array of other values: interpolate to an array of trees
476
+ var mappedValue = value.map(function (v) {
477
+ return broadCast(interpolateValue(v, scope));
478
+ });
479
+ if (!mappedValue.every(isTree)) {
456
480
  // this is a bit of a hack when the values were all plain values
457
481
  // in that case, just return them, which will result in a single tree
458
- return mappedValue_1;
482
+ return mappedValue;
459
483
  }
460
484
  // then, for each value, combine the branches with the same index
461
- return nAryOnTreeBranch(mappedValue_1, function (branches) { return branches.flat(); });
462
- }
463
- else {
464
- // combine the trees into one
465
- var tree = value.reduce(function (acc, t) { return mergeTrees(acc, t); });
466
- return mapTree(tree, function (v) { return interpolateValue(v, scope); });
485
+ return mergeTrees.apply(void 0, __spreadArray([], __read(mappedValue), false));
467
486
  }
468
487
  }
488
+ // a tree is mapped over each value
469
489
  if (isTree(value)) {
470
490
  return mapTree(value, function (v) { return interpolateValue(v, scope); });
471
491
  }
492
+ // an object is interpolated recursively
472
493
  if (typeof value === "object" && value !== null) {
473
494
  if (Object.keys(value).length === 0) {
474
495
  return {};
@@ -483,18 +504,33 @@ function interpolateValue(value, scope) {
483
504
  }));
484
505
  });
485
506
  }
486
- if (!isReference(value)) {
487
- return value;
488
- }
489
- var parts = parseReference(value).split(".");
490
- var mappedValue = getValue$1(scope, parts.slice(0, 2).join("."));
491
- if (parts.length > 2) {
492
- // deep interpolate
493
- mappedValue = mapTree(mappedValue, function (v) {
494
- return getValue$1(v, parts.slice(2).join("."));
495
- });
507
+ if (hasReference(value)) {
508
+ if (isReference(value)) {
509
+ // it's a simple reference string
510
+ var parts_1 = parseReference(value).split(".");
511
+ var mappedValue = getValue$1(scope, parts_1.slice(0, 2).join("."));
512
+ if (parts_1.length > 2) {
513
+ // deep interpolate
514
+ mappedValue = mapTree(mappedValue, function (v) {
515
+ return getValue$1(v, parts_1.slice(2).join("."));
516
+ });
517
+ }
518
+ return mappedValue;
519
+ }
520
+ else {
521
+ // it's a template string, break up as an array of strings and references and interpolate each to a tree
522
+ var strings = value.split(/(<.*?>)/);
523
+ var trees = strings.map(function (str) {
524
+ return broadCast(interpolateValue(str, scope));
525
+ });
526
+ // create a combined string for each item, filling in missing items witht the last
527
+ var combined = nAryOnTree(trees, function (items) {
528
+ return items.join("");
529
+ }, true);
530
+ return combined;
531
+ }
496
532
  }
497
- return mappedValue;
533
+ return value;
498
534
  }
499
535
  function interpolate(inputs, scope) {
500
536
  return Object.fromEntries(Object.entries(inputs).map(function (_a) {
@@ -5126,6 +5162,7 @@ exports.getBranch = getBranch;
5126
5162
  exports.getReferences = getReferences;
5127
5163
  exports.getValue = getValue$1;
5128
5164
  exports.graftTree = graftTree;
5165
+ exports.hasReference = hasReference;
5129
5166
  exports.interpolate = interpolate;
5130
5167
  exports.isReference = isReference;
5131
5168
  exports.isSingleTon = isSingleTon;