@homer0/deep-assign
Advanced tools
Comparing version 2.0.1 to 2.0.2
@@ -37,3 +37,10 @@ "use strict"; | ||
var DeepAssign = class { | ||
/** | ||
* @param options Custom options for how the assignation it's going to work. | ||
* @throws {Error} If `options.arrayMode` is not a valid {@link DeepAssignArrayMode}. | ||
*/ | ||
constructor(options = {}) { | ||
/** | ||
* The options that define how {@link DeepAssign#assign} works. | ||
*/ | ||
__publicField(this, "options"); | ||
@@ -49,5 +56,18 @@ if (options.arrayMode && !["merge", "concat", "overwrite", "shallowMerge"].includes(options.arrayMode)) { | ||
} | ||
/** | ||
* Shortcut method to create a new instance and get its `assign` method. | ||
* | ||
* @param options The options for the class constructor. | ||
*/ | ||
static fn(options = {}) { | ||
return new DeepAssign(options).assign; | ||
} | ||
/** | ||
* Makes a deep merge of a list of objects and/or arrays. | ||
* | ||
* @param targets The objects to merge; if one of them is not an object nor an | ||
* array, it will be ignored. | ||
* @template T The type of the object that will be returned. | ||
* @throws {Error} If no targets are sent. | ||
*/ | ||
assign(...targets) { | ||
@@ -62,8 +82,29 @@ if (!targets.length) { | ||
} | ||
/** | ||
* Checks if an object is a plain `Object` and not an instance of some class. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isPlainObject(target) { | ||
return target !== null && Object.getPrototypeOf(target).constructor.name === "Object"; | ||
} | ||
/** | ||
* Checks if an object can be used on a merge: only arrays and plain objects are | ||
* supported. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isValidItem(target) { | ||
return Array.isArray(target) || this.isPlainObject(target); | ||
} | ||
/** | ||
* Merges two arrays into a new one. If the `concatArrays` option was set to `true` on | ||
* the constructor, the result will just be a concatenation with new references for the | ||
* items; but if the option was set to `false`, then the arrays will be merged over | ||
* their indexes. | ||
* | ||
* @param source The base array. | ||
* @param target The array that will be merged on top of `source`. | ||
* @param mode The assignment strategy. | ||
*/ | ||
mergeArrays(source, target, mode) { | ||
@@ -99,2 +140,8 @@ let result; | ||
} | ||
/** | ||
* Merges two plain objects and their children. | ||
* | ||
* @param source The base object. | ||
* @param target The object which properties will be merged in top of `source`. | ||
*/ | ||
mergeObjects(source, target) { | ||
@@ -113,2 +160,17 @@ const useSource = source; | ||
} | ||
/** | ||
* This is the method the class calls when it has to merge two objects and it doesn't | ||
* know which types they are; the method takes care of validating compatibility and | ||
* calling either {@link DeepAssign#mergeObjects} or {@link DeepAssign#mergeArrays}. | ||
* If the objects are not compatible, or `source` is not defined, it will return a copy | ||
* of `target`. | ||
* | ||
* @param source The base object. | ||
* @param target The object that will be merged in top of `source`. | ||
* @param ignoreArrayMode Whether or not to ignore the option that tells the class | ||
* how array assignments should be handled. This parameter | ||
* exists because, when called directly from | ||
* {@link DeepAssign#assign}, it doesn't make sense to use a | ||
* strategy different than 'merge'. | ||
*/ | ||
resolve(source, target, ignoreArrayMode = false) { | ||
@@ -133,2 +195,17 @@ let result; | ||
} | ||
/** | ||
* This method is a helper for {@link DeepAssign#resolve}, and it's used for when the | ||
* class has the `target` but not the `source`: depending on the type of the `target`, | ||
* it calls resolves with an empty object of the same type; if the `target` can't be | ||
* merged, it just returns it as it was received, which means that is a type that | ||
* doesn't hold references. | ||
* | ||
* @param target The target to copy. | ||
* @param [ignoreArrayMode=false] Whether or not to ignore the option that tells the | ||
* class how array assignments should be handled. | ||
* This parameter exists because, when called | ||
* directly from {@link DeepAssign#assign}, it | ||
* doesn't make sense to use a strategy different | ||
* than 'merge'. | ||
*/ | ||
resolveFromEmpty(target, ignoreArrayMode = false) { | ||
@@ -135,0 +212,0 @@ let result; |
@@ -10,3 +10,10 @@ var __defProp = Object.defineProperty; | ||
var DeepAssign = class { | ||
/** | ||
* @param options Custom options for how the assignation it's going to work. | ||
* @throws {Error} If `options.arrayMode` is not a valid {@link DeepAssignArrayMode}. | ||
*/ | ||
constructor(options = {}) { | ||
/** | ||
* The options that define how {@link DeepAssign#assign} works. | ||
*/ | ||
__publicField(this, "options"); | ||
@@ -22,5 +29,18 @@ if (options.arrayMode && !["merge", "concat", "overwrite", "shallowMerge"].includes(options.arrayMode)) { | ||
} | ||
/** | ||
* Shortcut method to create a new instance and get its `assign` method. | ||
* | ||
* @param options The options for the class constructor. | ||
*/ | ||
static fn(options = {}) { | ||
return new DeepAssign(options).assign; | ||
} | ||
/** | ||
* Makes a deep merge of a list of objects and/or arrays. | ||
* | ||
* @param targets The objects to merge; if one of them is not an object nor an | ||
* array, it will be ignored. | ||
* @template T The type of the object that will be returned. | ||
* @throws {Error} If no targets are sent. | ||
*/ | ||
assign(...targets) { | ||
@@ -35,8 +55,29 @@ if (!targets.length) { | ||
} | ||
/** | ||
* Checks if an object is a plain `Object` and not an instance of some class. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isPlainObject(target) { | ||
return target !== null && Object.getPrototypeOf(target).constructor.name === "Object"; | ||
} | ||
/** | ||
* Checks if an object can be used on a merge: only arrays and plain objects are | ||
* supported. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isValidItem(target) { | ||
return Array.isArray(target) || this.isPlainObject(target); | ||
} | ||
/** | ||
* Merges two arrays into a new one. If the `concatArrays` option was set to `true` on | ||
* the constructor, the result will just be a concatenation with new references for the | ||
* items; but if the option was set to `false`, then the arrays will be merged over | ||
* their indexes. | ||
* | ||
* @param source The base array. | ||
* @param target The array that will be merged on top of `source`. | ||
* @param mode The assignment strategy. | ||
*/ | ||
mergeArrays(source, target, mode) { | ||
@@ -72,2 +113,8 @@ let result; | ||
} | ||
/** | ||
* Merges two plain objects and their children. | ||
* | ||
* @param source The base object. | ||
* @param target The object which properties will be merged in top of `source`. | ||
*/ | ||
mergeObjects(source, target) { | ||
@@ -86,2 +133,17 @@ const useSource = source; | ||
} | ||
/** | ||
* This is the method the class calls when it has to merge two objects and it doesn't | ||
* know which types they are; the method takes care of validating compatibility and | ||
* calling either {@link DeepAssign#mergeObjects} or {@link DeepAssign#mergeArrays}. | ||
* If the objects are not compatible, or `source` is not defined, it will return a copy | ||
* of `target`. | ||
* | ||
* @param source The base object. | ||
* @param target The object that will be merged in top of `source`. | ||
* @param ignoreArrayMode Whether or not to ignore the option that tells the class | ||
* how array assignments should be handled. This parameter | ||
* exists because, when called directly from | ||
* {@link DeepAssign#assign}, it doesn't make sense to use a | ||
* strategy different than 'merge'. | ||
*/ | ||
resolve(source, target, ignoreArrayMode = false) { | ||
@@ -106,2 +168,17 @@ let result; | ||
} | ||
/** | ||
* This method is a helper for {@link DeepAssign#resolve}, and it's used for when the | ||
* class has the `target` but not the `source`: depending on the type of the `target`, | ||
* it calls resolves with an empty object of the same type; if the `target` can't be | ||
* merged, it just returns it as it was received, which means that is a type that | ||
* doesn't hold references. | ||
* | ||
* @param target The target to copy. | ||
* @param [ignoreArrayMode=false] Whether or not to ignore the option that tells the | ||
* class how array assignments should be handled. | ||
* This parameter exists because, when called | ||
* directly from {@link DeepAssign#assign}, it | ||
* doesn't make sense to use a strategy different | ||
* than 'merge'. | ||
*/ | ||
resolveFromEmpty(target, ignoreArrayMode = false) { | ||
@@ -108,0 +185,0 @@ let result; |
@@ -37,3 +37,10 @@ "use strict"; | ||
var DeepAssign = class { | ||
/** | ||
* @param options Custom options for how the assignation it's going to work. | ||
* @throws {Error} If `options.arrayMode` is not a valid {@link DeepAssignArrayMode}. | ||
*/ | ||
constructor(options = {}) { | ||
/** | ||
* The options that define how {@link DeepAssign#assign} works. | ||
*/ | ||
__publicField(this, "options"); | ||
@@ -49,5 +56,18 @@ if (options.arrayMode && !["merge", "concat", "overwrite", "shallowMerge"].includes(options.arrayMode)) { | ||
} | ||
/** | ||
* Shortcut method to create a new instance and get its `assign` method. | ||
* | ||
* @param options The options for the class constructor. | ||
*/ | ||
static fn(options = {}) { | ||
return new DeepAssign(options).assign; | ||
} | ||
/** | ||
* Makes a deep merge of a list of objects and/or arrays. | ||
* | ||
* @param targets The objects to merge; if one of them is not an object nor an | ||
* array, it will be ignored. | ||
* @template T The type of the object that will be returned. | ||
* @throws {Error} If no targets are sent. | ||
*/ | ||
assign(...targets) { | ||
@@ -62,8 +82,29 @@ if (!targets.length) { | ||
} | ||
/** | ||
* Checks if an object is a plain `Object` and not an instance of some class. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isPlainObject(target) { | ||
return target !== null && Object.getPrototypeOf(target).constructor.name === "Object"; | ||
} | ||
/** | ||
* Checks if an object can be used on a merge: only arrays and plain objects are | ||
* supported. | ||
* | ||
* @param target The object to validate. | ||
*/ | ||
isValidItem(target) { | ||
return Array.isArray(target) || this.isPlainObject(target); | ||
} | ||
/** | ||
* Merges two arrays into a new one. If the `concatArrays` option was set to `true` on | ||
* the constructor, the result will just be a concatenation with new references for the | ||
* items; but if the option was set to `false`, then the arrays will be merged over | ||
* their indexes. | ||
* | ||
* @param source The base array. | ||
* @param target The array that will be merged on top of `source`. | ||
* @param mode The assignment strategy. | ||
*/ | ||
mergeArrays(source, target, mode) { | ||
@@ -99,2 +140,8 @@ let result; | ||
} | ||
/** | ||
* Merges two plain objects and their children. | ||
* | ||
* @param source The base object. | ||
* @param target The object which properties will be merged in top of `source`. | ||
*/ | ||
mergeObjects(source, target) { | ||
@@ -113,2 +160,17 @@ const useSource = source; | ||
} | ||
/** | ||
* This is the method the class calls when it has to merge two objects and it doesn't | ||
* know which types they are; the method takes care of validating compatibility and | ||
* calling either {@link DeepAssign#mergeObjects} or {@link DeepAssign#mergeArrays}. | ||
* If the objects are not compatible, or `source` is not defined, it will return a copy | ||
* of `target`. | ||
* | ||
* @param source The base object. | ||
* @param target The object that will be merged in top of `source`. | ||
* @param ignoreArrayMode Whether or not to ignore the option that tells the class | ||
* how array assignments should be handled. This parameter | ||
* exists because, when called directly from | ||
* {@link DeepAssign#assign}, it doesn't make sense to use a | ||
* strategy different than 'merge'. | ||
*/ | ||
resolve(source, target, ignoreArrayMode = false) { | ||
@@ -133,2 +195,17 @@ let result; | ||
} | ||
/** | ||
* This method is a helper for {@link DeepAssign#resolve}, and it's used for when the | ||
* class has the `target` but not the `source`: depending on the type of the `target`, | ||
* it calls resolves with an empty object of the same type; if the `target` can't be | ||
* merged, it just returns it as it was received, which means that is a type that | ||
* doesn't hold references. | ||
* | ||
* @param target The target to copy. | ||
* @param [ignoreArrayMode=false] Whether or not to ignore the option that tells the | ||
* class how array assignments should be handled. | ||
* This parameter exists because, when called | ||
* directly from {@link DeepAssign#assign}, it | ||
* doesn't make sense to use a strategy different | ||
* than 'merge'. | ||
*/ | ||
resolveFromEmpty(target, ignoreArrayMode = false) { | ||
@@ -135,0 +212,0 @@ let result; |
{ | ||
"name": "@homer0/deep-assign", | ||
"description": "Deep merge (and copy) of objects and Arrays using native spread syntax", | ||
"version": "2.0.1", | ||
"version": "2.0.2", | ||
"repository": { | ||
@@ -26,6 +26,6 @@ "type": "git", | ||
"devDependencies": { | ||
"jest": "^29.4.1", | ||
"jest": "^29.4.3", | ||
"ts-jest": "^29.0.5", | ||
"tsup": "^6.5.0", | ||
"typescript": "^4.9.4" | ||
"tsup": "^6.6.3", | ||
"typescript": "^4.9.5" | ||
}, | ||
@@ -46,3 +46,3 @@ "engine-strict": true, | ||
}, | ||
"gitHead": "59c0f6af6901b9f4edb8d2ec36abaf11f6b0780d" | ||
"gitHead": "1ed6ea29e2035f10f0dff60203b6b1d54c601dc8" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
58092
670