spooder 4.2.8 → 4.2.9
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 +25 -2
- package/package.json +1 -1
- package/src/api.d.ts +3 -1
- package/src/api.ts +8 -3
package/README.md
CHANGED
|
@@ -158,7 +158,7 @@ The following differences will be observed when running in development mode:
|
|
|
158
158
|
It is possible to detect in userland if a server is running in development mode by checking the `SPOODER_ENV` environment variable.
|
|
159
159
|
|
|
160
160
|
```ts
|
|
161
|
-
if (process.env.
|
|
161
|
+
if (process.env.SPOODER_ENV === 'dev') {
|
|
162
162
|
// Server is running in development mode.
|
|
163
163
|
}
|
|
164
164
|
```
|
|
@@ -937,7 +937,7 @@ await safe(() => {
|
|
|
937
937
|
## API > Content
|
|
938
938
|
|
|
939
939
|
<a id="api-content-parse-template"></a>
|
|
940
|
-
### 🔧 `parse_template(template: string, replacements:
|
|
940
|
+
### 🔧 `parse_template(template: string, replacements: Replacements, drop_missing: boolean): string`
|
|
941
941
|
|
|
942
942
|
Replace placeholders in a template string with values from a replacement object.
|
|
943
943
|
|
|
@@ -995,6 +995,29 @@ parse_template(template, replacements, true);
|
|
|
995
995
|
</html>
|
|
996
996
|
```
|
|
997
997
|
|
|
998
|
+
`parse_template` supports passing a function instead of a replacement object. This function will be called for each placeholder and the return value will be used as the replacement.
|
|
999
|
+
|
|
1000
|
+
```ts
|
|
1001
|
+
const replacer = (placeholder: string) => {
|
|
1002
|
+
return placeholder.toUpperCase();
|
|
1003
|
+
};
|
|
1004
|
+
|
|
1005
|
+
parse_template('Hello {$world}', replacer);
|
|
1006
|
+
```
|
|
1007
|
+
|
|
1008
|
+
```html
|
|
1009
|
+
<html>
|
|
1010
|
+
<head>
|
|
1011
|
+
<title>TITLE</title>
|
|
1012
|
+
</head>
|
|
1013
|
+
<body>
|
|
1014
|
+
<h1>TITLE</h1>
|
|
1015
|
+
<p>CONTENT</p>
|
|
1016
|
+
<p>IGNORED</p>
|
|
1017
|
+
</body>
|
|
1018
|
+
</html>
|
|
1019
|
+
```
|
|
1020
|
+
|
|
998
1021
|
`parse_template` supports looping arrays with the following syntax.
|
|
999
1022
|
|
|
1000
1023
|
```html
|
package/package.json
CHANGED
package/src/api.d.ts
CHANGED
|
@@ -20,7 +20,9 @@ export declare function caution(err_message_or_obj: string | object, ...err: obj
|
|
|
20
20
|
type CallableFunction = (...args: any[]) => any;
|
|
21
21
|
type Callable = Promise<any> | CallableFunction;
|
|
22
22
|
export declare function safe(target_fn: Callable): Promise<void>;
|
|
23
|
-
|
|
23
|
+
type ReplacerFn = (key: string) => string | Array<string>;
|
|
24
|
+
type Replacements = Record<string, string | Array<string>> | ReplacerFn;
|
|
25
|
+
export declare function parse_template(template: string, replacements: Replacements, drop_missing?: boolean): string;
|
|
24
26
|
export declare function generate_hash_subs(length?: number, prefix?: string): Promise<Record<string, string>>;
|
|
25
27
|
type CookieOptions = {
|
|
26
28
|
same_site?: 'Strict' | 'Lax' | 'None';
|
package/src/api.ts
CHANGED
|
@@ -107,11 +107,16 @@ export async function safe(target_fn: Callable) {
|
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
109
|
|
|
110
|
-
|
|
110
|
+
type ReplacerFn = (key: string) => string | Array<string>;
|
|
111
|
+
type Replacements = Record<string, string | Array<string>> | ReplacerFn;
|
|
112
|
+
|
|
113
|
+
export function parse_template(template: string, replacements: Replacements, drop_missing = false): string {
|
|
111
114
|
let result = '';
|
|
112
115
|
let buffer = '';
|
|
113
116
|
let buffer_active = false;
|
|
114
117
|
|
|
118
|
+
const is_replacer_fn = typeof replacements === 'function';
|
|
119
|
+
|
|
115
120
|
const template_length = template.length;
|
|
116
121
|
for (let i = 0; i < template_length; i++) {
|
|
117
122
|
const char = template[i];
|
|
@@ -126,7 +131,7 @@ export function parse_template(template: string, replacements: Record<string, st
|
|
|
126
131
|
if (buffer.startsWith('for:')) {
|
|
127
132
|
const loop_key = buffer.substring(4);
|
|
128
133
|
|
|
129
|
-
const loop_entries = replacements[loop_key];
|
|
134
|
+
const loop_entries = is_replacer_fn ? replacements(loop_key) : replacements[loop_key];
|
|
130
135
|
const loop_content_start_index = i + 1;
|
|
131
136
|
const loop_close_index = template.indexOf('{/for}', loop_content_start_index);
|
|
132
137
|
|
|
@@ -147,7 +152,7 @@ export function parse_template(template: string, replacements: Record<string, st
|
|
|
147
152
|
i += loop_content.length + 6;
|
|
148
153
|
}
|
|
149
154
|
} else {
|
|
150
|
-
const replacement = replacements[buffer];
|
|
155
|
+
const replacement = is_replacer_fn ? replacements(buffer) : replacements[buffer];
|
|
151
156
|
if (replacement !== undefined)
|
|
152
157
|
result += replacement;
|
|
153
158
|
else if (!drop_missing)
|