Skip to content

Commit

Permalink
Merge pull request #79 from keithamus/remove-tolowercase
Browse files Browse the repository at this point in the history
perf: do not lowercase returned values
  • Loading branch information
meeber authored Oct 8, 2016
2 parents 3822696 + 48c8108 commit 638c09a
Show file tree
Hide file tree
Showing 5 changed files with 217 additions and 209 deletions.
56 changes: 28 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,15 @@ var type = require('type-detect');
#### array

```js
assert(type([]) === 'array');
assert(type(new Array()) === 'array');
assert(type([]) === 'Array');
assert(type(new Array()) === 'Array');
```

#### regexp

```js
assert(type(/a-z/gi) === 'regexp');
assert(type(new RegExp('a-z')) === 'regexp');
assert(type(/a-z/gi) === 'RegExp');
assert(type(new RegExp('a-z')) === 'RegExp');
```

#### function
Expand All @@ -132,7 +132,7 @@ assert(type(function () {}) === 'function');
#### date

```js
assert(type(new Date) === 'date');
assert(type(new Date) === 'Date');
```

#### number
Expand All @@ -144,14 +144,14 @@ assert(type(-1) === 'number');
assert(type(-1.234) === 'number');
assert(type(Infinity) === 'number');
assert(type(NaN) === 'number');
assert(type(new Number(1)) === 'number');
assert(type(new Number(1)) === 'Number'); // note - the object version has a capital N
```

#### string

```js
assert(type('hello world') === 'string');
assert(type(new String('hello')) === 'string');
assert(type(new String('hello')) === 'String'); // note - the object version has a capital S
```

#### null
Expand All @@ -172,34 +172,34 @@ assert(type(null) !== 'undefined');

```js
var Noop = function () {};
assert(type({}) === 'object');
assert(type(Noop) !== 'object');
assert(type(new Noop) === 'object');
assert(type(new Object) === 'object');
assert(type({}) === 'Object');
assert(type(Noop) !== 'Object');
assert(type(new Noop) === 'Object');
assert(type(new Object) === 'Object');
```

#### ECMA6 Types

All new ECMAScript 2015 objects are also supported, such as Promises and Symbols:

```js
assert(type(new Map() === 'map');
assert(type(new WeakMap()) === 'weakmap');
assert(type(new Set()) === 'set');
assert(type(new WeakSet()) === 'weakset');
assert(type(Symbol()) === 'symbol');
assert(type(new Promise(callback) === 'promise');
assert(type(new Int8Array()) === 'int8array');
assert(type(new Uint8Array()) === 'uint8array');
assert(type(new UInt8ClampedArray()) === 'uint8clampedarray');
assert(type(new Int16Array()) === 'int16array');
assert(type(new Uint16Array()) === 'uint16array');
assert(type(new Int32Array()) === 'int32array');
assert(type(new UInt32Array()) === 'uint32array');
assert(type(new Float32Array()) === 'float32array');
assert(type(new Float64Array()) === 'float64array');
assert(type(new ArrayBuffer()) === 'arraybuffer');
assert(type(new DataView(arrayBuffer)) === 'dataview');
assert(type(new Map() === 'Map');
assert(type(new WeakMap()) === 'WeakMap');
assert(type(new Set()) === 'Set');
assert(type(new WeakSet()) === 'WeakSet');
assert(type(Symbol()) === 'Symbol');
assert(type(new Promise(callback) === 'Promise');
assert(type(new Int8Array()) === 'Int8Array');
assert(type(new Uint8Array()) === 'Uint8Array');
assert(type(new UInt8ClampedArray()) === 'Uint8ClampedArray');
assert(type(new Int16Array()) === 'Int16Array');
assert(type(new Uint16Array()) === 'Uint16Array');
assert(type(new Int32Array()) === 'Int32Array');
assert(type(new UInt32Array()) === 'Uint32Array');
assert(type(new Float32Array()) === 'Float32Array');
assert(type(new Float64Array()) === 'Float64Array');
assert(type(new ArrayBuffer()) === 'ArrayBuffer');
assert(type(new DataView(arrayBuffer)) === 'DataView');
```
Also, if you use `Symbol.toStringTag` to change an Objects return value of the `toString()` Method, `type()` will return this value, e.g:
Expand Down
47 changes: 23 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ module.exports = function typeDetect(obj) {
* array literal x 22,479,650 ops/sec ±0.96% (81 runs sampled)
*/
if (isArrayExists && Array.isArray(obj)) {
return 'array';
return 'Array';
}

if (isDom) {
Expand All @@ -107,7 +107,7 @@ module.exports = function typeDetect(obj) {
* - IE Edge <=13 === "[object Object]"
*/
if (obj === globalObject.location) {
return 'location';
return 'Location';
}

/* ! Spec Conformance
Expand All @@ -130,7 +130,7 @@ module.exports = function typeDetect(obj) {
* - IE Edge <=13 === "[object HTMLDocument]"
*/
if (obj === globalObject.document) {
return 'document';
return 'Document';
}

/* ! Spec Conformance
Expand All @@ -140,7 +140,7 @@ module.exports = function typeDetect(obj) {
* - IE <=10 === "[object MSMimeTypesCollection]"
*/
if (obj === (globalObject.navigator || {}).mimeTypes) {
return 'mimetypearray';
return 'MimeTypeArray';
}

/* ! Spec Conformance
Expand All @@ -150,7 +150,7 @@ module.exports = function typeDetect(obj) {
* - IE <=10 === "[object MSPluginsCollection]"
*/
if (obj === (globalObject.navigator || {}).plugins) {
return 'pluginarray';
return 'PluginArray';
}

/* ! Spec Conformance
Expand All @@ -160,7 +160,7 @@ module.exports = function typeDetect(obj) {
* - IE <=10 === "[object HTMLBlockElement]"
*/
if (htmlElementExists && obj instanceof HTMLElement && obj.tagName === 'BLOCKQUOTE') {
return 'htmlquoteelement';
return 'HTMLQuoteElement';
}

/* ! Spec Conformance
Expand All @@ -176,7 +176,7 @@ module.exports = function typeDetect(obj) {
* - Safari === "[object HTMLTableCellElement]"
*/
if (htmlElementExists && obj instanceof HTMLElement && obj.tagName === 'TD') {
return 'htmltabledatacellelement';
return 'HTMLTableDataCellElement';
}

/* ! Spec Conformance
Expand All @@ -192,7 +192,7 @@ module.exports = function typeDetect(obj) {
* - Safari === "[object HTMLTableCellElement]"
*/
if (htmlElementExists && obj instanceof HTMLElement && obj.tagName === 'TH') {
return 'htmltableheadercellelement';
return 'HTMLTableHeaderCellElement';
}
}

Expand Down Expand Up @@ -220,7 +220,7 @@ module.exports = function typeDetect(obj) {
*/
var stringTag = (symbolToStringTagExists && obj[Symbol.toStringTag]);
if (typeof stringTag === 'string') {
return stringTag.toLowerCase();
return stringTag;
}

if (getPrototypeOfExists) {
Expand All @@ -234,7 +234,7 @@ module.exports = function typeDetect(obj) {
* regex constructor x 3,931,108 ops/sec ±0.58% (84 runs sampled)
*/
if (objPrototype === RegExp.prototype) {
return 'regexp';
return 'RegExp';
}

/* ! Speed optimisation
Expand All @@ -244,7 +244,7 @@ module.exports = function typeDetect(obj) {
* date x 3,953,779 ops/sec ±1.35% (77 runs sampled)
*/
if (objPrototype === Date.prototype) {
return 'date';
return 'Date';
}

/* ! Spec Conformance
Expand All @@ -257,7 +257,7 @@ module.exports = function typeDetect(obj) {
* - Safari 7.1-Latest === "[object Promise]"
*/
if (promiseExists && objPrototype === Promise.prototype) {
return 'promise';
return 'Promise';
}

/* ! Speed optimisation
Expand All @@ -267,7 +267,7 @@ module.exports = function typeDetect(obj) {
* set x 4,545,879 ops/sec ±1.13% (83 runs sampled)
*/
if (setExists && objPrototype === Set.prototype) {
return 'set';
return 'Set';
}

/* ! Speed optimisation
Expand All @@ -277,7 +277,7 @@ module.exports = function typeDetect(obj) {
* map x 4,183,945 ops/sec ±6.59% (82 runs sampled)
*/
if (mapExists && objPrototype === Map.prototype) {
return 'map';
return 'Map';
}

/* ! Speed optimisation
Expand All @@ -287,7 +287,7 @@ module.exports = function typeDetect(obj) {
* weakset x 4,237,510 ops/sec ±2.01% (77 runs sampled)
*/
if (weakSetExists && objPrototype === WeakSet.prototype) {
return 'weakset';
return 'WeakSet';
}

/* ! Speed optimisation
Expand All @@ -297,7 +297,7 @@ module.exports = function typeDetect(obj) {
* weakmap x 3,881,384 ops/sec ±1.45% (82 runs sampled)
*/
if (weakMapExists && objPrototype === WeakMap.prototype) {
return 'weakmap';
return 'WeakMap';
}

/* ! Spec Conformance
Expand All @@ -307,7 +307,7 @@ module.exports = function typeDetect(obj) {
* - Edge <=13 === "[object Object]"
*/
if (dataViewExists && objPrototype === DataView.prototype) {
return 'dataview';
return 'DataView';
}

/* ! Spec Conformance
Expand All @@ -317,7 +317,7 @@ module.exports = function typeDetect(obj) {
* - Edge <=13 === "[object Object]"
*/
if (mapExists && objPrototype === mapIteratorPrototype) {
return 'map iterator';
return 'Map Iterator';
}

/* ! Spec Conformance
Expand All @@ -327,7 +327,7 @@ module.exports = function typeDetect(obj) {
* - Edge <=13 === "[object Object]"
*/
if (setExists && objPrototype === setIteratorPrototype) {
return 'set iterator';
return 'Set Iterator';
}

/* ! Spec Conformance
Expand All @@ -337,7 +337,7 @@ module.exports = function typeDetect(obj) {
* - Edge <=13 === "[object Object]"
*/
if (arrayIteratorExists && objPrototype === arrayIteratorPrototype) {
return 'array iterator';
return 'Array Iterator';
}

/* ! Spec Conformance
Expand All @@ -347,7 +347,7 @@ module.exports = function typeDetect(obj) {
* - Edge <=13 === "[object Object]"
*/
if (stringIteratorExists && objPrototype === stringIteratorPrototype) {
return 'string iterator';
return 'String Iterator';
}

/* ! Speed optimisation
Expand All @@ -357,16 +357,15 @@ module.exports = function typeDetect(obj) {
* object from null x 5,838,000 ops/sec ±0.99% (84 runs sampled)
*/
if (objPrototype === null) {
return 'object';
return 'Object';
}
}

return Object
.prototype
.toString
.call(obj)
.slice(toStringLeftSliceLength, toStringRightSliceLength)
.toLowerCase();
.slice(toStringLeftSliceLength, toStringRightSliceLength);
};

module.exports.typeDetect = module.exports;
Loading

0 comments on commit 638c09a

Please sign in to comment.