A javascript library for working with objects
Adds a key to the end of a path
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
key | String , Number |
||
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { appendToPath } from 'object-agent';
appendToPath('first.0', 'last');
// => 'first.0.last'
Counts the number of instances of a string within another string
Param | Type |
---|---|
string | String |
match | String |
Example
import { countInString } from 'object-agent';
countInString('first.0', '.');
// => 1
Returns the first key in a path
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { firstInPath } from 'object-agent';
firstInPath('first.0.last');
// => 'first'
Returns the path without the last key
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { initialInPath } from 'object-agent';
initialInPath('first.0.last');
// => 'first.0'
Returns the last key in a path
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { lastInPath } from 'object-agent';
lastInPath('first.0.last');
// => 'last'
Returns the path without the first key
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { tailInPath } from 'object-agent';
tailInPath('first.0.last');
// => '0.last'
Calls a callback for every key in a path. If true is returned from the callback then no further calls will be made.
Param | Type | Default | Description |
---|---|---|---|
path | String |
||
callback | function |
Provides two args, the key and the tail path after key | |
[separator] | String |
. |
Defines the boundary between steps in the path. |
Example
import { walkPath } from 'object-agent';
walkPath('first.0.last', (key, tail) => {
console.log(key, tail);
});
// => 'first', '0.last'
// => '0', 'last'
// => 'last', ''
walkPath('first.0.last', (key, tail) => {
console.log(key, tail);
if (key === '0') {
return true;
}
});
// => 'first', '0.last'
// => '0', 'last'