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 +8 -11
- package/dist/index.js +8 -11
- package/package.json +1 -1
- package/src/nodes/replaceAllMustaches.ts +12 -13
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
|
-
|
|
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 (
|
|
93
|
+
if (last === current) {
|
|
95
94
|
throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
|
|
96
95
|
}
|
|
97
96
|
;
|
|
98
|
-
|
|
97
|
+
last = current;
|
|
99
98
|
}
|
|
100
99
|
;
|
|
101
100
|
const next = import_mustache.default.render(current, VariableView.all);
|
|
102
|
-
if (next !== "") {
|
|
103
|
-
|
|
104
|
-
|
|
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
|
-
|
|
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 (
|
|
53
|
+
if (last === current) {
|
|
55
54
|
throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
|
|
56
55
|
}
|
|
57
56
|
;
|
|
58
|
-
|
|
57
|
+
last = current;
|
|
59
58
|
}
|
|
60
59
|
;
|
|
61
60
|
const next = Mustache.render(current, VariableView.all);
|
|
62
|
-
if (next !== "") {
|
|
63
|
-
|
|
64
|
-
|
|
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
|
@@ -18,25 +18,25 @@ export function replaceAllMustaches(
|
|
|
18
18
|
{ maxPasses = 20, detectCycles = true }: MustacheIterationOptions = {},
|
|
19
19
|
): string {
|
|
20
20
|
let current = template;
|
|
21
|
-
|
|
21
|
+
let last = ""; // To ensure that the same token isn't replaced twice in a row
|
|
22
22
|
|
|
23
|
-
let pass = 0
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
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
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
};
|