@truto/replace-placeholders 1.0.0
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 +112 -0
- package/dist/main.cjs +102 -0
- package/dist/main.cjs.map +1 -0
- package/dist/module.js +90 -0
- package/dist/module.js.map +1 -0
- package/dist/types.d.ts +4 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +65 -0
package/README.md
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# @truto/replace-placeholders
|
|
2
|
+
|
|
3
|
+
Replace placeholders in strings, arrays, and objects easily. This package internally utilizes the [wild-wild-path](https://www.npmjs.com/package/wild-wild-path) library and supports all path formats provided by it.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @truto/replace-placeholders
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Basic
|
|
14
|
+
|
|
15
|
+
```javascript
|
|
16
|
+
import replacePlaceholders from '@truto/replace-placeholders';
|
|
17
|
+
|
|
18
|
+
const result = replacePlaceholders('{{foo}}', { foo: 'bar' });
|
|
19
|
+
console.log(result); // Outputs: 'bar'
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
### Advanced Usage with Different Data Types
|
|
23
|
+
|
|
24
|
+
#### Strings
|
|
25
|
+
|
|
26
|
+
```javascript
|
|
27
|
+
console.log(replacePlaceholders('{{foo}}', { foo: 'bar' })); // Outputs: 'bar'
|
|
28
|
+
console.log(replacePlaceholders('{{foo-bar.something}}', { 'foo-bar': { something: true } })); // Outputs: 'true'
|
|
29
|
+
console.log(replacePlaceholders('{{foo}} {{bar:bool}}', { foo: 'bar', bar: 'false' })); // Outputs: 'bar false'
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
#### Arrays
|
|
33
|
+
|
|
34
|
+
```javascript
|
|
35
|
+
console.log(replacePlaceholders(['{{foo}}'], { foo: 'bar' })); // Outputs: ['bar']
|
|
36
|
+
console.log(replacePlaceholders(['{{foo}}', '{{bar:num}}'], { foo: 'bar', bar: '1.34' })); // Outputs: ['bar', 1.34]
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
#### Objects
|
|
40
|
+
|
|
41
|
+
```javascript
|
|
42
|
+
console.log(replacePlaceholders({ foo: '{{foo}}' }, { foo: 'bar' })); // Outputs: { foo: 'bar' }
|
|
43
|
+
console.log(replacePlaceholders({ foo: '{{foo}}', bar: '{{bar:int}}' }, { foo: 'bar', bar: 1 })); // Outputs: { foo: 'bar', bar: 1 }
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### Conversion Types
|
|
47
|
+
|
|
48
|
+
```javascript
|
|
49
|
+
console.log(replacePlaceholders('{{foo:int}}', { foo: '1' })); // Outputs: 1
|
|
50
|
+
console.log(replacePlaceholders('{{foo:num}}', { foo: '1.1' })); // Outputs: 1.1
|
|
51
|
+
console.log(replacePlaceholders('{{foo:bool}}', { foo: 'true' })); // Outputs: true
|
|
52
|
+
console.log(replacePlaceholders('{{foo:json}}', { foo: '{"foo":"bar"}' })); // Outputs: { foo: 'bar' }
|
|
53
|
+
console.log(replacePlaceholders('{{foo:null}}', { foo: 'null' })); // Outputs: null
|
|
54
|
+
// ... add other examples ...
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Conditional Replacements (Fallback Values)
|
|
58
|
+
|
|
59
|
+
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.
|
|
60
|
+
|
|
61
|
+
```javascript
|
|
62
|
+
console.log(replacePlaceholders('{{foo|bar}}', { foo: 'foo', bar: 'bar' })); // Outputs: 'foo'
|
|
63
|
+
console.log(replacePlaceholders('{{foo:str|bar:int}}', { bar: 1 })); // Outputs: 1
|
|
64
|
+
console.log(replacePlaceholders('{{foo.bar:str|bar:str}}', { foo: { bar: 'bar' } })); // Outputs: 'bar'
|
|
65
|
+
console.log(replacePlaceholders('{{foo.bar|bar:str}}', { foo: { bar: true } })); // Outputs: 'true'
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## License
|
|
69
|
+
|
|
70
|
+
This project is licensed under the MIT License.
|
|
71
|
+
|
|
72
|
+
```
|
|
73
|
+
MIT License
|
|
74
|
+
|
|
75
|
+
Copyright (c) 2023 Yin Yang Inc.
|
|
76
|
+
|
|
77
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
78
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
79
|
+
in the Software without restriction, including without limitation the rights
|
|
80
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
81
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
82
|
+
furnished to do so, subject to the following conditions:
|
|
83
|
+
|
|
84
|
+
The above copyright notice and this permission notice shall be included in all
|
|
85
|
+
copies or substantial portions of the Software.
|
|
86
|
+
|
|
87
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
88
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
89
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
90
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
91
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
92
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
93
|
+
SOFTWARE.
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
## Contributing
|
|
97
|
+
|
|
98
|
+
We welcome contributions from the community! If you wish to contribute, please follow these steps:
|
|
99
|
+
|
|
100
|
+
1. **Fork the repository**: Click on the 'Fork' button at the top right of this page and clone your forked repository to your local machine.
|
|
101
|
+
|
|
102
|
+
2. **Create a new branch**: Create a new branch named after the feature or fix you are working on. For example: `feature/new-placeholder-syntax` or `fix/issue-123`.
|
|
103
|
+
|
|
104
|
+
3. **Make your changes**: Make the necessary modifications to the code. Ensure that you adhere to the existing coding standards and conventions.
|
|
105
|
+
|
|
106
|
+
4. **Commit your changes**: Commit your changes with a clear and concise commit message that describes the changes you made.
|
|
107
|
+
|
|
108
|
+
5. **Push to your fork**: Push your changes to your forked repository on GitHub.
|
|
109
|
+
|
|
110
|
+
6. **Submit a pull request**: Create a new pull request from your forked repository to the main repository. Please ensure that your pull request describes the changes you made, references any related issues, and has been tested on the latest version of the package.
|
|
111
|
+
|
|
112
|
+
Please note: By contributing to this project, you agree to abide by the code of conduct and that your contributions will be licensed under the MIT license (as per the LICENSE section).
|
package/dist/main.cjs
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
var $g5Y9E$traverse = require("traverse");
|
|
2
|
+
var $g5Y9E$lodashes = require("lodash-es");
|
|
3
|
+
var $g5Y9E$wildwildpath = require("wild-wild-path");
|
|
4
|
+
|
|
5
|
+
function $parcel$defineInteropFlag(a) {
|
|
6
|
+
Object.defineProperty(a, '__esModule', {value: true, configurable: true});
|
|
7
|
+
}
|
|
8
|
+
function $parcel$export(e, n, v, s) {
|
|
9
|
+
Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
|
|
10
|
+
}
|
|
11
|
+
function $parcel$interopDefault(a) {
|
|
12
|
+
return a && a.__esModule ? a.default : a;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
$parcel$defineInteropFlag(module.exports);
|
|
16
|
+
|
|
17
|
+
$parcel$export(module.exports, "default", () => $80bd448eb6ea085b$export$2e2bcd8739ae039);
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
function $a169b0c20575e4d3$var$replace(str = "", obj = {}) {
|
|
23
|
+
const regex = /{{([\w-.|:]+)}}/gi;
|
|
24
|
+
const matches = str.match(regex);
|
|
25
|
+
let result = str;
|
|
26
|
+
if (matches) matches.forEach((match)=>{
|
|
27
|
+
const parts = match.slice(2, -2).split("|");
|
|
28
|
+
const isNullAllowed = parts.some((part)=>part.includes(":null"));
|
|
29
|
+
let value = undefined;
|
|
30
|
+
for (const part of parts){
|
|
31
|
+
const [path, ...typeParts] = part.split(":");
|
|
32
|
+
const type = typeParts[0] || "str";
|
|
33
|
+
const tempValue = (0, $g5Y9E$wildwildpath.get)(obj, path.trim());
|
|
34
|
+
if (tempValue !== undefined) {
|
|
35
|
+
value = $a169b0c20575e4d3$var$typeCast(tempValue, type, match === str);
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (value === undefined) {
|
|
40
|
+
if (isNullAllowed) value = null;
|
|
41
|
+
else value = match // Keep the placeholder intact
|
|
42
|
+
;
|
|
43
|
+
}
|
|
44
|
+
if (match === str) result = value;
|
|
45
|
+
else result = result.replace(match, value);
|
|
46
|
+
});
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
function $a169b0c20575e4d3$var$typeCast(value, type, isWholeString) {
|
|
50
|
+
if (value === undefined || value === null) return value;
|
|
51
|
+
let castValue;
|
|
52
|
+
switch(type){
|
|
53
|
+
case "null":
|
|
54
|
+
if (value === "null" || value === null) return null;
|
|
55
|
+
return isWholeString ? value : "null";
|
|
56
|
+
case "int":
|
|
57
|
+
castValue = parseInt(value);
|
|
58
|
+
return isNaN(castValue) ? value : castValue;
|
|
59
|
+
case "num":
|
|
60
|
+
castValue = parseFloat(value);
|
|
61
|
+
return isNaN(castValue) ? value : castValue;
|
|
62
|
+
case "str":
|
|
63
|
+
return String(value);
|
|
64
|
+
case "bool":
|
|
65
|
+
if (value === "true" || value === true) return true;
|
|
66
|
+
if (value === "false" || value === false) return false;
|
|
67
|
+
return value // Return the original value if it's not 'true' or 'false'
|
|
68
|
+
;
|
|
69
|
+
case "json":
|
|
70
|
+
if (isWholeString) {
|
|
71
|
+
if ((0, $g5Y9E$lodashes.isPlainObject)(value)) return value;
|
|
72
|
+
try {
|
|
73
|
+
return JSON.parse(value);
|
|
74
|
+
} catch (err) {
|
|
75
|
+
return value;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
return value;
|
|
79
|
+
case "any":
|
|
80
|
+
return value;
|
|
81
|
+
default:
|
|
82
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
var $a169b0c20575e4d3$export$2e2bcd8739ae039 = $a169b0c20575e4d3$var$replace;
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
function $72af1098ef591494$export$2e2bcd8739ae039(obj, context) {
|
|
89
|
+
if ((0, $g5Y9E$lodashes.isString)(obj)) return (0, $a169b0c20575e4d3$export$2e2bcd8739ae039)(obj, context);
|
|
90
|
+
if ((0, $g5Y9E$lodashes.isArray)(obj)) return obj.map((item)=>$72af1098ef591494$export$2e2bcd8739ae039(item, context));
|
|
91
|
+
if ((0, $g5Y9E$lodashes.isPlainObject)(obj)) return (0, ($parcel$interopDefault($g5Y9E$traverse)))(obj).map(function(value) {
|
|
92
|
+
if (this.circular) this.remove();
|
|
93
|
+
else if ((0, $g5Y9E$lodashes.isString)(value)) this.update((0, $a169b0c20575e4d3$export$2e2bcd8739ae039)(value, context));
|
|
94
|
+
});
|
|
95
|
+
throw new Error("Invalid type");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
var $80bd448eb6ea085b$export$2e2bcd8739ae039 = (0, $72af1098ef591494$export$2e2bcd8739ae039);
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
//# sourceMappingURL=main.cjs.map
|
|
@@ -0,0 +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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AAe;gBACjB,IAAI,CAAA,GAAA,6BAAa,AAAD,EAAE,QAAQ,OAAO;gBACjC,IAAI;oBACF,OAAO,KAAK,KAAK,CAAC;gBACpB,EAAE,OAAO,KAAK;oBACZ,OAAO;gBACT;YACF,CAAC;YACD,OAAO;QACT,KAAK;YACH,OAAO;QACT;YACE,MAAM,IAAI,MAAM,CAAC,kBAAkB,EAAE,KAAK,CAAC,EAAC;IAChD;AACF;IAEA,2CAAe;;;ADnGA,kDACb,GAAsE,EACtE,OAAgC,EAC7B;IACH,IAAI,CAAA,GAAA,wBAAO,EAAE,MACX,OAAO,CAAA,GAAA,wCAAM,EAAE,KAAK;IAGtB,IAAI,CAAA,GAAA,uBAAO,AAAD,EAAE,MACV,OAAO,IAAI,GAAG,CAAC,CAAA,OAAQ,yCAAoB,MAAM;IAGnD,IAAI,CAAA,GAAA,6BAAY,EAAE,MAChB,OAAO,CAAA,GAAA,yCAAO,EAAE,KAAK,GAAG,CAAC,SAAU,KAAK,EAAE;QACxC,IAAI,IAAI,CAAC,QAAQ,EACf,IAAI,CAAC,MAAM;aACN,IAAI,CAAA,GAAA,wBAAQ,AAAD,EAAE,QAClB,IAAI,CAAC,MAAM,CAAC,CAAA,GAAA,wCAAO,AAAD,EAAE,OAAO;IAE/B;IAGF,MAAM,IAAI,MAAM,gBAAe;AACjC;;AD1BA;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 } 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 }\n })\n }\n\n throw new Error('Invalid type')\n}\n","import { get as getWild } from 'wild-wild-path'\nimport { isPlainObject } 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-.|:]+)}}/gi\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const parts = match.slice(2, -2).split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(':')\n const type: string = typeParts[0] || 'str'\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, match === str)\n break\n }\n }\n\n if (value === undefined) {\n 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 castValue: any\n switch (type) {\n case 'null':\n if (value === 'null' || value === null) return null\n return isWholeString ? value : 'null'\n case 'int':\n castValue = parseInt(value as string)\n return isNaN(castValue) ? value : castValue\n case 'num':\n castValue = parseFloat(value as string)\n return isNaN(castValue) ? value : castValue\n case 'str':\n return String(value)\n case 'bool':\n if (value === 'true' || value === true) return true\n if (value === 'false' || value === false) return false\n return value // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(value)) return value\n try {\n return JSON.parse(value as string)\n } catch (err) {\n return value\n }\n }\n return value\n case 'any':\n return value\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
ADDED
|
@@ -0,0 +1,90 @@
|
|
|
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$get} from "wild-wild-path";
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
function $67f2b908c91abfcc$var$replace(str = "", obj = {}) {
|
|
10
|
+
const regex = /{{([\w-.|:]+)}}/gi;
|
|
11
|
+
const matches = str.match(regex);
|
|
12
|
+
let result = str;
|
|
13
|
+
if (matches) matches.forEach((match)=>{
|
|
14
|
+
const parts = match.slice(2, -2).split("|");
|
|
15
|
+
const isNullAllowed = parts.some((part)=>part.includes(":null"));
|
|
16
|
+
let value = undefined;
|
|
17
|
+
for (const part of parts){
|
|
18
|
+
const [path, ...typeParts] = part.split(":");
|
|
19
|
+
const type = typeParts[0] || "str";
|
|
20
|
+
const tempValue = (0, $hCgyA$get)(obj, path.trim());
|
|
21
|
+
if (tempValue !== undefined) {
|
|
22
|
+
value = $67f2b908c91abfcc$var$typeCast(tempValue, type, match === str);
|
|
23
|
+
break;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
if (value === undefined) {
|
|
27
|
+
if (isNullAllowed) value = null;
|
|
28
|
+
else value = match // Keep the placeholder intact
|
|
29
|
+
;
|
|
30
|
+
}
|
|
31
|
+
if (match === str) result = value;
|
|
32
|
+
else result = result.replace(match, value);
|
|
33
|
+
});
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
function $67f2b908c91abfcc$var$typeCast(value, type, isWholeString) {
|
|
37
|
+
if (value === undefined || value === null) return value;
|
|
38
|
+
let castValue;
|
|
39
|
+
switch(type){
|
|
40
|
+
case "null":
|
|
41
|
+
if (value === "null" || value === null) return null;
|
|
42
|
+
return isWholeString ? value : "null";
|
|
43
|
+
case "int":
|
|
44
|
+
castValue = parseInt(value);
|
|
45
|
+
return isNaN(castValue) ? value : castValue;
|
|
46
|
+
case "num":
|
|
47
|
+
castValue = parseFloat(value);
|
|
48
|
+
return isNaN(castValue) ? value : castValue;
|
|
49
|
+
case "str":
|
|
50
|
+
return String(value);
|
|
51
|
+
case "bool":
|
|
52
|
+
if (value === "true" || value === true) return true;
|
|
53
|
+
if (value === "false" || value === false) return false;
|
|
54
|
+
return value // Return the original value if it's not 'true' or 'false'
|
|
55
|
+
;
|
|
56
|
+
case "json":
|
|
57
|
+
if (isWholeString) {
|
|
58
|
+
if ((0, $hCgyA$isPlainObject)(value)) return value;
|
|
59
|
+
try {
|
|
60
|
+
return JSON.parse(value);
|
|
61
|
+
} catch (err) {
|
|
62
|
+
return value;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
case "any":
|
|
67
|
+
return value;
|
|
68
|
+
default:
|
|
69
|
+
throw new Error(`Unsupported type: ${type}`);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
var $67f2b908c91abfcc$export$2e2bcd8739ae039 = $67f2b908c91abfcc$var$replace;
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
function $4e62acd4800cacd9$export$2e2bcd8739ae039(obj, context) {
|
|
76
|
+
if ((0, $hCgyA$isString)(obj)) return (0, $67f2b908c91abfcc$export$2e2bcd8739ae039)(obj, context);
|
|
77
|
+
if ((0, $hCgyA$isArray)(obj)) return obj.map((item)=>$4e62acd4800cacd9$export$2e2bcd8739ae039(item, context));
|
|
78
|
+
if ((0, $hCgyA$isPlainObject)(obj)) return (0, $hCgyA$traverse)(obj).map(function(value) {
|
|
79
|
+
if (this.circular) this.remove();
|
|
80
|
+
else if ((0, $hCgyA$isString)(value)) this.update((0, $67f2b908c91abfcc$export$2e2bcd8739ae039)(value, context));
|
|
81
|
+
});
|
|
82
|
+
throw new Error("Invalid type");
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
var $c3f6c693698dc7cd$export$2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laceholders.ts","replace.ts"],"sourcesContent":["import replacePlaceholders from './replacePlaceholders';\nexport default replacePlaceholders;\n","import traverse from 'traverse'\nimport { isString, isArray, isPlainObject } 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 }\n })\n }\n\n throw new Error('Invalid type')\n}\n","import { get as getWild } from 'wild-wild-path'\nimport { isPlainObject } 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-.|:]+)}}/gi\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const parts = match.slice(2, -2).split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(':')\n const type: string = typeParts[0] || 'str'\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, match === str)\n break\n }\n }\n\n if (value === undefined) {\n 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 castValue: any\n switch (type) {\n case 'null':\n if (value === 'null' || value === null) return null\n return isWholeString ? value : 'null'\n case 'int':\n castValue = parseInt(value as string)\n return isNaN(castValue) ? value : castValue\n case 'num':\n castValue = parseFloat(value as string)\n return isNaN(castValue) ? value : castValue\n case 'str':\n return String(value)\n case 'bool':\n if (value === 'true' || value === true) return true\n if (value === 'false' || value === false) return false\n return value // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(value)) return value\n try {\n return JSON.parse(value as string)\n } catch (err) {\n return value\n }\n }\n return value\n case 'any':\n return value\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
ADDED
|
@@ -0,0 +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,CAoBH;ACzBD,eAAe,mBAAmB,CAAC","sources":["replace.ts","replacePlaceholders.ts","index.ts"],"sourcesContent":["import { get as getWild } from 'wild-wild-path'\nimport { isPlainObject } 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-.|:]+)}}/gi\n const matches = str.match(regex)\n let result: string | any = str\n\n if (matches) {\n matches.forEach(match => {\n const parts = match.slice(2, -2).split('|')\n const isNullAllowed = parts.some(part => part.includes(':null'))\n\n let value: any = undefined\n for (const part of parts) {\n const [path, ...typeParts] = part.split(':')\n const type: string = typeParts[0] || 'str'\n\n const tempValue = getWild(obj, path.trim())\n if (tempValue !== undefined) {\n value = typeCast(tempValue, type, match === str)\n break\n }\n }\n\n if (value === undefined) {\n 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 castValue: any\n switch (type) {\n case 'null':\n if (value === 'null' || value === null) return null\n return isWholeString ? value : 'null'\n case 'int':\n castValue = parseInt(value as string)\n return isNaN(castValue) ? value : castValue\n case 'num':\n castValue = parseFloat(value as string)\n return isNaN(castValue) ? value : castValue\n case 'str':\n return String(value)\n case 'bool':\n if (value === 'true' || value === true) return true\n if (value === 'false' || value === false) return false\n return value // Return the original value if it's not 'true' or 'false'\n case 'json':\n if (isWholeString) {\n if (isPlainObject(value)) return value\n try {\n return JSON.parse(value as string)\n } catch (err) {\n return value\n }\n }\n return value\n case 'any':\n return value\n default:\n throw new Error(`Unsupported type: ${type}`)\n }\n}\n\nexport default replace\n","import traverse from 'traverse'\nimport { isString, isArray, isPlainObject } 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 }\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
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@truto/replace-placeholders",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"repository": "https://github.com/trutohq/replace-placeholders.git",
|
|
6
|
+
"dependencies": {
|
|
7
|
+
"wild-wild-path": "4.0.0",
|
|
8
|
+
"lodash-es": "4.17.21",
|
|
9
|
+
"traverse": "0.6.7"
|
|
10
|
+
},
|
|
11
|
+
"source": "index.ts",
|
|
12
|
+
"main": "dist/main.cjs",
|
|
13
|
+
"module": "dist/module.js",
|
|
14
|
+
"types": "dist/types.d.ts",
|
|
15
|
+
"files": [
|
|
16
|
+
"dist"
|
|
17
|
+
],
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"require": "./dist/main.cjs",
|
|
21
|
+
"import": "./dist/module.js"
|
|
22
|
+
}
|
|
23
|
+
},
|
|
24
|
+
"targets": {
|
|
25
|
+
"main": {
|
|
26
|
+
"isLibrary": true
|
|
27
|
+
},
|
|
28
|
+
"module": {
|
|
29
|
+
"isLibrary": true
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"type": "module",
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"volta": {
|
|
35
|
+
"node": "18.13.0",
|
|
36
|
+
"yarn": "1.22.19"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@parcel/packager-ts": "2.8.3",
|
|
40
|
+
"@parcel/transformer-typescript-types": "2.8.3",
|
|
41
|
+
"@types/lodash-es": "4.17.7",
|
|
42
|
+
"@types/traverse": "0.6.32",
|
|
43
|
+
"@typescript-eslint/eslint-plugin": "5.46.1",
|
|
44
|
+
"@typescript-eslint/parser": "5.46.1",
|
|
45
|
+
"eslint": "8.29.0",
|
|
46
|
+
"eslint-config-prettier": "8.5.0",
|
|
47
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
48
|
+
"parcel": "2.8.3",
|
|
49
|
+
"prettier": "2.8.4",
|
|
50
|
+
"ts-node": "10.9.1",
|
|
51
|
+
"typescript": "4.9.5",
|
|
52
|
+
"vitest": "0.34.2"
|
|
53
|
+
},
|
|
54
|
+
"private": false,
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
},
|
|
58
|
+
"scripts": {
|
|
59
|
+
"dev": "parcel watch",
|
|
60
|
+
"build": "rm -rf dist && parcel build",
|
|
61
|
+
"check": "tsc --noEmit",
|
|
62
|
+
"prepublishOnly": "yarn build",
|
|
63
|
+
"test": "vitest"
|
|
64
|
+
}
|
|
65
|
+
}
|