pte-interpolation-core 1.2.0 → 1.3.0
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/dist/index.cjs +65 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +23 -34
- package/dist/index.d.ts +23 -34
- package/dist/index.js +30 -15
- package/dist/index.js.map +1 -1
- package/package.json +4 -5
- package/src/__tests__/getMissingVariableKeys.test.ts +7 -7
- package/src/__tests__/interpolateToString.test.ts +9 -9
package/dist/index.cjs
CHANGED
|
@@ -1,32 +1,77 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
Object.defineProperty
|
|
3
|
-
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
VARIABLE_TYPE_PREFIX: () => VARIABLE_TYPE_PREFIX,
|
|
24
|
+
extractVariableKeys: () => extractVariableKeys,
|
|
25
|
+
getMissingVariableKeys: () => getMissingVariableKeys,
|
|
26
|
+
interpolateToString: () => interpolateToString
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(index_exports);
|
|
29
|
+
|
|
30
|
+
// src/constants.ts
|
|
31
|
+
var VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
|
|
32
|
+
|
|
33
|
+
// src/extractVariableKeys.ts
|
|
4
34
|
function extractVariableKeys(blocks) {
|
|
5
35
|
const seen = /* @__PURE__ */ new Set();
|
|
6
|
-
return blocks.reduce((keys, block) =>
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
36
|
+
return blocks.reduce((keys, block) => {
|
|
37
|
+
const children = block.children ?? [];
|
|
38
|
+
return children.reduce((accumulated, child) => {
|
|
39
|
+
if (child._type !== VARIABLE_TYPE_PREFIX) return accumulated;
|
|
40
|
+
const variableKey = child.variableKey;
|
|
41
|
+
if (typeof variableKey !== "string") return accumulated;
|
|
42
|
+
if (seen.has(variableKey)) return accumulated;
|
|
43
|
+
seen.add(variableKey);
|
|
44
|
+
return [...accumulated, variableKey];
|
|
45
|
+
}, keys);
|
|
46
|
+
}, []);
|
|
11
47
|
}
|
|
48
|
+
|
|
49
|
+
// src/getMissingVariableKeys.ts
|
|
12
50
|
function getMissingVariableKeys(blocks, values) {
|
|
13
51
|
return extractVariableKeys(blocks).filter((key) => values[key] === void 0);
|
|
14
52
|
}
|
|
53
|
+
|
|
54
|
+
// src/interpolateToString.ts
|
|
15
55
|
function defaultFallback(variableKey) {
|
|
16
56
|
return `{${variableKey}}`;
|
|
17
57
|
}
|
|
18
58
|
function interpolateToString(blocks, values, fallback = defaultFallback) {
|
|
19
|
-
return blocks.map((block) =>
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
59
|
+
return blocks.map((block) => {
|
|
60
|
+
const children = block.children ?? [];
|
|
61
|
+
return children.map((child) => {
|
|
62
|
+
if (child._type === VARIABLE_TYPE_PREFIX) {
|
|
63
|
+
const variableKey = child.variableKey;
|
|
64
|
+
return values[variableKey] !== void 0 ? values[variableKey] : fallback(variableKey);
|
|
65
|
+
}
|
|
66
|
+
return typeof child.text === "string" ? child.text : "";
|
|
67
|
+
}).join("");
|
|
68
|
+
}).join("\n");
|
|
27
69
|
}
|
|
28
|
-
|
|
29
|
-
exports
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
70
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
71
|
+
0 && (module.exports = {
|
|
72
|
+
VARIABLE_TYPE_PREFIX,
|
|
73
|
+
extractVariableKeys,
|
|
74
|
+
getMissingVariableKeys,
|
|
75
|
+
interpolateToString
|
|
76
|
+
});
|
|
77
|
+
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/constants.ts","../src/extractVariableKeys.ts","../src/getMissingVariableKeys.ts","../src/interpolateToString.ts"],"sourcesContent":["export {VARIABLE_TYPE_PREFIX} from './constants'\nexport {extractVariableKeys} from './extractVariableKeys'\nexport {getMissingVariableKeys} from './getMissingVariableKeys'\nexport {interpolateToString} from './interpolateToString'\nexport type {\n InterpolationFallback,\n InterpolationValues,\n PortableTextBlockLike,\n PortableTextChild,\n PteInterpolationVariableBlock,\n} from './types'\n","/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n","import {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {PortableTextBlockLike} from './types'\n\n/** @public */\nexport function extractVariableKeys(blocks: PortableTextBlockLike[]): string[] {\n const seen = new Set<string>()\n\n return blocks.reduce<string[]>((keys, block) => {\n const children = block.children ?? []\n\n return children.reduce((accumulated, child) => {\n if (child._type !== VARIABLE_TYPE_PREFIX) return accumulated\n\n const variableKey = child.variableKey\n if (typeof variableKey !== 'string') return accumulated\n if (seen.has(variableKey)) return accumulated\n\n seen.add(variableKey)\n return [...accumulated, variableKey]\n }, keys)\n }, [])\n}\n","import {extractVariableKeys} from './extractVariableKeys'\nimport type {InterpolationValues, PortableTextBlockLike} from './types'\n\n/** @public */\nexport function getMissingVariableKeys(\n blocks: PortableTextBlockLike[],\n values: InterpolationValues,\n): string[] {\n return extractVariableKeys(blocks).filter((key) => values[key] === undefined)\n}\n","import {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {InterpolationFallback, InterpolationValues, PortableTextBlockLike} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function interpolateToString(\n blocks: PortableTextBlockLike[],\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): string {\n return blocks\n .map((block) => {\n const children = block.children ?? []\n\n return children\n .map((child) => {\n if (child._type === VARIABLE_TYPE_PREFIX) {\n const variableKey = child.variableKey as string\n return values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n }\n\n return typeof child.text === 'string' ? child.text : ''\n })\n .join('')\n })\n .join('\\n')\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACCO,IAAM,uBAAuB;;;ACG7B,SAAS,oBAAoB,QAA2C;AAC7E,QAAM,OAAO,oBAAI,IAAY;AAE7B,SAAO,OAAO,OAAiB,CAAC,MAAM,UAAU;AAC9C,UAAM,WAAW,MAAM,YAAY,CAAC;AAEpC,WAAO,SAAS,OAAO,CAAC,aAAa,UAAU;AAC7C,UAAI,MAAM,UAAU,qBAAsB,QAAO;AAEjD,YAAM,cAAc,MAAM;AAC1B,UAAI,OAAO,gBAAgB,SAAU,QAAO;AAC5C,UAAI,KAAK,IAAI,WAAW,EAAG,QAAO;AAElC,WAAK,IAAI,WAAW;AACpB,aAAO,CAAC,GAAG,aAAa,WAAW;AAAA,IACrC,GAAG,IAAI;AAAA,EACT,GAAG,CAAC,CAAC;AACP;;;ACjBO,SAAS,uBACd,QACA,QACU;AACV,SAAO,oBAAoB,MAAM,EAAE,OAAO,CAAC,QAAQ,OAAO,GAAG,MAAM,MAAS;AAC9E;;;ACNA,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,oBACd,QACA,QACA,WAAkC,iBAC1B;AACR,SAAO,OACJ,IAAI,CAAC,UAAU;AACd,UAAM,WAAW,MAAM,YAAY,CAAC;AAEpC,WAAO,SACJ,IAAI,CAAC,UAAU;AACd,UAAI,MAAM,UAAU,sBAAsB;AACxC,cAAM,cAAc,MAAM;AAC1B,eAAO,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAAA,MACvF;AAEA,aAAO,OAAO,MAAM,SAAS,WAAW,MAAM,OAAO;AAAA,IACvD,CAAC,EACA,KAAK,EAAE;AAAA,EACZ,CAAC,EACA,KAAK,IAAI;AACd;","names":[]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,48 +1,37 @@
|
|
|
1
1
|
/** @public */
|
|
2
|
-
|
|
2
|
+
declare const VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
|
|
3
3
|
|
|
4
4
|
/** @public */
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
interface PortableTextChild {
|
|
6
|
+
_type: string;
|
|
7
|
+
_key?: string;
|
|
8
|
+
text?: string;
|
|
9
|
+
variableKey?: string;
|
|
10
|
+
}
|
|
10
11
|
/** @public */
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
interface PortableTextBlockLike {
|
|
13
|
+
_type: string;
|
|
14
|
+
_key?: string;
|
|
15
|
+
children?: PortableTextChild[];
|
|
16
|
+
}
|
|
17
17
|
/** @public */
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
interface PteInterpolationVariableBlock {
|
|
19
|
+
_type: 'pteInterpolationVariable';
|
|
20
|
+
_key: string;
|
|
21
|
+
variableKey: string;
|
|
22
|
+
}
|
|
20
23
|
/** @public */
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
type InterpolationValues = Record<string, string>;
|
|
23
25
|
/** @public */
|
|
24
|
-
|
|
25
|
-
_type: string
|
|
26
|
-
_key?: string
|
|
27
|
-
children?: PortableTextChild[]
|
|
28
|
-
}
|
|
26
|
+
type InterpolationFallback = (variableKey: string) => string;
|
|
29
27
|
|
|
30
28
|
/** @public */
|
|
31
|
-
|
|
32
|
-
_type: string
|
|
33
|
-
_key?: string
|
|
34
|
-
text?: string
|
|
35
|
-
variableKey?: string
|
|
36
|
-
}
|
|
29
|
+
declare function extractVariableKeys(blocks: PortableTextBlockLike[]): string[];
|
|
37
30
|
|
|
38
31
|
/** @public */
|
|
39
|
-
|
|
40
|
-
_type: 'pteInterpolationVariable'
|
|
41
|
-
_key: string
|
|
42
|
-
variableKey: string
|
|
43
|
-
}
|
|
32
|
+
declare function getMissingVariableKeys(blocks: PortableTextBlockLike[], values: InterpolationValues): string[];
|
|
44
33
|
|
|
45
34
|
/** @public */
|
|
46
|
-
|
|
35
|
+
declare function interpolateToString(blocks: PortableTextBlockLike[], values: InterpolationValues, fallback?: InterpolationFallback): string;
|
|
47
36
|
|
|
48
|
-
export {}
|
|
37
|
+
export { type InterpolationFallback, type InterpolationValues, type PortableTextBlockLike, type PortableTextChild, type PteInterpolationVariableBlock, VARIABLE_TYPE_PREFIX, extractVariableKeys, getMissingVariableKeys, interpolateToString };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,48 +1,37 @@
|
|
|
1
1
|
/** @public */
|
|
2
|
-
|
|
2
|
+
declare const VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
|
|
3
3
|
|
|
4
4
|
/** @public */
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
5
|
+
interface PortableTextChild {
|
|
6
|
+
_type: string;
|
|
7
|
+
_key?: string;
|
|
8
|
+
text?: string;
|
|
9
|
+
variableKey?: string;
|
|
10
|
+
}
|
|
10
11
|
/** @public */
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
interface PortableTextBlockLike {
|
|
13
|
+
_type: string;
|
|
14
|
+
_key?: string;
|
|
15
|
+
children?: PortableTextChild[];
|
|
16
|
+
}
|
|
17
17
|
/** @public */
|
|
18
|
-
|
|
19
|
-
|
|
18
|
+
interface PteInterpolationVariableBlock {
|
|
19
|
+
_type: 'pteInterpolationVariable';
|
|
20
|
+
_key: string;
|
|
21
|
+
variableKey: string;
|
|
22
|
+
}
|
|
20
23
|
/** @public */
|
|
21
|
-
|
|
22
|
-
|
|
24
|
+
type InterpolationValues = Record<string, string>;
|
|
23
25
|
/** @public */
|
|
24
|
-
|
|
25
|
-
_type: string
|
|
26
|
-
_key?: string
|
|
27
|
-
children?: PortableTextChild[]
|
|
28
|
-
}
|
|
26
|
+
type InterpolationFallback = (variableKey: string) => string;
|
|
29
27
|
|
|
30
28
|
/** @public */
|
|
31
|
-
|
|
32
|
-
_type: string
|
|
33
|
-
_key?: string
|
|
34
|
-
text?: string
|
|
35
|
-
variableKey?: string
|
|
36
|
-
}
|
|
29
|
+
declare function extractVariableKeys(blocks: PortableTextBlockLike[]): string[];
|
|
37
30
|
|
|
38
31
|
/** @public */
|
|
39
|
-
|
|
40
|
-
_type: 'pteInterpolationVariable'
|
|
41
|
-
_key: string
|
|
42
|
-
variableKey: string
|
|
43
|
-
}
|
|
32
|
+
declare function getMissingVariableKeys(blocks: PortableTextBlockLike[], values: InterpolationValues): string[];
|
|
44
33
|
|
|
45
34
|
/** @public */
|
|
46
|
-
|
|
35
|
+
declare function interpolateToString(blocks: PortableTextBlockLike[], values: InterpolationValues, fallback?: InterpolationFallback): string;
|
|
47
36
|
|
|
48
|
-
export {}
|
|
37
|
+
export { type InterpolationFallback, type InterpolationValues, type PortableTextBlockLike, type PortableTextChild, type PteInterpolationVariableBlock, VARIABLE_TYPE_PREFIX, extractVariableKeys, getMissingVariableKeys, interpolateToString };
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,42 @@
|
|
|
1
|
-
|
|
1
|
+
// src/constants.ts
|
|
2
|
+
var VARIABLE_TYPE_PREFIX = "pteInterpolationVariable";
|
|
3
|
+
|
|
4
|
+
// src/extractVariableKeys.ts
|
|
2
5
|
function extractVariableKeys(blocks) {
|
|
3
6
|
const seen = /* @__PURE__ */ new Set();
|
|
4
|
-
return blocks.reduce((keys, block) =>
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
7
|
+
return blocks.reduce((keys, block) => {
|
|
8
|
+
const children = block.children ?? [];
|
|
9
|
+
return children.reduce((accumulated, child) => {
|
|
10
|
+
if (child._type !== VARIABLE_TYPE_PREFIX) return accumulated;
|
|
11
|
+
const variableKey = child.variableKey;
|
|
12
|
+
if (typeof variableKey !== "string") return accumulated;
|
|
13
|
+
if (seen.has(variableKey)) return accumulated;
|
|
14
|
+
seen.add(variableKey);
|
|
15
|
+
return [...accumulated, variableKey];
|
|
16
|
+
}, keys);
|
|
17
|
+
}, []);
|
|
9
18
|
}
|
|
19
|
+
|
|
20
|
+
// src/getMissingVariableKeys.ts
|
|
10
21
|
function getMissingVariableKeys(blocks, values) {
|
|
11
22
|
return extractVariableKeys(blocks).filter((key) => values[key] === void 0);
|
|
12
23
|
}
|
|
24
|
+
|
|
25
|
+
// src/interpolateToString.ts
|
|
13
26
|
function defaultFallback(variableKey) {
|
|
14
27
|
return `{${variableKey}}`;
|
|
15
28
|
}
|
|
16
29
|
function interpolateToString(blocks, values, fallback = defaultFallback) {
|
|
17
|
-
return blocks.map((block) =>
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
30
|
+
return blocks.map((block) => {
|
|
31
|
+
const children = block.children ?? [];
|
|
32
|
+
return children.map((child) => {
|
|
33
|
+
if (child._type === VARIABLE_TYPE_PREFIX) {
|
|
34
|
+
const variableKey = child.variableKey;
|
|
35
|
+
return values[variableKey] !== void 0 ? values[variableKey] : fallback(variableKey);
|
|
36
|
+
}
|
|
37
|
+
return typeof child.text === "string" ? child.text : "";
|
|
38
|
+
}).join("");
|
|
39
|
+
}).join("\n");
|
|
25
40
|
}
|
|
26
41
|
export {
|
|
27
42
|
VARIABLE_TYPE_PREFIX,
|
|
@@ -29,4 +44,4 @@ export {
|
|
|
29
44
|
getMissingVariableKeys,
|
|
30
45
|
interpolateToString
|
|
31
46
|
};
|
|
32
|
-
//# sourceMappingURL=index.js.map
|
|
47
|
+
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/constants.ts","../src/extractVariableKeys.ts","../src/getMissingVariableKeys.ts","../src/interpolateToString.ts"],"sourcesContent":["/** @public */\nexport const VARIABLE_TYPE_PREFIX = 'pteInterpolationVariable'\n","import {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {PortableTextBlockLike} from './types'\n\n/** @public */\nexport function extractVariableKeys(blocks: PortableTextBlockLike[]): string[] {\n const seen = new Set<string>()\n\n return blocks.reduce<string[]>((keys, block) => {\n const children = block.children ?? []\n\n return children.reduce((accumulated, child) => {\n if (child._type !== VARIABLE_TYPE_PREFIX) return accumulated\n\n const variableKey = child.variableKey\n if (typeof variableKey !== 'string') return accumulated\n if (seen.has(variableKey)) return accumulated\n\n seen.add(variableKey)\n return [...accumulated, variableKey]\n }, keys)\n }, [])\n}\n","import {extractVariableKeys} from './extractVariableKeys'\nimport type {InterpolationValues, PortableTextBlockLike} from './types'\n\n/** @public */\nexport function getMissingVariableKeys(\n blocks: PortableTextBlockLike[],\n values: InterpolationValues,\n): string[] {\n return extractVariableKeys(blocks).filter((key) => values[key] === undefined)\n}\n","import {VARIABLE_TYPE_PREFIX} from './constants'\nimport type {InterpolationFallback, InterpolationValues, PortableTextBlockLike} from './types'\n\nfunction defaultFallback(variableKey: string): string {\n return `{${variableKey}}`\n}\n\n/** @public */\nexport function interpolateToString(\n blocks: PortableTextBlockLike[],\n values: InterpolationValues,\n fallback: InterpolationFallback = defaultFallback,\n): string {\n return blocks\n .map((block) => {\n const children = block.children ?? []\n\n return children\n .map((child) => {\n if (child._type === VARIABLE_TYPE_PREFIX) {\n const variableKey = child.variableKey as string\n return values[variableKey] !== undefined ? values[variableKey] : fallback(variableKey)\n }\n\n return typeof child.text === 'string' ? child.text : ''\n })\n .join('')\n })\n .join('\\n')\n}\n"],"mappings":";AACO,IAAM,uBAAuB;;;ACG7B,SAAS,oBAAoB,QAA2C;AAC7E,QAAM,OAAO,oBAAI,IAAY;AAE7B,SAAO,OAAO,OAAiB,CAAC,MAAM,UAAU;AAC9C,UAAM,WAAW,MAAM,YAAY,CAAC;AAEpC,WAAO,SAAS,OAAO,CAAC,aAAa,UAAU;AAC7C,UAAI,MAAM,UAAU,qBAAsB,QAAO;AAEjD,YAAM,cAAc,MAAM;AAC1B,UAAI,OAAO,gBAAgB,SAAU,QAAO;AAC5C,UAAI,KAAK,IAAI,WAAW,EAAG,QAAO;AAElC,WAAK,IAAI,WAAW;AACpB,aAAO,CAAC,GAAG,aAAa,WAAW;AAAA,IACrC,GAAG,IAAI;AAAA,EACT,GAAG,CAAC,CAAC;AACP;;;ACjBO,SAAS,uBACd,QACA,QACU;AACV,SAAO,oBAAoB,MAAM,EAAE,OAAO,CAAC,QAAQ,OAAO,GAAG,MAAM,MAAS;AAC9E;;;ACNA,SAAS,gBAAgB,aAA6B;AACpD,SAAO,IAAI,WAAW;AACxB;AAGO,SAAS,oBACd,QACA,QACA,WAAkC,iBAC1B;AACR,SAAO,OACJ,IAAI,CAAC,UAAU;AACd,UAAM,WAAW,MAAM,YAAY,CAAC;AAEpC,WAAO,SACJ,IAAI,CAAC,UAAU;AACd,UAAI,MAAM,UAAU,sBAAsB;AACxC,cAAM,cAAc,MAAM;AAC1B,eAAO,OAAO,WAAW,MAAM,SAAY,OAAO,WAAW,IAAI,SAAS,WAAW;AAAA,MACvF;AAEA,aAAO,OAAO,MAAM,SAAS,WAAW,MAAM,OAAO;AAAA,IACvD,CAAC,EACA,KAAK,EAAE;AAAA,EACZ,CAAC,EACA,KAAK,IAAI;AACd;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pte-interpolation-core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"description": "Framework-agnostic utilities for Portable Text variable interpolation",
|
|
@@ -34,19 +34,18 @@
|
|
|
34
34
|
"src",
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
|
-
"browserslist": "extends @sanity/browserslist-config",
|
|
38
37
|
"publishConfig": {
|
|
39
38
|
"access": "public"
|
|
40
39
|
},
|
|
41
40
|
"devDependencies": {
|
|
42
|
-
"
|
|
41
|
+
"tsup": "^8.5.0",
|
|
43
42
|
"rimraf": "^6.1.3",
|
|
44
43
|
"typescript": "~5.9.3"
|
|
45
44
|
},
|
|
46
45
|
"scripts": {
|
|
47
|
-
"build": "
|
|
46
|
+
"build": "tsup",
|
|
48
47
|
"clean": "rimraf dist",
|
|
49
|
-
"link-watch": "
|
|
48
|
+
"link-watch": "tsup --watch",
|
|
50
49
|
"type-check": "tsc --noEmit",
|
|
51
50
|
"yalc:publish": "npx yalc publish"
|
|
52
51
|
}
|
|
@@ -22,14 +22,14 @@ describe('getMissingVariableKeys', () => {
|
|
|
22
22
|
})
|
|
23
23
|
|
|
24
24
|
it('returns empty array when all variables provided', () => {
|
|
25
|
-
expect(getMissingVariableKeys(singleVariableBlock, {firstName: '
|
|
25
|
+
expect(getMissingVariableKeys(singleVariableBlock, {firstName: 'Patrick'})).toEqual([])
|
|
26
26
|
})
|
|
27
27
|
|
|
28
28
|
it('returns empty array when all multiple variables provided', () => {
|
|
29
29
|
expect(
|
|
30
30
|
getMissingVariableKeys(multipleVariablesBlock, {
|
|
31
|
-
firstName: '
|
|
32
|
-
lastName: '
|
|
31
|
+
firstName: 'Patrick',
|
|
32
|
+
lastName: 'Pickles',
|
|
33
33
|
email: 'a@b.com',
|
|
34
34
|
}),
|
|
35
35
|
).toEqual([])
|
|
@@ -40,7 +40,7 @@ describe('getMissingVariableKeys', () => {
|
|
|
40
40
|
})
|
|
41
41
|
|
|
42
42
|
it('returns only missing keys when some values provided', () => {
|
|
43
|
-
expect(getMissingVariableKeys(multipleVariablesBlock, {firstName: '
|
|
43
|
+
expect(getMissingVariableKeys(multipleVariablesBlock, {firstName: 'Patrick'})).toEqual([
|
|
44
44
|
'lastName',
|
|
45
45
|
'email',
|
|
46
46
|
])
|
|
@@ -51,7 +51,7 @@ describe('getMissingVariableKeys', () => {
|
|
|
51
51
|
})
|
|
52
52
|
|
|
53
53
|
it('returns missing keys across multiple blocks', () => {
|
|
54
|
-
expect(getMissingVariableKeys(multiBlockContent, {firstName: '
|
|
54
|
+
expect(getMissingVariableKeys(multiBlockContent, {firstName: 'Patrick'})).toEqual(['email'])
|
|
55
55
|
})
|
|
56
56
|
|
|
57
57
|
it('deduplicates missing keys', () => {
|
|
@@ -67,14 +67,14 @@ describe('getMissingVariableKeys', () => {
|
|
|
67
67
|
})
|
|
68
68
|
|
|
69
69
|
it('returns missing keys for consecutive variables', () => {
|
|
70
|
-
expect(getMissingVariableKeys(consecutiveVariablesBlock, {firstName: '
|
|
70
|
+
expect(getMissingVariableKeys(consecutiveVariablesBlock, {firstName: 'Patrick'})).toEqual([
|
|
71
71
|
'lastName',
|
|
72
72
|
])
|
|
73
73
|
})
|
|
74
74
|
|
|
75
75
|
it('ignores extra values not in blocks', () => {
|
|
76
76
|
expect(
|
|
77
|
-
getMissingVariableKeys(singleVariableBlock, {firstName: '
|
|
77
|
+
getMissingVariableKeys(singleVariableBlock, {firstName: 'Patrick', extraKey: 'ignored'}),
|
|
78
78
|
).toEqual([])
|
|
79
79
|
})
|
|
80
80
|
|
|
@@ -19,7 +19,7 @@ describe('interpolateToString', () => {
|
|
|
19
19
|
})
|
|
20
20
|
|
|
21
21
|
it('resolves a single variable from values', () => {
|
|
22
|
-
expect(interpolateToString(singleVariableBlock, {firstName: '
|
|
22
|
+
expect(interpolateToString(singleVariableBlock, {firstName: 'Patrick'})).toBe('Hello, Patrick!')
|
|
23
23
|
})
|
|
24
24
|
|
|
25
25
|
it('uses default fallback for missing variables', () => {
|
|
@@ -37,23 +37,23 @@ describe('interpolateToString', () => {
|
|
|
37
37
|
|
|
38
38
|
it('resolves consecutive variables', () => {
|
|
39
39
|
expect(
|
|
40
|
-
interpolateToString(consecutiveVariablesBlock, {firstName: '
|
|
41
|
-
).toBe('
|
|
40
|
+
interpolateToString(consecutiveVariablesBlock, {firstName: 'Patrick', lastName: 'Pickles'}),
|
|
41
|
+
).toBe('PatrickPickles')
|
|
42
42
|
})
|
|
43
43
|
|
|
44
44
|
it('joins multiple blocks with newlines', () => {
|
|
45
45
|
expect(
|
|
46
|
-
interpolateToString(multiBlockContent, {firstName: '
|
|
47
|
-
).toBe('Dear
|
|
46
|
+
interpolateToString(multiBlockContent, {firstName: 'Patrick', email: 'patrick@example.com'}),
|
|
47
|
+
).toBe('Dear Patrick,\nYour email is patrick@example.com.')
|
|
48
48
|
})
|
|
49
49
|
|
|
50
50
|
it('resolves multiple variables in one block', () => {
|
|
51
51
|
expect(
|
|
52
52
|
interpolateToString(multipleVariablesBlock, {
|
|
53
|
-
firstName: '
|
|
54
|
-
lastName: '
|
|
55
|
-
email: '
|
|
53
|
+
firstName: 'Patrick',
|
|
54
|
+
lastName: 'Pickles',
|
|
55
|
+
email: 'patrick@example.com',
|
|
56
56
|
}),
|
|
57
|
-
).toBe('Name:
|
|
57
|
+
).toBe('Name: Patrick Pickles, Email: patrick@example.com')
|
|
58
58
|
})
|
|
59
59
|
})
|