我想要有个公共
options, 但可以通过不同的ID, justinit/get/set
npm install weoptions
yarn add weoptions
node example/main.jsresult
source:yobrave > in main.js get
yobrave
in main.js set > other
in other.js, get
other
in other.js,set > yobrave
after other.js set ,in main.js get >
yobrave
// index.js
let strict = false;
const weoptions = require('weoptions')('id', strict);
let options = {
name: 'yobrave',
a: {
b: {
c: 1,
},
},
// ...
};
let w = weoptions(options);
// init
w.set('a.b.c', 2); //
// 2
w.set('a.b.e.d', 2); //
// 2
// if strict == false, create e d, set d=2
// if strict == true, create e d throw TypeError
w.get();
// options
// or
w.get('a.b.c');
// 2
// also you can, or no
module.exports = w// other.js
// use the lib, no index.js exports
const weoptions = require('weoptions')('id'); // after init
w.get();
// options
w.set('a.b.c', 1);
// 1
w._setStrict(true);
// change strict with id| name: | id |
|---|---|
| Type: | string |
| Desc: | the id of your options, just like debug |
| name: | strict |
|---|---|
| Type: | boolean |
| Default: | false |
| Desc: | the strict of your options |
if true, anything after WEOPTIONS(options) add/remove/new willthrow Error |
| name: | return |
|---|---|
| Type: | WEOPTIONS | W |
| Desc: | if id had init, return W , else return WEOPTIONS |
| name: | options |
|---|---|
| Type: | any |
| Desc: | options init |
| name: | position |
|---|---|
| Type: | string |
| Desc: | options position |
| name: | value |
|---|---|
| Type: | any |
| Desc: | options value |
use dset API
| name: | position |
|---|---|
| Type: | string |
| Desc: | options position |
use dlv API
| name: | bool |
|---|---|
| Type: | boolean |
| Desc: | change strict to bool |
MIT © chinanf-boy