New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

check-types-mini

Package Overview
Dependencies
Maintainers
1
Versions
198
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

check-types-mini - npm Package Compare versions

Comparing version 1.3.0 to 1.4.0

5

changelog.md

@@ -7,2 +7,6 @@ # Change Log

## [1.4.0] - 2017-06-10
### Added
- ✨ `opts.enforceStrictKeyset` will now by default `throw` if there are any keys in the options object, that don't exist in the reference object.
## [1.3.0] - 2017-05-22

@@ -28,1 +32,2 @@ ### Added

[1.3.0]: https://github.com/codsen/check-types-mini/compare/v1.2.2...v1.3.0
[1.4.0]: https://github.com/codsen/check-types-mini/compare/v1.3.0...v1.4.0

13

index.js

@@ -8,2 +8,3 @@ 'use strict'

const includes = require('lodash.includes')
const pullAll = require('lodash.pullall')
const arrayiffyIfString = require('arrayiffy-if-string')

@@ -38,3 +39,4 @@

acceptArrays: false,
acceptArraysIgnore: []
acceptArraysIgnore: [],
enforceStrictKeyset: true
}

@@ -46,10 +48,13 @@ 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))
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_03] opts.ignoreKeys should be an array, currently it's: ${type(opts.ignoreKeys)}`)
}
if (!isBool(opts.acceptArrays)) {
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_04] opts.acceptArrays should be a Boolean, currently it\'s: ' + type(opts.acceptArrays))
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_04] opts.acceptArrays should be a Boolean, currently it's: ${type(opts.acceptArrays)}`)
}
if (!isArr(opts.acceptArraysIgnore)) {
throw new TypeError('check-types-mini/checkTypes(): [THROW_ID_03] opts.acceptArraysIgnore should be an array, currently it\'s: ' + type(opts.acceptArraysIgnore))
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_05] opts.acceptArraysIgnore should be an array, currently it's: ${type(opts.acceptArraysIgnore)}`)
}
if (opts.enforceStrictKeyset && (pullAll(Object.keys(obj), Object.keys(ref)).length !== 0)) {
throw new TypeError(`check-types-mini/checkTypes(): [THROW_ID_06] object has some unrecongnised keys: ${pullAll(Object.keys(ref), Object.keys(obj))}`)
}

@@ -56,0 +61,0 @@ Object.keys(obj).forEach(function (key) {

{
"name": "check-types-mini",
"version": "1.3.0",
"version": "1.4.0",
"description": "Check the types of your options object's values after user has customised them",

@@ -39,3 +39,6 @@ "main": "index.js",

"arrayiffy-if-string": "*",
"lodash.clonedeep": "^4.5.0",
"lodash.includes": "*",
"lodash.pullall": "^4.2.0",
"object-assign": "^4.1.1",
"type-detect": "^4.0.0"

@@ -47,9 +50,9 @@ },

"coveralls": "*",
"husky": "*",
"husky": "^0.13.4",
"nodemon": "*",
"nyc": "*",
"nyc": "^11.0.2",
"standard": "^10.0.0"
},
"ava": {
"verbose": false
"verbose": true
},

@@ -56,0 +59,0 @@ "engines": {

@@ -24,3 +24,4 @@ # check-types-mini

- [Options object](#options-object)
- [For example](#for-example)
- [For example](#for-example)
- [`opts.enforceStrictKeyset`](#optsenforcestrictkeyset)
- [Contributing](#contributing)

@@ -39,6 +40,8 @@ - [Licence](#licence)

[check-types](https://www.npmjs.com/package/check-types) is good but it's too big. All I need is to `throw` if somebody sets my input settings to a wrong type.
[`check-types`](https://www.npmjs.com/package/check-types) is good but it's too big. All I need is to `throw` if somebody sets my input settings to a wrong type.
In few occasions, I copied the predecessor function (which later became this library) from one library of mine to another, along with its unit tests. Then I got fed up with that and here were are. Its point is to cut corners publishing new libraries. Every library that has options needs some checks, has user set things to be of a correct type.
I had a working prototype of this library ages ago. Pieces of it were finding a new life (and evolving) in every new non-trivial library I created. Then I got fed up with chasing 100% code coverage each time, duplicating unit tests and decided to split it into a standalone library.
The point of `check-types-mini` is to save your time creating new libraries. Every library that has options object will need some type checks if you let user tinker with it.
## API

@@ -55,3 +58,3 @@

`obj` | Plain object | yes | Options object after user's customisation
`ref` | Plain object | yes | Default options — used to compare the types
`ref` | Plain object | yes | Default options - used to compare the types
`msg` | String | no | A message to show. I like to include the name of the calling library, parent function and numeric throw ID.

@@ -69,5 +72,6 @@ `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.

`acceptArraysIgnore` | Array of strings or String | no | `[]` (empty array) | If you want to ignore `acceptArrays` on certain keys, pass them in an array here.
`enforceStrictKeyset` | Boolean | no | `true` | If it's set to `true`, your object must not have any unique keys that reference object does not have.
} | | | |
## For example
### For example

@@ -117,3 +121,3 @@ ```js

But this does not `throw`:
But this does not `throw` when we allow arrays:

@@ -144,2 +148,6 @@ ```js

### `opts.enforceStrictKeyset`
When I was coding a new major version of [posthtml-ast-delete-object](https://github.com/codsen/posthtml-ast-delete-object) I had to update all the unit tests too. Previously, the settings was only one argument - Boolean. I had to change it to be a plain object. I noticed that old Boolean argument was not causing problems! Then I came up with the idea to enforce the keys of the object to match the reference. This was released in `v1.4.0`. It's on by default because I can't imagine how you would end up with settings object that does not match your default settings object, key-wise, but if you don't like that, feel free to turn it off. It's `opts.enforceStrictKeyset` Boolean flag.
## Contributing

@@ -146,0 +154,0 @@

@@ -18,3 +18,3 @@ 'use strict'

test('01.03 - third argument and fourth arguments are missing', t => {
test('01.03 - throws when one of the arguments is of a wrong type', t => {
t.throws(function () {

@@ -36,3 +36,3 @@ checkTypes(

test('01.03 - third argument and fourth arguments are missing', t => {
test('01.04 - throws when input args are of a wrong type', t => {
t.throws(function () {

@@ -49,5 +49,7 @@ checkTypes(

option3: false
}
},
'zzz',
1
)
}, 'opts.option2 was customised to "false" which is not boolean but string')
})
t.throws(function () {

@@ -66,8 +68,8 @@ checkTypes(

1,
1
'zzz'
)
}, 'option2 was customised to "false" which is not boolean but string')
})
})
test('01.03 - fourth argument is missing', t => {
test('01.05 - throws if fourth argument is missing', t => {
t.throws(function () {

@@ -126,3 +128,3 @@ checkTypes(

test('01.04 - throws when opts are set wrong', t => {
test('01.06 - throws when opts are set wrong', t => {
t.throws(function () {

@@ -355,1 +357,39 @@ checkTypes({a: 'a'}, {a: 'b'}, 'aa', 'bbb', { ignoreKeys: false })

})
// ======================
// 03. opts.enforceStrictKeyset
// ======================
test('02.04 - opts.acceptArrays, strings+arrays', t => {
t.throws(function () {
checkTypes(
{
option1: 'setting1',
option2: 'setting2',
rogueKey: false
},
{
option1: 'zz',
option2: 'yy'
}
)
})
t.notThrows(function () {
checkTypes(
{
option1: 'setting1',
option2: 'setting2',
rogueKey: false
},
{
option1: 'zz',
option2: 'yy'
},
null,
null,
{
enforceStrictKeyset: false
}
)
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc