@tsfun/object
Advanced tools
| import { SimpleDeepMerge } from './utils/types'; | ||
| /** | ||
| * Merge two objects of the same interface | ||
| * | ||
| * `b` is prioritized for overlapping non-object properties | ||
| * | ||
| * @param a Object or value to merge | ||
| * @param b Object or value to merge | ||
| * @returns Result of the merge | ||
| */ | ||
| export declare function deepMergeOverwrite<Value>(a: Value, b: Value): Value; | ||
| /** | ||
| * Merge two objects | ||
| * | ||
| * The two objects are expected to not have overlapping non-object properties | ||
| * | ||
| * @param a Object to merge | ||
| * @param b Object to merge | ||
| * @param onerror Function to handle should error occurs | ||
| * @returns A merged object of `a` and `b` | ||
| */ | ||
| export declare function deepMergeWithoutCollision<A extends object, B extends object>(a: A, b: B, onerror?: deepMergeWithoutCollision.ErrorProcessor): SimpleDeepMerge<A, B>; | ||
| export declare namespace deepMergeWithoutCollision { | ||
| interface ErrorProcessor { | ||
| (param: ErrorProcessorParam): unknown; | ||
| } | ||
| type ErrorProcessorParam = PropertyCollision; | ||
| const enum ErrorType { | ||
| PropertyCollision = 1 | ||
| } | ||
| interface PropertyCollision { | ||
| type: ErrorType.PropertyCollision; | ||
| objects: [object, object]; | ||
| key: string | symbol; | ||
| values: [unknown, unknown]; | ||
| } | ||
| } |
| "use strict"; | ||
| Object.defineProperty(exports, "__esModule", { value: true }); | ||
| const isObject = (value) => value && typeof value === 'object' && !Array.isArray(value); | ||
| /** | ||
| * Merge two objects of the same interface | ||
| * | ||
| * `b` is prioritized for overlapping non-object properties | ||
| * | ||
| * @param a Object or value to merge | ||
| * @param b Object or value to merge | ||
| * @returns Result of the merge | ||
| */ | ||
| function deepMergeOverwrite(a, b) { | ||
| if (!isObject(a) || !isObject(b)) | ||
| return b; | ||
| const result = {}; | ||
| for (const [key, aValue] of Object.entries(a)) { | ||
| if (key in b) { | ||
| const bValue = b[key]; | ||
| result[key] = deepMergeOverwrite(aValue, bValue); | ||
| } | ||
| else { | ||
| result[key] = aValue; | ||
| } | ||
| } | ||
| for (const [key, bValue] of Object.entries(b)) { | ||
| if (key in a) | ||
| continue; | ||
| result[key] = bValue; | ||
| } | ||
| return result; | ||
| } | ||
| exports.deepMergeOverwrite = deepMergeOverwrite; | ||
| const DMWOC_DEF_ERR_HDLR = param => { | ||
| throw Object.assign(new TypeError(`Property collision`), param); | ||
| }; | ||
| /** | ||
| * Merge two objects | ||
| * | ||
| * The two objects are expected to not have overlapping non-object properties | ||
| * | ||
| * @param a Object to merge | ||
| * @param b Object to merge | ||
| * @param onerror Function to handle should error occurs | ||
| * @returns A merged object of `a` and `b` | ||
| */ | ||
| function deepMergeWithoutCollision(a, b, onerror = DMWOC_DEF_ERR_HDLR) { | ||
| const result = {}; | ||
| for (const [key, aValue] of Object.entries(a)) { | ||
| if (key in b) { | ||
| const bValue = b[key]; | ||
| if (isObject(aValue) && isObject(bValue)) { | ||
| result[key] = deepMergeWithoutCollision(aValue, bValue, onerror); | ||
| } | ||
| else { | ||
| throw onerror({ | ||
| type: 1 /* PropertyCollision */, | ||
| objects: [a, b], | ||
| key, | ||
| values: [aValue, bValue] | ||
| }); | ||
| } | ||
| } | ||
| else { | ||
| result[key] = aValue; | ||
| } | ||
| } | ||
| for (const [key, bValue] of Object.entries(b)) { | ||
| if (key in a) | ||
| continue; | ||
| result[key] = bValue; | ||
| } | ||
| return result; | ||
| } | ||
| exports.deepMergeWithoutCollision = deepMergeWithoutCollision; | ||
| /* istanbul ignore next */ | ||
| (function (deepMergeWithoutCollision) { | ||
| let ErrorType; | ||
| (function (ErrorType) { | ||
| ErrorType[ErrorType["PropertyCollision"] = 1] = "PropertyCollision"; | ||
| })(ErrorType = deepMergeWithoutCollision.ErrorType || (deepMergeWithoutCollision.ErrorType = {})); | ||
| })(deepMergeWithoutCollision = exports.deepMergeWithoutCollision || (exports.deepMergeWithoutCollision = {})); | ||
| //# sourceMappingURL=deep-merge.js.map |
| const isObject = value => value && typeof value === 'object' && !Array.isArray(value); | ||
| /** | ||
| * Merge two objects of the same interface | ||
| * | ||
| * `b` is prioritized for overlapping non-object properties | ||
| * | ||
| * @param a Object or value to merge | ||
| * @param b Object or value to merge | ||
| * @returns Result of the merge | ||
| */ | ||
| export function deepMergeOverwrite(a, b) { | ||
| if (!isObject(a) || !isObject(b)) return b; | ||
| const result = {}; | ||
| for (const [key, aValue] of Object.entries(a)) { | ||
| if (key in b) { | ||
| const bValue = b[key]; | ||
| result[key] = deepMergeOverwrite(aValue, bValue); | ||
| } else { | ||
| result[key] = aValue; | ||
| } | ||
| } | ||
| for (const [key, bValue] of Object.entries(b)) { | ||
| if (key in a) continue; | ||
| result[key] = bValue; | ||
| } | ||
| return result; | ||
| } | ||
| const DMWOC_DEF_ERR_HDLR = param => { | ||
| throw Object.assign(new TypeError(`Property collision`), param); | ||
| }; | ||
| /** | ||
| * Merge two objects | ||
| * | ||
| * The two objects are expected to not have overlapping non-object properties | ||
| * | ||
| * @param a Object to merge | ||
| * @param b Object to merge | ||
| * @param onerror Function to handle should error occurs | ||
| * @returns A merged object of `a` and `b` | ||
| */ | ||
| export function deepMergeWithoutCollision(a, b, onerror = DMWOC_DEF_ERR_HDLR) { | ||
| const result = {}; | ||
| for (const [key, aValue] of Object.entries(a)) { | ||
| if (key in b) { | ||
| const bValue = b[key]; | ||
| if (isObject(aValue) && isObject(bValue)) { | ||
| result[key] = deepMergeWithoutCollision(aValue, bValue, onerror); | ||
| } else { | ||
| throw onerror({ | ||
| type: 1 | ||
| /* PropertyCollision */ | ||
| , | ||
| objects: [a, b], | ||
| key, | ||
| values: [aValue, bValue] | ||
| }); | ||
| } | ||
| } else { | ||
| result[key] = aValue; | ||
| } | ||
| } | ||
| for (const [key, bValue] of Object.entries(b)) { | ||
| if (key in a) continue; | ||
| result[key] = bValue; | ||
| } | ||
| return result; | ||
| } | ||
| /* istanbul ignore next */ | ||
| (function (deepMergeWithoutCollision) { | ||
| let ErrorType; | ||
| (function (ErrorType) { | ||
| ErrorType[ErrorType["PropertyCollision"] = 1] = "PropertyCollision"; | ||
| })(ErrorType = deepMergeWithoutCollision.ErrorType || (deepMergeWithoutCollision.ErrorType = {})); | ||
| })(deepMergeWithoutCollision || (deepMergeWithoutCollision = {})); //# sourceMappingURL=deep-merge.js.map |
| import { SimpleDeepMerge } from './utils/types'; | ||
| /** | ||
| * Merge two objects of the same interface | ||
| * | ||
| * `b` is prioritized for overlapping non-object properties | ||
| * | ||
| * @param a Object or value to merge | ||
| * @param b Object or value to merge | ||
| * @returns Result of the merge | ||
| */ | ||
| export declare function deepMergeOverwrite<Value>(a: Value, b: Value): Value; | ||
| /** | ||
| * Merge two objects | ||
| * | ||
| * The two objects are expected to not have overlapping non-object properties | ||
| * | ||
| * @param a Object to merge | ||
| * @param b Object to merge | ||
| * @param onerror Function to handle should error occurs | ||
| * @returns A merged object of `a` and `b` | ||
| */ | ||
| export declare function deepMergeWithoutCollision<A extends object, B extends object>(a: A, b: B, onerror?: deepMergeWithoutCollision.ErrorProcessor): SimpleDeepMerge<A, B>; | ||
| export declare namespace deepMergeWithoutCollision { | ||
| interface ErrorProcessor { | ||
| (param: ErrorProcessorParam): unknown; | ||
| } | ||
| type ErrorProcessorParam = PropertyCollision; | ||
| const enum ErrorType { | ||
| PropertyCollision = 1 | ||
| } | ||
| interface PropertyCollision { | ||
| type: ErrorType.PropertyCollision; | ||
| objects: [object, object]; | ||
| key: string | symbol; | ||
| values: [unknown, unknown]; | ||
| } | ||
| } |
+1
-0
| export * from './add-property'; | ||
| export * from './object-extends'; | ||
| export * from './deep-merge'; | ||
| export * from './get-property'; | ||
| export * from './property-path'; | ||
| export * from './call-method'; |
+1
-0
@@ -6,2 +6,3 @@ "use strict"; | ||
| tslib_1.__exportStar(require("./object-extends"), exports); | ||
| tslib_1.__exportStar(require("./deep-merge"), exports); | ||
| tslib_1.__exportStar(require("./get-property"), exports); | ||
@@ -8,0 +9,0 @@ tslib_1.__exportStar(require("./property-path"), exports); |
+1
-0
| export * from "./add-property.mjs"; | ||
| export * from "./object-extends.mjs"; | ||
| export * from "./deep-merge.mjs"; | ||
| export * from "./get-property.mjs"; | ||
| export * from "./property-path.mjs"; | ||
| export * from "./call-method.mjs"; //# sourceMappingURL=index.js.map |
+1
-0
| export * from './add-property'; | ||
| export * from './object-extends'; | ||
| export * from './deep-merge'; | ||
| export * from './get-property'; | ||
| export * from './property-path'; | ||
| export * from './call-method'; |
+2
-2
| { | ||
| "name": "@tsfun/object", | ||
| "version": "0.0.9", | ||
| "version": "0.0.10", | ||
| "description": "Utilities related to objects", | ||
@@ -24,4 +24,4 @@ "author": "Hoàng Văn Khải <hvksmr1996@gmail.com>", | ||
| "tslib": "^1.10.0", | ||
| "@types/node": "^13.1.6" | ||
| "@types/node": "^13.1.7" | ||
| } | ||
| } |
+10
-0
@@ -13,2 +13,12 @@ import { Assign } from 'utility-types'; | ||
| /** | ||
| * Turn a type into a set of properties | ||
| */ | ||
| export declare type Properties<Type> = { | ||
| [key in keyof Type]: Type[key]; | ||
| }; | ||
| /** | ||
| * Merge two types | ||
| */ | ||
| export declare type SimpleDeepMerge<A, B> = Properties<A> & Properties<B>; | ||
| /** | ||
| * Return type of `objectExtends` | ||
@@ -15,0 +25,0 @@ */ |
+10
-0
@@ -13,2 +13,12 @@ import { Assign } from 'utility-types'; | ||
| /** | ||
| * Turn a type into a set of properties | ||
| */ | ||
| export declare type Properties<Type> = { | ||
| [key in keyof Type]: Type[key]; | ||
| }; | ||
| /** | ||
| * Merge two types | ||
| */ | ||
| export declare type SimpleDeepMerge<A, B> = Properties<A> & Properties<B>; | ||
| /** | ||
| * Return type of `objectExtends` | ||
@@ -15,0 +25,0 @@ */ |
31679
33.69%37
12.12%802
46.08%Updated