Skip to content
/ type Public

Tiny library for determining the type. Can detect custom types.

License

Notifications You must be signed in to change notification settings

ariarzer/type

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Tiny library for determining the type. Can detect custom types.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published