ee-types
Reliable & easy Js & ES Type detection
Last updated a year ago by ee .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install ee-types 
SYNC missed versions from official npm registry.

ee-types

Easy and reliable type detection with ES6+ support.

Supported by joinbox.com, your swiss node.js & javascript agency :rocket:

npm Travis

Compatibility

For a version supporting older browsers and node version please use ee-types version < 3.0.0. Compatible with node 10+ (--experimental-modules flag) and browsers supporting es modules.

Importing

node

import types from 'ee-types';

browser

from html

<script type="module" src="node_modules/ee-types/src/types.mjs"></script>

from within a module

import types from 'node_modules/ee-types/src/types.mjs';

API

Be aware that the different object types like Maps, Promises and so on are not detected as objects but as their respective type. If you need to detect them as objects please use the types.someObject() method instead of the types.object() method.

Explicitly test for a type.

types.array([]) // true

Get the type of some variable

types(/[a-z]+/gi) // regexp

Supported Types

  • string
  • number
  • boolean
  • array
  • intArray
  • floatArray
  • object
  • function
  • symbol
  • date
  • regexp
  • error
  • undefined
  • buffer
  • null
  • arrayBuffer
  • map
  • weakMap
  • set
  • weakSet
  • dataView
  • float32Array
  • float64Array
  • int8Array
  • int16Array
  • int32Array
  • uInt8Array
  • uInt16Array
  • uInt32Array
  • uInt8ClampedArray
  • generator
  • promise
  • someObject

Object Types

Since many of the types defined by javascript are just special objects that also can be treated as normal objects ee-types has a bunch of methods that allow you to handle that correctly.

For example, Map is an object, or a custom class where you define a getter that returns a specific name for your object:

const X = class {
    get [Symbol.toStringTag]() {
        return 'AQL query';;
    }
};

console.log(Object.prototype.toString.call(new X()));
// prints: [object AQL Query]
Check for a classic object
types.object({}); // true
types.object(new Map()); // false

// be aware that if you are not explicitly testing
// for an object any object will be treated as one
types({}) // 'object'
types(new Map()) // 'object'

Check any type of object
types.someObject({}); // true
types.someObject(new Map()); // false
types(new Map()) // 'object'

Examples

var types = require('ee-types');


types.string('nope');                // true
types.strign(new String('yeah'));    // true


types(2) // number

types([]]) // array
types(new Array()]) // array
types(new Int8Array()]) // int8Array


types.promise(Promise.all()) // true

Current Tags

  • 3.0.2                                ...           latest (a year ago)

19 Versions

  • 3.0.2                                ...           a year ago
  • 3.0.1                                ...           2 years ago
  • 3.0.0                                ...           2 years ago
  • 2.2.1                                ...           2 years ago
  • 2.2.0                                ...           2 years ago
  • 2.1.4                                ...           3 years ago
  • 2.1.2                                ...           4 years ago
  • 2.1.1                                ...           4 years ago
  • 2.1.0                                ...           4 years ago
  • 2.0.4                                ...           5 years ago
  • 2.0.3                                ...           5 years ago
  • 2.0.2                                ...           5 years ago
  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.1.3                                ...           6 years ago
  • 0.1.2                                ...           7 years ago
  • 0.1.1                                ...           7 years ago
  • 0.1.0                                ...           7 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 3
Last Day 0
Last Week 3
Last Month 3
Dependencies (0)
None
Dev Dependencies (1)
Dependents (147)

Copyright 2014 - 2017 © taobao.org |