Skip to content

harvey-woo/key-serializer

Repository files navigation

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

About

serilize keys and query keys deep in an object

Resources

License

Stars

Watchers

Forks

Packages

No packages published