@itrocks/template 0.0.35 → 0.0.36

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/cjs/template.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { SortedArray } from '@itrocks/sorted-array';
2
2
  type BlockStackEntry = {
3
3
  blockStart: number;
4
- collection: any[];
5
4
  condition?: boolean;
6
5
  data: any;
7
- iteration: number;
8
- iterations: number;
6
+ iteration: IteratorResult<any> | {
7
+ done: boolean;
8
+ value?: any;
9
+ };
10
+ iterator?: Iterator<any>;
9
11
  };
10
12
  type Close = ')' | '}';
11
13
  type Final = '' | '-->';
package/cjs/template.js CHANGED
@@ -6,6 +6,7 @@ const rename_1 = require("@itrocks/rename");
6
6
  const sorted_array_1 = require("@itrocks/sorted-array");
7
7
  const promises_1 = require("node:fs/promises");
8
8
  const node_path_1 = require("node:path");
9
+ const done = { done: true };
9
10
  exports.frontScripts = new sorted_array_1.SortedArray();
10
11
  exports.frontScripts.distinct = true;
11
12
  class Template {
@@ -64,7 +65,7 @@ class Template {
64
65
  this.doneLinks.distinct = true;
65
66
  this.headLinks.distinct = true;
66
67
  if (containerData) {
67
- this.blockStack.push({ blockStart: 0, collection: [], data: containerData, iteration: 0, iterations: 1 });
68
+ this.blockStack.push({ blockStart: 0, data: containerData, iteration: done });
68
69
  }
69
70
  }
70
71
  applyLiterals(text, parts = []) {
@@ -424,11 +425,10 @@ class Template {
424
425
  }
425
426
  async parseVars() {
426
427
  let blockStart = 0;
427
- let collection = [];
428
428
  let data = this.data;
429
429
  let inHead = false;
430
- let iteration = 0;
431
- let iterations = 0;
430
+ let iteration = done;
431
+ let iterator;
432
432
  while (this.index < this.length) {
433
433
  let char = this.source[this.index];
434
434
  // expression
@@ -472,17 +472,17 @@ class Template {
472
472
  // end condition / loop block
473
473
  if ((firstChar === 'e') && (this.source.substring(this.index, this.index + 6) === 'end-->')) {
474
474
  this.target += this.trimEndLine(this.source.substring(this.start, tagIndex));
475
- iteration++;
476
- if (iteration < iterations) {
477
- data = collection[iteration];
475
+ iteration = iterator?.next() ?? done;
476
+ if (!iteration.done) {
477
+ data = iteration.value;
478
478
  this.index = this.start = blockStart;
479
479
  if (this.inLiteral && (this.index > this.start)) {
480
480
  this.sourceToTarget();
481
481
  }
482
482
  continue;
483
483
  }
484
- ({ blockStart, collection, data, iteration, iterations } = this.blockStack.pop()
485
- ?? { blockStart: 0, collection: [], data: undefined, iteration: 0, iterations: 0 });
484
+ ({ blockStart, data, iteration, iterator } = this.blockStack.pop()
485
+ ?? { blockStart: 0, data: undefined, iteration: done });
486
486
  this.index += 6;
487
487
  this.start = this.index;
488
488
  if (this.inLiteral && (this.index > this.start)) {
@@ -501,23 +501,22 @@ class Template {
501
501
  this.target = '';
502
502
  this.inLiteral = false;
503
503
  const condition = await this.parseExpression(data, '<', '}', '-->');
504
- this.blockStack.push({ blockStart, collection, condition, data, iteration, iterations });
504
+ this.blockStack.push({ blockStart, condition, data, iteration, iterator });
505
505
  let blockData = condition ? (this.target ? data : undefined) : this.target;
506
506
  blockStart = this.index;
507
- iteration = 0;
508
507
  this.target = backTarget;
509
508
  this.inLiteral = backInLiteral;
510
- if (Array.isArray(blockData)) {
511
- collection = blockData;
512
- data = collection[0];
513
- iterations = collection.length;
509
+ if (blockData && blockData[Symbol.iterator]) {
510
+ iterator = blockData[Symbol.iterator]();
511
+ iteration = iterator?.next() ?? done;
512
+ data = iteration.value;
514
513
  }
515
514
  else {
516
- collection = [];
517
515
  data = blockData;
518
- iterations = data ? 1 : 0;
516
+ iteration = { done: !data, value: data };
517
+ iterator = undefined;
519
518
  }
520
- if (!iterations) {
519
+ if (iteration.done) {
521
520
  this.skipBlock();
522
521
  continue;
523
522
  }
package/esm/template.d.ts CHANGED
@@ -1,11 +1,13 @@
1
1
  import { SortedArray } from '@itrocks/sorted-array';
2
2
  type BlockStackEntry = {
3
3
  blockStart: number;
4
- collection: any[];
5
4
  condition?: boolean;
6
5
  data: any;
7
- iteration: number;
8
- iterations: number;
6
+ iteration: IteratorResult<any> | {
7
+ done: boolean;
8
+ value?: any;
9
+ };
10
+ iterator?: Iterator<any>;
9
11
  };
10
12
  type Close = ')' | '}';
11
13
  type Final = '' | '-->';
package/esm/template.js CHANGED
@@ -3,6 +3,7 @@ import { Str } from '@itrocks/rename';
3
3
  import { SortedArray } from '@itrocks/sorted-array';
4
4
  import { readFile } from 'node:fs/promises';
5
5
  import { normalize, sep } from 'node:path';
6
+ const done = { done: true };
6
7
  export const frontScripts = new SortedArray();
7
8
  frontScripts.distinct = true;
8
9
  export class Template {
@@ -61,7 +62,7 @@ export class Template {
61
62
  this.doneLinks.distinct = true;
62
63
  this.headLinks.distinct = true;
63
64
  if (containerData) {
64
- this.blockStack.push({ blockStart: 0, collection: [], data: containerData, iteration: 0, iterations: 1 });
65
+ this.blockStack.push({ blockStart: 0, data: containerData, iteration: done });
65
66
  }
66
67
  }
67
68
  applyLiterals(text, parts = []) {
@@ -421,11 +422,10 @@ export class Template {
421
422
  }
422
423
  async parseVars() {
423
424
  let blockStart = 0;
424
- let collection = [];
425
425
  let data = this.data;
426
426
  let inHead = false;
427
- let iteration = 0;
428
- let iterations = 0;
427
+ let iteration = done;
428
+ let iterator;
429
429
  while (this.index < this.length) {
430
430
  let char = this.source[this.index];
431
431
  // expression
@@ -469,17 +469,17 @@ export class Template {
469
469
  // end condition / loop block
470
470
  if ((firstChar === 'e') && (this.source.substring(this.index, this.index + 6) === 'end-->')) {
471
471
  this.target += this.trimEndLine(this.source.substring(this.start, tagIndex));
472
- iteration++;
473
- if (iteration < iterations) {
474
- data = collection[iteration];
472
+ iteration = iterator?.next() ?? done;
473
+ if (!iteration.done) {
474
+ data = iteration.value;
475
475
  this.index = this.start = blockStart;
476
476
  if (this.inLiteral && (this.index > this.start)) {
477
477
  this.sourceToTarget();
478
478
  }
479
479
  continue;
480
480
  }
481
- ({ blockStart, collection, data, iteration, iterations } = this.blockStack.pop()
482
- ?? { blockStart: 0, collection: [], data: undefined, iteration: 0, iterations: 0 });
481
+ ({ blockStart, data, iteration, iterator } = this.blockStack.pop()
482
+ ?? { blockStart: 0, data: undefined, iteration: done });
483
483
  this.index += 6;
484
484
  this.start = this.index;
485
485
  if (this.inLiteral && (this.index > this.start)) {
@@ -498,23 +498,22 @@ export class Template {
498
498
  this.target = '';
499
499
  this.inLiteral = false;
500
500
  const condition = await this.parseExpression(data, '<', '}', '-->');
501
- this.blockStack.push({ blockStart, collection, condition, data, iteration, iterations });
501
+ this.blockStack.push({ blockStart, condition, data, iteration, iterator });
502
502
  let blockData = condition ? (this.target ? data : undefined) : this.target;
503
503
  blockStart = this.index;
504
- iteration = 0;
505
504
  this.target = backTarget;
506
505
  this.inLiteral = backInLiteral;
507
- if (Array.isArray(blockData)) {
508
- collection = blockData;
509
- data = collection[0];
510
- iterations = collection.length;
506
+ if (blockData && blockData[Symbol.iterator]) {
507
+ iterator = blockData[Symbol.iterator]();
508
+ iteration = iterator?.next() ?? done;
509
+ data = iteration.value;
511
510
  }
512
511
  else {
513
- collection = [];
514
512
  data = blockData;
515
- iterations = data ? 1 : 0;
513
+ iteration = { done: !data, value: data };
514
+ iterator = undefined;
516
515
  }
517
- if (!iterations) {
516
+ if (iteration.done) {
518
517
  this.skipBlock();
519
518
  continue;
520
519
  }
package/package.json CHANGED
@@ -69,5 +69,5 @@
69
69
  "test": "jest"
70
70
  },
71
71
  "types": "./esm/template.d.ts",
72
- "version": "0.0.35"
72
+ "version": "0.0.36"
73
73
  }