resobjectify 2.0.1 → 2.1.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/dist/index.d.ts CHANGED
@@ -1,23 +1,3 @@
1
- import type { FieldsBuilder, Fields, Prettify, Row } from "./types";
2
- export type { Fields, FieldsBuilder } from "./types";
3
- export declare function fieldsBuilder<R = Row, T = Row>(): FieldsBuilder<R, T>;
4
- export declare function objectify<R = unknown, T = Row>(
5
- data: T[],
6
- fields: Fields<R, T>,
7
- object: true,
8
- ): Record<PropertyKey, Prettify<R>>;
9
- export declare function objectify<R = unknown, T = Row>(
10
- data: T[],
11
- fields: Fields<R, T>,
12
- object?: false,
13
- ): Prettify<R>[];
14
- export declare function objectify<R = unknown>(
15
- data: Row[],
16
- fields: Fields<R>,
17
- object: true,
18
- ): Record<PropertyKey, Prettify<R>>;
19
- export declare function objectify<R = unknown>(
20
- data: Row[],
21
- fields: Fields<R>,
22
- object?: false,
23
- ): Prettify<R>[];
1
+ export * from "./src/fieldsBuilder";
2
+ export * from "./src/objectify";
3
+ export type { Field } from "./types";
package/dist/index.js CHANGED
@@ -1,153 +1,18 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.fieldsBuilder = fieldsBuilder;
4
- exports.objectify = objectify;
5
- function fieldsBuilder() {
6
- const fields = [];
7
- function newField(key, as, options) {
8
- if (!as && !options) {
9
- return key;
10
- }
11
- let entry = typeof key === "object" ? key : { key };
12
- if (as !== undefined) {
13
- entry = { ...entry, as: as };
14
- }
15
- if (options !== undefined) {
16
- entry = { ...entry, ...options };
17
- }
18
- return entry;
19
- }
20
- function newGroup(groupField, options) {
21
- if (!options) {
22
- return groupField;
23
- }
24
- let entry = typeof groupField === "object" ? groupField : { name: groupField };
25
- if (options) {
26
- entry = { ...entry, ...options };
27
- }
28
- return entry;
29
- }
30
- const field = (field, asOrOptions, options) => {
31
- const isOptions = typeof asOrOptions === "object"
32
- && asOrOptions !== null
33
- && "json" in asOrOptions;
34
- const resolvedAs = isOptions ? undefined : asOrOptions;
35
- const resolvedOptions = isOptions ? asOrOptions : options;
36
- //Create the new field entry
37
- const entry = newField(field, resolvedAs, resolvedOptions);
38
- fields.push(entry);
39
- return api;
40
- };
41
- const group = (name, optionsOrBuild, build) => {
42
- const options = typeof optionsOrBuild === "object" ? optionsOrBuild : undefined;
43
- const buildFn = typeof optionsOrBuild === "function" ? optionsOrBuild : build;
44
- if (!buildFn) {
45
- throw new Error("Group builder requires a builder callback.");
46
- }
47
- const nested = buildFn(fieldsBuilder()).build();
48
- const groupField = newGroup(name, options);
49
- fields.push([groupField, nested]);
50
- return api;
51
- };
52
- const build = () => {
53
- if (fields.length === 0 || Array.isArray(fields[0])) {
54
- throw new Error("Fields builder requires the first field to be a key field.");
55
- }
56
- return fields;
57
- };
58
- const api = { field, group, build };
59
- return api;
60
- }
61
- function objectify(data, fields, object = false) {
62
- // If the fields is a single field or object is false, group the result in an array, otherwise group the result in an object
63
- const result = fields.length === 1 || !object ? [] : {};
64
- const [keyField, ...restFields] = fields;
65
- const key = getKeyField(keyField);
66
- const name = getFieldName(keyField);
67
- // Pre-group by the current key so each recursion only sees its parent slice,
68
- // which removes the need for parent checks or duplicate tracking.
69
- const groups = groupByKey(data, key);
70
- for (const [keyValue, rows] of groups) {
71
- const row = rows[0];
72
- const obj = {};
73
- for (const field of fields) {
74
- // If the field is not an array, it is a key field, so we can get the value from the row
75
- if (!Array.isArray(field)) {
76
- const fieldName = getFieldName(field);
77
- obj[fieldName] = getFieldValue(row, field);
78
- }
79
- else { // If the field is an array, it is a group field, so we need to objectify the nested fields recursively
80
- const [rawGroupField, nestedFields] = field;
81
- const groupField = rawGroupField;
82
- const nestedObject = isObject(groupField, object);
83
- obj[getGroupName(groupField)] = nestedObject
84
- ? objectify(rows, nestedFields, true)
85
- : objectify(rows, nestedFields, false);
86
- }
87
- }
88
- if (obj[name] != null) {
89
- // If the result is an array, we need to push the object to the array
90
- if (Array.isArray(result)) {
91
- result.push(restFields.length ? obj : obj[name]);
92
- }
93
- else { // If the result is an object, we need to set the object to the key
94
- result[keyValue] = obj;
95
- }
96
- }
97
- }
98
- if (object) {
99
- return result;
100
- }
101
- return result;
102
- }
103
- // Groups rows by the current key, preserving first-seen order.
104
- function groupByKey(rows, key) {
105
- const groups = new Map();
106
- for (const row of rows) {
107
- const keyValue = row[key];
108
- const group = groups.get(keyValue);
109
- if (group) {
110
- group.push(row);
111
- }
112
- else {
113
- groups.set(keyValue, [row]);
114
- }
115
- }
116
- return groups;
117
- }
118
- function getKeyField(field) {
119
- return (typeof field === "string" ? field : field.key);
120
- }
121
- function getFieldName(field) {
122
- var _a;
123
- return (typeof field === "string" ? field : ((_a = field.as) !== null && _a !== void 0 ? _a : field.key));
124
- }
125
- function getFieldValue(row, field) {
126
- if (typeof field === "string") {
127
- return row[field];
128
- }
129
- const key = getKeyField(field);
130
- if (field.json) {
131
- try {
132
- return JSON.parse(row[key]);
133
- }
134
- catch (error) {
135
- console.error(`"${row[key]}" is not a valid JSON`, error);
136
- return null;
137
- }
138
- }
139
- return row[key];
140
- }
141
- function getGroupName(field) {
142
- if (typeof field === "string" || typeof field === "number" || typeof field === "symbol") {
143
- return field;
144
- }
145
- return field.name;
146
- }
147
- function isObject(field, defaultValue) {
148
- var _a;
149
- if (typeof field !== "object" || field == null) {
150
- return defaultValue;
151
- }
152
- return (_a = field.object) !== null && _a !== void 0 ? _a : defaultValue;
153
- }
17
+ __exportStar(require("./src/fieldsBuilder"), exports);
18
+ __exportStar(require("./src/objectify"), exports);
@@ -0,0 +1,5 @@
1
+ import type { FieldsBuilder, Row } from "../types";
2
+ /**
3
+ * Creates a fluent builder for composing `Fields` definitions.
4
+ */
5
+ export declare function fieldsBuilder<R = Row, T = Row>(): FieldsBuilder<R, T>;
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.fieldsBuilder = fieldsBuilder;
4
+ /**
5
+ * Creates a fluent builder for composing `Fields` definitions.
6
+ */
7
+ function fieldsBuilder() {
8
+ const fields = [];
9
+ const field = (field, asOrOptions, options) => {
10
+ const isOptions = typeof asOrOptions === "object" && asOrOptions !== null && "json" in asOrOptions;
11
+ const resolvedAs = isOptions ? undefined : asOrOptions;
12
+ const resolvedOptions = isOptions ? asOrOptions : options;
13
+ // Normalize overload inputs into a single key-field shape.
14
+ const entry = newField(field, resolvedAs, resolvedOptions);
15
+ fields.push(entry);
16
+ return api;
17
+ };
18
+ const group = (name, optionsOrBuild, build) => {
19
+ const options = typeof optionsOrBuild === "object" ? optionsOrBuild : undefined;
20
+ const buildFn = typeof optionsOrBuild === "function" ? optionsOrBuild : build;
21
+ if (!buildFn) {
22
+ throw new Error("Group builder requires a builder callback.");
23
+ }
24
+ const nested = buildFn(fieldsBuilder()).build();
25
+ const groupField = newGroup(name, options);
26
+ fields.push([groupField, nested]);
27
+ return api;
28
+ };
29
+ const build = () => {
30
+ return fields;
31
+ };
32
+ const api = { field, group, build };
33
+ return api;
34
+ }
35
+ /**
36
+ * Normalizes key field shorthand into object form when needed.
37
+ */
38
+ function newField(key, as, options) {
39
+ if (!as && !options) {
40
+ return key;
41
+ }
42
+ let entry = typeof key === "object" ? key : { key };
43
+ if (as !== undefined) {
44
+ entry = { ...entry, as: as };
45
+ }
46
+ if (options !== undefined) {
47
+ entry = { ...entry, ...options };
48
+ }
49
+ return entry;
50
+ }
51
+ /**
52
+ * Normalizes group shorthand into object form when options are provided.
53
+ */
54
+ function newGroup(groupField, options) {
55
+ if (!options) {
56
+ return groupField;
57
+ }
58
+ let entry = typeof groupField === "object" ? groupField : { name: groupField };
59
+ if (options) {
60
+ entry = { ...entry, ...options };
61
+ }
62
+ return entry;
63
+ }
@@ -1,11 +1,11 @@
1
- import type { Fields, Prettify, Row } from "../types";
1
+ import type { Field, Prettify, Row } from "../types";
2
2
  /**
3
3
  * Transforms flat rows into nested objects/arrays based on a field definition.
4
4
  *
5
5
  * When `object` is `true`, top-level output is keyed by the first field value.
6
6
  * Otherwise the output is an array.
7
7
  */
8
- export declare function objectify<R = unknown, T = Row>(data: T[], fields: Fields<R, T>, object: true): Record<PropertyKey, Prettify<R>>;
9
- export declare function objectify<R = unknown, T = Row>(data: T[], fields: Fields<R, T>, object?: false): Prettify<R>[];
10
- export declare function objectify<R = unknown>(data: Row[], fields: Fields<R>, object: true): Record<PropertyKey, Prettify<R>>;
11
- export declare function objectify<R = unknown>(data: Row[], fields: Fields<R>, object?: false): Prettify<R>[];
8
+ export declare function objectify<R = unknown, T = Row>(data: T[], fields: Field<R, T>[], object: true): Record<PropertyKey, Prettify<R>>;
9
+ export declare function objectify<R = unknown, T = Row>(data: T[], fields: Field<R, T>[], object?: false): Prettify<R>[];
10
+ export declare function objectify<R = unknown>(data: Row[], fields: Field<R>[], object: true): Record<PropertyKey, Prettify<R>>;
11
+ export declare function objectify<R = unknown>(data: Row[], fields: Field<R>[], object?: false): Prettify<R>[];
@@ -14,25 +14,25 @@ function objectify(data, fields, object = false) {
14
14
  const row = rows[0];
15
15
  const obj = {};
16
16
  for (const field of fields) {
17
- // If the field is not an array, it is a key field, so we can get the value from the row
18
- if (!Array.isArray(field)) {
19
- const fieldName = getFieldName(field);
20
- obj[fieldName] = getFieldValue(row, field);
21
- }
22
- else {
23
- // If the field is an array, it is a group field, so we need to objectify the nested fields recursively
17
+ const fieldName = getFieldName(field);
18
+ // If the field is an array, it is a group field, so we need to objectify the nested fields recursively
19
+ if (Array.isArray(field)) {
24
20
  const [rawGroupField, nestedFields] = field;
25
21
  const groupField = rawGroupField;
26
22
  const nestedObject = isObject(groupField, object);
27
23
  obj[getGroupName(groupField)] = nestedObject
28
24
  ? objectify(rows, nestedFields, true)
29
25
  : objectify(rows, nestedFields, false);
26
+ // If the field is not an array, it is a key field, so we can get the value from the row
27
+ }
28
+ else if (fieldName !== undefined) {
29
+ obj[fieldName] = getFieldValue(row, field);
30
30
  }
31
31
  }
32
- if (obj[name] != null) {
32
+ if (name === undefined || obj[name] != null) {
33
33
  // If the result is an array, we need to push the object to the array
34
34
  if (Array.isArray(result)) {
35
- result.push(restFields.length ? obj : obj[name]);
35
+ result.push(restFields.length || name === undefined ? obj : obj[name]);
36
36
  }
37
37
  else {
38
38
  // If the result is an object, we need to set the object to the key
@@ -51,7 +51,7 @@ function objectify(data, fields, object = false) {
51
51
  function groupByKey(rows, key) {
52
52
  const groups = new Map();
53
53
  for (const row of rows) {
54
- const keyValue = row[key];
54
+ const keyValue = key !== undefined ? row[key] : undefined;
55
55
  const group = groups.get(keyValue);
56
56
  if (group) {
57
57
  group.push(row);
@@ -66,14 +66,20 @@ function groupByKey(rows, key) {
66
66
  * Resolves the source key from either shorthand or object field syntax.
67
67
  */
68
68
  function getKeyField(field) {
69
- return (typeof field === "string" ? field : field.key);
69
+ if (Array.isArray(field)) {
70
+ return undefined;
71
+ }
72
+ return typeof field === "string" ? field : field === null || field === void 0 ? void 0 : field.key;
70
73
  }
71
74
  /**
72
75
  * Resolves the output property name for a key field.
73
76
  */
74
77
  function getFieldName(field) {
75
78
  var _a;
76
- return (typeof field === "string" ? field : ((_a = field.as) !== null && _a !== void 0 ? _a : field.key));
79
+ if (Array.isArray(field)) {
80
+ return undefined;
81
+ }
82
+ return (typeof field === "string" ? field : ((_a = field === null || field === void 0 ? void 0 : field.as) !== null && _a !== void 0 ? _a : field === null || field === void 0 ? void 0 : field.key));
77
83
  }
78
84
  /**
79
85
  * Reads a row value and optionally parses it as JSON.
@@ -83,7 +89,10 @@ function getFieldValue(row, field) {
83
89
  return row[field];
84
90
  }
85
91
  const key = getKeyField(field);
86
- if (field.json) {
92
+ if (key === undefined) {
93
+ return undefined;
94
+ }
95
+ if (field === null || field === void 0 ? void 0 : field.json) {
87
96
  try {
88
97
  return JSON.parse(row[key]);
89
98
  }
package/dist/types.d.ts CHANGED
@@ -5,7 +5,7 @@ export type Row = Record<PropertyKey, unknown>;
5
5
  /**
6
6
  * String-only key names for a given row type.
7
7
  */
8
- export type KeyName<T = Row> = Extract<keyof T, string>;
8
+ export type KeyName<T = Row> = Extract<keyof T, string> | undefined;
9
9
  /**
10
10
  * Fallback string type used when strict key extraction resolves to `never`.
11
11
  */
@@ -81,11 +81,7 @@ export type SimpleGroupField = DefaultString | {
81
81
  /**
82
82
  * Single field definition: direct key field or nested group tuple.
83
83
  */
84
- export type Field<R = Row, T = Row> = KeyField<R, T> | [GroupField<R>, Fields<R, T>];
85
- /**
86
- * Full field set, requiring the first entry to be a key field.
87
- */
88
- export type Fields<R = Row, T = Row> = [KeyField<R, T>, ...Field<R, T>[]];
84
+ export type Field<R = Row, T = Row> = KeyField<R, T> | [GroupField<R>, Field<R, T>[]];
89
85
  /**
90
86
  * Extra options when defining a key field.
91
87
  */
@@ -120,6 +116,6 @@ export type FieldsBuilder<R = Row, T = Row> = {
120
116
  /**
121
117
  * Finalizes and returns the accumulated field definition tuple.
122
118
  */
123
- build(): Fields<R, T>;
119
+ build(): Field<R, T>[];
124
120
  };
125
121
  export {};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resobjectify",
3
- "version": "2.0.1",
3
+ "version": "2.1.1",
4
4
  "description": "Parse an array of one dimensional objects to nested array/object",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",