should-type
Advanced tools
Comparing version 0.0.1 to 0.0.2
176
index.js
@@ -6,41 +6,43 @@ var toString = Object.prototype.toString; | ||
var NUMBER = 'number';// | ||
var UNDEFINED = 'undefined';// | ||
var STRING = 'string';// | ||
var BOOLEAN = 'boolean';// | ||
var OBJECT = 'object'; | ||
var FUNCTION = 'function';// | ||
var NULL = 'null';// | ||
var ARRAY = 'array'; | ||
var REGEXP = 'regexp';// | ||
var DATE = 'date';// | ||
var ERROR = 'error';// | ||
var ARGUMENTS = 'arguments';// | ||
var SYMBOL = 'symbol'; | ||
var ARRAY_BUFFER = 'array-buffer';// | ||
var TYPED_ARRAY = 'typed-array';// | ||
var DATA_VIEW = 'data-view'; | ||
var MAP = 'map'; | ||
var SET = 'set'; | ||
var WEAK_SET = 'weak-set'; | ||
var WEAK_MAP = 'weak-map'; | ||
var PROMISE = 'promise';// | ||
var type = { | ||
NUMBER: 'number', | ||
UNDEFINED: 'undefined', | ||
STRING: 'string', | ||
BOOLEAN: 'boolean', | ||
OBJECT: 'object', | ||
FUNCTION: 'function', | ||
NULL: 'null', | ||
ARRAY: 'array', | ||
REGEXP: 'regexp', | ||
DATE: 'date', | ||
ERROR: 'error', | ||
ARGUMENTS: 'arguments', | ||
SYMBOL: 'symbol', | ||
ARRAY_BUFFER: 'array-buffer', | ||
TYPED_ARRAY: 'typed-array', | ||
DATA_VIEW: 'data-view', | ||
MAP: 'map', | ||
SET: 'set', | ||
WEAK_SET: 'weak-set', | ||
WEAK_MAP: 'weak-map', | ||
PROMISE: 'promise', | ||
var WRAPPER_NUMBER = 'object-number';// | ||
var WRAPPER_BOOLEAN = 'object-boolean';// | ||
var WRAPPER_STRING = 'object-string';// | ||
WRAPPER_NUMBER: 'object-number', | ||
WRAPPER_BOOLEAN: 'object-boolean', | ||
WRAPPER_STRING: 'object-string', | ||
// node buffer | ||
var BUFFER = 'buffer';// | ||
BUFFER: 'buffer', | ||
// dom html element | ||
var HTML_ELEMENT = 'html-element';// | ||
var HTML_ELEMENT_TEXT = 'html-element-text';// | ||
var DOCUMENT = 'document';// | ||
var WINDOW = 'window';// | ||
var FILE = 'file'; | ||
var FILE_LIST = 'file-list'; | ||
var BLOB = 'blob'; | ||
HTML_ELEMENT: 'html-element', | ||
HTML_ELEMENT_TEXT: 'html-element-text', | ||
DOCUMENT: 'document', | ||
WINDOW: 'window', | ||
FILE: 'file', | ||
FILE_LIST: 'file-list', | ||
BLOB: 'blob', | ||
var XHR = 'xhr';// | ||
XHR: 'xhr' | ||
}; | ||
@@ -50,97 +52,99 @@ module.exports = function getType(instance) { | ||
switch (type) { | ||
case NUMBER: | ||
return NUMBER; | ||
case UNDEFINED: | ||
return UNDEFINED; | ||
case STRING: | ||
return STRING; | ||
case BOOLEAN: | ||
return BOOLEAN; | ||
case FUNCTION: | ||
return FUNCTION; | ||
case SYMBOL: | ||
return SYMBOL; | ||
case OBJECT: | ||
if (instance === null) return NULL; | ||
switch(type) { | ||
case type.NUMBER: | ||
return type.NUMBER; | ||
case type.UNDEFINED: | ||
return type.UNDEFINED; | ||
case type.STRING: | ||
return type.STRING; | ||
case type.BOOLEAN: | ||
return type.BOOLEAN; | ||
case type.FUNCTION: | ||
return type.FUNCTION; | ||
case type.SYMBOL: | ||
return type.SYMBOL; | ||
case type.OBJECT: | ||
if(instance === null) return type.NULL; | ||
var clazz = toString.call(instance); | ||
switch (clazz) { | ||
switch(clazz) { | ||
case '[object String]': | ||
return WRAPPER_STRING; | ||
return type.WRAPPER_STRING; | ||
case '[object Boolean]': | ||
return WRAPPER_BOOLEAN; | ||
return type.WRAPPER_BOOLEAN; | ||
case '[object Number]': | ||
return WRAPPER_NUMBER; | ||
return type.WRAPPER_NUMBER; | ||
case '[object Array]': | ||
return ARRAY; | ||
return type.ARRAY; | ||
case '[object RegExp]': | ||
return REGEXP; | ||
return type.REGEXP; | ||
case '[object Error]': | ||
return ERROR; | ||
return type.ERROR; | ||
case '[object Date]': | ||
return DATE; | ||
return type.DATE; | ||
case '[object Arguments]': | ||
return ARGUMENTS; | ||
return type.ARGUMENTS; | ||
case '[object Math]': | ||
return OBJECT; | ||
return type.OBJECT; | ||
case '[object JSON]': | ||
return OBJECT; | ||
return type.OBJECT; | ||
case '[object ArrayBuffer]': | ||
return ARRAY_BUFFER; | ||
return type.ARRAY_BUFFER; | ||
case '[object Int8Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Uint8Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Uint8ClampedArray]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Int16Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Uint16Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Int32Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Uint32Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Float32Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object Float64Array]': | ||
return TYPED_ARRAY; | ||
return type.TYPED_ARRAY; | ||
case '[object DataView]': | ||
return DATA_VIEW; | ||
return type.DATA_VIEW; | ||
case '[object Map]': | ||
return MAP; | ||
return type.MAP; | ||
case '[object WeakMap]': | ||
return WEAK_MAP; | ||
return type.WEAK_MAP; | ||
case '[object Set]': | ||
return SET; | ||
return type.SET; | ||
case '[object WeakSet]': | ||
return WEAK_SET; | ||
return type.WEAK_SET; | ||
case '[object Promise]': | ||
return PROMISE; | ||
return type.PROMISE; | ||
case '[object Window]': | ||
return WINDOW; | ||
return type.WINDOW; | ||
case '[object HTMLDocument]': | ||
return DOCUMENT; | ||
return type.DOCUMENT; | ||
case '[object Blob]': | ||
return BLOB; | ||
return type.BLOB; | ||
case '[object File]': | ||
return FILE; | ||
return type.FILE; | ||
case '[object FileList]': | ||
return FILE_LIST; | ||
return type.FILE_LIST; | ||
case '[object XMLHttpRequest]': | ||
return XHR; | ||
return type.XHR; | ||
case '[object Text]': | ||
return HTML_ELEMENT_TEXT; | ||
return type.HTML_ELEMENT_TEXT; | ||
default: | ||
if (isPromiseExist && instance instanceof Promise) return PROMISE; | ||
if(isPromiseExist && instance instanceof Promise && getType(instance.then) === FUNCTION && instance.then.length >= 2) return type.PROMISE; | ||
if (isBufferExist && instance instanceof Buffer) return BUFFER; | ||
if(isBufferExist && instance instanceof Buffer) return type.BUFFER; | ||
if (/^\[object HTML\w+Element\]$/.test(clazz)) return HTML_ELEMENT; | ||
if(/^\[object HTML\w+Element\]$/.test(clazz)) return type.HTML_ELEMENT; | ||
if (clazz === '[object Object]') return OBJECT; | ||
if(clazz === '[object Object]') return type.OBJECT; | ||
} | ||
} | ||
}; | ||
module.exports.type = type; |
{ | ||
"name": "should-type", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Simple module to get instance type", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
Sorry, the diff of this file is not supported yet
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
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
6367
134