replace-all-mustaches 0.0.11 → 0.0.12
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 +6 -6
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +5 -5
- package/package.json +1 -1
- package/src/nodes/helperFunctions.ts +2 -2
- package/src/nodes/replaceAllMustaches.ts +4 -4
package/dist/index.cjs
CHANGED
|
@@ -33,7 +33,7 @@ __export(index_exports, {
|
|
|
33
33
|
Variable: () => Variable,
|
|
34
34
|
VariableView: () => VariableView,
|
|
35
35
|
hasDuplicates: () => hasDuplicates,
|
|
36
|
-
|
|
36
|
+
hasValidMustacheTags: () => hasValidMustacheTags,
|
|
37
37
|
replaceAllMustaches: () => replaceAllMustaches
|
|
38
38
|
});
|
|
39
39
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -75,8 +75,8 @@ var VariableView = class _VariableView {
|
|
|
75
75
|
};
|
|
76
76
|
|
|
77
77
|
// src/nodes/helperFunctions.ts
|
|
78
|
-
function
|
|
79
|
-
return template.includes("{{")
|
|
78
|
+
function hasValidMustacheTags(template) {
|
|
79
|
+
return template.includes("{{") && template.includes("}}");
|
|
80
80
|
}
|
|
81
81
|
function hasDuplicates(arr) {
|
|
82
82
|
return new Set(arr).size !== arr.length;
|
|
@@ -88,7 +88,7 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
|
|
|
88
88
|
let current = template;
|
|
89
89
|
let last = "";
|
|
90
90
|
for (let pass = 0; pass < maxPasses; pass += 1) {
|
|
91
|
-
if (
|
|
91
|
+
if (hasValidMustacheTags(current)) {
|
|
92
92
|
if (detectCycles) {
|
|
93
93
|
if (last === current) {
|
|
94
94
|
throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
|
|
@@ -98,7 +98,7 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
|
|
|
98
98
|
}
|
|
99
99
|
;
|
|
100
100
|
const next = import_mustache.default.render(current, VariableView.all);
|
|
101
|
-
if (next
|
|
101
|
+
if (next !== current) {
|
|
102
102
|
current = next;
|
|
103
103
|
} else {
|
|
104
104
|
return current;
|
|
@@ -126,6 +126,6 @@ var Variable = class {
|
|
|
126
126
|
Variable,
|
|
127
127
|
VariableView,
|
|
128
128
|
hasDuplicates,
|
|
129
|
-
|
|
129
|
+
hasValidMustacheTags,
|
|
130
130
|
replaceAllMustaches
|
|
131
131
|
});
|
package/dist/index.d.cts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @param template
|
|
4
4
|
* @returns
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
6
|
+
declare function hasValidMustacheTags(template: string): boolean;
|
|
7
7
|
declare function hasDuplicates(arr: any[]): boolean;
|
|
8
8
|
|
|
9
9
|
type MustacheIterationOptions = {
|
|
@@ -42,4 +42,4 @@ declare class VariableView {
|
|
|
42
42
|
replace(variableKey: string, value: string | Function): void;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export { type MustacheValue, Variable, VariableView, hasDuplicates,
|
|
45
|
+
export { type MustacheValue, Variable, VariableView, hasDuplicates, hasValidMustacheTags, replaceAllMustaches };
|
package/dist/index.d.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
* @param template
|
|
4
4
|
* @returns
|
|
5
5
|
*/
|
|
6
|
-
declare function
|
|
6
|
+
declare function hasValidMustacheTags(template: string): boolean;
|
|
7
7
|
declare function hasDuplicates(arr: any[]): boolean;
|
|
8
8
|
|
|
9
9
|
type MustacheIterationOptions = {
|
|
@@ -42,4 +42,4 @@ declare class VariableView {
|
|
|
42
42
|
replace(variableKey: string, value: string | Function): void;
|
|
43
43
|
}
|
|
44
44
|
|
|
45
|
-
export { type MustacheValue, Variable, VariableView, hasDuplicates,
|
|
45
|
+
export { type MustacheValue, Variable, VariableView, hasDuplicates, hasValidMustacheTags, replaceAllMustaches };
|
package/dist/index.js
CHANGED
|
@@ -35,8 +35,8 @@ var VariableView = class _VariableView {
|
|
|
35
35
|
};
|
|
36
36
|
|
|
37
37
|
// src/nodes/helperFunctions.ts
|
|
38
|
-
function
|
|
39
|
-
return template.includes("{{")
|
|
38
|
+
function hasValidMustacheTags(template) {
|
|
39
|
+
return template.includes("{{") && template.includes("}}");
|
|
40
40
|
}
|
|
41
41
|
function hasDuplicates(arr) {
|
|
42
42
|
return new Set(arr).size !== arr.length;
|
|
@@ -48,7 +48,7 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
|
|
|
48
48
|
let current = template;
|
|
49
49
|
let last = "";
|
|
50
50
|
for (let pass = 0; pass < maxPasses; pass += 1) {
|
|
51
|
-
if (
|
|
51
|
+
if (hasValidMustacheTags(current)) {
|
|
52
52
|
if (detectCycles) {
|
|
53
53
|
if (last === current) {
|
|
54
54
|
throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
|
|
@@ -58,7 +58,7 @@ function replaceAllMustaches(template, { maxPasses = 20, detectCycles = true } =
|
|
|
58
58
|
}
|
|
59
59
|
;
|
|
60
60
|
const next = Mustache.render(current, VariableView.all);
|
|
61
|
-
if (next
|
|
61
|
+
if (next !== current) {
|
|
62
62
|
current = next;
|
|
63
63
|
} else {
|
|
64
64
|
return current;
|
|
@@ -85,6 +85,6 @@ export {
|
|
|
85
85
|
Variable,
|
|
86
86
|
VariableView,
|
|
87
87
|
hasDuplicates,
|
|
88
|
-
|
|
88
|
+
hasValidMustacheTags,
|
|
89
89
|
replaceAllMustaches
|
|
90
90
|
};
|
package/package.json
CHANGED
|
@@ -5,8 +5,8 @@ import { VariableView } from "./VariableView";
|
|
|
5
5
|
* @param template
|
|
6
6
|
* @returns
|
|
7
7
|
*/
|
|
8
|
-
export function
|
|
9
|
-
return template.includes("{{")
|
|
8
|
+
export function hasValidMustacheTags(template: string): boolean {
|
|
9
|
+
return template.includes("{{") && template.includes("}}");
|
|
10
10
|
};
|
|
11
11
|
|
|
12
12
|
export function hasDuplicates(arr:any[]){
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Mustache from "mustache";
|
|
2
2
|
import { VariableView } from "./VariableView";
|
|
3
|
-
import {
|
|
3
|
+
import { hasValidMustacheTags } from "./helperFunctions";
|
|
4
4
|
|
|
5
5
|
type MustacheIterationOptions = {
|
|
6
6
|
maxPasses?: number;
|
|
@@ -21,7 +21,7 @@ export function replaceAllMustaches(
|
|
|
21
21
|
let last = ""; // To ensure that the same token isn't replaced twice in a row
|
|
22
22
|
|
|
23
23
|
for (let pass = 0; pass < maxPasses; pass += 1) {
|
|
24
|
-
if (
|
|
24
|
+
if (hasValidMustacheTags(current)){ // If any {{}} remain...
|
|
25
25
|
if (detectCycles) { // If you check the number of cycles, which you do by default, throw an error and stop
|
|
26
26
|
if (last === current) {
|
|
27
27
|
throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
|
|
@@ -32,8 +32,8 @@ export function replaceAllMustaches(
|
|
|
32
32
|
|
|
33
33
|
const next = Mustache.render(current, VariableView.all);
|
|
34
34
|
|
|
35
|
-
// If the string's not
|
|
36
|
-
if (next
|
|
35
|
+
// If the string's not the same as the current string, set it as the new template and re-loop
|
|
36
|
+
if (next !== current){
|
|
37
37
|
current = next;
|
|
38
38
|
} else {
|
|
39
39
|
return current; // no more substitutions happened
|