replace-all-mustaches 0.0.1

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/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 jjkubik
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # replace-all-mustaches
2
+ Replace all mustache templates in a string, including recursive mustache values
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "replace-all-mustaches",
3
+ "version": "0.0.1",
4
+ "description": "Replace all mustache templates in a string, including recursive mustache values",
5
+ "main": "./dist/index.js",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsup"
10
+ },
11
+ "keywords": [],
12
+ "author": "",
13
+ "license": "MIT",
14
+ "type": "module",
15
+ "devDependencies": {
16
+ "@types/mustache": "^4.2.6",
17
+ "tsup": "^8.5.1",
18
+ "typescript": "^6.0.3"
19
+ },
20
+ "dependencies": {
21
+ "mustache": "^4.2.0"
22
+ },
23
+ "allowScripts": {
24
+ "esbuild@0.27.7": true,
25
+ "fsevents@2.3.3": true
26
+ }
27
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./nodes/helperFunctions"
2
+ export * from "./nodes/renderUntilStable"
3
+ export * from "./nodes/Variable"
4
+ export * from "./nodes/VariableView"
@@ -0,0 +1,14 @@
1
+ export type MustacheValue = string|string[]|Function;
2
+
3
+ /**
4
+ * A key-value pair for a variable name and its value.
5
+ */
6
+ export class Variable{
7
+ key:string;
8
+ value:MustacheValue;
9
+
10
+ constructor(key:string,value:MustacheValue){
11
+ this.key = key;
12
+ this.value = value;
13
+ }
14
+ };
@@ -0,0 +1,50 @@
1
+ import { Variable, type MustacheValue } from "./Variable"
2
+ import { hasDuplicates } from "./helperFunctions"
3
+
4
+ /**
5
+ * A generic class to create a view (the context for a Mustache.render() call). This class's _collection property holds all the names in any subsequently created variable registry via the allKeys getter.
6
+ * @todo Prevent duplicates
7
+ */
8
+ export class VariableView {
9
+ private static _collection:VariableView[] = [];
10
+
11
+ static get all(): Record<string, MustacheValue> {
12
+ return Object.fromEntries(
13
+ VariableView._collection.flatMap(view => Object.entries(view))
14
+ );
15
+ };
16
+ static get allNames(){
17
+ return VariableView._collection.map(variable=>Object.keys(variable)).flat();
18
+ };
19
+
20
+ [key: string]: MustacheValue; // [key:string] gives permission for arbitrary string keys
21
+
22
+ constructor(...variableValuePairs:Variable[]){
23
+ variableValuePairs.forEach(pair=>{
24
+ this[pair.key] = pair.value;
25
+
26
+ if (VariableView.allNames.includes(pair.key)){
27
+ console.warn(`Master variable list already contains the variable ${pair.key} being added by ${this.constructor.name}.`);
28
+
29
+ console.trace();
30
+ }
31
+ });
32
+
33
+ VariableView._collection.push(this);
34
+
35
+ if (hasDuplicates(variableValuePairs.map(pair=>pair.key))){
36
+ console.warn(`Duplicated values in ${this.constructor.name}. (And yes, I should and will add where those occur.)`)
37
+ };
38
+ };
39
+
40
+ add(...variableValuePair:Variable[]){
41
+ variableValuePair.forEach(pair=>{
42
+ // It would be a good idea to include a check for duplicate keys
43
+ this[pair.key] = pair.value
44
+ })
45
+ };
46
+
47
+ replace(variableKey:string,value:string|Function){
48
+ this[variableKey] = value;
49
+ };
50
+ };
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Confirm whether there are valid mustache tags remaining in the template string.
3
+ * @param template
4
+ * @returns
5
+ */
6
+ export function hasMustacheTags(template: string): boolean {
7
+ return template.includes("{{") || template.includes("}}");
8
+ };
9
+
10
+ export function hasDuplicates(arr:any[]){
11
+ return new Set(arr).size !== arr.length;
12
+ };
@@ -0,0 +1,44 @@
1
+ import Mustache from "mustache";
2
+ import { VariableView } from "./VariableView";
3
+ import { hasMustacheTags } from "./helperFunctions";
4
+
5
+ type RenderLoopOptions = {
6
+ maxPasses?: number;
7
+ detectCycles?: boolean;
8
+ };
9
+
10
+ export function renderUntilStable(
11
+ template: string,
12
+ view: VariableView,
13
+ { maxPasses = 20, detectCycles = true }: RenderLoopOptions = {},
14
+ ): string {
15
+ let current = template;
16
+ const seen = new Set<string>();
17
+
18
+ let pass = 0
19
+ for (pass; pass < maxPasses; pass += 1) {
20
+ if (hasMustacheTags(current)){
21
+ if (detectCycles) {
22
+ if (seen.has(current)) {
23
+ throw new Error(`Render cycle detected at pass ${pass}: "${current}"`);
24
+ };
25
+
26
+ seen.add(current);
27
+ };
28
+
29
+ const next = Mustache.render(current, VariableView.all);
30
+
31
+ if (next === current) {
32
+ return next; // no more substitutions happened
33
+ };
34
+
35
+ current = next;
36
+ } else {
37
+ console.log("passes:",pass)
38
+ return current;
39
+ }
40
+ };
41
+
42
+ console.log("passes:",pass)
43
+ return current;
44
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ // Visit https://aka.ms/tsconfig to read more about this file
3
+ "compilerOptions": {
4
+ // File Layout
5
+ // "rootDir": "./src",
6
+ // "outDir": "./dist",
7
+
8
+ // Environment Settings
9
+ // See also https://aka.ms/tsconfig/module
10
+ "module": "es2022",
11
+ "target": "es2022",
12
+ "types": [],
13
+ // For nodejs:
14
+ // "lib": ["esnext"],
15
+ // "types": ["node"],
16
+ // and npm install -D @types/node
17
+
18
+ // Other Outputs
19
+ "sourceMap": true,
20
+ "declaration": true,
21
+ "declarationMap": true,
22
+
23
+ // Stricter Typechecking Options
24
+ "noUncheckedIndexedAccess": true,
25
+ "exactOptionalPropertyTypes": true,
26
+
27
+ // Style Options
28
+ // "noImplicitReturns": true,
29
+ // "noImplicitOverride": true,
30
+ // "noUnusedLocals": true,
31
+ // "noUnusedParameters": true,
32
+ // "noFallthroughCasesInSwitch": true,
33
+ // "noPropertyAccessFromIndexSignature": true,
34
+
35
+ // Recommended Options
36
+ "strict": true,
37
+ "jsx": "react-jsx",
38
+ "verbatimModuleSyntax": true,
39
+ "isolatedModules": true,
40
+ "noUncheckedSideEffectImports": true,
41
+ "moduleDetection": "force",
42
+ "skipLibCheck": true,
43
+ },
44
+ "include": ["src"],
45
+ "exclude": ["node_modules"]
46
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'tsup';
2
+
3
+ export default defineConfig({
4
+ format: ['cjs', 'esm'],
5
+ entry: ['./src/index.ts'],
6
+ dts: true,
7
+ shims: true,
8
+ skipNodeModulesBundle: true,
9
+ clean: true,
10
+ });