npm.io
0.1.1 • Published 7 years ago

access-by-path

Licence
MIT
Version
0.1.1
Deps
1
Size
14 kB
Vulns
0
Weekly
0

access-by-path

NPM version Build Status Downloads License

Functions of getting and setting the values of the object fields by the string representation of the path to them.

Russian readme

Install

npm install access-by-path

Usage

getByPath
import { getByPath } from 'access-by-path';

let obj = { a: 1, b: { c: 2, d: 3}};
let result = getByPath(obj, 'b.d'); // 3

getByPath(obj, path[, defaultValue[, splitChar[, escapeChar]]])

  • obj {Object} - object
  • path {string} - path to field
  • defaultValue {*} [undefined] - return value if the field is not found in object
  • splitChar {string} ['.'] - path delimiter character
  • escapeChar {string} ['\\'] - character to escape delimiter
setByPath
import { setByPath } from 'access-by-path';

let obj = { a: 1, b: { c: 2, d: 3}};
setByPath(obj, 'b.d', 4); // { a: 1, b: { c: 2, d: 4}}

setByPath(obj, path, value[, splitChar[, escapeChar]])

  • obj {Object} - object
  • path {string} - path to field
  • value {*} - set value
  • splitChar {string} ['.'] - path delimiter character
  • escapeChar {string} ['\\'] - character to escape delimiter

Testing

To run tests, use:

npm test