-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
50 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,38 @@ | ||
import { inspect } from 'util' | ||
|
||
import filterObj from 'filter-obj' | ||
import isPlainObj from 'is-plain-obj' | ||
|
||
export const normalizeOpts = function (rules, { soft = false, all } = {}) { | ||
validateRules(rules) | ||
return { soft, all } | ||
// Normalize `options` | ||
export const normalizeOpts = function (options = {}) { | ||
if (!isPlainObj(options)) { | ||
throw new TypeError(`Options must be a plain object: ${inspect(options)}`) | ||
} | ||
|
||
const { soft = false, all } = options | ||
validateSoft(soft) | ||
const allA = normalizeAll(all) | ||
return { soft, all: allA } | ||
} | ||
|
||
const validateRules = function (rules) { | ||
if (!Array.isArray(rules)) { | ||
throw new TypeError(`Rules must be an array: ${rules}`) | ||
const validateSoft = function (soft) { | ||
if (typeof soft !== 'boolean') { | ||
throw new TypeError(`Option "soft" must be a boolean: ${inspect(soft)}`) | ||
} | ||
|
||
rules.forEach(validateRule) | ||
} | ||
|
||
const validateRule = function (rule) { | ||
if (!isPlainObj(rule)) { | ||
throw new TypeError(`Rules must be plain objects: ${rule}`) | ||
const normalizeAll = function (all) { | ||
if (all === undefined) { | ||
return | ||
} | ||
|
||
if (!isPlainObj(all)) { | ||
throw new TypeError(`Option "all" must be a plain object: ${inspect(all)}`) | ||
} | ||
|
||
return filterObj(all, isDefined) | ||
} | ||
|
||
const isDefined = function (key, value) { | ||
return value !== undefined | ||
} |