https://github.com/mcstreetguy/isset-helper
Get rid of weak typing and improve your assertions: A tiny helper method for Javascript
https://github.com/mcstreetguy/isset-helper
algorithm helper-functions helper-methods isset-helper javascript javascript-library javascript-tools node-module nodejs tiny-helper type typecast typification validation
Last synced: 9 months ago
JSON representation
Get rid of weak typing and improve your assertions: A tiny helper method for Javascript
- Host: GitHub
- URL: https://github.com/mcstreetguy/isset-helper
- Owner: MCStreetguy
- License: mit
- Created: 2018-02-11T10:49:24.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-07-12T02:39:16.000Z (over 2 years ago)
- Last Synced: 2025-03-06T07:40:09.292Z (9 months ago)
- Topics: algorithm, helper-functions, helper-methods, isset-helper, javascript, javascript-library, javascript-tools, node-module, nodejs, tiny-helper, type, typecast, typification, validation
- Language: JavaScript
- Homepage:
- Size: 256 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# IsSet Helper
[](https://github.com/MCStreetguy/isset-helper/issues)
[](https://github.com/MCStreetguy/isset-helper/blob/master/LICENSE)




**Get rid of weak typing and improve your assertions: A tiny helper method for Javascript**
Since checking variable types and existence always has been painful in Javascript, validation assertions normally are exhausting to write.
This module solves this definitely.
- [IsSet Helper](#isset-helper)
- [Browser Compatibility](#browser-compatibility)
- [Installation](#installation)
- [as a module](#as-a-module)
- [in the browser](#in-the-browser)
- [Usage](#usage)
- [Contributing](#contributing)
- [License](#license)
---
## Browser Compatibility
| IE | Edge | Firefox | Chrome | Safari | Opera | Node.js |
|:-:|:-:|:-:|:-:|:-:|:-:|:-:|
| 9+ | 12+ | 4+ | 5+ | 5+ | 11.5+ | all |
## Installation
### as a module
Install the library through your preferred package manager:
```bash
$ npm install --save isset-helper
# or
$ yarn add isset-helper
```
Then you can require the module as usual:
```js
const isset = require('isset-helper');
```
### in the browser
Include _one_ of the following script tags in your pages head:
```html
```
The library registers the `isset` function automatically on the `window` object.
## Usage
```js
isset(variable, type);
```
`variable` can be anything, it's value is going to be checked.
`type` can be a string, object constructor or even be omitted.
The algorithm follows the subsequent rules:
1. `variable` is not `null`
2. `variable` is not `undefined`
3. If `type` is a string, `typeof variable` has to match `type`,
otherwise `variable` has to match `instanceof type`
4. If `type` is `"string"`, the length of `variable` is greater than zero
5. If `type` is `"array"`, `variable` has to pass the [`Array.isArray()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/isArray) check
6. If `type` is `"integer"`, `variable` has to pass the [`Number.isInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) check
7. If `type` is `"float"` or `"double"`, the inverse of rule 6 applies ([`Number.isInteger()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/isInteger) has to return `false`)
**Please notice:**
- The algorithm doesn't check for the exact value (apart from the empty string case mentioned above), thus `false`, `0` and other "falsy" values will also be considered valid.
## Contributing
If, contrary to expectations, you find an error in the function, please report it to the Issues page.
Feel free to make changes to a fork yourself and propose a detailed pull request when finished.
## License
This library is licensed under the MIT License.
You may find [a copy of the license](/LICENSE) at the root of the project source.