Skip to content

Commit

Permalink
Validate options is a plain object
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 15, 2019
1 parent 6964ce3 commit 79e1789
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { validate } from 'jest-validate'
import isCi from 'is-ci'

import { pickBy } from './utils.js'
import { isPlainObject, pickBy } from './utils.js'

// Parse options
export const parseOpts = function({
opts = {},
defaultOpts = {},
forcedOpts = {},
}) {
validateOpts({ opts })

const optsA = pickBy(opts, value => value !== undefined)

const exampleConfig = pickBy(
Expand All @@ -24,6 +26,12 @@ export const parseOpts = function({
return optsC
}

const validateOpts = function({ opts }) {
if (!isPlainObject(opts)) {
throw new Error(`options must be a plain object: ${opts}`)
}
}

const DEFAULT_OPTS = {
verbose: isCi,
}
Expand Down
9 changes: 9 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,12 @@ export const pickBy = function(object, condition) {
)
return Object.fromEntries(pairs)
}

// Is a plain object, including `Object.create(null)`
export const isPlainObject = function(val) {
return (
typeof val === 'object' &&
val !== null &&
(val.constructor === Object || val.constructor === undefined)
)
}

0 comments on commit 79e1789

Please sign in to comment.