Object helpers sub-module.
- Source:
Classes
Members
(static, constant) CLONE
An enum of supported modes for the clone() method.
- P → All properties. If unchecked, enumerable properties only.
- A → Uses
Array.slice()shortcut for shallowArraycloning. - R → Recursive (deep) cloning of nested objects.
- D → Uses descriptor cloning instead of direct assignment.
- T → Sets the
prototypeof the clone as well.
| Mode | P | A | R | D | T | Notes |
|---|---|---|---|---|---|---|
CLONE.N |
× | × | × | × | × | Can be used to manually specify options. |
CLONE.DEF |
× | ✓ | × | × | × | Default mode for cloning functions. |
CLONE.DEEP |
× | × | ✓ | ✓ | × | |
CLONE.FULL |
✓ | ✓ | × | ✓ | × | |
CLONE.ALL |
✓ | × | × | ✓ | × | |
CLONE.ENTIRE |
✓ | × | ✓ | ✓ | ✓ | |
CLONE.JSON |
- | - | ✓ | - | × | Uses JSON, so no function or symbol support. |
The ✓ and × marks signify the default settings in the mode.
In most cases there are options that can override the
defaults.
Any feature in the CLONE.JSON row marked with - are
incompatible with that mode and cannot be enabled at all.
- Source:
(static) duplicateOne
Make a copy of a single object using copyAll.
Use clone for a more robust version.
Alias: copyAll.clone
- Source:
(static) duplicateOne
Make a new object containing properties from other objects using copyAll.
Use copyProps.into({}).from(...sources) for a more robust version.
Alias: copyAll.duplicate
- Source:
Methods
(static) SOA()
Need a String or Array
(static) addClone(obj, defOptsopt)
Add a clone() method to an object.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
obj |
object | function | The object to add clone() to. |
||
defOpts |
object |
<optional> |
null | Default options for the clone() method. If
|
- Source:
(static) addLock(obj, optsopt) → {object}
Add a lock() method to an object.
Adds a wrapper version of lock() to the object as a method.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
obj |
object | The object we're adding |
|||||||||||||||||||||
opts |
object |
<optional> |
Options and defaults.
In addition to options specific to this function, any options
supported by Properties
|
- Source:
Returns:
obj
- Type
- object
(static) apply(obj, …fns) → {object}
Apply functions to an object
In addition to the parameters passed, we'll also define:
cpwill be aCopyPropsinstance viacopyProps.cache.into(obj);argswill be an array of[obj, opts];optswill be an object of{obj, cp, args};
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
obj |
object | function | The target we're applying to. |
|
fns |
function | object |
<repeatable> |
Values we are applying. For each value of
|
- Source:
Throws:
-
If a
fnsvalue is not valid. - Type
- TypeError
Returns:
obj
- Type
- object
(static) clone(obj, optsopt) → {object}
Clone an object or function.
Parameters:
| Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
obj |
object | The object we want to clone. |
||||||||||||||||||||||||||||||||||||||||||||||||||||
opts |
object | number |
<optional> |
{} | Options for the cloning process. If this is a Properties
|
- Source:
Returns:
The clone of the object.
- Type
- object
(static) cloneIfLocked(obj, optsopt) → {object}
Clone an object if it's not extensible (locked, sealed, frozen, etc.)
If the object is extensible, it's returned as is.
If not, if the object has a clone() method it will be used.
Otherwise use our clone() function.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
obj |
object | The object to clone if needed. |
|
opts |
object |
<optional> |
Options to pass to |
- Source:
Returns:
- Either the original object, or an extensible clone.
- Type
- object
(static) copyAll()
This is a 'dumb' copy method.
It only copies enumerable properties, does no type checking, and has no qualms about overwriting properties.
Use copyProps, or mergeNested for more robust versions.
- Source:
(static) copyProps(source, target, optsopt) → {object}
Copy properties from one object to another.
Parameters:
| Name | Type | Attributes | Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
source |
object | function | The object to copy properties from. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
target |
object | function | The target to copy properties to. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
opts |
object |
<optional> |
Options for how to copy properties. Properties
|
- Source:
Returns:
The target object.
- Type
- object
(static) getNamespace(proppath, optsopt) → {*}
Get a global namespace path if it exists.
This literally just calls getObjectPath() on the root object.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
proppath |
string | Array | Property path we're looking for.
Generally a string of dot ( |
|
opts |
object |
<optional> |
See |
- Source:
- See:
Returns:
The property if found, or opts.default if not.
- Type
- *
(static) getObjectPath(obj, proppath, optsopt) → {*}
Get a (nested) property from an object with a given path.
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
obj |
object | function | Object we're looking in. |
||||||||||||||||
proppath |
string | Array | Property path we're looking for.
Generally a string of dot ( |
||||||||||||||||
opts |
object | boolean |
<optional> |
Options changing the behaviours.
If this is a Properties
|
Returns:
The property if found, or opts.default if not.
- Type
- *
(static) getProperty(obj, prop, defvalopt) → {mixed}
Get a property descriptor.
This is like Object.getOwnPropertyDescriptor, except unlike
that method, this will travel through the entire prototype chain
until it finds the descriptor.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
obj |
object | function | Object to find a property in. |
|
prop |
string | Name of the property we want the descriptor of. |
|
defval |
mixed |
<optional> |
The fallback value if no descriptor is found. |
- Source:
Returns:
The descriptor if found, defval if not.
- Type
- mixed
(static) lock(obj, clonableopt, cloneOptsopt, useSealopt) → {object}
Lock an object using Object.freeze()
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
obj |
object | The object we want to lock. |
||
clonable |
boolean |
<optional> |
true | Pass to |
cloneOpts |
object |
<optional> |
null | Options for |
useSeal |
boolean |
<optional> |
false | Use Object.seal() instead of freeze. If cloneOpts is
|
- Source:
Returns:
The locked object.
- Type
- object
(static) mergeNested(source, target, optsopt) → {object}
Merge two objects recursively.
This used to be a standalone function, but was poorly designed. It's now a wrapper around the copyProps() method.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
source |
object | The source object we're copying from. |
|
target |
object | The target object we're copying into. |
|
opts |
object | boolean |
<optional> |
Options for If Also, if For backwards compatibility, if this specified as a |
- Source:
Returns:
The target object.
- Type
- object
(static) nsArray(ns, name) → {Array.<string>}
Get a namespace path array.
If it's already an array, return it as is.
If it's a string, split it into an array, with the . delimiter.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
ns |
string | Array.<string> | A dotted string, or array of paths. |
|
name |
* | Namespace | Name to use in the SOA error |
Returns:
- Type
- Array.<string>
(static) nsString(ns, name) → {string}
Get a namespace path string.
If it's already a string, return it as is.
If it's an array of strings, join the elements with a . character.
Parameters:
| Name | Type | Default | Description |
|---|---|---|---|
ns |
string | Array.<string> | A dotted string, or array of paths. |
|
name |
* | Namespace | Name to use in the SOA error |
Returns:
- Type
- string
(static) setNamespace(proppath, optsopt) → {*}
Create a global namespace path if it does not exist.
This literally just calls setObjectPath() on the root object.
Parameters:
| Name | Type | Attributes | Description |
|---|---|---|---|
proppath |
string | Array | Property path to create. |
|
opts |
object |
<optional> |
See |
- Source:
- See:
Returns:
See setObjectPath() for details.
- Type
- *
(static) setObjectPath(obj, proppath, optsopt) → {*}
Create a nested property path if it does not exist.
Parameters:
| Name | Type | Attributes | Description | |||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
obj |
object | function | Object the property path is for. |
||||||||||||||||||||||||||||||||||||
proppath |
string | Array | Property path to create. |
||||||||||||||||||||||||||||||||||||
opts |
object |
<optional> |
Options changing the behaviours. Properties
|
Returns:
Generally the last object in the nested property paths.
Unless one of opts.returnThis or opts.returnObj was true.
- Type
- *
(static) syncNested(obj1, obj2, opts1opt, opts2opt)
Synchronize two objects.
Literally just calls mergeNested twice, with the two objects swapped.
Probably has all kinds of screwy behaviours because of how it works.
Parameters:
| Name | Type | Attributes | Default | Description |
|---|---|---|---|---|
obj1 |
object | The first object.
Because overwrite mode is on by default, any properties in |
||
obj2 |
object | The second object.
Any properties in |
||
opts1 |
object |
<optional> |
Options for the first merge operation.
See |
|
opts2 |
object |
<optional> |
opts1 | Options for the second merge operation.
If this is not specified, |
- Source: