@oscarpalmer/atoms 0.92.0 → 0.93.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/value/merge.cjs +16 -10
- package/dist/value/merge.js +16 -10
- package/package.json +1 -1
- package/src/value/merge.ts +15 -21
- package/types/index.d.cts +1 -8
- package/types/value/index.d.cts +1 -8
- package/types/value/merge.d.cts +1 -8
- package/types/value/merge.d.ts +1 -9
package/dist/value/merge.cjs
CHANGED
|
@@ -3,12 +3,20 @@
|
|
|
3
3
|
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
4
|
|
|
5
5
|
const is = require('../is.cjs');
|
|
6
|
+
const math = require('../math.cjs');
|
|
6
7
|
|
|
7
|
-
function
|
|
8
|
+
function getIndices(arrays) {
|
|
9
|
+
return Array.from(
|
|
10
|
+
{
|
|
11
|
+
length: math.min(arrays.map((array) => array.length))
|
|
12
|
+
},
|
|
13
|
+
(_, index) => String(index)
|
|
14
|
+
);
|
|
15
|
+
}
|
|
16
|
+
function merge(values) {
|
|
8
17
|
if (!Array.isArray(values) || values.length === 0) {
|
|
9
18
|
return {};
|
|
10
19
|
}
|
|
11
|
-
const skipNullable = options?.skipNullable === true;
|
|
12
20
|
const actual = values.filter((value) => is.isArrayOrPlainObject(value));
|
|
13
21
|
if (actual.length === 0) {
|
|
14
22
|
return {};
|
|
@@ -16,22 +24,20 @@ function merge(values, options) {
|
|
|
16
24
|
if (actual.length === 1) {
|
|
17
25
|
return actual[0];
|
|
18
26
|
}
|
|
19
|
-
const
|
|
20
|
-
const
|
|
27
|
+
const isArray = actual.every(Array.isArray);
|
|
28
|
+
const result = isArray ? [] : {};
|
|
29
|
+
const indices = isArray ? getIndices(actual) : void 0;
|
|
21
30
|
const { length } = actual;
|
|
22
31
|
for (let outerIndex = 0; outerIndex < length; outerIndex += 1) {
|
|
23
32
|
const item = actual[outerIndex];
|
|
24
|
-
const keys = Object.keys(item);
|
|
33
|
+
const keys = indices ?? Object.keys(item);
|
|
25
34
|
const size = keys.length;
|
|
26
35
|
for (let innerIndex = 0; innerIndex < size; innerIndex += 1) {
|
|
27
36
|
const key = keys[innerIndex];
|
|
28
37
|
const next = item[key];
|
|
29
38
|
const previous = result[key];
|
|
30
|
-
if (
|
|
31
|
-
|
|
32
|
-
}
|
|
33
|
-
if (is.isArrayOrPlainObject(next)) {
|
|
34
|
-
result[key] = is.isArrayOrPlainObject(previous) ? merge([previous, next]) : merge([next]);
|
|
39
|
+
if (is.isArrayOrPlainObject(next) && is.isArrayOrPlainObject(previous)) {
|
|
40
|
+
result[key] = merge([previous, next]);
|
|
35
41
|
} else {
|
|
36
42
|
result[key] = next;
|
|
37
43
|
}
|
package/dist/value/merge.js
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
import { isArrayOrPlainObject } from '../is.js';
|
|
2
|
+
import { min } from '../math.js';
|
|
2
3
|
|
|
3
|
-
function
|
|
4
|
+
function getIndices(arrays) {
|
|
5
|
+
return Array.from(
|
|
6
|
+
{
|
|
7
|
+
length: min(arrays.map((array) => array.length))
|
|
8
|
+
},
|
|
9
|
+
(_, index) => String(index)
|
|
10
|
+
);
|
|
11
|
+
}
|
|
12
|
+
function merge(values) {
|
|
4
13
|
if (!Array.isArray(values) || values.length === 0) {
|
|
5
14
|
return {};
|
|
6
15
|
}
|
|
7
|
-
const skipNullable = options?.skipNullable === true;
|
|
8
16
|
const actual = values.filter((value) => isArrayOrPlainObject(value));
|
|
9
17
|
if (actual.length === 0) {
|
|
10
18
|
return {};
|
|
@@ -12,22 +20,20 @@ function merge(values, options) {
|
|
|
12
20
|
if (actual.length === 1) {
|
|
13
21
|
return actual[0];
|
|
14
22
|
}
|
|
15
|
-
const
|
|
16
|
-
const
|
|
23
|
+
const isArray = actual.every(Array.isArray);
|
|
24
|
+
const result = isArray ? [] : {};
|
|
25
|
+
const indices = isArray ? getIndices(actual) : void 0;
|
|
17
26
|
const { length } = actual;
|
|
18
27
|
for (let outerIndex = 0; outerIndex < length; outerIndex += 1) {
|
|
19
28
|
const item = actual[outerIndex];
|
|
20
|
-
const keys = Object.keys(item);
|
|
29
|
+
const keys = indices ?? Object.keys(item);
|
|
21
30
|
const size = keys.length;
|
|
22
31
|
for (let innerIndex = 0; innerIndex < size; innerIndex += 1) {
|
|
23
32
|
const key = keys[innerIndex];
|
|
24
33
|
const next = item[key];
|
|
25
34
|
const previous = result[key];
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
if (isArrayOrPlainObject(next)) {
|
|
30
|
-
result[key] = isArrayOrPlainObject(previous) ? merge([previous, next]) : merge([next]);
|
|
35
|
+
if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous)) {
|
|
36
|
+
result[key] = merge([previous, next]);
|
|
31
37
|
} else {
|
|
32
38
|
result[key] = next;
|
|
33
39
|
}
|
package/package.json
CHANGED
package/src/value/merge.ts
CHANGED
|
@@ -1,27 +1,26 @@
|
|
|
1
1
|
import {isArrayOrPlainObject} from '../is';
|
|
2
|
+
import {min} from '../math';
|
|
2
3
|
import type {ArrayOrPlainObject, PlainObject} from '../models';
|
|
3
4
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
function getIndices(arrays: unknown[][]): string[] {
|
|
6
|
+
return Array.from(
|
|
7
|
+
{
|
|
8
|
+
length: min(arrays.map(array => array.length)),
|
|
9
|
+
},
|
|
10
|
+
(_, index) => String(index),
|
|
11
|
+
);
|
|
12
|
+
}
|
|
11
13
|
|
|
12
14
|
/**
|
|
13
15
|
* Merge multiple arrays or objects into a single one
|
|
14
16
|
*/
|
|
15
17
|
export function merge<Model extends ArrayOrPlainObject>(
|
|
16
18
|
values: Model[],
|
|
17
|
-
options?: Partial<MergeOptions>,
|
|
18
19
|
): Model {
|
|
19
20
|
if (!Array.isArray(values) || values.length === 0) {
|
|
20
21
|
return {} as Model;
|
|
21
22
|
}
|
|
22
23
|
|
|
23
|
-
const skipNullable = options?.skipNullable === true;
|
|
24
|
-
|
|
25
24
|
const actual = values.filter(value => isArrayOrPlainObject(value)) as Model[];
|
|
26
25
|
|
|
27
26
|
if (actual.length === 0) {
|
|
@@ -32,13 +31,14 @@ export function merge<Model extends ArrayOrPlainObject>(
|
|
|
32
31
|
return actual[0];
|
|
33
32
|
}
|
|
34
33
|
|
|
35
|
-
const
|
|
36
|
-
const
|
|
34
|
+
const isArray = actual.every(Array.isArray);
|
|
35
|
+
const result = (isArray ? [] : {}) as PlainObject;
|
|
36
|
+
const indices = isArray ? getIndices(actual as unknown[][]) : undefined;
|
|
37
37
|
const {length} = actual;
|
|
38
38
|
|
|
39
39
|
for (let outerIndex = 0; outerIndex < length; outerIndex += 1) {
|
|
40
40
|
const item = actual[outerIndex] as PlainObject;
|
|
41
|
-
const keys = Object.keys(item);
|
|
41
|
+
const keys = indices ?? Object.keys(item);
|
|
42
42
|
const size = keys.length;
|
|
43
43
|
|
|
44
44
|
for (let innerIndex = 0; innerIndex < size; innerIndex += 1) {
|
|
@@ -46,14 +46,8 @@ export function merge<Model extends ArrayOrPlainObject>(
|
|
|
46
46
|
const next = item[key];
|
|
47
47
|
const previous = result[key];
|
|
48
48
|
|
|
49
|
-
if (
|
|
50
|
-
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
if (isArrayOrPlainObject(next)) {
|
|
54
|
-
result[key] = isArrayOrPlainObject(previous)
|
|
55
|
-
? merge([previous, next])
|
|
56
|
-
: merge([next]);
|
|
49
|
+
if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous)) {
|
|
50
|
+
result[key] = merge([previous, next]);
|
|
57
51
|
} else {
|
|
58
52
|
result[key] = next;
|
|
59
53
|
}
|
package/types/index.d.cts
CHANGED
|
@@ -2817,17 +2817,10 @@ export declare function getValue<Data extends ArrayOrPlainObject, Path extends P
|
|
|
2817
2817
|
* - Returns `undefined` if the value is not found
|
|
2818
2818
|
*/
|
|
2819
2819
|
export declare function getValue<Data extends ArrayOrPlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
|
|
2820
|
-
export type MergeOptions = {
|
|
2821
|
-
/**
|
|
2822
|
-
* - Skip nullable values when merging arrays?
|
|
2823
|
-
* - E.g. `merge([1, 2, 3], [null, null, 99])` => `[1, 2, 99]`
|
|
2824
|
-
*/
|
|
2825
|
-
skipNullable?: boolean;
|
|
2826
|
-
};
|
|
2827
2820
|
/**
|
|
2828
2821
|
* Merge multiple arrays or objects into a single one
|
|
2829
2822
|
*/
|
|
2830
|
-
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]
|
|
2823
|
+
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]): Model;
|
|
2831
2824
|
/**
|
|
2832
2825
|
* - Set the value in an object using a known path
|
|
2833
2826
|
* - You can set a nested value by using dot notation, e.g., `foo.bar.baz`
|
package/types/value/index.d.cts
CHANGED
|
@@ -1723,17 +1723,10 @@ export declare function getValue<Data extends ArrayOrPlainObject, Path extends P
|
|
|
1723
1723
|
* - Returns `undefined` if the value is not found
|
|
1724
1724
|
*/
|
|
1725
1725
|
export declare function getValue<Data extends ArrayOrPlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
|
|
1726
|
-
export type MergeOptions = {
|
|
1727
|
-
/**
|
|
1728
|
-
* - Skip nullable values when merging arrays?
|
|
1729
|
-
* - E.g. `merge([1, 2, 3], [null, null, 99])` => `[1, 2, 99]`
|
|
1730
|
-
*/
|
|
1731
|
-
skipNullable?: boolean;
|
|
1732
|
-
};
|
|
1733
1726
|
/**
|
|
1734
1727
|
* Merge multiple arrays or objects into a single one
|
|
1735
1728
|
*/
|
|
1736
|
-
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]
|
|
1729
|
+
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]): Model;
|
|
1737
1730
|
/**
|
|
1738
1731
|
* - Set the value in an object using a known path
|
|
1739
1732
|
* - You can set a nested value by using dot notation, e.g., `foo.bar.baz`
|
package/types/value/merge.d.cts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
1
|
// Generated by dts-bundle-generator v9.5.1
|
|
2
2
|
|
|
3
3
|
export type ArrayOrPlainObject = unknown[] | Record<PropertyKey, unknown>;
|
|
4
|
-
export type MergeOptions = {
|
|
5
|
-
/**
|
|
6
|
-
* - Skip nullable values when merging arrays?
|
|
7
|
-
* - E.g. `merge([1, 2, 3], [null, null, 99])` => `[1, 2, 99]`
|
|
8
|
-
*/
|
|
9
|
-
skipNullable?: boolean;
|
|
10
|
-
};
|
|
11
4
|
/**
|
|
12
5
|
* Merge multiple arrays or objects into a single one
|
|
13
6
|
*/
|
|
14
|
-
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]
|
|
7
|
+
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]): Model;
|
|
15
8
|
|
|
16
9
|
export {};
|
package/types/value/merge.d.ts
CHANGED
|
@@ -1,13 +1,5 @@
|
|
|
1
1
|
import type { ArrayOrPlainObject } from '../models';
|
|
2
|
-
type MergeOptions = {
|
|
3
|
-
/**
|
|
4
|
-
* - Skip nullable values when merging arrays?
|
|
5
|
-
* - E.g. `merge([1, 2, 3], [null, null, 99])` => `[1, 2, 99]`
|
|
6
|
-
*/
|
|
7
|
-
skipNullable?: boolean;
|
|
8
|
-
};
|
|
9
2
|
/**
|
|
10
3
|
* Merge multiple arrays or objects into a single one
|
|
11
4
|
*/
|
|
12
|
-
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]
|
|
13
|
-
export {};
|
|
5
|
+
export declare function merge<Model extends ArrayOrPlainObject>(values: Model[]): Model;
|