Additional methods for the javascript Function object
- Copyright:
- Copyright (C) 1985..2021 Martin Baker. http://y-d-r.co.uk
- License:
- ISC Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Methods
-
inner assign()(…objects) → {Object}
-
perform an assign of all the enumerable own properties of the given object
Unlike
Object.assign()this will copy all the defined properties of the submitted items - so eg if your submitted object is writeOnly, it will still be copied leaving a write only result. You should not include thethisobject in the arguments list.Beware that when assigning functions, getters and setters, that they still remain those functions in their original objects but being called with
thisbeing the current object into which they are now also inserted.If a key in the current object is updated, then any defined properties you have already set are also overwritten.
Parameters:
Name Type Attributes Description objectsobjects <repeatable>
any number of objects whose members are to be assigned to this current object
Throws:
-
if a key in this current object cannot be updated because the key is already in use and is not updatable
- Type
- Error
Returns:
Object -being the same as this current object
-
-
inner deepMerge()(mergeItems, settingsopt) → {Object}
-
Perform a deep merge of the current object with another object revising the current object
A scan is made of all the enumerable own properties of the given ** mergeItems ** and the action is chosen as below for each result found
-
if the result is undefined the value is deleted from the current object if it already exists
-
if the result is another native {Object} it will also have a
wider_deepMerge()method - this is called recursively to merge into the matching keyName of the current item (creating the keyName's empty object first if one does not yet exist) -
if the result is any other object the object itself is placed as this current value
-
for any other value, eg string or function, the keyName is set to the value, retaining the defined properties of the new value (eg immutability, readonly, etc)
Important, child objects being deepMerged are never linked as the entire object - only enumerable members are processed. This means that the mergeItems object will never reflect changes in the current item and changes in the current item will not be reflected into the mergeItems
This does not perform the same impact as the package "npm install deepMerge" - the main differences are that object.wider_deepMerge():
-
is this faster or significantly faster
-
updates your original object in situ so that any prior references to objects in it now refer to same object with possibly amended properties
-
has control as to whether matching keyNames defer or overwrite
-
has control over error handling
-
has different management of merging arrays
Parameters:
Name Type Attributes Description mergeItemsObject new items to merge into the current object.
settingsobject <optional>
settings that impact the behaviour - to use more than one value on the same key use a space separated list
{ array : "push"}if the new and the old values are arrays the new values are pushed into the array. As this always creates new elements in the array, this overrides the action of mode for arrays{ array : "pushUnique"}as for push except that duplicate values are discarded{ array : "unshift"}if the new and the old values are arrays the new values are unshifted into the array. This overrides the action of mode for arrays{ array : "unshiftUnique"}as for push except that duplicate values are discarded- `{ error : fn(Error) } if an error occurs it is sent to the function (eg console.error) and not, as per default, thrown. The value passes is the same error object that would have been thrown
- `{ mode : 'defer' } if a value appears in the mergeItems that is already in original object, the original object is not updated - the default is overwrite*
- '{ custom : {handler : fn(path,value), pattern : regExp]}` if the path to the item matches the regExp the callback is used to process the behaviour
Throws:
-
when any of the following occur
- if the mergeItem is not a javascript native {Object}
- if the value of a mergeItem keyName is write only
- if the value in the current object of keyName is immutable or readOnly AND is different from the value being supplied
- if you use a combination of settings that when used are incompatible
If an error is thrown and not trapped by your error setting function, your initial object will have been left partially updated
- Type
- Error
Returns:
Object -the current object - use of this return value is optional
-