@truto/replace-placeholders 1.0.2 → 1.0.4
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/README.md +37 -0
- package/dist/main.cjs +47 -22
- package/dist/main.cjs.map +1 -1
- package/dist/module.js +50 -25
- package/dist/module.js.map +1 -1
- package/dist/types.d.ts.map +1 -1
- package/package.json +5 -1
package/README.md
CHANGED
|
@@ -27,6 +27,7 @@ console.log(result); // Outputs: 'Foo: bar'
|
|
|
27
27
|
console.log(replacePlaceholders('{{foo}}', { foo: 'bar' })); // Outputs: 'bar'
|
|
28
28
|
console.log(replacePlaceholders('{{foo-bar.something}}', { 'foo-bar': { something: true } })); // Outputs: 'true'
|
|
29
29
|
console.log(replacePlaceholders('{{foo}} {{bar:bool}}', { foo: 'bar', bar: 'false' })); // Outputs: 'bar false'
|
|
30
|
+
console.log(replacePlaceholders('{{foo.0.bar}}', { foo: [{ bar: 'baz' }] })); // Outputs: 'baz'
|
|
30
31
|
```
|
|
31
32
|
|
|
32
33
|
#### Arrays
|
|
@@ -45,14 +46,36 @@ console.log(replacePlaceholders({ foo: '{{foo}}', bar: '{{bar:int}}' }, { foo: '
|
|
|
45
46
|
|
|
46
47
|
### Conversion Types
|
|
47
48
|
|
|
49
|
+
You can coerce values to specific types.
|
|
50
|
+
|
|
51
|
+
**Only works when the placeholder is the complete string.**
|
|
52
|
+
|
|
48
53
|
```javascript
|
|
49
54
|
console.log(replacePlaceholders('{{foo:int}}', { foo: '1' })); // Outputs: 1
|
|
50
55
|
console.log(replacePlaceholders('{{foo:num}}', { foo: '1.1' })); // Outputs: 1.1
|
|
51
56
|
console.log(replacePlaceholders('{{foo:bool}}', { foo: 'true' })); // Outputs: true
|
|
52
57
|
console.log(replacePlaceholders('{{foo:json}}', { foo: '{"foo":"bar"}' })); // Outputs: { foo: 'bar' }
|
|
58
|
+
console.log(replacePlaceholders('{{foo:json}}', { foo: { foo: 'bar' } })); // Outputs: { foo: 'bar' }
|
|
53
59
|
console.log(replacePlaceholders('{{foo:null}}', { foo: 'null' })); // Outputs: null
|
|
54
60
|
```
|
|
55
61
|
|
|
62
|
+
### Remove empty placeholders
|
|
63
|
+
|
|
64
|
+
You can use `undefined` keyword to remove placeholders that are not found in the data object.
|
|
65
|
+
|
|
66
|
+
```javascript
|
|
67
|
+
console.log(replacePlaceholders('foo {{foo:undefined}} {{bar}}', { bar: 'bar' })); // Outputs: 'foo bar'
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Ignoring empty strings
|
|
71
|
+
|
|
72
|
+
You can use `ignore-empty-str` keyword to ignore empty strings in the output. Useful when used with conditional replacements below.
|
|
73
|
+
|
|
74
|
+
```javascript
|
|
75
|
+
console.log(replacePlaceholders('foo {{foo:ignore-empty-str}} {{bar}}', { foo: '', bar: 'bar' })) // Outputs: 'foo {{foo:ignore-empty-str}} bar'
|
|
76
|
+
console.lo(replacePlaceholders('{{foo|bar:ignore-empty-str}}', { foo: '', bar: 'bar' })) // Outputs: 'bar'
|
|
77
|
+
```
|
|
78
|
+
|
|
56
79
|
### Conditional Replacements (Fallback Values)
|
|
57
80
|
|
|
58
81
|
Using a `|` (pipe) character, you can provide fallback values right within the placeholder. The function will pick the first non-`undefined` value for the replacement.
|
|
@@ -64,6 +87,20 @@ console.log(replacePlaceholders('{{foo.bar:str|bar:str}}', { foo: { bar: 'bar' }
|
|
|
64
87
|
console.log(replacePlaceholders('{{foo.bar|bar:str}}', { foo: { bar: true } })); // Outputs: 'true'
|
|
65
88
|
```
|
|
66
89
|
|
|
90
|
+
### Default values
|
|
91
|
+
|
|
92
|
+
Using the Elvis operator `?:`, you can provide default values for placeholders that are not found in the data object.
|
|
93
|
+
|
|
94
|
+
```javascript
|
|
95
|
+
console.log(replacePlaceholders('{{foo?:bar}}', { bar: 'bar' })); // Outputs: 'bar'
|
|
96
|
+
````
|
|
97
|
+
|
|
98
|
+
You can also combine it with type casting
|
|
99
|
+
|
|
100
|
+
```javascript
|
|
101
|
+
console.log(replacePlaceholders('{{foo?:1:int}}', { foo: '1' })); // Outputs: 1
|
|
102
|
+
````
|
|
103
|
+
|
|
67
104
|
## License
|
|
68
105
|
|
|
69
106
|
This project is licensed under the MIT License.
|
package/dist/main.cjs
CHANGED
|
@@ -20,24 +20,37 @@ $parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8
|
|
|
20
20
|
|
|
21
21
|
|
|
22
22
|
function $a169b0c20575e4d3$var$replace(str = "", obj = {}) {
|
|
23
|
-
const regex = /{{([\w
|
|
23
|
+
const regex = /{{([\w-./|:?\s]+)}}/gi;
|
|
24
|
+
const typeSplitRegex = /:(?=[\w\s]+)/gm;
|
|
24
25
|
const matches = str.match(regex);
|
|
25
26
|
let result = str;
|
|
26
27
|
if (matches) matches.forEach((match)=>{
|
|
27
|
-
const
|
|
28
|
+
const [matchStr, defaultStr] = match.slice(2, -2).split("?:");
|
|
29
|
+
const parts = matchStr.split("|");
|
|
28
30
|
const isNullAllowed = parts.some((part)=>part.includes(":null"));
|
|
31
|
+
const ignoreEmptyString = parts.some((part)=>part.includes(":ignore-empty-str"));
|
|
32
|
+
const setUndefined = parts.some((part)=>part.includes(":undefined"));
|
|
33
|
+
const isWholeString = match === str;
|
|
29
34
|
let value = undefined;
|
|
30
35
|
for (const part of parts){
|
|
31
|
-
const [path, ...typeParts] = part.split(
|
|
32
|
-
const type = typeParts[0] || "str";
|
|
36
|
+
const [path, ...typeParts] = part.split(typeSplitRegex);
|
|
37
|
+
const type = (typeParts[0] || "str").trim();
|
|
33
38
|
const tempValue = (0, $g5Y9E$wildwildpath.get)(obj, path.trim());
|
|
39
|
+
if (tempValue === "" && ignoreEmptyString) continue;
|
|
34
40
|
if (tempValue !== undefined) {
|
|
35
|
-
value = $a169b0c20575e4d3$var$typeCast(tempValue, type,
|
|
41
|
+
value = $a169b0c20575e4d3$var$typeCast(tempValue, type, isWholeString);
|
|
36
42
|
break;
|
|
37
43
|
}
|
|
38
44
|
}
|
|
39
|
-
if (value === undefined) {
|
|
40
|
-
|
|
45
|
+
if (value === undefined && defaultStr) {
|
|
46
|
+
const [defaultValue, defaultType] = defaultStr.split(typeSplitRegex);
|
|
47
|
+
const type = defaultType || "str";
|
|
48
|
+
value = $a169b0c20575e4d3$var$typeCast(defaultValue, type, isWholeString);
|
|
49
|
+
} else if (value === undefined) {
|
|
50
|
+
if (setUndefined) {
|
|
51
|
+
if (isWholeString) value = undefined;
|
|
52
|
+
else value = "";
|
|
53
|
+
} else if (isNullAllowed) value = null;
|
|
41
54
|
else value = match // Keep the placeholder intact
|
|
42
55
|
;
|
|
43
56
|
}
|
|
@@ -48,36 +61,43 @@ function $a169b0c20575e4d3$var$replace(str = "", obj = {}) {
|
|
|
48
61
|
}
|
|
49
62
|
function $a169b0c20575e4d3$var$typeCast(value, type, isWholeString) {
|
|
50
63
|
if (value === undefined || value === null) return value;
|
|
64
|
+
let valueToCheck = value;
|
|
65
|
+
if ((0, $g5Y9E$lodashes.isString)(value) && type !== "str") valueToCheck = (0, $g5Y9E$lodashes.trim)(value);
|
|
66
|
+
if (valueToCheck instanceof Blob || valueToCheck instanceof ReadableStream || valueToCheck instanceof WritableStream || valueToCheck instanceof TransformStream) return valueToCheck;
|
|
51
67
|
let castValue;
|
|
52
68
|
switch(type){
|
|
69
|
+
case "ignore-empty-str":
|
|
70
|
+
return valueToCheck;
|
|
71
|
+
case "undefined":
|
|
72
|
+
return valueToCheck;
|
|
53
73
|
case "null":
|
|
54
|
-
if (
|
|
55
|
-
return isWholeString ?
|
|
74
|
+
if (valueToCheck === "null" || valueToCheck === null) return null;
|
|
75
|
+
return isWholeString ? valueToCheck : "null";
|
|
56
76
|
case "int":
|
|
57
|
-
castValue = parseInt(
|
|
58
|
-
return isNaN(castValue) ?
|
|
77
|
+
castValue = parseInt(valueToCheck);
|
|
78
|
+
return isNaN(castValue) ? valueToCheck : castValue;
|
|
59
79
|
case "num":
|
|
60
|
-
castValue = parseFloat(
|
|
61
|
-
return isNaN(castValue) ?
|
|
80
|
+
castValue = parseFloat(valueToCheck);
|
|
81
|
+
return isNaN(castValue) ? valueToCheck : castValue;
|
|
62
82
|
case "str":
|
|
63
|
-
return
|
|
83
|
+
return (0, $g5Y9E$lodashes.toString)(value);
|
|
64
84
|
case "bool":
|
|
65
|
-
if (
|
|
66
|
-
if (
|
|
67
|
-
return
|
|
85
|
+
if (valueToCheck === "true" || valueToCheck === true) return true;
|
|
86
|
+
if (valueToCheck === "false" || valueToCheck === false) return false;
|
|
87
|
+
return valueToCheck // Return the original value if it's not 'true' or 'false'
|
|
68
88
|
;
|
|
69
89
|
case "json":
|
|
70
90
|
if (isWholeString) {
|
|
71
|
-
if ((0, $g5Y9E$lodashes.isPlainObject)(
|
|
91
|
+
if ((0, $g5Y9E$lodashes.isPlainObject)(valueToCheck) || (0, $g5Y9E$lodashes.isArray)(valueToCheck)) return valueToCheck;
|
|
72
92
|
try {
|
|
73
|
-
return JSON.parse(
|
|
93
|
+
return JSON.parse(valueToCheck);
|
|
74
94
|
} catch (err) {
|
|
75
|
-
return
|
|
95
|
+
return valueToCheck;
|
|
76
96
|
}
|
|
77
97
|
}
|
|
78
|
-
return
|
|
98
|
+
return valueToCheck;
|
|
79
99
|
case "any":
|
|
80
|
-
return
|
|
100
|
+
return valueToCheck;
|
|
81
101
|
default:
|
|
82
102
|
throw new Error(`Unsupported type: ${type}`);
|
|
83
103
|
}
|
|
@@ -91,6 +111,11 @@ function $72af1098ef591494$export$2e2bcd8739ae039(obj, context) {
|
|
|
91
111
|
if ((0, $g5Y9E$lodashes.isPlainObject)(obj)) return (0, ($parcel$interopDefault($g5Y9E$traverse)))(obj).map(function(value) {
|
|
92
112
|
if (this.circular) this.remove();
|
|
93
113
|
else if ((0, $g5Y9E$lodashes.isString)(value)) this.update((0, $a169b0c20575e4d3$export$2e2bcd8739ae039)(value, context));
|
|
114
|
+
else if ((0, $g5Y9E$lodashes.isArrayBuffer)(value) || value instanceof Blob) {
|
|
115
|
+
const val = (0, $g5Y9E$lodashes.get)(obj, this.path);
|
|
116
|
+
this.update(val);
|
|
117
|
+
this.block();
|
|
118
|
+
}
|
|
94
119
|
});
|
|
95
120
|
throw new Error("Invalid type");
|
|
96
121
|
}
|
package/dist/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;ACAA;;ACAA;;AAGA,SAAS,8BACP,MAAM,EAAE,EACR,MAAc,CAAC,CAAC,EAQN;IACV,MAAM,QAAQ;IACd,MAAM,UAAU,IAAI,KAAK,CAAC;IAC1B,IAAI,SAAuB;IAE3B,IAAI,SACF,QAAQ,OAAO,CAAC,CAAA,QAAS;QACvB,MAAM,
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;ACAA;;ACAA;;AAGA,SAAS,8BACP,MAAM,EAAE,EACR,MAAc,CAAC,CAAC,EAQN;IACV,MAAM,QAAQ;IACd,MAAM,iBAAiB;IACvB,MAAM,UAAU,IAAI,KAAK,CAAC;IAC1B,IAAI,SAAuB;IAE3B,IAAI,SACF,QAAQ,OAAO,CAAC,CAAA,QAAS;QACvB,MAAM,CAAC,UAAU,WAAW,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC;QACxD,MAAM,QAAQ,SAAS,KAAK,CAAC;QAC7B,MAAM,gBAAgB,MAAM,IAAI,CAAC,CAAA,OAAQ,KAAK,QAAQ,CAAC;QACvD,MAAM,oBAAoB,MAAM,IAAI,CAAC,CAAA,OACnC,KAAK,QAAQ,CAAC;QAEhB,MAAM,eAAe,MAAM,IAAI,CAAC,CAAA,OAAQ,KAAK,QAAQ,CAAC;QAEtD,MAAM,gBAAgB,UAAU;QAEhC,IAAI,QAAa;QACjB,KAAK,MAAM,QAAQ,MAAO;YACxB,MAAM,CAAC,MAAM,GAAG,UAAU,GAAG,KAAK,KAAK,CAAC;YACxC,MAAM,OAAe,AAAC,CAAA,SAAS,CAAC,EAAE,IAAI,KAAI,EAAG,IAAI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IACA,2CAAe,CAAA,GAAA,wCAAkB","sources":["index.ts","replacePlaceholders.ts","replace.ts"],"sourcesContent":["import replacePlaceholders from './replacePlaceholders';\nexport default replacePlaceholders;\n","import traverse from 'traverse'\nimport { isString, isArray, isPlainObject, isArrayBuffer, get } from 'lodash-es'\nimport replace from './replace'\nexport default function replacePlaceholders<T>(\n obj: T extends string | string[] | Record<string, unknown> ? T : never,\n context: Record<string, unknown>\n): T {\n if (isString(obj)) {\n return replace(obj, context) as T\n }\n\n if (isArray(obj)) {\n return obj.map(item => replacePlaceholders(item, context)) as T\n }\n\n if (isPlainObject(obj)) {\n return traverse(obj).map(function (value) {\n if (this.circular) {\n this.remove()\n } else if (isString(value)) {\n this.update(replace(value, context))\n } else if (isArrayBuffer(value) || value instanceof Blob) {\n const val = get(obj, this.path)\n this.update(val)\n this.block()\n }\n })\n }\n\n throw new Error('Invalid type')\n}\n","import { get as getWild } from 'wild-wild-path'\nimport { isArray, isPlainObject, isString, toString, trim } from 'lodash-es'\n\nfunction replace(\n str = '',\n obj: object = {}\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n const regex = /{{([\\w-./|:?\\s]+)}}/gi\n const typeSplitRegex = /:(?=[\\w\\s]+)/gm\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const [matchStr, defaultStr] = match.slice(2, -2).split('?:')\n const parts = matchStr.split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n const ignoreEmptyString = parts.some(part =>\n part.includes(':ignore-empty-str')\n )\n const setUndefined = parts.some(part => part.includes(':undefined'))\n\n const isWholeString = match === str\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(typeSplitRegex)\n const type: string = (typeParts[0] || 'str').trim()\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue === '' && ignoreEmptyString) {\n continue\n }\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, isWholeString)\n break\n }\n }\n\n if (value === undefined && defaultStr) {\n const [defaultValue, defaultType] = defaultStr.split(typeSplitRegex)\n const type = defaultType || 'str'\n value = typeCast(defaultValue, type, isWholeString)\n } else if (value === undefined) {\n if (setUndefined) {\n if (isWholeString) {\n value = undefined\n } else {\n value = ''\n }\n } else if (isNullAllowed) {\n value = null\n } else {\n value = match // Keep the placeholder intact\n }\n }\n\n if (match === str) {\n result = value\n } else {\n result = result.replace(match, value)\n }\n })\n }\n\n return result\n}\n\nfunction typeCast(\n value: unknown,\n type: string,\n isWholeString: boolean\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n if (value === undefined || value === null) return value\n\n let valueToCheck = value\n if (isString(value) && type !== 'str') {\n valueToCheck = trim(value)\n }\n\n if (\n valueToCheck instanceof Blob ||\n valueToCheck instanceof ReadableStream ||\n valueToCheck instanceof WritableStream ||\n valueToCheck instanceof TransformStream\n ) {\n return valueToCheck\n }\n\n let castValue: any\n switch (type) {\n case 'ignore-empty-str':\n return valueToCheck\n case 'undefined':\n return valueToCheck\n case 'null':\n if (valueToCheck === 'null' || valueToCheck === null) return null\n return isWholeString ? valueToCheck : 'null'\n case 'int':\n castValue = parseInt(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'num':\n castValue = parseFloat(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'str':\n return toString(value)\n case 'bool':\n if (valueToCheck === 'true' || valueToCheck === true) return true\n if (valueToCheck === 'false' || valueToCheck === false) return false\n return valueToCheck // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(valueToCheck) || isArray(valueToCheck))\n return valueToCheck\n try {\n return JSON.parse(valueToCheck as string)\n } catch (err) {\n return valueToCheck\n }\n }\n return valueToCheck\n case 'any':\n return valueToCheck\n default:\n throw new Error(`Unsupported type: ${type}`)\n }\n}\n\nexport default replace\n"],"names":[],"version":3,"file":"main.cjs.map"}
|
package/dist/module.js
CHANGED
|
@@ -1,30 +1,43 @@
|
|
|
1
1
|
import $hCgyA$traverse from "traverse";
|
|
2
|
-
import {isString as $hCgyA$isString, isArray as $hCgyA$isArray, isPlainObject as $hCgyA$isPlainObject} from "lodash-es";
|
|
3
|
-
import {get as $hCgyA$
|
|
2
|
+
import {isString as $hCgyA$isString, isArray as $hCgyA$isArray, isPlainObject as $hCgyA$isPlainObject, isArrayBuffer as $hCgyA$isArrayBuffer, get as $hCgyA$get, trim as $hCgyA$trim, toString as $hCgyA$toString} from "lodash-es";
|
|
3
|
+
import {get as $hCgyA$get1} from "wild-wild-path";
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
|
|
9
9
|
function $67f2b908c91abfcc$var$replace(str = "", obj = {}) {
|
|
10
|
-
const regex = /{{([\w
|
|
10
|
+
const regex = /{{([\w-./|:?\s]+)}}/gi;
|
|
11
|
+
const typeSplitRegex = /:(?=[\w\s]+)/gm;
|
|
11
12
|
const matches = str.match(regex);
|
|
12
13
|
let result = str;
|
|
13
14
|
if (matches) matches.forEach((match)=>{
|
|
14
|
-
const
|
|
15
|
+
const [matchStr, defaultStr] = match.slice(2, -2).split("?:");
|
|
16
|
+
const parts = matchStr.split("|");
|
|
15
17
|
const isNullAllowed = parts.some((part)=>part.includes(":null"));
|
|
18
|
+
const ignoreEmptyString = parts.some((part)=>part.includes(":ignore-empty-str"));
|
|
19
|
+
const setUndefined = parts.some((part)=>part.includes(":undefined"));
|
|
20
|
+
const isWholeString = match === str;
|
|
16
21
|
let value = undefined;
|
|
17
22
|
for (const part of parts){
|
|
18
|
-
const [path, ...typeParts] = part.split(
|
|
19
|
-
const type = typeParts[0] || "str";
|
|
20
|
-
const tempValue = (0, $hCgyA$
|
|
23
|
+
const [path, ...typeParts] = part.split(typeSplitRegex);
|
|
24
|
+
const type = (typeParts[0] || "str").trim();
|
|
25
|
+
const tempValue = (0, $hCgyA$get1)(obj, path.trim());
|
|
26
|
+
if (tempValue === "" && ignoreEmptyString) continue;
|
|
21
27
|
if (tempValue !== undefined) {
|
|
22
|
-
value = $67f2b908c91abfcc$var$typeCast(tempValue, type,
|
|
28
|
+
value = $67f2b908c91abfcc$var$typeCast(tempValue, type, isWholeString);
|
|
23
29
|
break;
|
|
24
30
|
}
|
|
25
31
|
}
|
|
26
|
-
if (value === undefined) {
|
|
27
|
-
|
|
32
|
+
if (value === undefined && defaultStr) {
|
|
33
|
+
const [defaultValue, defaultType] = defaultStr.split(typeSplitRegex);
|
|
34
|
+
const type = defaultType || "str";
|
|
35
|
+
value = $67f2b908c91abfcc$var$typeCast(defaultValue, type, isWholeString);
|
|
36
|
+
} else if (value === undefined) {
|
|
37
|
+
if (setUndefined) {
|
|
38
|
+
if (isWholeString) value = undefined;
|
|
39
|
+
else value = "";
|
|
40
|
+
} else if (isNullAllowed) value = null;
|
|
28
41
|
else value = match // Keep the placeholder intact
|
|
29
42
|
;
|
|
30
43
|
}
|
|
@@ -35,36 +48,43 @@ function $67f2b908c91abfcc$var$replace(str = "", obj = {}) {
|
|
|
35
48
|
}
|
|
36
49
|
function $67f2b908c91abfcc$var$typeCast(value, type, isWholeString) {
|
|
37
50
|
if (value === undefined || value === null) return value;
|
|
51
|
+
let valueToCheck = value;
|
|
52
|
+
if ((0, $hCgyA$isString)(value) && type !== "str") valueToCheck = (0, $hCgyA$trim)(value);
|
|
53
|
+
if (valueToCheck instanceof Blob || valueToCheck instanceof ReadableStream || valueToCheck instanceof WritableStream || valueToCheck instanceof TransformStream) return valueToCheck;
|
|
38
54
|
let castValue;
|
|
39
55
|
switch(type){
|
|
56
|
+
case "ignore-empty-str":
|
|
57
|
+
return valueToCheck;
|
|
58
|
+
case "undefined":
|
|
59
|
+
return valueToCheck;
|
|
40
60
|
case "null":
|
|
41
|
-
if (
|
|
42
|
-
return isWholeString ?
|
|
61
|
+
if (valueToCheck === "null" || valueToCheck === null) return null;
|
|
62
|
+
return isWholeString ? valueToCheck : "null";
|
|
43
63
|
case "int":
|
|
44
|
-
castValue = parseInt(
|
|
45
|
-
return isNaN(castValue) ?
|
|
64
|
+
castValue = parseInt(valueToCheck);
|
|
65
|
+
return isNaN(castValue) ? valueToCheck : castValue;
|
|
46
66
|
case "num":
|
|
47
|
-
castValue = parseFloat(
|
|
48
|
-
return isNaN(castValue) ?
|
|
67
|
+
castValue = parseFloat(valueToCheck);
|
|
68
|
+
return isNaN(castValue) ? valueToCheck : castValue;
|
|
49
69
|
case "str":
|
|
50
|
-
return
|
|
70
|
+
return (0, $hCgyA$toString)(value);
|
|
51
71
|
case "bool":
|
|
52
|
-
if (
|
|
53
|
-
if (
|
|
54
|
-
return
|
|
72
|
+
if (valueToCheck === "true" || valueToCheck === true) return true;
|
|
73
|
+
if (valueToCheck === "false" || valueToCheck === false) return false;
|
|
74
|
+
return valueToCheck // Return the original value if it's not 'true' or 'false'
|
|
55
75
|
;
|
|
56
76
|
case "json":
|
|
57
77
|
if (isWholeString) {
|
|
58
|
-
if ((0, $hCgyA$isPlainObject)(
|
|
78
|
+
if ((0, $hCgyA$isPlainObject)(valueToCheck) || (0, $hCgyA$isArray)(valueToCheck)) return valueToCheck;
|
|
59
79
|
try {
|
|
60
|
-
return JSON.parse(
|
|
80
|
+
return JSON.parse(valueToCheck);
|
|
61
81
|
} catch (err) {
|
|
62
|
-
return
|
|
82
|
+
return valueToCheck;
|
|
63
83
|
}
|
|
64
84
|
}
|
|
65
|
-
return
|
|
85
|
+
return valueToCheck;
|
|
66
86
|
case "any":
|
|
67
|
-
return
|
|
87
|
+
return valueToCheck;
|
|
68
88
|
default:
|
|
69
89
|
throw new Error(`Unsupported type: ${type}`);
|
|
70
90
|
}
|
|
@@ -78,6 +98,11 @@ function $4e62acd4800cacd9$export$2e2bcd8739ae039(obj, context) {
|
|
|
78
98
|
if ((0, $hCgyA$isPlainObject)(obj)) return (0, $hCgyA$traverse)(obj).map(function(value) {
|
|
79
99
|
if (this.circular) this.remove();
|
|
80
100
|
else if ((0, $hCgyA$isString)(value)) this.update((0, $67f2b908c91abfcc$export$2e2bcd8739ae039)(value, context));
|
|
101
|
+
else if ((0, $hCgyA$isArrayBuffer)(value) || value instanceof Blob) {
|
|
102
|
+
const val = (0, $hCgyA$get)(obj, this.path);
|
|
103
|
+
this.update(val);
|
|
104
|
+
this.block();
|
|
105
|
+
}
|
|
81
106
|
});
|
|
82
107
|
throw new Error("Invalid type");
|
|
83
108
|
}
|
package/dist/module.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;ACAA;;ACAA;;AAGA,SAAS,8BACP,MAAM,EAAE,EACR,MAAc,CAAC,CAAC,EAQN;IACV,MAAM,QAAQ;IACd,MAAM,UAAU,IAAI,KAAK,CAAC;IAC1B,IAAI,SAAuB;IAE3B,IAAI,SACF,QAAQ,OAAO,CAAC,CAAA,QAAS;QACvB,MAAM,
|
|
1
|
+
{"mappings":";;;;ACAA;;ACAA;;AAGA,SAAS,8BACP,MAAM,EAAE,EACR,MAAc,CAAC,CAAC,EAQN;IACV,MAAM,QAAQ;IACd,MAAM,iBAAiB;IACvB,MAAM,UAAU,IAAI,KAAK,CAAC;IAC1B,IAAI,SAAuB;IAE3B,IAAI,SACF,QAAQ,OAAO,CAAC,CAAA,QAAS;QACvB,MAAM,CAAC,UAAU,WAAW,GAAG,MAAM,KAAK,CAAC,GAAG,IAAI,KAAK,CAAC;QACxD,MAAM,QAAQ,SAAS,KAAK,CAAC;QAC7B,MAAM,gBAAgB,MAAM,IAAI,CAAC,CAAA,OAAQ,KAAK,QAAQ,CAAC;QACvD,MAAM,oBAAoB,MAAM,IAAI,CAAC,CAAA,OACnC,KAAK,QAAQ,CAAC;QAEhB,MAAM,eAAe,MAAM,IAAI,CAAC,CAAA,OAAQ,KAAK,QAAQ,CAAC;QAEtD,MAAM,gBAAgB,UAAU;QAEhC,IAAI,QAAa;QACjB,KAAK,MAAM,QAAQ,MAAO;YACxB,MAAM,CAAC,MAAM,GAAG,UAAU,GAAG,KAAK,KAAK,CAAC;YACxC,M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;IACH;IAGF,MAAM,IAAI,MAAM,gBAAe;AACjC;;AD9BA;IACA,2CAAe,CAAA,GAAA,wCAAkB","sources":["index.ts","replacePlaceholders.ts","replace.ts"],"sourcesContent":["import replacePlaceholders from './replacePlaceholders';\nexport default replacePlaceholders;\n","import traverse from 'traverse'\nimport { isString, isArray, isPlainObject, isArrayBuffer, get } from 'lodash-es'\nimport replace from './replace'\nexport default function replacePlaceholders<T>(\n obj: T extends string | string[] | Record<string, unknown> ? T : never,\n context: Record<string, unknown>\n): T {\n if (isString(obj)) {\n return replace(obj, context) as T\n }\n\n if (isArray(obj)) {\n return obj.map(item => replacePlaceholders(item, context)) as T\n }\n\n if (isPlainObject(obj)) {\n return traverse(obj).map(function (value) {\n if (this.circular) {\n this.remove()\n } else if (isString(value)) {\n this.update(replace(value, context))\n } else if (isArrayBuffer(value) || value instanceof Blob) {\n const val = get(obj, this.path)\n this.update(val)\n this.block()\n }\n })\n }\n\n throw new Error('Invalid type')\n}\n","import { get as getWild } from 'wild-wild-path'\nimport { isArray, isPlainObject, isString, toString, trim } from 'lodash-es'\n\nfunction replace(\n str = '',\n obj: object = {}\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n const regex = /{{([\\w-./|:?\\s]+)}}/gi\n const typeSplitRegex = /:(?=[\\w\\s]+)/gm\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const [matchStr, defaultStr] = match.slice(2, -2).split('?:')\n const parts = matchStr.split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n const ignoreEmptyString = parts.some(part =>\n part.includes(':ignore-empty-str')\n )\n const setUndefined = parts.some(part => part.includes(':undefined'))\n\n const isWholeString = match === str\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(typeSplitRegex)\n const type: string = (typeParts[0] || 'str').trim()\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue === '' && ignoreEmptyString) {\n continue\n }\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, isWholeString)\n break\n }\n }\n\n if (value === undefined && defaultStr) {\n const [defaultValue, defaultType] = defaultStr.split(typeSplitRegex)\n const type = defaultType || 'str'\n value = typeCast(defaultValue, type, isWholeString)\n } else if (value === undefined) {\n if (setUndefined) {\n if (isWholeString) {\n value = undefined\n } else {\n value = ''\n }\n } else if (isNullAllowed) {\n value = null\n } else {\n value = match // Keep the placeholder intact\n }\n }\n\n if (match === str) {\n result = value\n } else {\n result = result.replace(match, value)\n }\n })\n }\n\n return result\n}\n\nfunction typeCast(\n value: unknown,\n type: string,\n isWholeString: boolean\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n if (value === undefined || value === null) return value\n\n let valueToCheck = value\n if (isString(value) && type !== 'str') {\n valueToCheck = trim(value)\n }\n\n if (\n valueToCheck instanceof Blob ||\n valueToCheck instanceof ReadableStream ||\n valueToCheck instanceof WritableStream ||\n valueToCheck instanceof TransformStream\n ) {\n return valueToCheck\n }\n\n let castValue: any\n switch (type) {\n case 'ignore-empty-str':\n return valueToCheck\n case 'undefined':\n return valueToCheck\n case 'null':\n if (valueToCheck === 'null' || valueToCheck === null) return null\n return isWholeString ? valueToCheck : 'null'\n case 'int':\n castValue = parseInt(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'num':\n castValue = parseFloat(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'str':\n return toString(value)\n case 'bool':\n if (valueToCheck === 'true' || valueToCheck === true) return true\n if (valueToCheck === 'false' || valueToCheck === false) return false\n return valueToCheck // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(valueToCheck) || isArray(valueToCheck))\n return valueToCheck\n try {\n return JSON.parse(valueToCheck as string)\n } catch (err) {\n return valueToCheck\n }\n }\n return valueToCheck\n case 'any':\n return valueToCheck\n default:\n throw new Error(`Unsupported type: ${type}`)\n }\n}\n\nexport default replace\n"],"names":[],"version":3,"file":"module.js.map"}
|
package/dist/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":"ACGA,qCAA4C,CAAC,EAC3C,GAAG,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,EACtE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,CAAC,
|
|
1
|
+
{"mappings":"ACGA,qCAA4C,CAAC,EAC3C,GAAG,EAAE,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,CAAC,GAAG,KAAK,EACtE,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC/B,CAAC,CAwBH;AC7BD,eAAe,mBAAmB,CAAC","sources":["replace.ts","replacePlaceholders.ts","index.ts"],"sourcesContent":["import { get as getWild } from 'wild-wild-path'\nimport { isArray, isPlainObject, isString, toString, trim } from 'lodash-es'\n\nfunction replace(\n str = '',\n obj: object = {}\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n const regex = /{{([\\w-./|:?\\s]+)}}/gi\n const typeSplitRegex = /:(?=[\\w\\s]+)/gm\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const [matchStr, defaultStr] = match.slice(2, -2).split('?:')\n const parts = matchStr.split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n const ignoreEmptyString = parts.some(part =>\n part.includes(':ignore-empty-str')\n )\n const setUndefined = parts.some(part => part.includes(':undefined'))\n\n const isWholeString = match === str\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(typeSplitRegex)\n const type: string = (typeParts[0] || 'str').trim()\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue === '' && ignoreEmptyString) {\n continue\n }\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, isWholeString)\n break\n }\n }\n\n if (value === undefined && defaultStr) {\n const [defaultValue, defaultType] = defaultStr.split(typeSplitRegex)\n const type = defaultType || 'str'\n value = typeCast(defaultValue, type, isWholeString)\n } else if (value === undefined) {\n if (setUndefined) {\n if (isWholeString) {\n value = undefined\n } else {\n value = ''\n }\n } else if (isNullAllowed) {\n value = null\n } else {\n value = match // Keep the placeholder intact\n }\n }\n\n if (match === str) {\n result = value\n } else {\n result = result.replace(match, value)\n }\n })\n }\n\n return result\n}\n\nfunction typeCast(\n value: unknown,\n type: string,\n isWholeString: boolean\n):\n | string\n | number\n | boolean\n | Record<string, unknown>\n | null\n | undefined\n | unknown {\n if (value === undefined || value === null) return value\n\n let valueToCheck = value\n if (isString(value) && type !== 'str') {\n valueToCheck = trim(value)\n }\n\n if (\n valueToCheck instanceof Blob ||\n valueToCheck instanceof ReadableStream ||\n valueToCheck instanceof WritableStream ||\n valueToCheck instanceof TransformStream\n ) {\n return valueToCheck\n }\n\n let castValue: any\n switch (type) {\n case 'ignore-empty-str':\n return valueToCheck\n case 'undefined':\n return valueToCheck\n case 'null':\n if (valueToCheck === 'null' || valueToCheck === null) return null\n return isWholeString ? valueToCheck : 'null'\n case 'int':\n castValue = parseInt(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'num':\n castValue = parseFloat(valueToCheck as string)\n return isNaN(castValue) ? valueToCheck : castValue\n case 'str':\n return toString(value)\n case 'bool':\n if (valueToCheck === 'true' || valueToCheck === true) return true\n if (valueToCheck === 'false' || valueToCheck === false) return false\n return valueToCheck // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(valueToCheck) || isArray(valueToCheck))\n return valueToCheck\n try {\n return JSON.parse(valueToCheck as string)\n } catch (err) {\n return valueToCheck\n }\n }\n return valueToCheck\n case 'any':\n return valueToCheck\n default:\n throw new Error(`Unsupported type: ${type}`)\n }\n}\n\nexport default replace\n","import traverse from 'traverse'\nimport { isString, isArray, isPlainObject, isArrayBuffer, get } from 'lodash-es'\nimport replace from './replace'\nexport default function replacePlaceholders<T>(\n obj: T extends string | string[] | Record<string, unknown> ? T : never,\n context: Record<string, unknown>\n): T {\n if (isString(obj)) {\n return replace(obj, context) as T\n }\n\n if (isArray(obj)) {\n return obj.map(item => replacePlaceholders(item, context)) as T\n }\n\n if (isPlainObject(obj)) {\n return traverse(obj).map(function (value) {\n if (this.circular) {\n this.remove()\n } else if (isString(value)) {\n this.update(replace(value, context))\n } else if (isArrayBuffer(value) || value instanceof Blob) {\n const val = get(obj, this.path)\n this.update(val)\n this.block()\n }\n })\n }\n\n throw new Error('Invalid type')\n}\n","import replacePlaceholders from './replacePlaceholders';\nexport default replacePlaceholders;\n"],"names":[],"version":3,"file":"types.d.ts.map"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@truto/replace-placeholders",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "Efficiently replace placeholders in strings, arrays, and objects using data from specified paths. Powered by 'wild-wild-path' and 'lodash' for robust functionality.",
|
|
5
5
|
"repository": "https://github.com/trutohq/replace-placeholders.git",
|
|
6
6
|
"dependencies": {
|
|
@@ -40,6 +40,7 @@
|
|
|
40
40
|
"@parcel/transformer-typescript-types": "2.8.3",
|
|
41
41
|
"@types/lodash-es": "4.17.7",
|
|
42
42
|
"@types/traverse": "0.6.32",
|
|
43
|
+
"@cloudflare/workers-types": "4.20250214.0",
|
|
43
44
|
"@typescript-eslint/eslint-plugin": "5.46.1",
|
|
44
45
|
"@typescript-eslint/parser": "5.46.1",
|
|
45
46
|
"eslint": "8.29.0",
|
|
@@ -61,5 +62,8 @@
|
|
|
61
62
|
"check": "tsc --noEmit",
|
|
62
63
|
"prepublishOnly": "yarn build",
|
|
63
64
|
"test": "vitest"
|
|
65
|
+
},
|
|
66
|
+
"resolutions": {
|
|
67
|
+
"postcss": "npm:postcss@8.4.31"
|
|
64
68
|
}
|
|
65
69
|
}
|