Comparing version 1.0.0 to 2.0.0
170
lib/types.js
(function(){ | ||
var util = require('util') | ||
, Class = require('ee-class') | ||
, Types | ||
, types | ||
, proto | ||
, type | ||
; | ||
var util = require('util') | ||
, Class = require('ee-class') | ||
, Types | ||
, types | ||
, proto | ||
, type | ||
; | ||
Types = new Class({ | ||
Types = new Class({ | ||
init: function(){ | ||
['string', 'number', 'boolean', 'array', 'object', 'function', 'date', 'regexp', 'error', 'undefined', 'buffer', 'null', 'symbol'].forEach(function(type) { | ||
this[type+'Array'] = function(arg) { | ||
return this.__array(arg, type); | ||
}.bind( this ); | ||
if (type === 'string' || type === 'number' || type === 'boolean' || type === 'undefined') { | ||
this[type.substr(0, 1)] = this[type].bind(this); | ||
this[type.substr(0, 1) + 'a'] = this[type + 'Array'].bind(this); | ||
} | ||
}.bind(this)); | ||
} | ||
string: function(arg){ return typeof arg === 'string' || Object.prototype.toString.call(arg) === '[object String]'; } | ||
, number: function(arg){ return typeof arg === 'number' || Object.prototype.toString.call(arg) === '[object Number]'; } | ||
, boolean: function(arg){ return typeof arg === 'boolean' || Object.prototype.toString.call(arg) === '[object Boolean]'; } | ||
, array: function(arg){ return util.isArray(arg); } | ||
, intArray: function(arg){ return types.int8Array(arg) || types.int16Array(arg) || types.int32Array(arg) || types.uInt8Array(arg) || types.uInt16Array(arg) || types.uInt32Array(arg) || types.uInt8ClampedArray(arg); } | ||
, floatArray: function(arg){ return util.isArray(arg) || types.float32Array(arg); } | ||
, object: function(arg){ return Object.prototype.toString.call(arg) === '[object Object]' && !types.promise(arg);} | ||
, function: function(arg){ return typeof arg === 'function' } | ||
, symbol: function(arg){ return typeof arg === 'symbol' } | ||
, date: function(arg){ return util.isDate(arg); } | ||
, regexp: function(arg){ return util.isRegExp(arg); } | ||
, error: function(arg){ return util.isError(arg); } | ||
, undefined: function(arg){ return typeof arg === 'undefined' } | ||
, buffer: function(arg){ return Buffer.isBuffer(arg); } | ||
, null: function(arg){ return null === arg; } | ||
, arrayBuffer: function(arg){ return Object.prototype.toString.call(arg) === '[object ArrayBuffer]'; } | ||
, map: function(arg){ return Object.prototype.toString.call(arg) === '[object Map]'; } | ||
, weakMap: function(arg){ return Object.prototype.toString.call(arg) === '[object WeakMap]'; } | ||
, set: function(arg){ return Object.prototype.toString.call(arg) === '[object Set]'; } | ||
, weakSet: function(arg){ return Object.prototype.toString.call(arg) === '[object WeakSet]'; } | ||
, dataView: function(arg){ return Object.prototype.toString.call(arg) === '[object DataView]'; } | ||
, float32Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Float32Array]'; } | ||
, float64Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Float64Array]'; } | ||
, int8Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Int8Array]'; } | ||
, int16Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Int16Array]'; } | ||
, int32Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Int32Array]'; } | ||
, uInt8Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Uint8Array]'; } | ||
, uInt16Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Uint16Array]'; } | ||
, uInt32Array: function(arg){ return Object.prototype.toString.call(arg) === '[object Uint32Array]'; } | ||
, uInt8ClampedArray: function(arg){ return Object.prototype.toString.call(arg) === '[object Uint8ClampedArray]'; } | ||
, generator: function(arg){ return Object.prototype.toString.call(arg) === '[object Generator]'; } | ||
, promise: function(arg){ return Object.prototype.toString.call(arg) === '[object Object]' && types.function(arg.then) && types.function(arg.catch); } | ||
, string: function(arg){ return typeof arg === 'string' || Object.prototype.toString.call(arg) === '[object String]'; } | ||
, number: function(arg){ return typeof arg === 'number' || Object.prototype.toString.call(arg) === '[object Number]'; } | ||
, boolean: function(arg){ return typeof arg === 'boolean' || Object.prototype.toString.call(arg) === '[object Boolean]'; } | ||
, array: function(arg){ return util.isArray(arg); } | ||
, object: function(arg){ return Object.prototype.toString.call(arg) === '[object Object]'} | ||
, function: function(arg){ return typeof arg === 'function' } | ||
, symbol: function(arg){ return typeof arg === 'symbol' } | ||
, date: function(arg){ return util.isDate(arg); } | ||
, regexp: function(arg){ return util.isRegExp(arg); } | ||
, error: function(arg){ return util.isError(arg); } | ||
, undefined: function(arg){ return typeof arg === 'undefined' } | ||
, buffer: function(arg){ return Buffer.isBuffer(arg); } | ||
, null: function(arg){ return null === arg; } | ||
, __array: function(arg, type) { | ||
// has to an array | ||
if (!this.array(arg)) return false; | ||
// check every item | ||
return arg.every(this[type].bind(this)); | ||
} | ||
} ); | ||
, __array: function(arg, type) { | ||
// has to an array | ||
if (!this.array(arg)) return false; | ||
// check every item | ||
return arg.every(this[type].bind(this)); | ||
} | ||
} ); | ||
// instantiate the singleton | ||
types = new Types(); | ||
// instantiate the singleton | ||
types = new Types(); | ||
// this is the exported method, it returns | ||
// the type as string | ||
type = function(item) { | ||
return types.s(item) ? 'string' : | ||
types.n(item) ? 'number' : | ||
types.b(item) ? 'boolean' : | ||
types.object(item) ? 'object' : | ||
types.array(item) ? 'array' : | ||
types.null(item) ? 'null' : | ||
types.u(item) ? 'undefined' : | ||
types.function(item) ? 'function' : | ||
types.date(item) ? 'date' : | ||
types.regexp(item) ? 'regexp' : | ||
types.error(item) ? 'error' : | ||
types.buffer(item) ? 'buffer' : | ||
types.symbol(item) ? 'symbol' : | ||
types.map(item) ? 'map' : | ||
types.weakMap(item) ? 'weakMap' : | ||
types.set(item) ? 'set' : | ||
types.weakSet(item) ? 'weakSet' : | ||
types.promise(item) ? 'promise' : | ||
types.dataView(item) ? 'dataView' : | ||
types.float32Array(item) ? 'float32Array' : | ||
types.float64Array(item) ? 'float64Array' : | ||
types.int8Array(item) ? 'int8Array' : | ||
types.int16Array(item) ? 'int16Array' : | ||
types.int32Array(item) ? 'int32Array' : | ||
types.generator(item) ? 'generator' : | ||
types.uInt8Array(item) ? 'uInt8Array' : | ||
types.uInt16Array(item) ? 'uInt16Array' : | ||
types.uInt32Array(item) ? 'uInt32Array' : | ||
types.intArray(item) ? 'intArray' : | ||
types.floatArray(item) ? 'floatArray' : | ||
types.uInt8ClampedArray(item) ? 'uInt8ClampedArray' : | ||
types.arrayBuffer(item) ? 'arrayBuffer' : 'unknown'; | ||
}; | ||
// gets its proto | ||
proto = Object.getPrototypeOf(types); | ||
// this is the exported method, it returns | ||
// the type as string | ||
type = function(item) { | ||
return types.s(item) ? 'string' : | ||
types.n(item) ? 'number' : | ||
types.b(item) ? 'boolean' : | ||
types.array(item) ? 'array' : | ||
types.function(item) ? 'function' : | ||
types.date(item) ? 'date' : | ||
types.regexp(item) ? 'regexp' : | ||
types.error(item) ? 'error' : | ||
types.buffer(item) ? 'buffer' : | ||
types.null(item) ? 'null' : | ||
types.u(item) ? 'undefined' : | ||
types.symbol(item) ? 'symbol' : | ||
types.object(item) ? 'object' : 'unknown'; | ||
}; | ||
// apply the class methods to the type function | ||
Object.keys(proto).forEach(function(key){ | ||
if (!type[key]) type[key] = types[key].bind(types); | ||
else throw new Error('Failed to map property «'+key+'» of types to type :('); | ||
}); | ||
// gets its proto | ||
proto = Object.getPrototypeOf(types); | ||
Object.keys(types).forEach(function(key){ | ||
if (!type[key]) type[key] = types[key]; | ||
else throw new Error('Failed to map property «'+key+'» of types to type :('); | ||
}); | ||
// apply the class methods to the type function | ||
Object.keys(proto).forEach(function(key){ | ||
if (!type[key]) type[key] = types[key].bind(types); | ||
else throw new Error('Failed to map property «'+key+'» of types to type :('); | ||
}); | ||
Object.keys(types).forEach(function(key){ | ||
if (!type[key]) type[key] = types[key]; | ||
else throw new Error('Failed to map property «'+key+'» of types to type :('); | ||
}); | ||
module.exports = type; | ||
module.exports = type; | ||
})(); |
{ | ||
"name" : "ee-types" | ||
, "description" : "Reliabale type detection" | ||
, "version" : "1.0.0" | ||
, "description" : "Reliable type detection" | ||
, "version" : "2.0.0" | ||
, "homepage" : "https://github.com/eventEmitter/ee-types" | ||
@@ -6,0 +6,0 @@ , "author" : "Michael van der Weg <michael@joinbox.com> (http://joinbox.com/)" |
# ee-types | ||
Reliabale type detection | ||
Easy and reliable type detection with ES6+ support. | ||
@@ -13,42 +13,68 @@ | ||
Explicity test for a type | ||
var type = require('ee-types'); | ||
types.array([]) // true | ||
type.string('nope'); // true | ||
type.strign(new String('yeah')); // true | ||
type.s('michael'); // true | ||
Get the type of some input | ||
types(/[a-z]+/gi) // regexp | ||
type(2) // number | ||
## API | ||
## Supported Types | ||
type() // returns the loawercase type | ||
- 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 | ||
type.string() | ||
type.number() | ||
type.boolean() | ||
type.function() | ||
type.object() | ||
type.date() | ||
type.error() | ||
type.regexp() | ||
type.array() | ||
type.buffer() | ||
type.null() | ||
type.undefined() | ||
type.symbol() | ||
shortcut methods | ||
type.s() // string | ||
type.n() // number | ||
type.b() // boolean | ||
type.u() // undefined | ||
## Examples | ||
check array and their contents. for every type above there is an array method like the one below. | ||
type.stringArray([ 'hi', new String('name'), 'is', 'michael' ]) // true | ||
type.sa([ 'hi', new String('name'), 'is', 'michael' ]) // true | ||
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 | ||
(function() { | ||
'use strict'; | ||
let Class = require('ee-class'); | ||
let assert = require('assert'); | ||
let type = require('../'); | ||
var Class = require('ee-class') | ||
, assert = require('assert'); | ||
var type = require('../') | ||
describe('Types', function() { | ||
it('must be detected reliable', function() { | ||
describe('Type Checking [<ES6]', function() { | ||
it('Strings', function() { | ||
assert(type.string('s')); | ||
assert(type.string(new String('s'))); | ||
}); | ||
it('Numbers', function() { | ||
assert(type.number(-1)); | ||
assert(type.number(new Number(0))); | ||
assert(type.number(Infinity)); | ||
}); | ||
it('Booleans', function() { | ||
assert(type.boolean(false)); | ||
assert(type.boolean(new Boolean(true))); | ||
}); | ||
it('Arrays', function() { | ||
assert(type.array([])); | ||
assert(type.array(new Array())); | ||
assert(!type.array(new Float32Array())); | ||
assert(!type.array(new Float64Array())); | ||
assert(!type.array(new Int8Array())); | ||
assert(!type.array(new Int16Array())); | ||
assert(!type.array(new Int32Array())); | ||
assert(!type.array(new Uint8Array())); | ||
assert(!type.array(new Uint16Array())); | ||
assert(!type.array(new Uint32Array())); | ||
assert(!type.array(new Uint8ClampedArray())); | ||
}); | ||
it('Objects', function() { | ||
assert(type.object({})); | ||
assert(!type.object(new String('s'))); | ||
assert(!type.object(null)); | ||
assert(!type.object([])); | ||
assert(!type.object(new Map())); | ||
assert(!type.object(new Set())); | ||
assert(!type.object(new Promise(() => {}))); | ||
assert(!type.object((function* (){yield 1;})())); | ||
}); | ||
assert(type.symbol(Symbol())); | ||
it('Null', function() { | ||
assert(type.null(null)); | ||
}); | ||
it('Undefined', function() { | ||
assert(type.undefined(undefined)); | ||
assert(type.undefined()); | ||
}); | ||
it('Function', function() { | ||
assert(type.function(function(){})); | ||
assert(type.function(() => {})); | ||
assert(type.function(function* a() {})); | ||
assert(!type.function((function* (){yield 1;})())); | ||
}); | ||
it('Dates', function() { | ||
assert(type.date(new Date())); | ||
}); | ||
it('RegExps', function() { | ||
assert(type.regexp(/s/gi)); | ||
}); | ||
it('Errors', function() { | ||
assert(type.error(new Error())); | ||
assert(type.error(new EvalError())); | ||
assert(type.error(new RangeError())); | ||
assert(type.error(new ReferenceError())); | ||
assert(type.error(new SyntaxError())); | ||
assert(type.error(new TypeError())); | ||
assert(type.error(new URIError())); | ||
}); | ||
it('Buffers', function() { | ||
assert(type.buffer(new Buffer(12))); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('Type Checking [>=ES6]', function() { | ||
it('Symbols', function() { | ||
assert(type.symbol(Symbol())); | ||
}); | ||
it('ArrayBuffers', function() { | ||
assert(type.arrayBuffer(new ArrayBuffer())); | ||
}); | ||
it('Maps', function() { | ||
assert(type.map(new Map())); | ||
}); | ||
it('Sets', function() { | ||
assert(type.set(new Set())); | ||
}); | ||
it('WeakMaps', function() { | ||
assert(type.weakMap(new WeakMap())); | ||
}); | ||
it('WeakSets', function() { | ||
assert(type.weakSet(new WeakSet())); | ||
}); | ||
it('DataViews', function() { | ||
assert(type.dataView(new DataView(new ArrayBuffer()))); | ||
}); | ||
it('Promises', function() { | ||
assert(type.promise(new Promise(() => {}))); | ||
assert(type.promise(Promise.all())); | ||
assert(type.promise(Promise.resolve())); | ||
}); | ||
it('Float32Arrays', function() { | ||
assert(type.float32Array(new Float32Array())); | ||
}); | ||
it('Float64Arrays', function() { | ||
assert(type.float64Array(new Float64Array())); | ||
}); | ||
it('Int8Arrays', function() { | ||
assert(type.int8Array(new Int8Array())); | ||
}); | ||
it('Int16Arrays', function() { | ||
assert(type.int16Array(new Int16Array())); | ||
}); | ||
it('Int32Arrays', function() { | ||
assert(type.int32Array(new Int32Array())); | ||
}); | ||
it('UInt8Arrays', function() { | ||
assert(type.uInt8Array(new Uint8Array())); | ||
}); | ||
it('UInt16Arrays', function() { | ||
assert(type.uInt16Array(new Uint16Array())); | ||
}); | ||
it('UInt32Arrays', function() { | ||
assert(type.uInt32Array(new Uint32Array())); | ||
}); | ||
it('IntArrays', function() { | ||
assert(type.intArray(new Uint32Array())); | ||
}); | ||
it('FloatArrays', function() { | ||
assert(type.floatArray(new Float32Array())); | ||
}); | ||
it('Uint8ClampedArrays', function() { | ||
assert(type.uInt8ClampedArray(new Uint8ClampedArray())); | ||
}); | ||
it('Generators', function() { | ||
assert(type.generator((function* (){yield 1;})())); | ||
}); | ||
}); | ||
})(); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16189
266
80
1