spooder 4.2.4 → 4.2.6
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 +42 -9
- package/package.json +1 -1
- package/src/api.d.ts +1 -1
- package/src/api.ts +19 -17
package/README.md
CHANGED
|
@@ -432,7 +432,7 @@ In addition to the information provided by the developer, `spooder` also include
|
|
|
432
432
|
- [`panic(err_message_or_obj: string | object, ...err: object[]): Promise<void>`](#api-error-handling-panic)
|
|
433
433
|
- [`safe(fn: Callable): Promise<void>`](#api-error-handling-safe)
|
|
434
434
|
- [API > Content](#api-content)
|
|
435
|
-
- [`parse_template(template: string, replacements: Record<string, string
|
|
435
|
+
- [`parse_template(template: string, replacements: Record<string, string>, drop_missing: boolean): string`](#api-content-parse-template)
|
|
436
436
|
- [`generate_hash_subs(length: number, prefix: string): Promise<Record<string, string>>`](#api-content-generate-hash-subs)
|
|
437
437
|
- [`apply_range(file: BunFile, request: Request): HandlerReturnType`](#api-content-apply-range)
|
|
438
438
|
- [API > State Management](#api-state-management)
|
|
@@ -901,13 +901,10 @@ await safe(() => {
|
|
|
901
901
|
## API > Content
|
|
902
902
|
|
|
903
903
|
<a id="api-content-parse-template"></a>
|
|
904
|
-
### 🔧 `parse_template(template: string, replacements: Record<string, string
|
|
904
|
+
### 🔧 `parse_template(template: string, replacements: Record<string, string>, drop_missing: boolean): string`
|
|
905
905
|
|
|
906
906
|
Replace placeholders in a template string with values from a replacement object.
|
|
907
907
|
|
|
908
|
-
> [!NOTE]
|
|
909
|
-
> Placeholders that do not appear in the replacement object will be left as-is. See `ignored` in below example.
|
|
910
|
-
|
|
911
908
|
```ts
|
|
912
909
|
const template = `
|
|
913
910
|
<html>
|
|
@@ -943,15 +940,34 @@ const html = parse_template(template, replacements);
|
|
|
943
940
|
</html>
|
|
944
941
|
```
|
|
945
942
|
|
|
943
|
+
By default, placeholders that do not appear in the replacement object will be left as-is. Set `drop_missing` to `true` to remove them.
|
|
944
|
+
|
|
945
|
+
```ts
|
|
946
|
+
parse_template(template, replacements, true);
|
|
947
|
+
```
|
|
948
|
+
|
|
949
|
+
```html
|
|
950
|
+
<html>
|
|
951
|
+
<head>
|
|
952
|
+
<title>Hello, world!</title>
|
|
953
|
+
</head>
|
|
954
|
+
<body>
|
|
955
|
+
<h1>Hello, world!</h1>
|
|
956
|
+
<p>This is a test.</p>
|
|
957
|
+
<p></p>
|
|
958
|
+
</body>
|
|
959
|
+
</html>
|
|
960
|
+
```
|
|
961
|
+
|
|
946
962
|
`parse_template` supports looping arrays with the following syntax.
|
|
947
963
|
|
|
948
964
|
```html
|
|
949
|
-
{$for:foo}My colour is
|
|
965
|
+
{$for:foo}My colour is {$entry}{/for}
|
|
950
966
|
```
|
|
951
967
|
```ts
|
|
952
968
|
const template = `
|
|
953
969
|
<ul>
|
|
954
|
-
{$for:foo}<li
|
|
970
|
+
{$for:foo}<li>{$entry}</li>{/for}
|
|
955
971
|
</ul>
|
|
956
972
|
`;
|
|
957
973
|
|
|
@@ -970,8 +986,25 @@ const html = parse_template(template, replacements);
|
|
|
970
986
|
</ul>
|
|
971
987
|
```
|
|
972
988
|
|
|
973
|
-
|
|
974
|
-
|
|
989
|
+
All placeholders inside a `{$for:}` loop are substituted, but only if the loop variable exists.
|
|
990
|
+
|
|
991
|
+
In the following example, `missing` does not exist, so `test` is not substituted inside the loop, but `test` is still substituted outside the loop.
|
|
992
|
+
|
|
993
|
+
```html
|
|
994
|
+
<div>Hello {$test}!</div>
|
|
995
|
+
{$for:missing}<div>Loop {$test}</div>{/for}
|
|
996
|
+
```
|
|
997
|
+
|
|
998
|
+
```ts
|
|
999
|
+
parse_template(..., {
|
|
1000
|
+
test: 'world'
|
|
1001
|
+
});
|
|
1002
|
+
```
|
|
1003
|
+
|
|
1004
|
+
```html
|
|
1005
|
+
<div>Hello world!</div>
|
|
1006
|
+
{$for}Loop <div>{$test}</div>{/for}
|
|
1007
|
+
```
|
|
975
1008
|
|
|
976
1009
|
<a id="api-content-generate-hash-subs"></a>
|
|
977
1010
|
### 🔧 `generate_hash_subs(prefix: string): Promise<Record<string, string>>`
|
package/package.json
CHANGED
package/src/api.d.ts
CHANGED
|
@@ -18,7 +18,7 @@ export declare function caution(err_message_or_obj: string | object, ...err: obj
|
|
|
18
18
|
type CallableFunction = (...args: any[]) => any;
|
|
19
19
|
type Callable = Promise<any> | CallableFunction;
|
|
20
20
|
export declare function safe(target_fn: Callable): Promise<void>;
|
|
21
|
-
export declare function parse_template(template: string, replacements: Record<string, string | Array<string
|
|
21
|
+
export declare function parse_template(template: string, replacements: Record<string, string | Array<string>>, drop_missing?: boolean): string;
|
|
22
22
|
export declare function generate_hash_subs(length?: number, prefix?: string): Promise<Record<string, string>>;
|
|
23
23
|
type CookieOptions = {
|
|
24
24
|
same_site?: 'Strict' | 'Lax' | 'None';
|
package/src/api.ts
CHANGED
|
@@ -103,7 +103,7 @@ export async function safe(target_fn: Callable) {
|
|
|
103
103
|
}
|
|
104
104
|
}
|
|
105
105
|
|
|
106
|
-
export function parse_template(template: string, replacements: Record<string, string | Array<string
|
|
106
|
+
export function parse_template(template: string, replacements: Record<string, string | Array<string>>, drop_missing = false): string {
|
|
107
107
|
let result = '';
|
|
108
108
|
let buffer = '';
|
|
109
109
|
let buffer_active = false;
|
|
@@ -127,30 +127,32 @@ export function parse_template(template: string, replacements: Record<string, st
|
|
|
127
127
|
const loop_close_index = template.indexOf('{/for}', loop_content_start_index);
|
|
128
128
|
|
|
129
129
|
if (loop_close_index === -1) {
|
|
130
|
-
|
|
130
|
+
if (!drop_missing)
|
|
131
|
+
result += '{$' + buffer + '}';
|
|
131
132
|
} else {
|
|
132
133
|
const loop_content = template.substring(loop_content_start_index, loop_close_index);
|
|
133
134
|
if (loop_entries !== undefined) {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
for (const index of indicies)
|
|
144
|
-
content_parts[index] = loop_entry;
|
|
145
|
-
|
|
146
|
-
result += content_parts.join('');
|
|
135
|
+
const inner_replacements = {
|
|
136
|
+
...replacements,
|
|
137
|
+
entry: ''
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
for (const loop_entry of loop_entries) {
|
|
141
|
+
inner_replacements.entry = loop_entry;
|
|
142
|
+
result += parse_template(loop_content, inner_replacements, drop_missing);
|
|
143
|
+
}
|
|
147
144
|
} else {
|
|
148
|
-
|
|
145
|
+
if (!drop_missing)
|
|
146
|
+
result += '{$' + buffer + '}' + loop_content + '{/for}';
|
|
149
147
|
}
|
|
150
148
|
i += loop_content.length + 6;
|
|
151
149
|
}
|
|
152
150
|
} else {
|
|
153
|
-
|
|
151
|
+
const replacement = replacements[buffer];
|
|
152
|
+
if (replacement !== undefined)
|
|
153
|
+
result += replacement;
|
|
154
|
+
else if (!drop_missing)
|
|
155
|
+
result += '{$' + buffer + '}';
|
|
154
156
|
}
|
|
155
157
|
buffer = '';
|
|
156
158
|
} else if (buffer_active) {
|