Git Product home page Git Product logo

type's Introduction

Type

Build status GitHub Maintenance intention for this crate GitHub package version

Tiny library for determining the type.

Correctly handles arrays and null and can detection different objects for the name of the constructor.

Everything works out of the box.

Size: 133 B.

Install

npm i ariarzer/type

Usage

const type = require('type');

type([]); //=> 'array'
type(new Date()); //=> 'object'

Also you can use the second argument true to detection objects. In this case the constructor name is returned in lowercase.

type(new Date(), true); //=> 'date'
type(new myObject(), true); //=> 'myobject'

Configuration

Modes:

  • Normal: works like almost typeof, but it is correct to handle null and arrays.

  • All: distinguishes types of objects, returns the name of the object constructor in lowercase.

Examples:

normal all
undefined 'undefined' 'undefined'
null 'null' 'null'
[] 'array' 'array'
Symbol() 'symbol' 'symbol'
new WeakSet() 'object' 'weakset'
new Date() 'object' 'date'
new MyObject() 'object' 'myobject'
document.createElement('div') 'object' 'htmldivelement'
const config = {
  mode: 'all', // string: 'all' || 'normal' (default: 'normal')
}

Example of using modes:

const type = require('type');

const myType = type.create({mode: 'all'});
myType(new myObject()); //=> 'myobject'

// But, you can run that... 
type(new myObject(), true); //=> 'myobject';
// ...and get a similar result. 

Custom types:

If you want to detect s custom type, for example, the days of the week, you can do this with use a config.

// write a config..
const week =  ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];

const customTypes = week.map((item, index) => {
  return {
    name: item,
    is: function (arg) {
      return type(arg, true) === 'date' && arg.getDay() === index;
    },
  };
});

// ...create a custom function...
const daysDetector = type.create(customTypes);

// ..and use it
daysDetector(new Date(2019, 4, 25)) //=> 'saturday'  (May 25 2019) 
daysDetector(new Date(2019, 4, 26)) //=> 'sunday'    (May 26 2019) 
daysDetector(new myObject()) //=> 'object'

You can use modes and custom types together.

type's People

Contributors

ariarzer avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.