resobjectify 2.1.1 → 2.1.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.
@@ -27,7 +27,7 @@ function fieldsBuilder() {
27
27
  return api;
28
28
  };
29
29
  const build = () => {
30
- return fields;
30
+ return [...fields];
31
31
  };
32
32
  const api = { field, group, build };
33
33
  return api;
@@ -36,7 +36,7 @@ function fieldsBuilder() {
36
36
  * Normalizes key field shorthand into object form when needed.
37
37
  */
38
38
  function newField(key, as, options) {
39
- if (!as && !options) {
39
+ if (as === undefined && options === undefined) {
40
40
  return key;
41
41
  }
42
42
  let entry = typeof key === "object" ? key : { key };
@@ -2,11 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.objectify = objectify;
4
4
  function objectify(data, fields, object = false) {
5
- // If the fields is a single field or object is false, group the result in an array, otherwise group the result in an object
6
- const result = fields.length === 1 || !object ? [] : {};
5
+ if (!Array.isArray(fields) || fields.length === 0) {
6
+ return (object ? {} : []);
7
+ }
7
8
  const [keyField, ...restFields] = fields;
8
9
  const key = getKeyField(keyField);
9
10
  const name = getFieldName(keyField);
11
+ // Keep single root-key selections as arrays, but allow keyless group-only selections to be objects in object mode.
12
+ const shouldUseObjectResult = object && (fields.length > 1 || name === undefined);
13
+ const result = shouldUseObjectResult ? {} : [];
10
14
  // Pre-group by the current key so each recursion only sees its parent slice,
11
15
  // which removes the need for parent checks or duplicate tracking.
12
16
  const groups = groupByKey(data, key);
@@ -29,22 +33,33 @@ function objectify(data, fields, object = false) {
29
33
  obj[fieldName] = getFieldValue(row, field);
30
34
  }
31
35
  }
32
- if (name === undefined || obj[name] != null) {
33
- // If the result is an array, we need to push the object to the array
34
- if (Array.isArray(result)) {
35
- result.push(restFields.length || name === undefined ? obj : obj[name]);
36
- }
37
- else {
38
- // If the result is an object, we need to set the object to the key
39
- result[keyValue] = obj;
40
- }
41
- }
36
+ appendToResult(result, obj, name, keyValue, restFields.length > 0);
42
37
  }
43
- if (object) {
38
+ if (!Array.isArray(result)) {
44
39
  return result;
45
40
  }
46
41
  return result;
47
42
  }
43
+ function appendToResult(result, obj, keyName, keyValue, hasNestedFields) {
44
+ // Skip groups whose root key is missing/null; for keyless grouping we always keep the object.
45
+ const hasKeyValue = keyName === undefined || obj[keyName] != null;
46
+ if (!hasKeyValue) {
47
+ return;
48
+ }
49
+ if (Array.isArray(result)) {
50
+ // In array mode, single-key selections emit the key value; otherwise emit full objects.
51
+ const shouldPushWholeObject = keyName === undefined || hasNestedFields;
52
+ result.push(shouldPushWholeObject ? obj : obj[keyName]);
53
+ return;
54
+ }
55
+ // In object mode without a root key, emit the aggregated object itself.
56
+ if (keyName === undefined) {
57
+ Object.assign(result, obj);
58
+ return;
59
+ }
60
+ // In object mode with a root key, group output by the computed key value.
61
+ result[keyValue] = obj;
62
+ }
48
63
  /**
49
64
  * Groups rows by the provided key while preserving insertion order.
50
65
  */
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> | undefined;
8
+ export type KeyName<T = Row> = Extract<keyof T, string>;
9
9
  /**
10
10
  * Fallback string type used when strict key extraction resolves to `never`.
11
11
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "resobjectify",
3
- "version": "2.1.1",
3
+ "version": "2.1.2",
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",