resobjectify 2.0.1 → 2.1.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/src/objectify.d.ts +5 -5
- package/dist/src/objectify.js +22 -13
- package/dist/types.d.ts +3 -7
- package/package.json +1 -1
package/dist/src/objectify.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type {
|
|
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:
|
|
9
|
-
export declare function objectify<R = unknown, T = Row>(data: T[], fields:
|
|
10
|
-
export declare function objectify<R = unknown>(data: Row[], fields:
|
|
11
|
-
export declare function objectify<R = unknown>(data: Row[], fields:
|
|
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>[];
|
package/dist/src/objectify.js
CHANGED
|
@@ -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
|
-
|
|
18
|
-
|
|
19
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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 (
|
|
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>,
|
|
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():
|
|
119
|
+
build(): Field<R, T>[];
|
|
124
120
|
};
|
|
125
121
|
export {};
|