Class: CopyProps

@lumjs/core/obj~CopyProps()

A class providing a declarative copyProps() API, which makes it easy to copy one or more sources, into one or more targets.

This class is not directly accessible, and instead is available via some special sub-methods of copyProps; examples:

// Get a copy of the `copyProps` function.
const cp = require('@lumjs/core').obj.copyProps;

// Starting with the target(s):
cp.into(targetObj).given({all: true}).from(source1, source2);

// Starting with the source(s):
cp.from(sourceObj).given({exclude: ['dontCopy']}).into(target1, target2);

// Starting with the options:
cp.given({recursive: cp.RECURSE_ALL}).from(source1, source2).into(target1, target2);

// Call `cp.into()` and cache the instance in a `Map`.
// Future calls with the same `targetObj` will return the cached instance.
// Unlike `cp.into()`, only supports a single `targetObj`.
cp.cache.into(targetObj);

// Call `cp.from()` and cache the instance in a `Map`.
// Future calls with the same `sourceObj` will return the cached instance.
// Unlike `cp.from()`, only supports a single `sourceObj`.
cp.cache.from(sourceObj);

// Clear the `Map` instances for `cp.cache.into` and `cp.cache.from`
cp.cache.clear();

Constructor

new CopyProps()

Source:

Methods

from(…sourcesopt) → {object}

Specify the sources to copy properties from.

Parameters:
Name Type Attributes Description
sources object <optional>
<repeatable>

The source objects.

If this.targets has objects in it already, then we'll run copyProps() for each of the sources into each of the targets.

If this.targets is empty, then this will set this.sources to the specified value.

You can specify no sources at all to clear the currently set this.sources value.

Source:
Returns:

this

Type
object

given(opts) → {object}

Set all options.

This replaces any existing options entirely.

Parameters:
Name Type Description
opts object

The options to set.

Source:
Returns:

this

Type
object

into(…targetsopt) → {object}

Specify the targets to copy properties into.

Parameters:
Name Type Attributes Description
targets object <optional>
<repeatable>

The target objects

If this.sources has objects in it already, then we'll run copyProps() for each of the sources into each of the targets.

If this.sources is empty, then this will set this.target to the specified value.

You can specify no sources at all to clear the currently set this.targets value.

Source:
Returns:

this

Type
object

set(opt, value) → {object}

Set options.

Parameters:
Name Type Description
opt string | object

Option(s) to set.

If this is a string it's the name of an option to set. If this is an object, it's map of options to set with copyAll.

value *

The option value.

Only used if option is a string.

Source:
Returns:

this

Type
object