Miss any of our Open RFC calls?Watch the recordings here! »

parse-json-config

0.2.0 • Public • Published

parse-json-config

NPM version NPM downloads CircleCI codecov donate

Install

yarn add parse-json-config

Usage

const parse = require('parse-json-config')
 
parse([
  'foo',
  'bar'
])
//=> return
[
  require('foo')(),
  require('bar')()
]

Use options

To provide options while calling the function:

parse([
  ['foo', options],
  'bar'
])
//=> return
[
  require('foo')(options),
  require('bar')()
]

Local path or function

You can also use a local path or function:

parse([
  function () { return 'foo' },
  ['./bar.js', options]
])
//=> return
[
  function () { return 'foo' },
  require('./bar.js')(options)
]

If you're using a function, we directly return it without calling the options even if you provide it as [function, options], since if you can use a function you can already execute it with function(options), but you can use isCalled option 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:

parse([
  (options, extra) => extra || options
], {
  caller: (fn, options) => fn(options, 'hi')
})
//=> return
[
  'hi'
]

Default caller is (fn, options) => fn(options)

Prefix

Use a prefix:

parse([
  'es2015',
  'babel-preset-stage-2'
], { prefix: 'babel-preset-' })
//=> return
[
  require('babel-preset-es2015')(),
  require('babel-preset-stage-2')()
]

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: (fn, options) => fn(options)

The caller the execute resolved function and options

prefix

Type: string
Default: undefined

Prefix for package name.

isCalled

Type: function
Default: () => true

Check if a function is called with options, by default considering all functions as called, but you can customize it:

parse([
  function foo() { return 'foo' },
  function bar() { return 'bar' }
], {
  // Only used when config item is provided as function
  isCalled(fn) {
    // Consider it's called when its name is foo
    return fn.name === 'foo'
  }
})
//=> return
[
  function foo() { return 'foo' },
  'bar'
]
// function bar will be called as `bar(options)`

fn is always a function.

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. 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).

egoistian.com · GitHub @egoist · Twitter @rem_rin_rin

Keywords

none

Install

npm i [email protected]

Version

0.2.0

License

MIT

Last publish

Collaborators

  • avatar