@cat5th/key-serializer
TypeScript icon, indicating that this package has built-in type declarations

0.2.5 • Public • Published

key-serializer

serilize keys and query keys deep in an object

documentation

npm version coverage npm downloads Build Status

how to use

It is very easy to get value of keys deep into your object, or stringify keys to a string from array

import serializer

import keySerializer from '@cat5th/key-serializer'

stringify keys

keySerializer.stringify(['a', 1, 'b', 0])
// result: "a[1].b[0]"

parse keys

keySerializer.parse('a[1].b[0]')
// result: ['a', 1, 'b', 0]

query object

const obj = { a: [{ b: [1, 2], c: [3, 4] }, { b: [5, 6], c: [7, 8] }] }
keySerializer.query(obj, 'a[1].b[0]')
// result: { value: 5, key: 0, target: [5, 6]}

get value

keySerializer.get(obj, 'a[1].b[0]')
// result: 5

set value

keySerializer.set(obj, 'a[1].b[0]', 1)
console.log(obj.a[1].b[0])
// result: 1

notice: get/set/query function will throw TypeError when target is null or undefined, keep in mind, and always use get/query in try block

Readme

Keywords

Package Sidebar

Install

npm i @cat5th/key-serializer

Weekly Downloads

0

Version

0.2.5

License

MIT

Unpacked Size

350 kB

Total Files

23

Last publish

Collaborators

  • alphmega