resobjectify 2.1.0 → 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 +3 -23
- package/dist/index.js +16 -151
- package/dist/src/fieldsBuilder.d.ts +5 -0
- package/dist/src/fieldsBuilder.js +63 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
export
|
|
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
|
-
|
|
4
|
-
|
|
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,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
|
+
}
|