replace-all-mustaches 0.0.8 → 0.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/dist/index.cjs CHANGED
@@ -86,24 +86,22 @@ function hasDuplicates(arr) {
86
86
  var import_mustache = __toESM(require("mustache"), 1);
87
87
  function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } = {}) {
88
88
  let current = template;
89
- const seen = /* @__PURE__ */ new Set();
90
- let pass = 0;
91
- for (pass; pass < maxPasses; pass += 1) {
89
+ let last = "";
90
+ for (let pass = 0; pass < maxPasses; pass += 1) {
92
91
  if (hasMustacheTags(current)) {
93
92
  if (detectCycles) {
94
- if (seen.has(current)) {
93
+ if (last === current) {
95
94
  throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
96
95
  }
97
96
  ;
98
- seen.add(current);
97
+ last = current;
99
98
  }
100
99
  ;
101
100
  const next = import_mustache.default.render(current, VariableView.all);
102
- if (next !== "") {
103
- if (next === current) {
104
- return current;
105
- }
106
- ;
101
+ if (next !== "" && next === current) {
102
+ current = next;
103
+ } else {
104
+ return current;
107
105
  }
108
106
  } else {
109
107
  console.log("passes:", pass);
@@ -111,7 +109,6 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
111
109
  }
112
110
  }
113
111
  ;
114
- console.log("passes:", pass);
115
112
  return current;
116
113
  }
117
114
 
package/dist/index.js CHANGED
@@ -46,24 +46,22 @@ function hasDuplicates(arr) {
46
46
  import Mustache from "mustache";
47
47
  function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } = {}) {
48
48
  let current = template;
49
- const seen = /* @__PURE__ */ new Set();
50
- let pass = 0;
51
- for (pass; pass < maxPasses; pass += 1) {
49
+ let last = "";
50
+ for (let pass = 0; pass < maxPasses; pass += 1) {
52
51
  if (hasMustacheTags(current)) {
53
52
  if (detectCycles) {
54
- if (seen.has(current)) {
53
+ if (last === current) {
55
54
  throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
56
55
  }
57
56
  ;
58
- seen.add(current);
57
+ last = current;
59
58
  }
60
59
  ;
61
60
  const next = Mustache.render(current, VariableView.all);
62
- if (next !== "") {
63
- if (next === current) {
64
- return current;
65
- }
66
- ;
61
+ if (next !== "" && next === current) {
62
+ current = next;
63
+ } else {
64
+ return current;
67
65
  }
68
66
  } else {
69
67
  console.log("passes:", pass);
@@ -71,7 +69,6 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
71
69
  }
72
70
  }
73
71
  ;
74
- console.log("passes:", pass);
75
72
  return current;
76
73
  }
77
74
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "replace-all-mustaches",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Replace all mustache templates in a string, including recursive mustache values",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -18,25 +18,25 @@ export function replaceAllMustaches(
18
18
  { maxPasses = 20, detectCycles = true }: MustacheIterationOptions = {},
19
19
  ): string {
20
20
  let current = template;
21
- const seen = new Set<string>();
21
+ let last = ""; // To ensure that the same token isn't replaced twice in a row
22
22
 
23
- let pass = 0
24
- for (pass; pass < maxPasses; pass += 1) {
25
- if (hasMustacheTags(current)){
26
- if (detectCycles) {
27
- if (seen.has(current)) {
23
+ for (let pass = 0; pass < maxPasses; pass += 1) {
24
+ if (hasMustacheTags(current)){ // If any {{}} remain...
25
+ if (detectCycles) { // If you check the number of cycles, which you do by default, throw an error and stop
26
+ if (last === current) {
28
27
  throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
29
28
  };
30
29
 
31
- seen.add(current);
30
+ last = current; // Now the string it's looping over will be checked next time
32
31
  };
33
32
 
34
33
  const next = Mustache.render(current, VariableView.all);
35
-
36
- if (next !== ""){
37
- if (next === current) {
38
- return current; // no more substitutions happened
39
- };
34
+
35
+ // If the string's not empty and it's not the same as the current string...
36
+ if (next !== "" && next === current){
37
+ current = next;
38
+ } else {
39
+ return current; // no more substitutions happened
40
40
  }
41
41
  } else {
42
42
  console.log("passes:",pass)
@@ -44,6 +44,5 @@ export function replaceAllMustaches(
44
44
  }
45
45
  };
46
46
 
47
- console.log("passes:",pass)
48
47
  return current;
49
48
  };