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

Check the types of your options object's values after user has customised them

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.9K
decreased by-7.64%
Maintainers
1
Weekly downloads
 
Created
Source

check-types-mini

Standard JavaScript

Check the types of your options object's values after user has customised them

Build Status Coverage Status bitHound Score bitHound Dependencies bitHound Dev Dependencies Downloads/Month

Table of Contents

Install

$ npm i -S check-types-mini

Idea

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.

API

checkTypes(obj, ref[, msg, optsVarName, opts])

As a result, it throws TypeErrors for you, containing your custom message, similar to:

check-types-mini/checkTypes(): opts.mode was customised to "zzz" which is not Boolean but string
Input argumentTypeObligatory?Description
objPlain objectyesOptions object after user's customisation
refPlain objectyesDefault options — used to compare the types
msgStringnoA message to show. I like to include the name of the calling library, parent function and numeric throw ID.
optsVarNameStringnoHow is your options variable called? It does not matter much, but it's nicer to keep references consistent with your API documentation.
optsPlain objectnoOptional options go here.

Options object

options object's keyTypeObligatory?DefaultDescription
{
ignoreKeysArrayno[] (empty array)Instructs to skip all and any checks on keys, specified in this array. Put them as strings.
}

For example

const checkTypes = require('check-types-mini')
const objectAssign = require('object-assign')
const clone = require('lodash.clonedeep')

function yourFunction (input, opts) {
  // declare defaults, so we can enforce types later:
  var defaults = {
    placeholder: false
  }
  // fill any settings with defaults if missing:
  opts = objectAssign(clone(defaults), opts)
  // the check:
  checkTypes(opts, defaults, 'newLibrary/yourFunction(): [THROW_ID_01]', 'opts')
  // ...
}

var res = yourFunction(1, {placeholder: 'zzz'})

// =>> [TypeError: 'newLibrary/yourFunction(): [THROW_ID_01] opts.placeholder was customised to "false" which is not boolean but string']

If you are happy with opts variable name, you can omit the fourth argument; it will be set to that by default.

Contributing

All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js with new unit tests covering your feature(s).

If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged as soon as possible. If you have any comments on the code, including ideas how to improve something, don't hesitate to contact me by email.

Licence

MIT License (MIT)

Copyright (c) 2017 Codsen Ltd, Roy Reveltas

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Keywords

FAQs

Package last updated on 15 May 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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