content-entry-transform 1.5.18 → 1.6.1

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,12 +18,20 @@ transform content entries
18
18
 
19
19
  ### Table of Contents
20
20
 
21
- * [createExpressionTransformer](#createexpressiontransformer)
21
+ * [createPropertiesInterceptor](#createpropertiesinterceptor)
22
22
  * [Parameters](#parameters)
23
- * [createPropertiesTransformer](#createpropertiestransformer)
23
+ * [createExpressionTransformer](#createexpressiontransformer)
24
24
  * [Parameters](#parameters-1)
25
- * [transform](#transform)
25
+ * [createPropertiesTransformer](#createpropertiestransformer)
26
26
  * [Parameters](#parameters-2)
27
+ * [transform](#transform)
28
+ * [Parameters](#parameters-3)
29
+
30
+ ## createPropertiesInterceptor
31
+
32
+ ### Parameters
33
+
34
+ * `evaluate` **[Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function)** 
27
35
 
28
36
  ## createExpressionTransformer
29
37
 
@@ -32,7 +40,7 @@ Transformer expanding '{{}}' expressions
32
40
  ### Parameters
33
41
 
34
42
  * `match` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** 
35
- * `properties` **[Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object)** 
43
+ * `properties` **([Object](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object) | [Function](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Statements/function))** 
36
44
  * `name` **[string](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/String)** (optional, default `"expression"`)
37
45
 
38
46
  ## createPropertiesTransformer
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "content-entry-transform",
3
- "version": "1.5.18",
3
+ "version": "1.6.1",
4
4
  "publishConfig": {
5
5
  "access": "public",
6
6
  "provenance": true
@@ -31,7 +31,7 @@
31
31
  "iterable-string-interceptor": "^3.0.5"
32
32
  },
33
33
  "devDependencies": {
34
- "@types/node": "^25.0.8",
34
+ "@types/node": "^25.0.9",
35
35
  "ava": "^6.4.1",
36
36
  "browser-ava": "^2.3.50",
37
37
  "c8": "^10.1.3",
@@ -1,7 +1,11 @@
1
1
  import { IteratorContentEntry } from "content-entry";
2
2
  import { iterableStringInterceptor } from "iterable-string-interceptor";
3
3
 
4
- export function createPropertiesInterceptor(properties) {
4
+ /**
5
+ *
6
+ * @param {Function} evaluate
7
+ */
8
+ export function createPropertiesInterceptor(evaluate) {
5
9
  return async function* transformer(
6
10
  expression,
7
11
  remainder,
@@ -10,13 +14,13 @@ export function createPropertiesInterceptor(properties) {
10
14
  leadIn,
11
15
  leadOut
12
16
  ) {
13
- function ev(e, deepth) {
14
- if (deepth > 9) {
17
+ function ev(e, depth) {
18
+ if (depth > 9) {
15
19
  throw new Error(`Circular reference evaluating: ${expression}`, {
16
20
  cause: expression
17
21
  });
18
22
  }
19
- let value = properties[e];
23
+ let value = evaluate(e);
20
24
  if (value !== undefined) {
21
25
  if (typeof value === "string") {
22
26
  while (true) {
@@ -25,7 +29,7 @@ export function createPropertiesInterceptor(properties) {
25
29
  const lo = value.indexOf(leadOut, li + leadIn.length);
26
30
  value =
27
31
  value.substring(0, li) +
28
- ev(value.substring(li + leadIn.length, lo), deepth + 1) +
32
+ ev(value.substring(li + leadIn.length, lo), depth + 1) +
29
33
  value.substring(lo + leadOut.length);
30
34
  } else {
31
35
  break;
@@ -36,7 +40,6 @@ export function createPropertiesInterceptor(properties) {
36
40
  } else {
37
41
  return leadIn + e + leadOut;
38
42
  }
39
- // return "";
40
43
  }
41
44
 
42
45
  yield ev(expression, 0);
@@ -46,7 +49,7 @@ export function createPropertiesInterceptor(properties) {
46
49
  /**
47
50
  * Transformer expanding '{{}}' expressions
48
51
  * @param {string} match
49
- * @param {Object} properties
52
+ * @param {Object|Function} properties
50
53
  * @param {string} name
51
54
  * @returns
52
55
  */
@@ -63,6 +66,10 @@ export function createExpressionTransformer(
63
66
  }
64
67
  }
65
68
 
69
+ const interceptor = createPropertiesInterceptor(
70
+ typeof properties === "function" ? properties : name => properties[name]
71
+ );
72
+
66
73
  return {
67
74
  name,
68
75
  match,
@@ -75,11 +82,7 @@ export function createExpressionTransformer(
75
82
  const ne = new IteratorContentEntry(
76
83
  entry.name,
77
84
  { destination: entry.destination },
78
- () =>
79
- iterableStringInterceptor(
80
- streamToText(stream),
81
- createPropertiesInterceptor(properties)
82
- )
85
+ () => iterableStringInterceptor(streamToText(stream), interceptor)
83
86
  );
84
87
  return ne;
85
88
  }