Skip to content

chinanf-boy/weoptions

Repository files navigation

weoptions Build Status codecov

我想要有个公共 options, 但可以通过不同的ID, just init/get/set

Install

npm install weoptions
yarn add weoptions

Demo

->example main.js other.js

node example/main.js

result

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

Usage

// 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

API

1. weoptions(id, strict)

id

name: id
Type: string
Desc: the id of your options, just like debug

strict

name: strict
Type: boolean
Default: false
Desc: the strict of your options
⚠️: if true, anything after WEOPTIONS(options) add/remove/new willthrow Error

return

name: return
Type: WEOPTIONS | W
Desc: if id had init, return W , else return WEOPTIONS

- WEOPTIONS(options):W

options

name: options
Type: any
Desc: options init

- W.set(position, value)

position

name: position
Type: string
Desc: options position

value

name: value
Type: any
Desc: options value

use dset API

- W.get(position)

position

name: position
Type: string
Desc: options position

use dlv API

- W._setStrict(bool)

bool

name: bool
Type: boolean
Desc: change strict to bool

License

MIT © chinanf-boy

About

global worker options, with id store set/get, that it 我想要有个公共 options, 但可以通过不同的ID ✅

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published