Skip to content

Commit

Permalink
fix(codeclimate): simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
tunnckoCore committed Jan 3, 2017
1 parent a2d2b85 commit a3ff8ac
Showing 1 changed file with 37 additions and 29 deletions.
66 changes: 37 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,6 @@

const extend = require('extend-shallow')

const arrayify = (val) => {
if (!val) return []
if (Array.isArray(val)) return val
return [val]
}

const resolvePluginsSync = (plugins, opts) => {
plugins = arrayify(plugins).filter(Boolean)

Expand All @@ -27,34 +21,13 @@ const resolvePluginsSync = (plugins, opts) => {
return arrayify(plugins).map((plugin) => {
// allows `plugins: ['foo', 'bar', 'baz']`
if (typeof plugin === 'string') {
let id = `${opts.prefix}${plugin}`
let func = require(id)
let argz = opts.args ? opts.args : [opts.first]
return func.apply(opts.context, argz)
return resolveFromString(opts, plugin)
}

// allows nesting and passing options to each plugin
// e.g. `plugins: [ fn, ['foo', {opts: 'here'}], 'bar', quix() ]
if (Array.isArray(plugin)) {
plugin = plugin.filter(Boolean)
let second = opts.first ? plugin[1] : undefined
let first = opts.first ? opts.first : plugin[1]
let args = opts.args ? opts.args : [first, second]

if (typeof plugin[0] === 'string') {
let id = `${opts.prefix}${plugin[0]}`
return require(id).apply(opts.context, args)
}
if (typeof plugin[0] === 'function') {
let fn = plugin[0]
return fn.apply(opts.context, args)
}
if (typeof plugin[0] === 'object') {
return plugin[0]
}

let msg = 'First item of array should be function, string or object'
throw new TypeError(msg)
return resolveFromArray(opts, plugin)
}

// allows `plugins: [fn1, fn2]`
Expand All @@ -74,4 +47,39 @@ const resolvePluginsSync = (plugins, opts) => {
})
}

const arrayify = (val) => {
if (!val) return []
if (Array.isArray(val)) return val
return [val]
}

const resolveFromString = (opts, plugin) => {
let id = `${opts.prefix}${plugin}`
let func = require(id)
let argz = opts.args ? opts.args : [opts.first]
return func.apply(opts.context, argz)
}

const resolveFromArray = (opts, plugin) => {
plugin = plugin.filter(Boolean)
let second = opts.first ? plugin[1] : undefined
let first = opts.first ? opts.first : plugin[1]
let args = opts.args ? opts.args : [first, second]

if (typeof plugin[0] === 'string') {
let id = `${opts.prefix}${plugin[0]}`
return require(id).apply(opts.context, args)
}
if (typeof plugin[0] === 'function') {
let fn = plugin[0]
return fn.apply(opts.context, args)
}
if (typeof plugin[0] === 'object') {
return plugin[0]
}

let msg = 'First item of array should be function, string or object'
throw new TypeError(msg)
}

module.exports = resolvePluginsSync

0 comments on commit a3ff8ac

Please sign in to comment.