Constructor
new ObjectUtils()
- Source:
- Tutorials:
Throws:
-
If instantiated. This class is meant to be have only static methods.
- Type
- Error
Methods
(static) copy(target) → {Object}
- Source:
Creates a deep copy of a given object.
Parameters:
Name | Type | Description |
---|---|---|
target |
Object | The object to copy. |
Returns:
- Type
- Object
(static) dashToLowerCamelKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
dash-case
to lowerCamelCase
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) dashToSnakeKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
dash-case
to snake_case
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) delete(target, objPath, pathDelimiteropt, cleanEmptyPropertiesopt, failWithErroropt) → {Object}
- Source:
Deletes a property of an object using a path.
Example
const target = {
propOne: {
propOneSub: 'Charito!',
},
propTwo: '!!!',
};
console.log(ObjectUtils.delete(target, 'propOne.propOneSub'));
// Will output { propTwo: '!!!' }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object from where the property will be removed. |
||
objPath |
string | The path to the property. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
cleanEmptyProperties |
boolean |
<optional> |
true
|
If this flag is |
failWithError |
boolean |
<optional> |
false
|
Whether or not to throw an error when
the path is invalid. If this is
|
Returns:
A copy of the original object with the removed property/properties.
- Type
- Object
(static) extract(target, objPaths, pathDelimiteropt, failWithErroropt) → {Object}
- Source:
Extracts a property or properties from an object in order to create a new one.
Example
const target = {
name: {
first: 'Rosario',
},
age: 3,
address: {
planet: 'earth',
something: 'else',
},
};
console.log(
ObjectUtils.set(obj, [{ name: 'name.first' }, 'age', 'address.planet']),
);
// Will output { name: 'Rosario', age: 3, address: { planet: 'earth' } }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object from where the property/properties will be extracted. |
||
objPaths |
ObjectUtilsExtractPath | Array.<ObjectUtilsExtractPath> | This can be a single path or a list of them. And for this method, the paths are not only strings but can also be an object with a single key, the would be the path to where to "do the extraction", and the value the path on the target object. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
failWithError |
boolean |
<optional> |
false
|
Whether or not to throw an error when the path is invalid. If this is |
Returns:
- Type
- Object
(static) flat(target, pathDelimiteropt, prefixopt, shouldFlatternopt, nullable) → {Object}
- Source:
Flatterns an object properties into a single level dictionary.
Example
const target = {
propOne: {
propOneSub: 'Charito!',
},
propTwo: '!!!',
};
console.log(ObjectUtils.flat(target);
// Will output { 'propOne.propOneSub': 'Charito!', propTwo: '!!!' }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object to transform. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
prefix |
string |
<optional> |
''
|
A custom prefix to be added before the name of the properties. This can be used on custom cases and it's also used when the method calls itself in order to flattern a sub object. |
shouldFlattern |
ObjectUtilsShouldFlatFn |
<optional> <nullable> |
null
|
A custom function that can be used in order to tell the method whether an Object or an Array property should be flattern or not. It will receive the key for the property and the Object/Array itself. |
Returns:
- Type
- Object
(static) formatKeys(target, searchExpression, replaceWith, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
Formats all the keys on an object using a way similar to .replace(regexp, ...)
but
that also works recursively and with "object paths".
Example
const target = {
prop_one: 'Charito!',
};
console.log(
ObjectUtils.formatKeys(
target,
// Find all the keys with snake case.
/([a-z])_([a-z])/g,
// Using the same .replace style callback, replace it with lower camel case.
(fullMatch, firstLetter, secondLetter) => {
const newSecondLetter = secondLetter.toUpperCase();
return `${firstLetter}${newSecondLetter}`;
},
),
);
// Will output { propOne: 'Charito!}.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
searchExpression |
RegExp | The regular expression the method will use "match" the keys. |
||
replaceWith |
function | The callback the method will call when
formatting a replacement. Think of
|
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) get(target, objPath, pathDelimiteropt, failWithErroropt) → {*}
- Source:
Returns the value of an object property using a path.
Example
const obj = {
propOne: {
propOneSub: 'Charito!',
},
propTwo: '!!!',
};
console.log(ObjectUtils.get(obj, 'propOne.propOneSub'));
// Will output 'Charito!'
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object from where the property will be read. |
||
objPath |
string | The path to the property. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
failWithError |
boolean |
<optional> |
false
|
Whether or not to throw an error when the
path is invalid. If this is |
Throws:
-
If the path is invalid and
failWithError
is set totrue
. - Type
- Error
Returns:
- Type
- *
(static) lowerCamelToDashKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
lowerCamelCase
to dash-case
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) lowerCamelToSnakeKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
lowerCamelCase
to snake_case
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) merge(…targets) → {Object}
- Source:
This method makes a deep merge of a list of objects into a new one. The method also supports arrays.
Examples
const objA = { a: 'first' };
const objB = { b: 'second' };
console.log(ObjectUtils.merge(objA, objB));
// Will output { a: 'first', b: 'second' }
const arrA = [{ a: 'first' }];
const arrB = [{ b: 'second' }];
console.log(ObjectUtils.merge(objA, objB));
// Will output [{ a: 'first', b: 'second' }]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
targets |
Object |
<repeatable> |
The objects to merge. |
Returns:
- Type
- Object
(static) set(target, objPath, value, pathDelimiteropt, failWithErroropt) → {Object}
- Source:
Sets a property on an object using a path. If the path doesn't exist, it will be created.
Example
const target = {};
console.log(ObjectUtils.set(target, 'some.prop.path', 'some-value'));
// Will output { some: { prop: { path: 'some-value' } } }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object where the property will be set. |
||
objPath |
string | The path for the property. |
||
value |
* | The value to set on the property. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
failWithError |
boolean |
<optional> |
false
|
Whether or not to throw an error when the
path is invalid. If this is |
Throws:
-
If one of the path components is for a non-object property and
failWithError
is set totrue
. - Type
- Error
Returns:
A copy of the original object with the added property/properties.
- Type
- Object
(static) snakeToDashKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
snake_case
to dash-case
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) snakeToLowerCamelKeys(target, includeopt, excludeopt, pathDelimiteropt) → {Object}
- Source:
A shorthand method for ObjectUtils.formatKeys
that transforms the keys from
snake_case
to lowerCamelCase
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object for format. |
||
include |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation will be made. If not specified, the method will use all the keys from the object. |
exclude |
Array.<string> |
<optional> |
[]
|
A list of keys or paths where the transformation won't be made. |
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path
components for both |
Returns:
- Type
- Object
(static) unflat(target, pathDelimiteropt) → {Object}
- Source:
This method does the exact opposite from flat
: It takes an already flattern object
and restores it structure.
Example
const target = {
'propOne.propOneSub': 'Charito!',
propTwo: '!!!',
};
console.log(ObjectUtils.unflat(target));
// Will output { propOne: { propOneSub: 'Charito!' }, 'propTwo': '!!!' }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
target |
Object | The object to transform. |
||
pathDelimiter |
string |
<optional> |
'.'
|
The delimiter that will separate the path components. |
Returns:
- Type
- Object