@narrative.io/jsonforms-provider-protocols 1.2.0-beta.1 → 1.2.0-beta.2

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.
@@ -17,37 +17,46 @@ function flattenTransform(items, config) {
17
17
  const flattenConfig = config;
18
18
  const { key, labelFormat } = flattenConfig;
19
19
  const flattened = [];
20
- for (const item of items) {
21
- if (typeof item !== "object" || item === null) continue;
20
+ function flattenRecursive(item, parent = null, depth = 0) {
21
+ if (typeof item !== "object" || item === null) return;
22
22
  const itemObj = item;
23
+ if (labelFormat && parent) {
24
+ const formattedItem = { ...itemObj };
25
+ let formattedLabel = labelFormat;
26
+ formattedLabel = formattedLabel.replace(
27
+ /\{parent\.(\w+)\}/g,
28
+ (_, prop) => String(parent[prop] ?? "")
29
+ );
30
+ formattedLabel = formattedLabel.replace(
31
+ /\{(\w+)\}/g,
32
+ (_, prop) => String(itemObj[prop] ?? "")
33
+ );
34
+ formattedItem._formattedLabel = formattedLabel;
35
+ formattedItem._parent = parent;
36
+ formattedItem._depth = depth;
37
+ flattened.push(formattedItem);
38
+ } else if (parent) {
39
+ flattened.push({
40
+ ...itemObj,
41
+ _parent: parent,
42
+ _depth: depth
43
+ });
44
+ } else {
45
+ flattened.push({
46
+ ...itemObj,
47
+ _depth: depth
48
+ });
49
+ }
23
50
  const children = itemObj[key];
24
51
  if (Array.isArray(children)) {
25
52
  for (const child of children) {
26
- if (typeof child !== "object" || child === null) continue;
27
- const childObj = child;
28
- if (labelFormat) {
29
- const formattedChild = { ...childObj };
30
- let formattedLabel = labelFormat;
31
- formattedLabel = formattedLabel.replace(
32
- /\{parent\.(\w+)\}/g,
33
- (_, prop) => String(itemObj[prop] ?? "")
34
- );
35
- formattedLabel = formattedLabel.replace(
36
- /\{(\w+)\}/g,
37
- (_, prop) => String(childObj[prop] ?? "")
38
- );
39
- formattedChild._formattedLabel = formattedLabel;
40
- formattedChild._parent = itemObj;
41
- flattened.push(formattedChild);
42
- } else {
43
- flattened.push({
44
- ...childObj,
45
- _parent: itemObj
46
- });
47
- }
53
+ flattenRecursive(child, itemObj, depth + 1);
48
54
  }
49
55
  }
50
56
  }
57
+ for (const item of items) {
58
+ flattenRecursive(item, null, 0);
59
+ }
51
60
  return flattened;
52
61
  }
53
62
  registerTransform("flatten", flattenTransform);
@@ -1 +1 @@
1
- {"version":3,"file":"transforms.js","sources":["../../src/core/transforms.ts"],"sourcesContent":["/**\n * Transform pipeline system for manipulating API response data\n * Transforms are applied sequentially in the order they appear in the pipeline\n */\n\nexport interface Transform {\n name: string;\n [key: string]: unknown;\n}\n\nexport interface FlattenTransform extends Transform {\n name: \"flatten\";\n key: string; // The key containing the nested array to flatten\n labelFormat?: string; // Optional format string like \"{parent.name} → {name}\"\n}\n\nexport type TransformStep = FlattenTransform;\n\nexport type TransformPipeline = TransformStep[];\n\n/**\n * Registry of transform functions\n */\ntype TransformFunction = (items: unknown[], config: Transform) => unknown[];\n\nconst transformRegistry: Record<string, TransformFunction> = {};\n\n/**\n * Register a transform function\n */\nexport function registerTransform(name: string, fn: TransformFunction): void {\n transformRegistry[name] = fn;\n}\n\n/**\n * Apply a pipeline of transforms to data\n */\nexport function applyTransformPipeline(\n items: unknown[],\n pipeline: TransformPipeline,\n): unknown[] {\n let result = items;\n\n for (const transform of pipeline) {\n const fn = transformRegistry[transform.name];\n if (!fn) {\n throw new Error(`Unknown transform: ${transform.name}`);\n }\n result = fn(result, transform);\n }\n\n return result;\n}\n\n/**\n * Flatten transform - flattens nested arrays into a single level\n */\nfunction flattenTransform(items: unknown[], config: Transform): unknown[] {\n const flattenConfig = config as FlattenTransform;\n const { key, labelFormat } = flattenConfig;\n const flattened: unknown[] = [];\n\n for (const item of items) {\n if (typeof item !== \"object\" || item === null) continue;\n\n const itemObj = item as Record<string, unknown>;\n const children = itemObj[key];\n\n if (Array.isArray(children)) {\n for (const child of children) {\n if (typeof child !== \"object\" || child === null) continue;\n\n const childObj = child as Record<string, unknown>;\n\n // If labelFormat is provided, use it to format the label\n if (labelFormat) {\n const formattedChild = { ...childObj };\n\n // Replace placeholders like {parent.name} and {name}\n let formattedLabel = labelFormat;\n formattedLabel = formattedLabel.replace(\n /\\{parent\\.(\\w+)\\}/g,\n (_, prop) => String(itemObj[prop] ?? \"\"),\n );\n formattedLabel = formattedLabel.replace(/\\{(\\w+)\\}/g, (_, prop) =>\n String(childObj[prop] ?? \"\"),\n );\n\n formattedChild._formattedLabel = formattedLabel;\n formattedChild._parent = itemObj;\n flattened.push(formattedChild);\n } else {\n // Just add parent reference\n flattened.push({\n ...childObj,\n _parent: itemObj,\n });\n }\n }\n }\n }\n\n return flattened;\n}\n\n// Register built-in transforms\nregisterTransform(\"flatten\", flattenTransform);\n"],"names":[],"mappings":"AAyBA,MAAM,oBAAuD,CAAA;AAKtD,SAAS,kBAAkB,MAAc,IAA6B;AAC3E,oBAAkB,IAAI,IAAI;AAC5B;AAKO,SAAS,uBACd,OACA,UACW;AACX,MAAI,SAAS;AAEb,aAAW,aAAa,UAAU;AAChC,UAAM,KAAK,kBAAkB,UAAU,IAAI;AAC3C,QAAI,CAAC,IAAI;AACP,YAAM,IAAI,MAAM,sBAAsB,UAAU,IAAI,EAAE;AAAA,IACxD;AACA,aAAS,GAAG,QAAQ,SAAS;AAAA,EAC/B;AAEA,SAAO;AACT;AAKA,SAAS,iBAAiB,OAAkB,QAA8B;AACxE,QAAM,gBAAgB;AACtB,QAAM,EAAE,KAAK,YAAA,IAAgB;AAC7B,QAAM,YAAuB,CAAA;AAE7B,aAAW,QAAQ,OAAO;AACxB,QAAI,OAAO,SAAS,YAAY,SAAS,KAAM;AAE/C,UAAM,UAAU;AAChB,UAAM,WAAW,QAAQ,GAAG;AAE5B,QAAI,MAAM,QAAQ,QAAQ,GAAG;AAC3B,iBAAW,SAAS,UAAU;AAC5B,YAAI,OAAO,UAAU,YAAY,UAAU,KAAM;AAEjD,cAAM,WAAW;AAGjB,YAAI,aAAa;AACf,gBAAM,iBAAiB,EAAE,GAAG,SAAA;AAG5B,cAAI,iBAAiB;AACrB,2BAAiB,eAAe;AAAA,YAC9B;AAAA,YACA,CAAC,GAAG,SAAS,OAAO,QAAQ,IAAI,KAAK,EAAE;AAAA,UAAA;AAEzC,2BAAiB,eAAe;AAAA,YAAQ;AAAA,YAAc,CAAC,GAAG,SACxD,OAAO,SAAS,IAAI,KAAK,EAAE;AAAA,UAAA;AAG7B,yBAAe,kBAAkB;AACjC,yBAAe,UAAU;AACzB,oBAAU,KAAK,cAAc;AAAA,QAC/B,OAAO;AAEL,oBAAU,KAAK;AAAA,YACb,GAAG;AAAA,YACH,SAAS;AAAA,UAAA,CACV;AAAA,QACH;AAAA,MACF;AAAA,IACF;AAAA,EACF;AAEA,SAAO;AACT;AAGA,kBAAkB,WAAW,gBAAgB;"}
1
+ {"version":3,"file":"transforms.js","sources":["../../src/core/transforms.ts"],"sourcesContent":["/**\n * Transform pipeline system for manipulating API response data\n * Transforms are applied sequentially in the order they appear in the pipeline\n */\n\nexport interface Transform {\n name: string;\n [key: string]: unknown;\n}\n\nexport interface FlattenTransform extends Transform {\n name: \"flatten\";\n key: string; // The key containing the nested array to flatten\n labelFormat?: string; // Optional format string like \"{parent.name} → {name}\"\n}\n\nexport type TransformStep = FlattenTransform;\n\nexport type TransformPipeline = TransformStep[];\n\n/**\n * Registry of transform functions\n */\ntype TransformFunction = (items: unknown[], config: Transform) => unknown[];\n\nconst transformRegistry: Record<string, TransformFunction> = {};\n\n/**\n * Register a transform function\n */\nexport function registerTransform(name: string, fn: TransformFunction): void {\n transformRegistry[name] = fn;\n}\n\n/**\n * Apply a pipeline of transforms to data\n */\nexport function applyTransformPipeline(\n items: unknown[],\n pipeline: TransformPipeline,\n): unknown[] {\n let result = items;\n\n for (const transform of pipeline) {\n const fn = transformRegistry[transform.name];\n if (!fn) {\n throw new Error(`Unknown transform: ${transform.name}`);\n }\n result = fn(result, transform);\n }\n\n return result;\n}\n\n/**\n * Flatten transform - recursively flattens nested arrays into a single level\n */\nfunction flattenTransform(items: unknown[], config: Transform): unknown[] {\n const flattenConfig = config as FlattenTransform;\n const { key, labelFormat } = flattenConfig;\n const flattened: unknown[] = [];\n\n function flattenRecursive(\n item: unknown,\n parent: Record<string, unknown> | null = null,\n depth: number = 0,\n ): void {\n if (typeof item !== \"object\" || item === null) return;\n\n const itemObj = item as Record<string, unknown>;\n\n // Add the current item\n if (labelFormat && parent) {\n const formattedItem = { ...itemObj };\n\n // Replace placeholders like {parent.name} and {name}\n let formattedLabel = labelFormat;\n formattedLabel = formattedLabel.replace(/\\{parent\\.(\\w+)\\}/g, (_, prop) =>\n String(parent[prop] ?? \"\"),\n );\n formattedLabel = formattedLabel.replace(/\\{(\\w+)\\}/g, (_, prop) =>\n String(itemObj[prop] ?? \"\"),\n );\n\n formattedItem._formattedLabel = formattedLabel;\n formattedItem._parent = parent;\n formattedItem._depth = depth;\n flattened.push(formattedItem);\n } else if (parent) {\n // Child node with parent reference\n flattened.push({\n ...itemObj,\n _parent: parent,\n _depth: depth,\n });\n } else {\n // Root node\n flattened.push({\n ...itemObj,\n _depth: depth,\n });\n }\n\n // Recursively flatten children\n const children = itemObj[key];\n if (Array.isArray(children)) {\n for (const child of children) {\n flattenRecursive(child, itemObj, depth + 1);\n }\n }\n }\n\n // Start flattening from root items\n for (const item of items) {\n flattenRecursive(item, null, 0);\n }\n\n return flattened;\n}\n\n// Register built-in transforms\nregisterTransform(\"flatten\", flattenTransform);\n"],"names":[],"mappings":"AAyBA,MAAM,oBAAuD,CAAA;AAKtD,SAAS,kBAAkB,MAAc,IAA6B;AAC3E,oBAAkB,IAAI,IAAI;AAC5B;AAKO,SAAS,uBACd,OACA,UACW;AACX,MAAI,SAAS;AAEb,aAAW,aAAa,UAAU;AAChC,UAAM,KAAK,kBAAkB,UAAU,IAAI;AAC3C,QAAI,CAAC,IAAI;AACP,YAAM,IAAI,MAAM,sBAAsB,UAAU,IAAI,EAAE;AAAA,IACxD;AACA,aAAS,GAAG,QA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provider-protocols",
3
- "version": "1.2.0-beta.1",
3
+ "version": "1.2.0-beta.2",
4
4
  "description": "Dynamic data provider capabilities for JSONForms with Vue 3 integration",
5
5
  "type": "module",
6
6
  "author": "Narrative I/O",
@@ -53,53 +53,68 @@ export function applyTransformPipeline(
53
53
  }
54
54
 
55
55
  /**
56
- * Flatten transform - flattens nested arrays into a single level
56
+ * Flatten transform - recursively flattens nested arrays into a single level
57
57
  */
58
58
  function flattenTransform(items: unknown[], config: Transform): unknown[] {
59
59
  const flattenConfig = config as FlattenTransform;
60
60
  const { key, labelFormat } = flattenConfig;
61
61
  const flattened: unknown[] = [];
62
62
 
63
- for (const item of items) {
64
- if (typeof item !== "object" || item === null) continue;
63
+ function flattenRecursive(
64
+ item: unknown,
65
+ parent: Record<string, unknown> | null = null,
66
+ depth: number = 0,
67
+ ): void {
68
+ if (typeof item !== "object" || item === null) return;
65
69
 
66
70
  const itemObj = item as Record<string, unknown>;
67
- const children = itemObj[key];
68
71
 
72
+ // Add the current item
73
+ if (labelFormat && parent) {
74
+ const formattedItem = { ...itemObj };
75
+
76
+ // Replace placeholders like {parent.name} and {name}
77
+ let formattedLabel = labelFormat;
78
+ formattedLabel = formattedLabel.replace(/\{parent\.(\w+)\}/g, (_, prop) =>
79
+ String(parent[prop] ?? ""),
80
+ );
81
+ formattedLabel = formattedLabel.replace(/\{(\w+)\}/g, (_, prop) =>
82
+ String(itemObj[prop] ?? ""),
83
+ );
84
+
85
+ formattedItem._formattedLabel = formattedLabel;
86
+ formattedItem._parent = parent;
87
+ formattedItem._depth = depth;
88
+ flattened.push(formattedItem);
89
+ } else if (parent) {
90
+ // Child node with parent reference
91
+ flattened.push({
92
+ ...itemObj,
93
+ _parent: parent,
94
+ _depth: depth,
95
+ });
96
+ } else {
97
+ // Root node
98
+ flattened.push({
99
+ ...itemObj,
100
+ _depth: depth,
101
+ });
102
+ }
103
+
104
+ // Recursively flatten children
105
+ const children = itemObj[key];
69
106
  if (Array.isArray(children)) {
70
107
  for (const child of children) {
71
- if (typeof child !== "object" || child === null) continue;
72
-
73
- const childObj = child as Record<string, unknown>;
74
-
75
- // If labelFormat is provided, use it to format the label
76
- if (labelFormat) {
77
- const formattedChild = { ...childObj };
78
-
79
- // Replace placeholders like {parent.name} and {name}
80
- let formattedLabel = labelFormat;
81
- formattedLabel = formattedLabel.replace(
82
- /\{parent\.(\w+)\}/g,
83
- (_, prop) => String(itemObj[prop] ?? ""),
84
- );
85
- formattedLabel = formattedLabel.replace(/\{(\w+)\}/g, (_, prop) =>
86
- String(childObj[prop] ?? ""),
87
- );
88
-
89
- formattedChild._formattedLabel = formattedLabel;
90
- formattedChild._parent = itemObj;
91
- flattened.push(formattedChild);
92
- } else {
93
- // Just add parent reference
94
- flattened.push({
95
- ...childObj,
96
- _parent: itemObj,
97
- });
98
- }
108
+ flattenRecursive(child, itemObj, depth + 1);
99
109
  }
100
110
  }
101
111
  }
102
112
 
113
+ // Start flattening from root items
114
+ for (const item of items) {
115
+ flattenRecursive(item, null, 0);
116
+ }
117
+
103
118
  return flattened;
104
119
  }
105
120