Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@corex/deepmerge

Package Overview
Dependencies
Maintainers
1
Versions
110
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@corex/deepmerge - npm Package Compare versions

Comparing version 2.2.6 to 2.2.7

README.md

10

dist/index.d.ts

@@ -5,3 +5,11 @@ interface IObject {

declare type TUnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
declare const merge: <T extends IObject[]>(...objects: T) => TUnionToIntersection<T[number]>;
export declare type ArrayMergeType = 'combine' | 'overwrite';
export interface IMergeOptions {
arrayMergeType?: ArrayMergeType;
arrayMerge?: (prevArr: any[], currArr: any[]) => any;
}
export declare const combineMerge: (prevArr: any[], currArr: any[]) => any[];
export declare const overwriteMerge: (_: any[], currArr: any[]) => any[];
export declare const getMergeType: (type?: "combine" | "overwrite" | undefined) => (prevArr: any[], currArr: any[]) => any[];
declare const merge: <T extends IObject[]>(objects: T, options?: IMergeOptions | undefined) => TUnionToIntersection<T[number]>;
export default merge;

52

dist/index.js

@@ -12,2 +12,3 @@ (function (factory) {

Object.defineProperty(exports, "__esModule", { value: true });
exports.getMergeType = exports.overwriteMerge = exports.combineMerge = void 0;
const isObject = (obj) => {

@@ -23,17 +24,40 @@ if (typeof obj === 'object' && obj !== null) {

};
const merge = (...objects) => objects.reduce((result, current) => {
Object.keys(current).forEach((key) => {
if (Array.isArray(result[key]) && Array.isArray(current[key])) {
result[key] = Array.from(new Set(result[key].concat(current[key])));
}
else if (isObject(result[key]) && isObject(current[key])) {
result[key] = merge(result[key], current[key]);
}
else {
result[key] = current[key];
}
});
return result;
}, {});
exports.combineMerge = (prevArr, currArr) => [
...new Set([...prevArr, ...currArr]),
];
exports.overwriteMerge = (_, currArr) => currArr;
exports.getMergeType = (type) => {
switch (type) {
case 'overwrite':
return exports.overwriteMerge;
case 'combine':
default:
return exports.combineMerge;
}
};
const withDefaultOptions = (options) => {
return {
arrayMergeType: 'combine',
arrayMerge: exports.getMergeType(options ? options.arrayMergeType : 'combine'),
...options,
};
};
const merge = (objects, options) => {
const opts = withDefaultOptions(options);
return objects.reduce((prev, curr) => {
Object.keys(curr).forEach((key) => {
if (Array.isArray(prev[key]) && Array.isArray(curr[key])) {
prev[key] = opts.arrayMerge(prev[key], curr[key]);
}
else if (isObject(prev[key]) && isObject(curr[key])) {
prev[key] = merge([prev[key], curr[key]], opts);
}
else {
prev[key] = curr[key];
}
});
return prev;
}, {});
};
exports.default = merge;
});

@@ -31,3 +31,3 @@ var __importDefault = (this && this.__importDefault) || function (mod) {

};
expect(index_1.default(obj1, obj2)).toStrictEqual({
expect(index_1.default([obj1, obj2])).toStrictEqual({
a: 2,

@@ -47,3 +47,3 @@ b: {

};
expect(index_1.default(arr1, arr2)).toStrictEqual({
expect(index_1.default([arr1, arr2])).toStrictEqual({
a: [1, 2, 3, 4, 5],

@@ -80,6 +80,71 @@ });

};
const result = index_1.default(obj1, obj2, obj3);
const result = index_1.default([obj1, obj2, obj3]);
expect(result.b(7, 5)).toStrictEqual(35);
});
test('arrayMergeTypes: combine', () => {
const obj1 = {
a: [1, 2, 3],
};
const obj2 = {
a: [1, 7, 6],
};
const obj3 = {
a: [20, 40],
};
expect(index_1.default([obj1, obj2, obj3])).toStrictEqual({
a: [1, 2, 3, 7, 6, 20, 40],
});
});
test('arrayMergeTypes: overwrite', () => {
const obj1 = {
a: [1, 2, 3],
b: ['c', 'd'],
c: {
d: [1, 2],
},
};
const obj2 = {
a: [1, 7, 6],
b: ['e', 'f'],
c: {
d: [9, 10],
},
};
const obj3 = {
a: [20, 40],
};
expect(index_1.default([obj1, obj2, obj3], {
arrayMergeType: 'overwrite',
})).toStrictEqual({
a: [20, 40],
b: ['e', 'f'],
c: {
d: [9, 10],
},
});
});
test('arrayMergeType: custom', () => {
const obj1 = {
a: ['1', '2'],
c: {
d: [1000, 500],
},
};
const obj2 = {
a: ['4', '5'],
c: {
d: [60, 80],
},
};
const customMergeFn = (_, __) => 42;
expect(index_1.default([obj1, obj2], {
arrayMerge: customMergeFn,
})).toStrictEqual({
a: 42,
c: {
d: 42,
},
});
});
});
});
{
"name": "@corex/deepmerge",
"version": "2.2.6",
"version": "2.2.7",
"main": "./dist/index.js",
"license": "MIT",
"description": "A zero dependency object merger with typescript support and built in common merge utilities.",
"keywords": [
"deepmerge",
"typescript"
],
"publishConfig": {

@@ -7,0 +12,0 @@ "access": "public"

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc