npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

pretty-easy-data-types

v1.1.1

Published

Utility library for checking the data type of a supplied parameter

Downloads

2,481

Readme

pretty-easy-data-types

 

NPM Version Build Status - Travis CI Build Status - Appveyor Tests Dependancies

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.