pretty-easy-data-types
v1.1.1
Published
Utility library for checking the data type of a supplied parameter
Downloads
2,481
Maintainers
Readme
pretty-easy-data-types
What is pretty-easy-data-types?
pretty-easy-data-types is a small utility NodeJS library for checking the data type values.
Install
This is a NodeJS module available through the npm registry. Installation is done using the npm install command:
$ npm install pretty-easy-data-types --save
--save flag is used to save the module as a project dependancy in your package.json file.
Usage
After installing the module (localy in your project directory), in order to use it in your file you first need to require it.
You can:
- import the whole library
const dataTypes = require('pretty-easy-data-types');
- or only the components you need (recommended way)
const { geType, isString, isArray, isNull } = require('pretty-easy-data-types');
If you use TypeScript:
- import the whole library
import * as dataTypes from 'pretty-easy-data-types';
- import the components you need (recommended way)
import { getType, isBoolean, isNumber } from 'pretty-easy-data-types';
The library exposes a few utility functions for you to call and supply with a value for which you'd like to get a data type or you'd like to check if it is of certain data type value.
Examples
Get data type of value
/**
* We're going to import only the data type checker here,
* but you're free to import the whole library and then
* use the methods on the returned object; it's up to you
*/
const { getType } = require('pretty-easy-data-types');
// Perform the checks
getType(['f00', false, 2]) // 'array'
getType(false) // 'boolean'
getType(new Date()) // 'date'
getType(new Error()) // 'error'
getType(() => {}) // 'function'
getType(null) // 'null'
getType(25) // 'number'
getType({bar: 'baz'}) // 'object'
getType('foo') // 'string'
getType() // 'undefined'
Do NOT use built-in constructors for primitive values! (JavaScript 101)
If for some reason you do use built in constructor classes for constructing your primitive values, such as :
- Strings,
- Numbers and
- Booleans
You should stop doing it asap, before your hurt somebody! Consider the following example.
/*
* It has no impact on string values
*/
const myString = new String('f00');
console.log(myString); // 'f00'
getType(myString); // 'string'
/*
* But do note that using the Number constructor
* can produce some unwanted results and introduce
* hard to trace bugs due to the quirky nature of JavaScript.
*
* Consider the following :
*/
const myNum = new Number('This is not a number');
console.log(myNum); // NaN
isNumber(myNum); // true
This is due how JavaScript language works as NaN value is treated as an instance of Number class! That's why you should AVOID using built-in constructor classes for primitive values and just use the simpler, shorter and more conviniant way of just declaring them instead.
Check for certain data type
// Import the checks
const {
isArray, isBoolean, isDate,
isError, isFunction ,isNull,
isNumber, isObject,
isString, isUndefined
} = require('pretty-easy-data-types');
isArray([ 'f00' ]) // true
isBoolean(false) // true
isDate(new Date()) // true
isError(new Error()) // true
isFunction(() => {}) // true
isNull(null) // true
isNumber(25) // true
isObject({val: false}) // true
isString('') // true
isUndefined() // true
Gotchas!
There are a few gotchas that you should be aware of. One of which, that objects and arrays are of different data types. This is exteremely important! Instances of classes that derive from an Object class, such as Array, Error and Date data type values, have their own constructors - meaning, they're not instances of Object classes, but rather instances of their respective classes.
const array = [ 'f00', 12, null ];
isObject(array) // false
isArray(array) // true
Releases
The module follows the Semantic Versioning standard to communicate what kinds of changes are introduced in the new releases.
Versioning
Patch releases : n.n.X -> Bug fixes, documentation updates, code cleanups, new test cases, optimization stuff and other minor changes that you should probably not be aware of;
Minor releases : n.X.n -> New feature(s) which don't break the existing ones. These ofter refer to minor TypeScript API changes (mainly due to declarations; JavaScript code will not be affected by these changes), code refactoring, some under the sheet changes that you should not worry about too much;
Major releases : X.n.n -> Changes that could possibly introduce the backwards compatibility issues. These are however very rare and could be relevant to you only in the case of an endpoint API change and the way you communicate with the module.
Changelogs
04/06 - v1.1.1
- Code refactor
- Documentation typos corrected
04/05 - v1.1.0
- Update to unit test approach
- New test cases
- Minor improvements to the code
03/20 - v1.0.3
- Documentation updates
03/19 - v1.0.2
- Support for Node < 1.8 abbandoned
- Modular approach to unit tests
- New test cases
- Documentation updates to reflect the changes
03/16 - v1.0.1
- Documentation updates
03/15 - v1.0.0
- Initial release
Want to contribute?
Great! Anyone can help make this project better - check out the github repository and make a pull request!
Found a bug?
Please open a an issue.
License
Copyright (c) 2017 Ognjen Jevremović
Licensed under the MIT License.