new ElasticObject(dataopt)
Implementation on top of plain objects that brings a variety of array-like functionality.
It also supports dotted.string.notation
for accessing properties.
- Source:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
data |
Object
|
<optional> |
Extends
- Object
Methods
assign(…sources) → {ElasticObject}
Copies all enumerable own properties from one or more source objects to the Elastic Object.
Instance flavor of ElasticObject.assign()
. Note that this uses this
as the target object.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
sources |
Object
|
<repeatable> |
The source object(s), regular or elastic — objects containing the properties you want to apply. |
Returns:
- Type:
-
ElasticObject
Example
const eObj = new ElasticObject({
a: 1,
b: 2
});
const source1 = {
b: 4,
c: 5
}
const source2 = {
d: 6,
e: 7
}
console.log(eObj.assign(source1, source2)); // ElasticObject { a: 1, b: 4, c: 5, d: 6, e: 7 }
clone() → {ElasticObject}
Get a deep clone of an ElasticObject
- Source:
- See:
Returns:
- Type:
-
ElasticObject
Example
const eObj = new ElasticObject({
a: {
b: {
c: 1
}
}
});
const clone = eObj.clone();
console.log(clone.get('a.b.c')); // 1
console.log(eObj.get('a.b.c')); // 1
eObj.set('a.b.c', 2);
console.log(clone.get('a.b.c')); // 1
console.log(eObj.get('a.b.c')); // 2
cloneProperty(path) → {*}
Returns a deep clone of a particular property
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
path |
String
|
Returns:
- Type:
-
*
ElasticObject if possible, otherwise the value
Example
const eObj = new ElasticObject({
a: {
b: {
c: 1
}
}
});
const clone = eObj.cloneProperty('a.b');
console.log(clone.get('a.b')); // { c: 1 }
console.log(eObj.get('a.b')); // { c: 1 }
eObj.set('a.b', 2);
console.log(clone.get('a.b')); // { c: 1 }
console.log(eObj.get('a.b')); // 2
entries() → {Array}
Returns an array of the object's own enumerable string-keyed property [key, value] pairs.
Instance flavor of ElasticObject.entries()
. Note that this uses this
instead of an argument.
Not to be confused with Array.entries()
which returns an iterator rather than an array.
- Source:
- See:
Returns:
- Type:
-
Array
Example
const eObj = new ElasticObject({
a: {
ab: 1
},
b: {
bb: 2
}
});
console.log(eObj.entries()); // [["a",{"ab":1}],["b",{"bb":2}]]
every(callbackFn, thisArgopt) → {Boolean}
Checks whether all entries satisfy the provided callback function.
Equivalent of Array.every()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, entries [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
Boolean
Example
const eObj = new ElasticObject({
a: 1,
b: 2,
c: 3
});
console.log(eObj.every(value => typeof value === 'number')); // true
filter(callbackFn, thisArgopt) → {ElasticObject}
Returns an ElasticObject with all entries that satisfy the provided callback function.
Equivalent of Array.filter()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, entries [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
ElasticObject
Example
const eObj = new ElasticObject({
a: 1,
b: {
bb: 2
},
c: 3,
d: 'foo'
});
console.log(eObj.filter(value => typeof value === 'number')); // {a:1,c:3}
find(callbackFn, thisArgopt) → {*}
Returns the first element that satisfies the provided callback function.
Equivalent of Array.find()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, entries [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
*
Example
const eObj = new ElasticObject({
a: 1,
b: {
bb: 2
},
c: 3,
d: 'foo'
});
console.log(eObj.find(value => typeof value === 'number' && value > 1)); // 3
findPath(callbackFn, thisArgopt) → {String|undefined}
Returns the path of the first value that matches the condition in the callback function.
Equivalent of Array.findIndex()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, flattenedObject [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
String
|undefined
Example
const eObj = new ElasticObject({
a: 1,
b: {
bb: 2
},
c: 3,
d: 'foo'
});
console.log(eObj.findPath(value => typeof value === 'number' && value > 1)); // 'b.bb'
flatten() → {ElasticObject}
Retrieve a version of the object with all 'keys.flattened.to.paths'.
- Source:
Returns:
- Type:
-
ElasticObject
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.flatten()); // ElasticObject { a: { aa: 1 }, 'a.aa': 1, b: { bb: 2 }, 'b.bb': 2 }
forEach(callbackFn, thisArgopt) → {undefined}
Equivalent of Array.forEach()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, entries [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
undefined
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
eObj.forEach((value, path) => console.log(value)); // {a.aa: 1, b.bb: 2}
fromEntries(iterable) → {ElasticObject}
Transforms a list of key-value pairs into an Elastic Object.
Instance flavor of ElasticObject.fromEntries()
.
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
iterable |
Iterable
|
An iterable such as Array or Map or other objects implementing the iterable protocol. |
Returns:
- Type:
-
ElasticObject
Example
const entries = new Map([
['foo', 'bar'],
['baz', 42]
]);
const obj = Object.fromEntries(entries);
console.log(obj); // ElasticObject { foo: 'bar', baz: 42 }
get(path) → {*}
Gets the value at path of object. If the resolved value is a plain object, it will be converted to a ElasticObject
.
Check https://www.npmjs.com/package/dot-prop#readme for an example of how to escape dots in keys with \\
Parameters:
Name | Type | Description |
---|---|---|
path |
Array
|
string
|
The path of the property to get |
Returns:
- Type:
-
*
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: function() {}
}
});
console.log(eObj.get('a.aa')); // 1
console.log(eObj.get('b.bb')); // [Function: bb]
console.log(eObj.get('c.cc')); // undefined
console.log(eObj.get('c.cc', 'default')); // default
console.log(eObj.get('b.bb', () => 'default')); // [Function: bb]
console.log(eObj.get('a')); // { aa: 1 }
has(path) → {Boolean}
Checks if path is a direct property of object.
Parameters:
Name | Type | Description |
---|---|---|
path |
Array
|
string
|
The path to check |
Returns:
- Type:
-
Boolean
Returns true if path exists, else false.
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.has('a.aa')); // true
console.log(eObj.has('c.cc')); // false
console.log(eObj.has('a')); // true
includes(searchElement) → {Boolean}
Checks whether the object includes searchElement
.
Equivalent of Array.includes()
, though without the fromIndex
argument. Like its array counterpart, it con only search for primitive values, not for objects.
- Source:
- See:
Parameters:
Name | Type | Description |
---|---|---|
searchElement |
*
|
The value to search for |
Returns:
- Type:
-
Boolean
Example
const eObj = new ElasticObject({
a: 1,
b: 2
});
console.log(eObj.includes(1)); // true
console.log(eObj.includes(3)); // false
keys() → {Array}
Retrieve an array of all keys at the top level of the object, equivalent to Object.keys(eObj)
.
Instance flavor of ElasticObject.assign()
.
- Source:
- See:
Returns:
- Type:
-
Array
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.keys()); // ['a','b']
length() → {Number}
Get the number of keys at the top level of the object.
Equivalent of Array.length
, but implemented as a function.
- Source:
- See:
Returns:
- Type:
-
Number
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.length()); // 2
loadPlugins(plugins)
Load the plugins. This method can also be used to add new plugins Elastic Objects created with
ElasticObject.create()
, ElasticObject.createFrom()
or ElasticObject.assign()
.
Note that in this case this method needs to be called before the ElasticObject
is used.
Parameters:
Name | Type | Description |
---|---|---|
plugins |
Object
|
An object containing additinal methods. |
Example
const myPlugins = {
methodA: function() {
console.log(this);
}
// more methods
}
const eObj = ElasticObject.create({a: 1, b: 2});
eObj.loadPlugins(myPlugins);
eObj.methodA(); // logs ElasticObject { bar: 42 }
map(callbackFn, thisArgopt) → {ElasticObject}
Creates a new ElasticObject populated with the results of calling a callback function on every element the original object.
Equivalent of Array.map()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
callbackFn Args: value, path, entries [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
ElasticObject
Example
const eObj = new ElasticObject({
a: 1,
b: 2
});
const newHObj = eObj.map((value, path) => value * 2);
console.log(newHObj.get('a')); // 2
paths() → {Array}
Retrieve an array with all 'keys.flattened.to.paths'
- Source:
Returns:
- Type:
-
Array
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.paths()); // ['a.aa','b.bb']
reduce(callbackFn, initialValueopt, thisArgopt) → {*}
Executes a user-supplied "reducer" callback function on each entry of the object, in order, passing in the return value from the calculation on the preceding entry. The final result of running the reducer across all entries of the object is a single value. Equivalent of Array.reduce
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: accumulator, value, path, values [, thisArg] |
|
initialValue |
Object
|
undefined
|
<optional> |
Value to use as the first argument to the first call of the callback. |
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
*
Example
const obj = {
a: 1,
b: 2,
c: 3,
};
const result = obj.reduce((accumulator, value, path, values) => {
return accumulator + value;
}, 0);
console.log(result); // 6
reduceRight(callbackFn, initialValueopt, thisArgopt) → {*}
Applies a function against an accumulator and each entry of the object (from right-to-left) to reduce it to a single value. Equivalent of Array.reduceRight
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: accumulator, value, path, values [, thisArg] |
|
initialValue |
Object
|
undefined
|
<optional> |
Value to use as the first argument to the first call of the callback. |
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
*
Example
const obj = {
a: 'l',
b: 't',
c: 'r'
};
const result = obj.reduceRight((accumulator, value, path, values) => {
return accumulator + value;
}, 0);
console.log(result); // 'rtl'
set(path, value) → {ElasticObject}
Sets the value at path of object. If a portion of path doesn't exist, it will be created. Arrays are created for missing index properties while objects are created for all other missing properties.
Parameters:
Name | Type | Description |
---|---|---|
path |
Array
|
string
|
The path of the property to set |
value |
*
|
Returns:
- Type:
-
ElasticObject
Example
const obj = new ElasticObject({
a: {
b: {
c: 1
}
}
});
obj.set('a.b.c', 2); // ElasticObject { a: { b: { c: 2 } } }
obj.get('a.b.c'); // 2
obj.set('a.b.d', 2); // ElasticObject { a: { b: { c: 2, d: 2 } } }
obj.get('a.b.d'); // 2
obj.set('a.b.e.f', 2); // ElasticObject { a: { b: { c: 2, d: 2, e: { f: 2 } } } }
obj.get('a.b.e.f'); // 2
some(callbackFn, thisArgopt) → {Boolean}
Tests whether at least one entry in the object passes the test implemented by the callback function.
Equivalent of Array.some()
.
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
callbackFn |
function
|
Args: value, path, flattenedObject [, thisArg] |
|
thisArg |
Object
|
undefined
|
<optional> |
Value to use as |
Returns:
- Type:
-
Boolean
Example
const obj = {
a: 1,
b: 2,
c: 3,
};
const result = obj.some((value, path, flattenedObject) => {
return value > 2;
});
console.log(result); // true
sort(compareFnopt) → {ElasticObject}
Sort an array by its values, optionally with a comparator function. Key-value associations are preserved. Equivalent of Array.sort
- Source:
- See:
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
compareFn |
function
|
<optional> |
Args: a, b |
Returns:
- Type:
-
ElasticObject
Example
const obj = {
a: 3,
b: 2,
c: 1,
};
const sorted = obj.sort((a, b) => {
return a - b;
});
console.log(sorted); // ElasticObject { c: 1, b: 2, a: 3 }
console.log(sorted.keys()); // ['c', 'b', 'a']
console.log(sorted.values()); // [1, 2, 3]
toJson(prettyopt) → {String}
Convert the object to JSON
- Source:
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
pretty |
Boolean
|
<optional> |
false |
Returns:
- Type:
-
String
Example
const eObj = new ElasticObject({
a: {
aa: 1
},
b: {
bb: 2
}
});
console.log(eObj.toJSON()); // {"a":{"aa":1},"b":{"bb":2}}
console.log(eObj.toJSON(true));
// {
// "a": {
// "aa": 1
// },
// "b": {
// "bb": 2
// }
// }
unset(path) → {Boolean}
Removes the property at path of object.
Parameters:
Name | Type | Description |
---|---|---|
path |
Array
|
string
|
The path of the property to unset |
Returns:
- Type:
-
Boolean
Example
const obj = new ElasticObject({
a: {
b: {
c: 1
}
});
obj.unset('a.b.c');
obj.get('a.b.c'); // undefined
values() → {Array}
Equivalent of Array.values, returns an array of all values
- Source:
Returns:
- Type:
-
Array
Example
const obj = new ElasticObject({
a: 1,
b: 2
});
obj.values(); // [1, 2]