https://github.com/fmartin5/type-checking
Runtime type checking and assertion library.
https://github.com/fmartin5/type-checking
assertions javascript predicates type-checking
Last synced: 5 months ago
JSON representation
Runtime type checking and assertion library.
- Host: GitHub
- URL: https://github.com/fmartin5/type-checking
- Owner: fmartin5
- License: agpl-3.0
- Created: 2017-12-21T20:31:10.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-08-09T22:57:27.000Z (almost 8 years ago)
- Last Synced: 2025-10-24T12:46:19.108Z (8 months ago)
- Topics: assertions, javascript, predicates, type-checking
- Language: JavaScript
- Size: 63.5 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# type-checking
[![NPM Version][npm-image]][npm-url]
[![NPM Downloads][downloads-image]][downloads-url]
Runtime type checking and type assertion library for Node and the browser
- Traditional syntax.
- Aims to cover every EcmaScript language type and specification type except Proxy.
- Offers a small set of composable numeric predicates and assertions for integers, signed numbers, and special numbers.
- Cross-realm/iframe support (i.e., predicates should remain correct across different realms).
- Does not get easily fooled by the value of the `Symbol.toStringTag` property.
- Handles `-0` and `NaN` correctly.
- Aims to provide readable error messages.
- Does not rely on Symbol.toStringTag
- You can easily disable `typeChecking.assert()` (but not `.expect()`) for production:
in Node: by setting the NODE_NDEBUG environment variable;
in Nashorn: by setting the "nashorn.ndebug" system property.
- Does not aim to cover any DOM API type.
- Does not aim to cover complex constrained string types like email address etc.
- Does not aim to offer custom type constructor.
## Installation
```sh
npm install @fmartin5/type-checking
```
## Usage
```js
const tc = require("@fmartin5/type-checking");
let x = 0;
tc.isNumber(x); // true
tc.isInteger(x); // true
tc.isPositiveNumber(x); // true
tc.isPositiveInteger(x); // true
tc.isStrictlyPositiveNumber(x); // false
tc.isStrictlyPositiveInteger(x); // false
let map = new Map();
tc.isMap(map); // true
tc.isWeakMap(map); // false
tc.expectMap(map); //
tc.expectWeakMap(map); // TypeError: expected a 'WeaMap' object.
```
## Tests
Clone the repo, then do:
```sh
npm install
npm test
```
## License
[AGPL-3.0](LICENSE)
[npm-image]: https://img.shields.io/npm/v/@fmartin5/type-checking.svg
[npm-url]: https://npmjs.org/package/@fmartin5/type-checking
[downloads-image]: https://img.shields.io/npm/dm/@fmartin5/type-checking.svg
[downloads-url]: https://npmjs.org/package/@fmartin5/type-checking