1.0.3 • Published 5 years ago

safe-object2 v1.0.3

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

safe-object

安全的操作对象的 getter 和 setter

example

yarn add safe-object2
or
npm install safe-object2

const safeObj2 = require("safe-object2");
const a = safeObj2({ a: 11, b: { c: [1, 2] }, c: { d: { x: "", b: 1 } } });
 console.log(a.setter(["z", "zd", 0], 222));
//{ a: 11, b: { c: [ 1, 2 ] },c: { d: { x: '', b: 1 } },z: { zd: [ 222 ] } }

 const objs = { a: { b: "c", d: { x: [1], foo: "" } } };
 const safe = safeObject(objs);
 safe.getter(["a", "b", "d", "foo", "x"]) // '' empty string
 safe.getter(["a", "b", "d", "foo", "x"], "a") // 'a'

API

nameparams1params2return
getterpaths (string | array)defaultValue("")(value | default)
setterpaths (string | array)valueobject

⚠ 注意 ️

1.不支持 Map,Set,WeakMap,WeakSet.