check-types-mini
Advanced tools
Comparing version 5.1.0 to 5.1.1
@@ -227,3 +227,3 @@ # Change Log | ||
[1.5.0]: https://bitbucket.org/codsen/check-types-mini/branches/compare/v1.5.0%0Dv1.4.1#diff | ||
[1.6.0]: https://bitbucket.org/codsen/check-types-mini/branches/compare/v1.6.0%0Dv5.0.0#diff | ||
[1.6.0]: https://bitbucket.org/codsen/check-types-mini/branches/compare/v1.6.0%0Dv5.1.0#diff | ||
[2.0.0]: https://bitbucket.org/codsen/check-types-mini/branches/compare/v2.0.0%0Dv1.5.0#diff | ||
@@ -230,0 +230,0 @@ [2.1.0]: https://bitbucket.org/codsen/check-types-mini/branches/compare/v2.1.0%0Dv2.0.3#diff |
{ | ||
"name": "check-types-mini", | ||
"version": "5.1.0", | ||
"version": "5.1.1", | ||
"description": "Check the types of your options object's values after user has customised them", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -22,2 +22,27 @@ # check-types-mini | ||
## TLDR - use default opts to enforce types | ||
```js | ||
const checkTypes = require("check-types-mini"); | ||
function yourFunction(input, originalOpts) { | ||
// 1. declare defaults: | ||
const defaults = { placeholderEnabled: false }; | ||
// 2. merge given opts into defaults: | ||
const opts = Object.assign({}, defaults, originalOpts); | ||
// 3. type check: | ||
checkTypes(opts, defaults, { | ||
msg: "newLibrary/yourFunction(): [THROW_ID_01]" | ||
}); | ||
// rest of your app goes here... | ||
} | ||
// NOW, let's call our function with wrong opts and see what happens: | ||
let res = yourFunction(1, { placeholderEnabled: "zzz" }); // <--- notice opts key was Boolean in defaults | ||
// WE GET A TYPE ERROR THROWN: | ||
// =>> TypeError: 'newLibrary/yourFunction(): [THROW_ID_01] opts.placeholderEnabled was customised to "zzz" which is not boolean but string' | ||
``` | ||
## Install | ||
@@ -52,13 +77,19 @@ | ||
Here's a challenge: how do you check (and throw) errors, easily, when users set your options to wrong things? | ||
Here's a challenge: how do you check (and throw) errors easily, when users set your options to wrong things? | ||
Answer: this library. It does all the work validating the inputs, lets you customise the `throw` error messages (like below) and even lets you provide the _schema_, that is, array of allowed types that each options key's value should be in. | ||
Answer: this library. | ||
**Features:** | ||
- Use a default options object to enforce types | ||
- Supplement or fully customise types (via a simple schema) | ||
- Customise error messages so that errors show source as your library, even though `check-types-mini` threw them | ||
For example, here's a typical throw error generated by this library: | ||
```js | ||
TypeError: fancyLibrary/fancyFunction(): [THROW_ID_01] opts.placeholder was customised to "false" which is not boolean but string | ||
TypeError: yourLibrary/yourFunction(): [THROW_ID_01] opts.placeholder was customised to "false" which is not boolean but string | ||
``` | ||
Originally this library started as a function within one of my libraries. When I was about to copy-paste the thing into another library, I stopped and put that into a separate library, _this library_. I'm glad I did it, because already ~~7~~ ~~22~~ 36 of my libraries [depend](https://www.npmjs.com/browse/depended/check-types-mini) on it, and improving `check-types` I improve my other libraries. DRY at its best. Feel free to tap `check-types-mini` in your libraries! | ||
Originally this library started as a function within one of my libraries. When I was about to copy-paste the thing into another library, I stopped and put that into a separate library, _this library_. I'm glad I did it, because already ~~7~~ ~~22~~ 36 of my libraries [depend](https://www.npmjs.com/browse/depended/check-types-mini) on it. | ||
@@ -65,0 +96,0 @@ The point of `check-types-mini` is to save your time: time spent coding up all these checks, time spent debugging, and even consumers' time spent debugging your API when they try to use it wrongly. Every library that has options object will need some **type checks** if you let user tinker with it. |
83208
340