0.1.1 • Published 5 years ago

access-by-path v0.1.1

Weekly downloads
8
License
MIT
Repository
github
Last release
5 years ago

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
0.1.1

5 years ago

0.1.0

5 years ago