spooder 4.4.0 → 4.4.2
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 +7 -7
- package/package.json +1 -1
- package/src/api.ts +7 -6
package/README.md
CHANGED
|
@@ -1029,7 +1029,7 @@ await safe(() => {
|
|
|
1029
1029
|
## API > Content
|
|
1030
1030
|
|
|
1031
1031
|
<a id="api-content-parse-template"></a>
|
|
1032
|
-
### 🔧 `parse_template(template: string, replacements: Replacements, drop_missing: boolean): string
|
|
1032
|
+
### 🔧 `parse_template(template: string, replacements: Replacements, drop_missing: boolean): Promise<string>`
|
|
1033
1033
|
|
|
1034
1034
|
Replace placeholders in a template string with values from a replacement object.
|
|
1035
1035
|
|
|
@@ -1052,7 +1052,7 @@ const replacements = {
|
|
|
1052
1052
|
content: 'This is a test.'
|
|
1053
1053
|
};
|
|
1054
1054
|
|
|
1055
|
-
const html = parse_template(template, replacements);
|
|
1055
|
+
const html = await parse_template(template, replacements);
|
|
1056
1056
|
```
|
|
1057
1057
|
|
|
1058
1058
|
```html
|
|
@@ -1071,7 +1071,7 @@ const html = parse_template(template, replacements);
|
|
|
1071
1071
|
By default, placeholders that do not appear in the replacement object will be left as-is. Set `drop_missing` to `true` to remove them.
|
|
1072
1072
|
|
|
1073
1073
|
```ts
|
|
1074
|
-
parse_template(template, replacements, true);
|
|
1074
|
+
await parse_template(template, replacements, true);
|
|
1075
1075
|
```
|
|
1076
1076
|
|
|
1077
1077
|
```html
|
|
@@ -1087,14 +1087,14 @@ parse_template(template, replacements, true);
|
|
|
1087
1087
|
</html>
|
|
1088
1088
|
```
|
|
1089
1089
|
|
|
1090
|
-
`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.
|
|
1090
|
+
`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. This function can be a Promise/async function.
|
|
1091
1091
|
|
|
1092
1092
|
```ts
|
|
1093
1093
|
const replacer = (placeholder: string) => {
|
|
1094
1094
|
return placeholder.toUpperCase();
|
|
1095
1095
|
};
|
|
1096
1096
|
|
|
1097
|
-
parse_template('Hello {$world}', replacer);
|
|
1097
|
+
await parse_template('Hello {$world}', replacer);
|
|
1098
1098
|
```
|
|
1099
1099
|
|
|
1100
1100
|
```html
|
|
@@ -1126,7 +1126,7 @@ const replacements = {
|
|
|
1126
1126
|
foo: ['red', 'green', 'blue']
|
|
1127
1127
|
};
|
|
1128
1128
|
|
|
1129
|
-
const html = parse_template(template, replacements);
|
|
1129
|
+
const html = await parse_template(template, replacements);
|
|
1130
1130
|
```
|
|
1131
1131
|
|
|
1132
1132
|
```html
|
|
@@ -1147,7 +1147,7 @@ In the following example, `missing` does not exist, so `test` is not substituted
|
|
|
1147
1147
|
```
|
|
1148
1148
|
|
|
1149
1149
|
```ts
|
|
1150
|
-
parse_template(..., {
|
|
1150
|
+
await parse_template(..., {
|
|
1151
1151
|
test: 'world'
|
|
1152
1152
|
});
|
|
1153
1153
|
```
|
package/package.json
CHANGED
package/src/api.ts
CHANGED
|
@@ -119,9 +119,10 @@ export async function safe(target_fn: Callable) {
|
|
|
119
119
|
}
|
|
120
120
|
|
|
121
121
|
type ReplacerFn = (key: string) => string | Array<string> | undefined;
|
|
122
|
-
type
|
|
122
|
+
type AsyncReplaceFn = (key: string) => Promise<string | Array<string> | undefined>;
|
|
123
|
+
type Replacements = Record<string, string | Array<string>> | ReplacerFn | AsyncReplaceFn;
|
|
123
124
|
|
|
124
|
-
export function parse_template(template: string, replacements: Replacements, drop_missing = false): string {
|
|
125
|
+
export async function parse_template(template: string, replacements: Replacements, drop_missing = false): Promise<string> {
|
|
125
126
|
let result = '';
|
|
126
127
|
let buffer = '';
|
|
127
128
|
let buffer_active = false;
|
|
@@ -142,7 +143,7 @@ export function parse_template(template: string, replacements: Replacements, dro
|
|
|
142
143
|
if (buffer.startsWith('for:')) {
|
|
143
144
|
const loop_key = buffer.substring(4);
|
|
144
145
|
|
|
145
|
-
const loop_entries = is_replacer_fn ? replacements(loop_key) : replacements[loop_key];
|
|
146
|
+
const loop_entries = is_replacer_fn ? await replacements(loop_key) : replacements[loop_key];
|
|
146
147
|
const loop_content_start_index = i + 1;
|
|
147
148
|
const loop_close_index = template.indexOf('{/for}', loop_content_start_index);
|
|
148
149
|
|
|
@@ -154,7 +155,7 @@ export function parse_template(template: string, replacements: Replacements, dro
|
|
|
154
155
|
if (loop_entries !== undefined) {
|
|
155
156
|
for (const loop_entry of loop_entries) {
|
|
156
157
|
const inner_content = loop_content.replaceAll('%s', loop_entry);
|
|
157
|
-
result += parse_template(inner_content, replacements, drop_missing);
|
|
158
|
+
result += await parse_template(inner_content, replacements, drop_missing);
|
|
158
159
|
}
|
|
159
160
|
} else {
|
|
160
161
|
if (!drop_missing)
|
|
@@ -163,7 +164,7 @@ export function parse_template(template: string, replacements: Replacements, dro
|
|
|
163
164
|
i += loop_content.length + 6;
|
|
164
165
|
}
|
|
165
166
|
} else {
|
|
166
|
-
const replacement = is_replacer_fn ? replacements(buffer) : replacements[buffer];
|
|
167
|
+
const replacement = is_replacer_fn ? await replacements(buffer) : replacements[buffer];
|
|
167
168
|
if (replacement !== undefined)
|
|
168
169
|
result += replacement;
|
|
169
170
|
else if (!drop_missing)
|
|
@@ -368,7 +369,7 @@ export async function db_init_schema_sqlite(db_path: string, schema_dir: string)
|
|
|
368
369
|
return db;
|
|
369
370
|
}
|
|
370
371
|
|
|
371
|
-
export async function db_init_schema_mysql(db_info: mysql_types.
|
|
372
|
+
export async function db_init_schema_mysql(db_info: mysql_types.ConnectionOptions, schema_dir: string): Promise<mysql_types.Connection> {
|
|
372
373
|
if (mysql === undefined)
|
|
373
374
|
throw new Error('{db_init_schema_mysql} cannot be called without optional dependency {mysql2} installed');
|
|
374
375
|
|