parse-json-config
Install
yarn add parse-json-config
Usage
const parse = ; //=> return ;
Use options
To provide options while calling the function:
//=> return options ;
Local path or function
You can also use a local path or function:
//=> return { return "foo"; } options;
If you're using a non-string, we directly return it without calling it with the options even if you provide it as [resolved, options]
, since if you can use a function / object as resolved
you can already execute it yourself, but there's also a isCalled option for you to customize it.
By default relative path is resolve from process.cwd()
Custom caller
Each item in the config is in name
or [name, options]
format, by default we directly call the name
with option
as the only argument, but you can use a custom caller:
//=> return options "hi";
Default caller is (resolved, options) => typeof resolved === 'object' ? resolve.apply(options) : resolved(options)
Prefix
Use a prefix:
//=> return
You can also use addPrefix
method to handle some edge cases:
//=> return
Pure object
Use pure object as config:
Similar to using an array:
"foo" options "./bar.js" isBar: true ;
is equivalent to:
'foo': options './bar.js': isBar: true
API
parse(config, [options])
options
cwd
Type: string
Default: process.cwd()
The path to resolve relative path and npm packages.
caller
Type: function
Default: (resolved, options) => resolved(options)
The caller defines what to do with resolved
and options
.
prefix
Type: string
Default: undefined
Prefix for package name.
addPrefix
Type: (originalItem, prefix) => string | boolean
Return a string as prefixed value, return true
to use default addPrefix
handler, return false
to skip adding prefix.
isResolved
Type: originalItem => boolean
Default: item => item && typeof item !== 'string'
Check if the item is resolved, by default considering all non-string types as resolved.
isCalled
Type: (originalItem, resolvedItem) => boolean
Default: Same as isResolved
.
Check if the item is called with options, by default considering all non-string types as called, but you can customize it:
//=> return { return "foo"; } "bar";// function bar will be called as `bar(options)`
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
parse-json-config © egoist, Released under the MIT License.
Authored and maintained by egoist with help from contributors (list).
egoist.moe · GitHub @egoist · Twitter @_egoistlily