check-types-mini
Advanced tools
Comparing version 1.0.1 to 1.1.0
@@ -7,4 +7,10 @@ # Change Log | ||
## [1.1.0] - 2017-05-15 | ||
### Added | ||
- opts.ignoreKeys | ||
## 1.0.0 - 2017-05-15 | ||
### New | ||
- First public release | ||
[1.1.0]: https://github.com/code-and-send/check-types-mini/compare/v1.0.1...v1.1.0 |
24
index.js
'use strict' | ||
const type = require('type-detect') | ||
function existy (x) { return x != null } | ||
const objectAssign = require('object-assign') | ||
const clone = require('lodash.clonedeep') | ||
const isArr = Array.isArray | ||
const includes = require('lodash.includes') | ||
function checkTypes (obj, ref, msg, optsVarName) { | ||
function checkTypes (obj, ref, msg, optsVarName, opts) { | ||
function existy (x) { return x != null } | ||
if (arguments.length === 0) { | ||
throw new Error('check-types-mini/checkTypes(): missing first two arguments!') | ||
throw new Error('check-types-mini/checkTypes(): [THROW_ID_01] missing first two arguments!') | ||
} | ||
if (arguments.length === 1) { | ||
throw new Error('check-types-mini/checkTypes(): missing second argument!') | ||
throw new Error('check-types-mini/checkTypes(): [THROW_ID_02] missing second argument!') | ||
} | ||
@@ -26,4 +31,13 @@ if (!existy(optsVarName)) { | ||
} | ||
var defaults = { | ||
ignoreKeys: [] | ||
} | ||
opts = objectAssign(clone(defaults), opts) | ||
if (!isArr(opts.ignoreKeys)) { | ||
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it\'s: ' + type(opts.ignoreKeys)) | ||
} | ||
Object.keys(obj).forEach(function (key) { | ||
if (existy(ref[key]) && (type(obj[key]) !== type(ref[key]))) { | ||
if (existy(ref[key]) && (type(obj[key]) !== type(ref[key])) && !includes(opts.ignoreKeys, key)) { | ||
throw new TypeError(`${msg}${optsVarName}${key} was customised to ${JSON.stringify(obj[key], null, 4)} which is not ${type(ref[key])} but ${type(obj[key])}`) | ||
@@ -30,0 +44,0 @@ } |
{ | ||
"name": "check-types-mini", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Check the types of your options object's values after user has customised them", | ||
@@ -9,3 +9,3 @@ "main": "index.js", | ||
"precommit": "npm test && doctoc readme.md", | ||
"test": "standard && nyc --reporter=html --reporter=text ava", | ||
"test": "clear && standard && nyc --reporter=html --reporter=text ava", | ||
"watch": "nodemon --quiet --watch . --exec npm run test" | ||
@@ -38,2 +38,3 @@ }, | ||
"dependencies": { | ||
"lodash.includes": "^4.3.0", | ||
"type-detect": "^4.0.0" | ||
@@ -43,2 +44,3 @@ }, | ||
"ava": "*", | ||
"clear-cli": "^1.0.1", | ||
"coveralls": "*", | ||
@@ -45,0 +47,0 @@ "husky": "*", |
@@ -23,2 +23,3 @@ # check-types-mini | ||
- [API](#api) | ||
- [Options object](#options-object) | ||
- [For example](#for-example) | ||
@@ -54,5 +55,14 @@ - [Contributing](#contributing) | ||
`ref` | Plain object | yes | Default options — used to compare the types | ||
`msg` | String | yes | A message to show. I like to include the name of the calling library, parent function and numeric throw ID. | ||
`optsVarName` | String | yes | How is your options variable called? It does not matter much, but it's nicer to keep references consistent with your API documentation. | ||
`msg` | String | no | A message to show. I like to include the name of the calling library, parent function and numeric throw ID. | ||
`optsVarName` | String | no | How is your options variable called? It does not matter much, but it's nicer to keep references consistent with your API documentation. | ||
`opts` | Plain object | no | Optional options go here. | ||
### Options object | ||
`options` object's key | Type | Obligatory? | Default | Description | ||
-------------------------------|----------|-------------|-------------|---------------------- | ||
{ | | | | | ||
`ignoreKeys` | Array | no | `[]` (empty array) | Instructs to skip all and any checks on keys, specified in this array. Put them as strings. | ||
} | | | | | ||
## For example | ||
@@ -59,0 +69,0 @@ |
27
test.js
@@ -9,3 +9,3 @@ 'use strict' | ||
checkTypes() | ||
}, 'check-types-mini/checkTypes(): missing first two arguments!') | ||
}, 'check-types-mini/checkTypes(): [THROW_ID_01] missing first two arguments!') | ||
}) | ||
@@ -16,3 +16,3 @@ | ||
checkTypes('zzzz') | ||
}, 'check-types-mini/checkTypes(): missing second argument!') | ||
}, 'check-types-mini/checkTypes(): [THROW_ID_02] missing second argument!') | ||
}) | ||
@@ -105,2 +105,25 @@ | ||
) | ||
t.notThrows(function () { | ||
checkTypes( | ||
{ | ||
option1: 'setting1', | ||
option2: 'false', | ||
option3: false | ||
}, | ||
{ | ||
option1: 'setting1', | ||
option2: false, | ||
option3: false | ||
}, | ||
'newLibrary/index.js: [THROW_ID_01] ', | ||
null, | ||
{ ignoreKeys: ['option2'] } | ||
) | ||
}) | ||
}) | ||
test('01.04 - throws when opts are set wrong', t => { | ||
t.throws(function () { | ||
checkTypes({a: 'a'}, {a: 'b'}, 'aa', 'bbb', { ignoreKeys: false }) | ||
}, 'check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it\'s: boolean') | ||
}) |
15320
160
139
2
7
+ Addedlodash.includes@^4.3.0
+ Addedlodash.includes@4.3.0(transitive)