Skip to content
This repository has been archived by the owner on Apr 1, 2023. It is now read-only.

Latest commit

 

History

History
175 lines (129 loc) · 5.03 KB

pathUtilities.md

File metadata and controls

175 lines (129 loc) · 5.03 KB

Object Agent

A javascript library for working with objects

npm build coverage deps size vulnerabilities license


appendToPath(path, key, [separator]) ⇒ String

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'


countInString(string, match) ⇒ Number

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


firstInPath(path, [separator]) ⇒ String

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'


initialInPath(path, [separator]) ⇒ String

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'


lastInPath(path, [separator]) ⇒ String

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'


tailInPath(path, [separator]) ⇒ String

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'


walkPath(path, callback, [separator]) ⇒ String

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'