jonschlinkert / kind-of

Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
https://github.com/jonschlnkert
MIT License
347 stars 38 forks source link

Uint8ClampedArray returns `object` instead of `Uint8ClampedArray ` #4

Closed miguelmota closed 8 years ago

miguelmota commented 8 years ago
var kind = require('kind-of');
console.log(kind(new Uint8ClampedArray(3))); // 'object'

Shouldn't it be Uint8ClampedArray?

tunnckoCore commented 8 years ago

Actually, it seems that this is correct. Because value of new Uint8ClampedArray(3) seems like object to me. I'm not sure what is the correct behaving here.

We can add few checks

  if (val instanceof Array) {
    return 'array';
  }

and something like that

if (type.indexOf('Array') !== -1) {
  return 'array';
}

which will handle this "issue".

miguelmota commented 8 years ago

My suggestion for typed arrays

var obj = new Uint8ClampedArray(1);

var typedArrayTypes = ['Int8Array', 'Uint8Array', 'Uint8ClampedArray', 'Int16Array', 'Uint16Array', 'Int32Array', 'Uint32Array', 'Float32Array', 'Float64Array'];
var typeString = Object.prototype.toString.call(obj).match(/\s+(\w+)/i)[1];
var typeIndex = typedArrayTypes.indexOf(typeString);

if (typeIndex > -1) {
 console.log(typeString.toLowerCase()); // uint8clampedarray
}
tunnckoCore commented 8 years ago

I think we should return array, imo.

miguelmota commented 8 years ago

That doesn't seem like the right thing to do considering that typed arrays are there own types, like Map and Set

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures

(new Uint8ClampedArray(1)) instanceof Array // false

PR https://github.com/jonschlinkert/kind-of/pull/5

jonschlinkert commented 8 years ago

I believe this is resolved