Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

fjl-is

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

fjl-is

[![Build Status](https://travis-ci.org/functional-jslib/fjl-is.png)](https://travis-ci.org/functional-jslib/fjl-is) [![GitHub version](https://badge.fury.io/gh/functional-jslib%2Ffjl-is.svg)](http://badge.fury.io/gh/functional-jslib%2Ffjl-is) [![NPM ver

  • 1.0.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

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

FAQs

Package last updated on 11 Jan 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc