@itrocks/template 0.0.6 → 0.0.8

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 CHANGED
@@ -18,7 +18,7 @@ npm i @itrocks/template
18
18
 
19
19
  ```ts
20
20
  console.log(
21
- new Template({
21
+ await new Template({
22
22
  users: [
23
23
  { age: 10, name: 'kid' },
24
24
  { age: 20, name: 'old-timer' }
@@ -68,6 +68,9 @@ Since the engine supports asynchronous operations (e.g., reading files, calling
68
68
  parsing returns a [Promise](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Promise)
69
69
  that you should handle with [await](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Operators/await).
70
70
 
71
+ This library is fully compatible with both ECMAScript modules (import) and CommonJS (require),
72
+ adapting seamlessly to your project's module system.
73
+
71
74
  ## Key features
72
75
 
73
76
  ### W3C Validation-Friendly Templates
@@ -193,7 +196,7 @@ if not properly spaced.
193
196
  Wrap a property in `{}` to display its value:
194
197
  ```ts
195
198
  console.log(
196
- new Template({ var: 15 }).parseBuffer('<span>{var}</span>')
199
+ await new Template({ var: 15 }).parseBuffer('<span>{var}</span>')
197
200
  )
198
201
  ```
199
202
  Result:
@@ -206,7 +209,7 @@ Result:
206
209
  If a context property is a function, it will be called and its return value displayed:
207
210
  ```ts
208
211
  console.log(
209
- new Template({ var: () => 15 }).parseBuffer('<span>{var}</span>')
212
+ await new Template({ var: () => 15 }).parseBuffer('<span>{var}</span>')
210
213
  )
211
214
  ```
212
215
  Result:
@@ -218,7 +221,7 @@ Result:
218
221
 
219
222
  Use `{.}` to reference the current data context:
220
223
  ```ts
221
- console.log(new Template(15).parseBuffer('<span>{.}</span>'))
224
+ console.log(await new Template(15).parseBuffer('<span>{.}</span>'))
222
225
  ```
223
226
  Result:
224
227
  ```html
@@ -262,14 +265,14 @@ Result on data `{ user: { age: 10, name: 'kid' } }`:
262
265
  You can use dot notation within `{your.expression}`:
263
266
  ```ts
264
267
  console.log(
265
- new Template({ user: { age: 10, name: 'kid' } })
268
+ await new Template({ user: { age: 10, name: 'kid' } })
266
269
  .parseBuffer('<span>{user.name} is {user.age} years old</span>')
267
270
  )
268
271
  ```
269
272
  Or use a block to avoid repeating:
270
273
  ```ts
271
274
  console.log(
272
- new Template({ user: { age: 10, name: 'kid' } })
275
+ await new Template({ user: { age: 10, name: 'kid' } })
273
276
  .parseBuffer('<span><!--user-->{name} is {age} years old<!--end--></span>')
274
277
  )
275
278
  ```
@@ -283,7 +286,7 @@ Both produce:
283
286
  Use `*` to iterate over all values of an object:
284
287
  ```ts
285
288
  console.log(
286
- new Template({ object: { first: 'kid', next: 'old-timer' } })
289
+ await new Template({ object: { first: 'kid', next: 'old-timer' } })
287
290
  .parseBuffer('<ul><!--object.*--><li>{.}<!--end--></ul>')
288
291
  )
289
292
  ```
@@ -296,7 +299,7 @@ Result:
296
299
 
297
300
  ```ts
298
301
  console.log(
299
- new Template({ users: ['kid', 'old-timer'] })
302
+ await new Template({ users: ['kid', 'old-timer'] })
300
303
  .parseBuffer('<ul><!--users--><li>{.}</li><!--end--></ul>')
301
304
  )
302
305
  ```
@@ -309,7 +312,7 @@ Result:
309
312
 
310
313
  ```ts
311
314
  console.log(
312
- new Template({
315
+ await new Template({
313
316
  users: [
314
317
  { age: 10, name: 'kid' },
315
318
  { age: 20, name: 'old-timer' }
@@ -337,7 +340,7 @@ Result:
337
340
  Use `-` to go back up one level in the data context:
338
341
  ```ts
339
342
  console.log(
340
- new Template({ name: 'Eddie', data: { age: 30, status: 'well' } })
343
+ await new Template({ name: 'Eddie', data: { age: 30, status: 'well' } })
341
344
  .parseBuffer(`
342
345
  <!--data-->
343
346
  <ol>
@@ -372,7 +375,7 @@ the engine attempts to use the [Str](https://www.npmjs.com/package/@itrocks/rena
372
375
  which provides string formatting functions. If no matching function is found, an error is thrown.
373
376
  ```ts
374
377
  console.log(
375
- new Template({ name: 'EDITH' })
378
+ await new Template({ name: 'EDITH' })
376
379
  .parseBuffer('<span>{name.lcFirst}</span>')
377
380
  )
378
381
  ```
@@ -449,11 +452,11 @@ class MyTemplate extends Template
449
452
  Using this class:
450
453
  ```ts
451
454
  console.log(
452
- new MyTemplate({ name: 'Nick' })
453
- .parseBuffer(`
454
- <h2>What is my name</h2>
455
- <p>My name is {name}</p>
456
- `)
455
+ await new MyTemplate({ name: 'Nick' })
456
+ .parseBuffer(`
457
+ <h2>What is my name</h2>
458
+ <p>My name is {name}</p>
459
+ `)
457
460
  )
458
461
  ```
459
462
  Results in:
@@ -466,11 +469,11 @@ Results in:
466
469
  are considered part of the phrase, so their text is also translated:
467
470
  ```ts
468
471
  console.log(
469
- new MyTemplate({ name: 'Nick' })
470
- .parseBuffer(`
471
- <h2>What is my name</h2>
472
- <p>My <span>name</span> is {name}</p>
473
- `)
472
+ await new MyTemplate({ name: 'Nick' })
473
+ .parseBuffer(`
474
+ <h2>What is my name</h2>
475
+ <p>My <span>name</span> is {name}</p>
476
+ `)
474
477
  )
475
478
  ```
476
479
  Results in:
@@ -0,0 +1,76 @@
1
+ import { SortedArray } from '@itrocks/sorted-array';
2
+ export declare const frontScripts: SortedArray<string>;
3
+ export type VariableParser = [parser: string, (variable: string, data: any) => any];
4
+ export { Template };
5
+ export default class Template {
6
+ data?: any;
7
+ containerData?: any;
8
+ doExpression: boolean;
9
+ doLiteral: boolean;
10
+ fileName?: string;
11
+ filePath?: string;
12
+ included: boolean;
13
+ inlineElements: SortedArray<string>;
14
+ literalAttributes: SortedArray<string>;
15
+ literalElements: SortedArray<string>;
16
+ onAttribute?: ((name: string, value: string) => void);
17
+ onTagOpen?: ((name: string) => void);
18
+ onTagOpened?: ((name: string) => void);
19
+ onTagClose?: ((name: string) => void);
20
+ parsers: VariableParser[];
21
+ prefixes: string;
22
+ unclosingTags: SortedArray<string>;
23
+ constructor(data?: any, containerData?: any);
24
+ applyLiterals(text: string, parts?: string[]): string;
25
+ closeTag(shouldInLiteral: boolean, targetIndex: number): boolean;
26
+ combineLiterals(text: string, parts?: string[]): string;
27
+ debugEvents(): void;
28
+ getCleanContext(): {
29
+ doHeadLinks: boolean;
30
+ doneLinks: SortedArray<string>;
31
+ headLinks: SortedArray<string>;
32
+ index: number;
33
+ length: number;
34
+ source: string;
35
+ start: number;
36
+ target: string;
37
+ targetStack: never[];
38
+ literalPartStack: never[];
39
+ literalParts: never[];
40
+ inLiteral: boolean;
41
+ };
42
+ getPosition(): {
43
+ index: number;
44
+ start: number;
45
+ target: string;
46
+ };
47
+ getContext(): {
48
+ doHeadLinks: boolean;
49
+ doneLinks: SortedArray<string>;
50
+ headLinks: SortedArray<string>;
51
+ index: number;
52
+ length: number;
53
+ source: string;
54
+ start: number;
55
+ target: string;
56
+ targetStack: string[];
57
+ literalPartStack: string[][];
58
+ literalParts: string[];
59
+ inLiteral: boolean;
60
+ };
61
+ include(path: string, data: any): Promise<any>;
62
+ isContextClean(): boolean;
63
+ literalTarget(index: number, isTitle?: boolean): void;
64
+ parseBuffer(buffer: string): Promise<string>;
65
+ parseExpression(data: any, close: string, finalClose?: string): Promise<boolean | undefined>;
66
+ parseFile(fileName: string, containerFileName?: string): Promise<string>;
67
+ parsePath(expression: string, data: any): Promise<any>;
68
+ parseVariable(variable: string, data: any): Promise<any>;
69
+ parseVars(): Promise<string>;
70
+ setSource(setSource: string, setIndex?: number, setStart?: number, setTarget?: string): void;
71
+ skipBlock(): void;
72
+ sourceToTarget(): void;
73
+ startsExpression(char: string, open?: string, close?: string): boolean;
74
+ trimEndLine(string: string): string;
75
+ }
76
+ //# sourceMappingURL=template.d.ts.map
package/cjs/template.js CHANGED
@@ -1,8 +1,11 @@
1
1
  "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
2
5
  Object.defineProperty(exports, "__esModule", { value: true });
3
6
  exports.Template = exports.frontScripts = void 0;
4
- const rename_1 = require("@itrocks/rename");
5
- const app_dir_1 = require("@itrocks/app-dir");
7
+ const rename_1 = __importDefault(require("@itrocks/rename"));
8
+ const app_dir_1 = __importDefault(require("@itrocks/app-dir"));
6
9
  const sorted_array_1 = require("@itrocks/sorted-array");
7
10
  const promises_1 = require("node:fs/promises");
8
11
  const node_path_1 = require("node:path");
@@ -775,3 +778,4 @@ class Template {
775
778
  }
776
779
  exports.default = Template;
777
780
  exports.Template = Template;
781
+ //# sourceMappingURL=template.js.map
package/package.json CHANGED
@@ -10,9 +10,9 @@
10
10
  },
11
11
  "description": "The W3C-valid, browser-previewable, concise, and fast HTML template engine that enables delimiter-less translations",
12
12
  "devDependencies": {
13
- "@types/jest": "^29.5.14",
13
+ "@types/jest": "^29.5",
14
14
  "@types/node": "^22.9",
15
- "jest": "^29.7.0",
15
+ "jest": "^29.7",
16
16
  "ts-jest": "^29.2",
17
17
  "typescript": "~5.6"
18
18
  },
@@ -20,15 +20,14 @@
20
20
  "node": ">=18"
21
21
  },
22
22
  "exports": {
23
- "import": "./template.js",
23
+ "import": "./esm/template.js",
24
24
  "require": "./cjs/template.js"
25
25
  },
26
26
  "files": [
27
27
  "LICENSE",
28
28
  "README.md",
29
- "cjs",
30
- "template.d.ts",
31
- "template.js"
29
+ "*/*.js",
30
+ "*/*.d.ts"
32
31
  ],
33
32
  "keywords": [
34
33
  "browser",
@@ -50,13 +49,13 @@
50
49
  ],
51
50
  "license": "LGPL-3.0-or-later",
52
51
  "name": "@itrocks/template",
53
- "repository": "https://github.com/itrocks-ts/template",
52
+ "repository": { "git": "https://github.com/itrocks-ts/template" },
54
53
  "scripts": {
55
54
  "build": "npm run build:cjs && npm run build:esm",
56
55
  "build:cjs": "tsc -p tsconfig.cjs.json",
57
56
  "build:esm": "tsc -p tsconfig.esm.json",
58
57
  "test": "jest"
59
58
  },
60
- "types": "./template.d.ts",
61
- "version": "0.0.6"
59
+ "types": "./esm/template.d.ts",
60
+ "version": "0.0.8"
62
61
  }
File without changes
File without changes