This package has been deprecated

Author message:

Deprecated in favor of 'fjl'. All 0.2X.X versions and above contain all the 'is' methods along with newer implementations and more additions.

fjl-is

1.0.1 • Public • Published

Build Status GitHub version NPM version Dependencies

fjl-is

Is type checkers (isType, isBoolean etc.). All type checkers return a boolean indicating if passed in value matches given type or not.

Installation

  • npm install fjl-is or
  • yarn add fjl-is

Including module

  • Module exported for es6 and commonjs modules by way of package.json by default.
// Es6/2015
import {isNumber, isType} from 'fjl-is';

// CommonJs
const {isNumber, isType} = require('fjl-is');
  • Module is also exported to other formats available via './dist' folder. Other formats here are:
  • iife, umd, and amd as well as the other two mentioned above (es6-module and cjs).

Usage:

import {isType, isNumber} from 'fjl-is';

// Filter an array by your type
[...].filter(isType(MyType)) // Filtering using `isType`'s curriable nature

// Checking with constructor name and/or actual constructor
isType('Array', []) === true; // Uses `(value).constructor.name` behind the scenes via `fjl-typeof` module.
isType(Array, []) === true; // ""
    
// Check for true `Number` (not `NaN` being reported as `Number`).
isNumber(0 / 0) === `false` // `0 / 0` is `NaN` 

// Check for `string`
isString('') === `true`

// etc..

Note: All checkers are strict; E.g., they do not check for instance-of/inheritance except for isFunction (this one uses instanceof (more useful)) and isArray (which is actually just Array.isArray).

Members:

isType (Type, value) (curried)

Params

  • Type - A type's name or the Type constructor itself.
    Returns a boolean indicating whether value matches given type or not.
  • value - Value to check.

Returns

Boolean

_isType (Type, value) (un-curried)

Same is isType but un-curried.

isArray (value)

Returns a boolean indicating whether value matches type or not.

isBoolean (value)

""

isFunction (value)

""

isNumber (value)

""

isObject (value)

""

isString (value)

""

isset (value)

Returns a boolean indicating whether value is not null and not undefined or not; E.g. useful for places where you need explicit not null and not undefined values (like when allowing 0 or empty string but not allowing null or undefined from a function/statement).

Motivation

Needed some stronger type-checking for javascript that didn't do any of the older hacks (Object.prototype.toString.call) and worked for all values and types (including null, undefined, and NaN); Example:

// Old hack etc. 
const isType = (Type, x) => {
    if (x === null || Type === 'Null') {
        return 'Null';
    }
    else if (x === undefined || Type === 'Undefined') {
        return 'Undefined';
    } 
    const typeName = (Type instanceof Function) ? Type.name : Type,
        typeDump = Object.prototype.toString.call(x), // === "[object Number]" // part of
        foundTypeName = typeDump.substring(8, typeDump.length - 1);  
     
    return (foundTypeName === 'Number' && isNaN(Number)) ?
        'NaN' === typeName : foundTypeName === typeName;
};

// Note: Old hack from above is still pretty good though it is even easier to get a constructor's `name` property
(x).constructor.name // which runs behind the scenes in the code below (after it's behind the scenes null checks and all..)

// New way (`typeOf` comes from `fjl-typeof` which does the type name getting part from above in a more modern way
const isType = (type, obj) => typeOf(obj) === (isFunction(type) ? type.name : type);

License

BSD 3 Clause

Readme

Keywords

none

Package Sidebar

Install

npm i fjl-is

Weekly Downloads

0

Version

1.0.1

License

BSD-3-Clause

Last publish

Collaborators

  • elycruz