spooder 4.2.1 → 4.2.3

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.
Files changed (3) hide show
  1. package/README.md +4 -4
  2. package/package.json +1 -1
  3. package/src/api.ts +22 -19
package/README.md CHANGED
@@ -987,7 +987,7 @@ let hash_sub_table = {};
987
987
  generate_hash_subs().then(subs => hash_sub_table = subs).catch(caution);
988
988
 
989
989
  server.route('/test', (req, url) => {
990
- return parse_template('Hello world {hash=docs/project-logo.png}', hash_sub_table);
990
+ return parse_template('Hello world {$hash=docs/project-logo.png}', hash_sub_table);
991
991
  });
992
992
  ```
993
993
 
@@ -1008,13 +1008,13 @@ generate_hash_subs(40).then(...);
1008
1008
  > [!NOTE]
1009
1009
  > SHA-1 hashes are `40` characters. Git is transitioning to SHA-256, which are `64` characters. Short hashes of `7` are generally sufficient for cache-busting.
1010
1010
 
1011
- Use a different prefix other than `hash=` by passing it as the first parameter.
1011
+ Use a different prefix other than `$hash=` by passing it as the first parameter.
1012
1012
 
1013
1013
  ```ts
1014
- generate_hash_subs(7, '#').then(subs => hash_sub_table = subs).catch(caution);
1014
+ generate_hash_subs(7, '$#').then(subs => hash_sub_table = subs).catch(caution);
1015
1015
 
1016
1016
  server.route('/test', (req, url) => {
1017
- return parse_template('Hello world {#docs/project-logo.png}', hash_sub_table);
1017
+ return parse_template('Hello world {$#docs/project-logo.png}', hash_sub_table);
1018
1018
  });
1019
1019
  ```
1020
1020
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "spooder",
3
3
  "type": "module",
4
- "version": "4.2.1",
4
+ "version": "4.2.3",
5
5
  "exports": {
6
6
  ".": {
7
7
  "bun": "./src/api.ts",
package/src/api.ts CHANGED
@@ -123,26 +123,29 @@ export function parse_template(template: string, replacements: Record<string, st
123
123
  const loop_key = buffer.substring(4);
124
124
 
125
125
  const loop_entries = replacements[loop_key];
126
- if (loop_entries !== undefined) {
127
- const loop_content_start_index = i + 1;
128
- const loop_close_index = template.indexOf('{/for}', loop_content_start_index);
126
+ const loop_content_start_index = i + 1;
127
+ const loop_close_index = template.indexOf('{/for}', loop_content_start_index);
128
+
129
+ if (loop_close_index === -1) {
130
+ result += '{$' + buffer + '}';
131
+ } else {
129
132
  const loop_content = template.substring(loop_content_start_index, loop_close_index);
130
-
131
- // More performat than replaceAll on larger arrays (and equal on tiny arrays).
132
- const content_parts = loop_content.split('%s');
133
- const indicies = [] as Array<number>;
134
-
135
- for (let j = 0; j < content_parts.length; j++)
136
- if (content_parts[j] === '%s')
137
- indicies.push(j);
138
-
139
- for (const loop_entry of loop_entries)
140
- for (const index of indicies)
141
- content_parts[index] = loop_entry;
142
-
133
+ if (loop_entries !== undefined) {
134
+ // More performat than replaceAll on larger arrays (and equal on tiny arrays).
135
+ const content_parts = loop_content.split('%s');
136
+ const indicies = [] as Array<number>;
137
+
138
+ for (let j = 0; j < content_parts.length; j++)
139
+ if (content_parts[j] === '%s')
140
+ indicies.push(j);
141
+
142
+ for (const loop_entry of loop_entries)
143
+ for (const index of indicies)
144
+ content_parts[index] = loop_entry;
145
+ } else {
146
+ result += '{$' + buffer + '}' + loop_content + '{/for}';
147
+ }
143
148
  i += loop_content.length + 6;
144
- } else {
145
- result += '{$' + buffer + '}';
146
149
  }
147
150
  } else {
148
151
  result += replacements[buffer] ?? '{$' + buffer + '}';
@@ -158,7 +161,7 @@ export function parse_template(template: string, replacements: Record<string, st
158
161
  return result;
159
162
  }
160
163
 
161
- export async function generate_hash_subs(length = 7, prefix = 'hash='): Promise<Record<string, string>> {
164
+ export async function generate_hash_subs(length = 7, prefix = '$hash='): Promise<Record<string, string>> {
162
165
  const cmd = ['git', 'ls-tree', '-r', 'HEAD'];
163
166
  const process = Bun.spawn(cmd, {
164
167
  stdout: 'pipe',