@bedelightful/es6-template-strings 0.0.5
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/LICENSE +8 -0
- package/README.md +60 -0
- package/dist/es/compile.js +105 -0
- package/dist/es/compile.js.map +1 -0
- package/dist/es/index.d.ts +22 -0
- package/dist/es/index.js +9 -0
- package/dist/es/index.js.map +1 -0
- package/dist/es/passthru-array.js +18 -0
- package/dist/es/passthru-array.js.map +1 -0
- package/dist/es/passthru.js +13 -0
- package/dist/es/passthru.js.map +1 -0
- package/dist/es/resolve-to-array.js +12 -0
- package/dist/es/resolve-to-array.js.map +1 -0
- package/dist/es/resolve-to-string.js +12 -0
- package/dist/es/resolve-to-string.js.map +1 -0
- package/dist/es/resolve.js +62 -0
- package/dist/es/resolve.js.map +1 -0
- package/dist/es/to-array.js +16 -0
- package/dist/es/to-array.js.map +1 -0
- package/dist/es/to-string.js +16 -0
- package/dist/es/to-string.js.map +1 -0
- package/dist/es/types.js +1 -0
- package/dist/es/types.js.map +1 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +3123 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +20 -0
- package/dist/index.min.js.map +1 -0
- package/dist/lib/compile.js +134 -0
- package/dist/lib/compile.js.map +1 -0
- package/dist/lib/index.d.cts +22 -0
- package/dist/lib/index.js +44 -0
- package/dist/lib/index.js.map +1 -0
- package/dist/lib/passthru-array.js +37 -0
- package/dist/lib/passthru-array.js.map +1 -0
- package/dist/lib/passthru.js +32 -0
- package/dist/lib/passthru.js.map +1 -0
- package/dist/lib/resolve-to-array.js +41 -0
- package/dist/lib/resolve-to-array.js.map +1 -0
- package/dist/lib/resolve-to-string.js +41 -0
- package/dist/lib/resolve-to-string.js.map +1 -0
- package/dist/lib/resolve.js +91 -0
- package/dist/lib/resolve.js.map +1 -0
- package/dist/lib/to-array.js +45 -0
- package/dist/lib/to-array.js.map +1 -0
- package/dist/lib/to-string.js +45 -0
- package/dist/lib/to-string.js.map +1 -0
- package/dist/lib/types.js +17 -0
- package/dist/lib/types.js.map +1 -0
- package/package.json +66 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
Copyright 2026 BE DELIGHTFUL
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
8
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @bedelightful/es6-template-strings
|
|
2
|
+
|
|
3
|
+
ES6 Template Strings Parser Engine
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
This package provides a template string parsing engine that supports ES6-style syntax. It allows you to interpolate variables and expressions within strings using the `${expression}` syntax.
|
|
8
|
+
|
|
9
|
+
## Usage
|
|
10
|
+
|
|
11
|
+
```typescript
|
|
12
|
+
import { resolveToString, resolveToArray } from "@delightful/es6-template-strings";
|
|
13
|
+
|
|
14
|
+
// Basic usage
|
|
15
|
+
console.log(resolveToString("hello ${name}", { name: "world" }));
|
|
16
|
+
// Output: "hello world"
|
|
17
|
+
|
|
18
|
+
// Return array of template parts and substitutions
|
|
19
|
+
console.log(resolveToArray("hello ${name}", { name: "world" }));
|
|
20
|
+
// Output: ["hello ", "world"]
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
## Configuration Options
|
|
24
|
+
|
|
25
|
+
| Option | Description | Type | Default | Required |
|
|
26
|
+
|:------:|:----------:|:----:|:-------:|:--------:|
|
|
27
|
+
| notation | Template syntax prefix | string | "$" | No |
|
|
28
|
+
| notationStart | Template syntax start marker | string | "{" | No |
|
|
29
|
+
| notationEnd | Template syntax end marker | string | "}" | No |
|
|
30
|
+
| partial | Skip failed expressions instead of returning undefined | boolean | false | No |
|
|
31
|
+
|
|
32
|
+
## Notes
|
|
33
|
+
|
|
34
|
+
- When an expression cannot be resolved:
|
|
35
|
+
- If `partial: true`, the original `${expression}` string will be preserved
|
|
36
|
+
- If `partial: false` (default), undefined will be returned for that expression
|
|
37
|
+
- The package handles nested expressions and escape sequences properly
|
|
38
|
+
|
|
39
|
+
## Development
|
|
40
|
+
|
|
41
|
+
To set up the development environment:
|
|
42
|
+
|
|
43
|
+
1. Clone the repository
|
|
44
|
+
2. Install dependencies: `npm install`
|
|
45
|
+
3. Build the package: `npm run build`
|
|
46
|
+
4. Run tests: `npm test`
|
|
47
|
+
|
|
48
|
+
## Iteration Process
|
|
49
|
+
|
|
50
|
+
The package follows semantic versioning:
|
|
51
|
+
|
|
52
|
+
1. Bug fixes result in patch version increments
|
|
53
|
+
2. New features that maintain backward compatibility result in minor version increments
|
|
54
|
+
3. Breaking changes result in major version increments
|
|
55
|
+
|
|
56
|
+
For contributing:
|
|
57
|
+
1. Fork the repository
|
|
58
|
+
2. Create a feature branch
|
|
59
|
+
3. Submit a pull request with detailed description of changes
|
|
60
|
+
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
3
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
4
|
+
var __publicField = (obj, key, value) => {
|
|
5
|
+
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
|
|
6
|
+
return value;
|
|
7
|
+
};
|
|
8
|
+
var _a;
|
|
9
|
+
import esniff from "esniff";
|
|
10
|
+
let i;
|
|
11
|
+
let current;
|
|
12
|
+
let literals;
|
|
13
|
+
let substitutions;
|
|
14
|
+
let template;
|
|
15
|
+
let Notation = "$";
|
|
16
|
+
let NotationStart = "{";
|
|
17
|
+
let NotationEnd = "}";
|
|
18
|
+
let Compile = (_a = class {
|
|
19
|
+
}, __name(_a, "Compile"), __publicField(_a, "sOut", /* @__PURE__ */ __name((char) => {
|
|
20
|
+
if (char === "\\")
|
|
21
|
+
return _a.sEscape;
|
|
22
|
+
if (char === Notation)
|
|
23
|
+
return _a.sAhead;
|
|
24
|
+
current += char;
|
|
25
|
+
return _a.sOut;
|
|
26
|
+
}, "sOut")), __publicField(_a, "sEscape", /* @__PURE__ */ __name((char) => {
|
|
27
|
+
if (char !== "\\" && char !== Notation) {
|
|
28
|
+
current += "\\";
|
|
29
|
+
}
|
|
30
|
+
current += char;
|
|
31
|
+
return _a.sOut;
|
|
32
|
+
}, "sEscape")), __publicField(_a, "sAhead", /* @__PURE__ */ __name((char) => {
|
|
33
|
+
if (char === NotationStart) {
|
|
34
|
+
literals == null ? void 0 : literals.push(current);
|
|
35
|
+
current = "";
|
|
36
|
+
return _a.sIn;
|
|
37
|
+
}
|
|
38
|
+
if (char === Notation) {
|
|
39
|
+
current += Notation;
|
|
40
|
+
return _a.sAhead;
|
|
41
|
+
}
|
|
42
|
+
current += Notation + char;
|
|
43
|
+
return _a.sOut;
|
|
44
|
+
}, "sAhead")), __publicField(_a, "sIn", /* @__PURE__ */ __name(() => {
|
|
45
|
+
const code = template.slice(i);
|
|
46
|
+
let end;
|
|
47
|
+
esniff(code, NotationEnd, (j) => {
|
|
48
|
+
if (esniff.nest >= 0)
|
|
49
|
+
return esniff.next();
|
|
50
|
+
end = j;
|
|
51
|
+
});
|
|
52
|
+
if (end != null) {
|
|
53
|
+
substitutions == null ? void 0 : substitutions.push(template.slice(i, i + end));
|
|
54
|
+
i += end;
|
|
55
|
+
current = "";
|
|
56
|
+
return _a.sOut;
|
|
57
|
+
}
|
|
58
|
+
end = code.length;
|
|
59
|
+
i += end;
|
|
60
|
+
current += code;
|
|
61
|
+
return _a.sIn;
|
|
62
|
+
}, "sIn")), __publicField(_a, "sInEscape", /* @__PURE__ */ __name(function(char) {
|
|
63
|
+
if (char !== "\\" && char !== NotationEnd) {
|
|
64
|
+
current += "\\";
|
|
65
|
+
}
|
|
66
|
+
current += char;
|
|
67
|
+
return _a.sIn;
|
|
68
|
+
}, "sInEscape")), _a);
|
|
69
|
+
function compile(str, options) {
|
|
70
|
+
current = "";
|
|
71
|
+
literals = [];
|
|
72
|
+
substitutions = [];
|
|
73
|
+
Notation = (options == null ? void 0 : options.notation) || "$";
|
|
74
|
+
NotationStart = (options == null ? void 0 : options.notationStart) || "{";
|
|
75
|
+
NotationEnd = (options == null ? void 0 : options.notationEnd) || "}";
|
|
76
|
+
template = String(str);
|
|
77
|
+
const { length } = template;
|
|
78
|
+
let state = Compile.sOut;
|
|
79
|
+
for (i = 0; i < length; i += 1) {
|
|
80
|
+
state = state(template[i]);
|
|
81
|
+
}
|
|
82
|
+
if (state === Compile.sOut) {
|
|
83
|
+
literals.push(current);
|
|
84
|
+
} else if (state === Compile.sEscape) {
|
|
85
|
+
literals.push(`${current}\\`);
|
|
86
|
+
} else if (state === Compile.sAhead) {
|
|
87
|
+
literals.push(current + Notation);
|
|
88
|
+
} else if (state === Compile.sIn) {
|
|
89
|
+
literals[literals.length - 1] += `${Notation}${NotationStart}${current}`;
|
|
90
|
+
} else if (state === Compile.sInEscape) {
|
|
91
|
+
literals[literals.length - 1] += `${Notation}${NotationStart}${current}\\`;
|
|
92
|
+
}
|
|
93
|
+
const result = {
|
|
94
|
+
literals,
|
|
95
|
+
substitutions
|
|
96
|
+
};
|
|
97
|
+
literals = null;
|
|
98
|
+
substitutions = null;
|
|
99
|
+
return result;
|
|
100
|
+
}
|
|
101
|
+
__name(compile, "compile");
|
|
102
|
+
export {
|
|
103
|
+
compile as default
|
|
104
|
+
};
|
|
105
|
+
//# sourceMappingURL=compile.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/compile.ts"],"sourcesContent":["import esniff from \"esniff\"\nimport type { CompileOptions } from \"./types\"\n\nlet i: number\nlet current: string\nlet literals: null | Array<string>\nlet substitutions: null | Array<string>\nlet template: string\n\n/** Prefix for template syntax */\nlet Notation: string = \"$\"\n/** Opening delimiter for template syntax */\nlet NotationStart: string = \"{\"\n/** Closing delimiter for template syntax */\nlet NotationEnd: string = \"}\"\n\nexport interface Result {\n\tliterals: string[]\n\tsubstitutions: string[]\n}\n\ntype State = (char: string) => State\n\nclass Compile {\n\tstatic sOut: State = (char) => {\n\t\tif (char === \"\\\\\") return Compile.sEscape\n\t\tif (char === Notation) return Compile.sAhead\n\t\tcurrent += char\n\t\treturn Compile.sOut\n\t}\n\n\tstatic sEscape: State = (char) => {\n\t\tif (char !== \"\\\\\" && char !== Notation) {\n\t\t\tcurrent += \"\\\\\"\n\t\t}\n\t\tcurrent += char\n\t\treturn Compile.sOut\n\t}\n\n\tstatic sAhead: State = (char) => {\n\t\tif (char === NotationStart) {\n\t\t\tliterals?.push(current)\n\t\t\tcurrent = \"\"\n\t\t\treturn Compile.sIn\n\t\t}\n\t\tif (char === Notation) {\n\t\t\tcurrent += Notation\n\t\t\treturn Compile.sAhead\n\t\t}\n\t\tcurrent += Notation + char\n\t\treturn Compile.sOut\n\t}\n\n\tstatic sIn: State = () => {\n\t\tconst code = template.slice(i)\n\t\tlet end\n\n\t\t// eslint-disable-next-line consistent-return\n\t\tesniff(code, NotationEnd, (j: number) => {\n\t\t\tif (esniff.nest >= 0) return esniff.next()\n\t\t\tend = j\n\t\t})\n\t\tif (end != null) {\n\t\t\tsubstitutions?.push(template.slice(i, i + end))\n\t\t\ti += end\n\t\t\tcurrent = \"\"\n\t\t\treturn Compile.sOut\n\t\t}\n\t\tend = code.length\n\t\ti += end\n\t\tcurrent += code\n\t\treturn Compile.sIn\n\t}\n\n\tstatic sInEscape: State = function (char) {\n\t\tif (char !== \"\\\\\" && char !== NotationEnd) {\n\t\t\tcurrent += \"\\\\\"\n\t\t}\n\t\tcurrent += char\n\t\treturn Compile.sIn\n\t}\n}\n\nexport default function compile(str: string, options?: CompileOptions): Result {\n\tcurrent = \"\"\n\tliterals = []\n\tsubstitutions = []\n\n\tNotation = options?.notation || \"$\"\n\tNotationStart = options?.notationStart || \"{\"\n\tNotationEnd = options?.notationEnd || \"}\"\n\n\ttemplate = String(str)\n\tconst { length } = template\n\n\tlet state: State = Compile.sOut\n\tfor (i = 0; i < length; i += 1) {\n\t\tstate = state(template[i])\n\t}\n\tif (state === Compile.sOut) {\n\t\tliterals.push(current)\n\t} else if (state === Compile.sEscape) {\n\t\tliterals.push(`${current}\\\\`)\n\t} else if (state === Compile.sAhead) {\n\t\tliterals.push(current + Notation)\n\t} else if (state === Compile.sIn) {\n\t\tliterals[literals.length - 1] += `${Notation}${NotationStart}${current}`\n\t} else if (state === Compile.sInEscape) {\n\t\tliterals[literals.length - 1] += `${Notation}${NotationStart}${current}\\\\`\n\t}\n\n\tconst result: Result = { literals, substitutions }\n\tliterals = null\n\tsubstitutions = null\n\treturn result\n}\n"],"mappings":"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","names":["esniff","i","current","literals","substitutions","template","Notation","NotationStart","NotationEnd","Compile","sOut","char","sEscape","sAhead","push","sIn","code","slice","end","j","nest","next","length","sInEscape","str","options","notation","notationStart","notationEnd","String","state","result","compile"]}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Parser options */
|
|
2
|
+
interface CompileOptions {
|
|
3
|
+
/** Prefix for template syntax */
|
|
4
|
+
notation?: string;
|
|
5
|
+
/** Opening delimiter for template syntax */
|
|
6
|
+
notationStart?: string;
|
|
7
|
+
/** Closing delimiter for template syntax */
|
|
8
|
+
notationEnd?: string;
|
|
9
|
+
}
|
|
10
|
+
interface ResolveOptions {
|
|
11
|
+
/** Skip failed expressions; otherwise return undefined for failures */
|
|
12
|
+
partial?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Template parsing options */
|
|
15
|
+
interface TemplateOptions extends CompileOptions, ResolveOptions {
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare function toArray(template: string, context: Record<string, any>, options?: TemplateOptions): string[];
|
|
19
|
+
|
|
20
|
+
declare function export_default(template: string, context: Record<string, any>, options?: TemplateOptions): string;
|
|
21
|
+
|
|
22
|
+
export { export_default as default, toArray as resolveToArray, export_default as resolveToString };
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"sourcesContent":["import resolveToArray from \"./to-array\"\nimport resolveToString from \"./to-string\"\n\nexport { resolveToString, resolveToArray }\nexport default resolveToString\n"],"mappings":"AAAA,OAAOA,oBAAoB;AAC3B,OAAOC,qBAAqB;AAG5B,IAAA,cAAeA;","names":["resolveToArray","resolveToString"]}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
var passthru_array_default = /* @__PURE__ */ __name((literals, ...substitutions) => {
|
|
4
|
+
const result = [];
|
|
5
|
+
const l = literals.length;
|
|
6
|
+
if (!l) {
|
|
7
|
+
return result;
|
|
8
|
+
}
|
|
9
|
+
result.push(literals[0]);
|
|
10
|
+
for (let i = 1; i < l; i += 1) {
|
|
11
|
+
result.push(arguments[i], literals[i]);
|
|
12
|
+
}
|
|
13
|
+
return result;
|
|
14
|
+
}, "default");
|
|
15
|
+
export {
|
|
16
|
+
passthru_array_default as default
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=passthru-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/passthru-array.ts"],"sourcesContent":["// @ts-ignore\nexport default (literals: Array<string>, ...substitutions: Array<string>): Array<string> => {\n\tconst result: Array<string> = []\n\tconst l = literals.length\n\tif (!l) {\n\t\treturn result\n\t}\n\tresult.push(literals[0])\n\tfor (let i = 1; i < l; i += 1) {\n\t\t// @ts-ignore\n\t\tresult.push(arguments[i], literals[i])\n\t}\n\treturn result\n}\n"],"mappings":";;AACA,IAAA,yBAAe,wBAACA,aAA4BC,kBAAAA;AAC3C,QAAMC,SAAwB,CAAA;AAC9B,QAAMC,IAAIH,SAASI;AACnB,MAAI,CAACD,GAAG;AACP,WAAOD;EACR;AACAA,SAAOG,KAAKL,SAAS,CAAA,CAAE;AACvB,WAASM,IAAI,GAAGA,IAAIH,GAAGG,KAAK,GAAG;AAE9BJ,WAAOG,KAAKE,UAAUD,CAAAA,GAAIN,SAASM,CAAAA,CAAE;EACtC;AACA,SAAOJ;AACR,GAZe;","names":["literals","substitutions","result","l","length","push","i","arguments"]}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
function passthru_default(literals, ..._substitutions) {
|
|
4
|
+
const args = arguments;
|
|
5
|
+
return literals.reduce((a, b, i) => {
|
|
6
|
+
return a + (args[i] === void 0 ? "" : String(args[i])) + b;
|
|
7
|
+
});
|
|
8
|
+
}
|
|
9
|
+
__name(passthru_default, "default");
|
|
10
|
+
export {
|
|
11
|
+
passthru_default as default
|
|
12
|
+
};
|
|
13
|
+
//# sourceMappingURL=passthru.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/passthru.ts"],"sourcesContent":["export default function (literals: Array<string>, ..._substitutions: Array<string>) {\n\t// eslint-disable-next-line prefer-rest-params\n\tconst args = arguments\n\treturn literals.reduce((a: string, b: string, i: number): string => {\n\t\treturn a + (args[i] === undefined ? \"\" : String(args[i])) + b\n\t})\n}\n"],"mappings":";;AAAe,SAAf,iBAAyBA,aAA4BC,gBAA6B;AAEjF,QAAMC,OAAOC;AACb,SAAOH,SAASI,OAAO,CAACC,GAAWC,GAAWC,MAAAA;AAC7C,WAAOF,KAAKH,KAAKK,CAAAA,MAAOC,SAAY,KAAKC,OAAOP,KAAKK,CAAAA,CAAE,KAAKD;EAC7D,CAAA;AACD;AANA;","names":["literals","_substitutions","args","arguments","reduce","a","b","i","undefined","String"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import resolve from "./resolve";
|
|
4
|
+
import passthru from "./passthru-array";
|
|
5
|
+
var resolve_to_array_default = /* @__PURE__ */ __name((data, context, options) => {
|
|
6
|
+
const [literals, ...result] = resolve(data, context, options);
|
|
7
|
+
return passthru.call(null, literals, ...result);
|
|
8
|
+
}, "default");
|
|
9
|
+
export {
|
|
10
|
+
resolve_to_array_default as default
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=resolve-to-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/resolve-to-array.ts"],"sourcesContent":["import resolve from \"./resolve\"\nimport passthru from \"./passthru-array\"\nimport type { Result } from \"./compile\"\nimport type { ResolveOptions } from \"./types\"\n\nexport default (data: Result, context: Record<string, any>, options?: ResolveOptions) => {\n\tconst [literals, ...result] = resolve(data, context, options)\n\treturn passthru.call(null, literals as Array<string>, ...(result as Array<string>))\n}\n"],"mappings":";;AAAA,OAAOA,aAAa;AACpB,OAAOC,cAAc;AAIrB,IAAA,2BAAe,wBAACC,MAAcC,SAA8BC,YAAAA;AAC3D,QAAM,CAACC,UAAU,GAAGC,MAAAA,IAAUN,QAAQE,MAAMC,SAASC,OAAAA;AACrD,SAAOH,SAASM,KAAK,MAAMF,UAAAA,GAA+BC,MAAAA;AAC3D,GAHe;","names":["resolve","passthru","data","context","options","literals","result","call"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import resolve from "./resolve";
|
|
4
|
+
import passthru from "./passthru";
|
|
5
|
+
var resolve_to_string_default = /* @__PURE__ */ __name((data, context, options) => {
|
|
6
|
+
const [literals, ...result] = resolve(data, context, options);
|
|
7
|
+
return passthru.call(null, literals, ...result);
|
|
8
|
+
}, "default");
|
|
9
|
+
export {
|
|
10
|
+
resolve_to_string_default as default
|
|
11
|
+
};
|
|
12
|
+
//# sourceMappingURL=resolve-to-string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/resolve-to-string.ts"],"sourcesContent":["import resolve from \"./resolve\"\nimport passthru from \"./passthru\"\nimport type { Result } from \"./compile\"\nimport type { ResolveOptions } from \"./types\"\n\nexport default (data: Result, context: Record<string, any>, options?: ResolveOptions) => {\n\tconst [literals, ...result] = resolve(data, context, options)\n\treturn passthru.call(null, literals as Array<string>, ...(result as Array<string>))\n}\n"],"mappings":";;AAAA,OAAOA,aAAa;AACpB,OAAOC,cAAc;AAIrB,IAAA,4BAAe,wBAACC,MAAcC,SAA8BC,YAAAA;AAC3D,QAAM,CAACC,UAAU,GAAGC,MAAAA,IAAUN,QAAQE,MAAMC,SAASC,OAAAA;AACrD,SAAOH,SAASM,KAAK,MAAMF,UAAAA,GAA+BC,MAAAA;AAC3D,GAHe;","names":["resolve","passthru","data","context","options","literals","result","call"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import isVarNameValid from "esniff/is-var-name-valid";
|
|
4
|
+
const { stringify } = JSON;
|
|
5
|
+
function isValue(value) {
|
|
6
|
+
if (value === null || value === void 0)
|
|
7
|
+
throw new TypeError("Cannot use null or undefined");
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
__name(isValue, "isValue");
|
|
11
|
+
function process(src, obj) {
|
|
12
|
+
for (const key in src) {
|
|
13
|
+
obj[key] = src[key];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
__name(process, "process");
|
|
17
|
+
function normalize(...args) {
|
|
18
|
+
const result = /* @__PURE__ */ Object.create(null);
|
|
19
|
+
args.forEach(function(options) {
|
|
20
|
+
if (!isValue(options))
|
|
21
|
+
return;
|
|
22
|
+
process(Object(options), result);
|
|
23
|
+
});
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
__name(normalize, "normalize");
|
|
27
|
+
function processData(data, context, options = {}) {
|
|
28
|
+
isValue(data) && isValue(data == null ? void 0 : data.literals) && isValue(data == null ? void 0 : data.substitutions);
|
|
29
|
+
context = normalize(context);
|
|
30
|
+
const names = Object.keys(context).filter(isVarNameValid);
|
|
31
|
+
const argNames = names.join(", ");
|
|
32
|
+
const argValues = names.map((name) => context[name]);
|
|
33
|
+
const { substitutions = [], literals = [] } = data;
|
|
34
|
+
return [
|
|
35
|
+
literals
|
|
36
|
+
].concat(substitutions.map((expr) => {
|
|
37
|
+
let resolver;
|
|
38
|
+
if (!expr)
|
|
39
|
+
return void 0;
|
|
40
|
+
try {
|
|
41
|
+
resolver = new Function(argNames, `return (${expr})`);
|
|
42
|
+
} catch (error) {
|
|
43
|
+
throw new TypeError(`Unable to compile expression:
|
|
44
|
+
args: ${stringify(argNames)}
|
|
45
|
+
body: ${stringify(expr)}
|
|
46
|
+
error: ${error.stack}`);
|
|
47
|
+
}
|
|
48
|
+
try {
|
|
49
|
+
return resolver.apply(null, argValues);
|
|
50
|
+
} catch (e) {
|
|
51
|
+
if (options.partial) {
|
|
52
|
+
return `\${${expr}}`;
|
|
53
|
+
}
|
|
54
|
+
return "undefined";
|
|
55
|
+
}
|
|
56
|
+
}));
|
|
57
|
+
}
|
|
58
|
+
__name(processData, "processData");
|
|
59
|
+
export {
|
|
60
|
+
processData as default
|
|
61
|
+
};
|
|
62
|
+
//# sourceMappingURL=resolve.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/resolve.ts"],"sourcesContent":["import isVarNameValid from \"esniff/is-var-name-valid\"\nimport type { Result } from \"./compile\"\nimport type { ResolveOptions } from \"./types\"\n\nconst { stringify } = JSON\n\nfunction isValue(value: any): any | never {\n\tif (value === null || value === undefined) throw new TypeError(\"Cannot use null or undefined\")\n\treturn value\n}\n\nfunction process(src: Record<string, any>, obj: Record<string, any>) {\n\t// eslint-disable-next-line guard-for-in,no-restricted-syntax\n\tfor (const key in src) {\n\t\tobj[key] = src[key]\n\t}\n}\n\nfunction normalize(...args: Array<any>) {\n\tconst result: Record<string, any> = Object.create(null)\n\targs.forEach(function (options) {\n\t\tif (!isValue(options)) return\n\t\tprocess(Object(options), result)\n\t})\n\treturn result\n}\n\nexport default function processData(\n\tdata: Result,\n\tcontext: Record<string, any>,\n\toptions: ResolveOptions = {},\n): Array<Array<string> | string> {\n\tisValue(data) && isValue(data?.literals) && isValue(data?.substitutions)\n\tcontext = normalize(context)\n\tconst names = Object.keys(context).filter(isVarNameValid)\n\tconst argNames = names.join(\", \")\n\tconst argValues = names.map((name: string) => context[name])\n\tconst { substitutions = [], literals = [] } = data\n\n\t// For failed expressions, return ${} only when options.partial is true; otherwise return undefined\n\treturn [literals].concat(\n\t\tsubstitutions.map((expr: string) => {\n\t\t\tlet resolver: Function\n\t\t\tif (!expr) return undefined\n\t\t\ttry {\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-implied-eval\n\t\t\t\tresolver = new Function(argNames, `return (${expr})`)\n\t\t\t} catch (error: any) {\n\t\t\t\tthrow new TypeError(\n\t\t\t\t\t`Unable to compile expression:\\n\\targs: ${stringify(argNames)}\\n\\tbody: ${stringify(\n\t\t\t\t\t\texpr,\n\t\t\t\t\t)}\\n\\terror: ${error.stack}`,\n\t\t\t\t)\n\t\t\t}\n\t\t\ttry {\n\t\t\t\t// eslint-disable-next-line prefer-spread\n\t\t\t\treturn resolver.apply(null, argValues)\n\t\t\t} catch (e) {\n\t\t\t\tif (options.partial) {\n\t\t\t\t\treturn `\\${${expr}}`\n\t\t\t\t}\n\t\t\t\treturn \"undefined\"\n\n\t\t\t\t// Error handling intentionally suppressed for now; returns undefined instead\n\t\t\t\t// throw new TypeError(\n\t\t\t\t// \t`Unable to resolve expression:\\n\\targs: ${ stringify(argNames) }\\n\\tbody: ${ stringify(\n\t\t\t\t// \t\texpr\n\t\t\t\t// \t) }\\n\\terror: ${ e.stack }`\n\t\t\t\t// )\n\t\t\t}\n\t\t}),\n\t)\n}\n"],"mappings":";;AAAA,OAAOA,oBAAoB;AAI3B,MAAM,EAAEC,UAAS,IAAKC;AAEtB,SAASC,QAAQC,OAAU;AAC1B,MAAIA,UAAU,QAAQA,UAAUC;AAAW,UAAM,IAAIC,UAAU,8BAAA;AAC/D,SAAOF;AACR;AAHSD;AAKT,SAASI,QAAQC,KAA0BC,KAAwB;AAElE,aAAWC,OAAOF,KAAK;AACtBC,QAAIC,GAAAA,IAAOF,IAAIE,GAAAA;EAChB;AACD;AALSH;AAOT,SAASI,aAAaC,MAAgB;AACrC,QAAMC,SAA8BC,uBAAOC,OAAO,IAAA;AAClDH,OAAKI,QAAQ,SAAUC,SAAO;AAC7B,QAAI,CAACd,QAAQc,OAAAA;AAAU;AACvBV,YAAQO,OAAOG,OAAAA,GAAUJ,MAAAA;EAC1B,CAAA;AACA,SAAOA;AACR;AAPSF;AASM,SAAf,YACCO,MACAC,SACAF,UAA0B,CAAC,GAAC;AAE5Bd,UAAQe,IAAAA,KAASf,QAAQe,6BAAME,QAAAA,KAAajB,QAAQe,6BAAMG,aAAAA;AAC1DF,YAAUR,UAAUQ,OAAAA;AACpB,QAAMG,QAAQR,OAAOS,KAAKJ,OAAAA,EAASK,OAAOxB,cAAAA;AAC1C,QAAMyB,WAAWH,MAAMI,KAAK,IAAA;AAC5B,QAAMC,YAAYL,MAAMM,IAAI,CAACC,SAAiBV,QAAQU,IAAAA,CAAK;AAC3D,QAAM,EAAER,gBAAgB,CAAA,GAAID,WAAW,CAAA,EAAE,IAAKF;AAG9C,SAAO;IAACE;IAAUU,OACjBT,cAAcO,IAAI,CAACG,SAAAA;AAClB,QAAIC;AACJ,QAAI,CAACD;AAAM,aAAO1B;AAClB,QAAI;AAEH2B,iBAAW,IAAIC,SAASR,UAAU,WAAWM,IAAAA,GAAO;IACrD,SAASG,OAAY;AACpB,YAAM,IAAI5B,UACT;SAA0CL,UAAUwB,QAAAA,CAAAA;SAAsBxB,UACzE8B,IAAAA,CAAAA;UACcG,MAAMC,KAAK,EAAE;IAE9B;AACA,QAAI;AAEH,aAAOH,SAASI,MAAM,MAAMT,SAAAA;IAC7B,SAASU,GAAG;AACX,UAAIpB,QAAQqB,SAAS;AACpB,eAAO,MAAMP,IAAAA;MACd;AACA,aAAO;IAQR;EACD,CAAA,CAAA;AAEF;AA7CwBQ;","names":["isVarNameValid","stringify","JSON","isValue","value","undefined","TypeError","process","src","obj","key","normalize","args","result","Object","create","forEach","options","data","context","literals","substitutions","names","keys","filter","argNames","join","argValues","map","name","concat","expr","resolver","Function","error","stack","apply","e","partial","processData"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { omit } from "lodash-es";
|
|
4
|
+
import compile from "./compile";
|
|
5
|
+
import resolve from "./resolve-to-array";
|
|
6
|
+
function toArray(template, context, options) {
|
|
7
|
+
const compileOptions = omit(options, "partial");
|
|
8
|
+
return resolve(compile(template, compileOptions), context, {
|
|
9
|
+
partial: options == null ? void 0 : options.partial
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
__name(toArray, "toArray");
|
|
13
|
+
export {
|
|
14
|
+
toArray as default
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=to-array.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/to-array.ts"],"sourcesContent":["import { omit } from \"lodash-es\"\nimport compile from \"./compile\"\nimport resolve from \"./resolve-to-array\"\nimport type { CompileOptions, TemplateOptions } from \"./types\"\n\nexport default function toArray(\n\ttemplate: string,\n\tcontext: Record<string, any>,\n\toptions?: TemplateOptions,\n) {\n\tconst compileOptions: CompileOptions = omit(options, \"partial\")\n\treturn resolve(compile(template, compileOptions), context, { partial: options?.partial })\n}\n"],"mappings":";;AAAA,SAASA,YAAY;AACrB,OAAOC,aAAa;AACpB,OAAOC,aAAa;AAGL,SAAf,QACCC,UACAC,SACAC,SAAyB;AAEzB,QAAMC,iBAAiCN,KAAKK,SAAS,SAAA;AACrD,SAAOH,QAAQD,QAAQE,UAAUG,cAAAA,GAAiBF,SAAS;IAAEG,SAASF,mCAASE;EAAQ,CAAA;AACxF;AAPwBC;","names":["omit","compile","resolve","template","context","options","compileOptions","partial","toArray"]}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
import { omit } from "lodash-es";
|
|
4
|
+
import compile from "./compile";
|
|
5
|
+
import resolve from "./resolve-to-string";
|
|
6
|
+
function to_string_default(template, context, options) {
|
|
7
|
+
const compileOptions = omit(options, "partial");
|
|
8
|
+
return resolve(compile(template, compileOptions), context, {
|
|
9
|
+
partial: options == null ? void 0 : options.partial
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
__name(to_string_default, "default");
|
|
13
|
+
export {
|
|
14
|
+
to_string_default as default
|
|
15
|
+
};
|
|
16
|
+
//# sourceMappingURL=to-string.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/to-string.ts"],"sourcesContent":["import { omit } from \"lodash-es\"\nimport compile from \"./compile\"\nimport resolve from \"./resolve-to-string\"\nimport type { CompileOptions, TemplateOptions } from \"./types\"\n\nexport default function (\n\ttemplate: string,\n\tcontext: Record<string, any>,\n\toptions?: TemplateOptions,\n) {\n\tconst compileOptions: CompileOptions = omit(options, \"partial\")\n\treturn resolve(compile(template, compileOptions), context, { partial: options?.partial })\n}\n"],"mappings":";;AAAA,SAASA,YAAY;AACrB,OAAOC,aAAa;AACpB,OAAOC,aAAa;AAGL,SAAf,kBACCC,UACAC,SACAC,SAAyB;AAEzB,QAAMC,iBAAiCN,KAAKK,SAAS,SAAA;AACrD,SAAOH,QAAQD,QAAQE,UAAUG,cAAAA,GAAiBF,SAAS;IAAEG,SAASF,mCAASE;EAAQ,CAAA;AACxF;AAPA;","names":["omit","compile","resolve","template","context","options","compileOptions","partial"]}
|
package/dist/es/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/** Parser options */
|
|
2
|
+
interface CompileOptions {
|
|
3
|
+
/** Prefix for template syntax */
|
|
4
|
+
notation?: string;
|
|
5
|
+
/** Opening delimiter for template syntax */
|
|
6
|
+
notationStart?: string;
|
|
7
|
+
/** Closing delimiter for template syntax */
|
|
8
|
+
notationEnd?: string;
|
|
9
|
+
}
|
|
10
|
+
interface ResolveOptions {
|
|
11
|
+
/** Skip failed expressions; otherwise return undefined for failures */
|
|
12
|
+
partial?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/** Template parsing options */
|
|
15
|
+
interface TemplateOptions extends CompileOptions, ResolveOptions {
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare function toArray(template: string, context: Record<string, any>, options?: TemplateOptions): string[];
|
|
19
|
+
|
|
20
|
+
declare function export_default(template: string, context: Record<string, any>, options?: TemplateOptions): string;
|
|
21
|
+
|
|
22
|
+
export { export_default as default, toArray as resolveToArray, export_default as resolveToString };
|