which-typed-array
Advanced tools
Comparing version 1.1.10 to 1.1.11
@@ -8,2 +8,9 @@ # Changelog | ||
## [v1.1.11](https://github.com/inspect-js/which-typed-array/compare/v1.1.10...v1.1.11) - 2023-07-17 | ||
### Commits | ||
- [Fix] `node < v0.6` lacks proper Object toString behavior [`b8fd654`](https://github.com/inspect-js/which-typed-array/commit/b8fd65479c0bd18385378cfae79750ebf7cb6ee7) | ||
- [Dev Deps] update `tape` [`e1734c9`](https://github.com/inspect-js/which-typed-array/commit/e1734c99d79880ab11efa55220498a7a1e887834) | ||
## [v1.1.10](https://github.com/inspect-js/which-typed-array/compare/v1.1.9...v1.1.10) - 2023-07-10 | ||
@@ -10,0 +17,0 @@ |
77
index.js
@@ -16,42 +16,75 @@ 'use strict'; | ||
var $slice = callBound('String.prototype.slice'); | ||
var toStrTags = { __proto__: null }; | ||
var getPrototypeOf = Object.getPrototypeOf; // require('getprototypeof'); | ||
var $indexOf = callBound('Array.prototype.indexOf', true) || function indexOf(array, value) { | ||
for (var i = 0; i < array.length; i += 1) { | ||
if (array[i] === value) { | ||
return i; | ||
} | ||
} | ||
return -1; | ||
}; | ||
var cache = { __proto__: null }; | ||
if (hasToStringTag && gOPD && getPrototypeOf) { | ||
forEach(typedArrays, function (typedArray) { | ||
if (typeof g[typedArray] === 'function') { | ||
var arr = new g[typedArray](); | ||
if (Symbol.toStringTag in arr) { | ||
var proto = getPrototypeOf(arr); | ||
var descriptor = gOPD(proto, Symbol.toStringTag); | ||
if (!descriptor) { | ||
var superProto = getPrototypeOf(proto); | ||
descriptor = gOPD(superProto, Symbol.toStringTag); | ||
} | ||
toStrTags[typedArray] = callBind(descriptor.get); | ||
var arr = new g[typedArray](); | ||
if (Symbol.toStringTag in arr) { | ||
var proto = getPrototypeOf(arr); | ||
var descriptor = gOPD(proto, Symbol.toStringTag); | ||
if (!descriptor) { | ||
var superProto = getPrototypeOf(proto); | ||
descriptor = gOPD(superProto, Symbol.toStringTag); | ||
} | ||
cache['$' + typedArray] = callBind(descriptor.get); | ||
} | ||
}); | ||
} else { | ||
forEach(typedArrays, function (typedArray) { | ||
var arr = new g[typedArray](); | ||
cache['$' + typedArray] = callBind(arr.slice); | ||
}); | ||
} | ||
var tryTypedArrays = function tryAllTypedArrays(value) { | ||
var foundName = false; | ||
forEach(toStrTags, function (getter, typedArray) { | ||
if (!foundName) { | ||
var found = false; | ||
forEach(cache, function (getter, typedArray) { | ||
if (!found) { | ||
try { | ||
var name = getter(value); | ||
if (name === typedArray) { | ||
foundName = name; | ||
if ('$' + getter(value) === typedArray) { | ||
found = $slice(typedArray, 1); | ||
} | ||
} catch (e) {} | ||
} catch (e) { /**/ } | ||
} | ||
}); | ||
return foundName; | ||
return found; | ||
}; | ||
var isTypedArray = require('is-typed-array'); | ||
var trySlices = function tryAllSlices(value) { | ||
var found = false; | ||
forEach(cache, function (getter, name) { | ||
if (!found) { | ||
try { | ||
getter(value); | ||
found = $slice(name, 1); | ||
} catch (e) { /**/ } | ||
} | ||
}); | ||
return found; | ||
}; | ||
module.exports = function whichTypedArray(value) { | ||
if (!isTypedArray(value)) { return false; } | ||
if (!hasToStringTag || !(Symbol.toStringTag in value)) { return $slice($toString(value), 8, -1); } | ||
if (!value || typeof value !== 'object') { return false; } | ||
if (!hasToStringTag) { | ||
var tag = $slice($toString(value), 8, -1); | ||
if ($indexOf(typedArrays, tag) > -1) { | ||
return tag; | ||
} | ||
if (tag !== 'Object') { | ||
return false; | ||
} | ||
// node < 0.6 hits here on real Typed Arrays | ||
return trySlices(value); | ||
} | ||
if (!gOPD) { return null; } // unknown engine | ||
return tryTypedArrays(value); | ||
}; |
{ | ||
"name": "which-typed-array", | ||
"version": "1.1.10", | ||
"version": "1.1.11", | ||
"author": { | ||
@@ -65,4 +65,3 @@ "name": "Jordan Harband", | ||
"gopd": "^1.0.1", | ||
"has-tostringtag": "^1.0.0", | ||
"is-typed-array": "^1.1.10" | ||
"has-tostringtag": "^1.0.0" | ||
}, | ||
@@ -81,3 +80,3 @@ "devDependencies": { | ||
"safe-publish-latest": "^2.0.0", | ||
"tape": "^5.6.4" | ||
"tape": "^5.6.5" | ||
}, | ||
@@ -84,0 +83,0 @@ "testling": { |
@@ -93,3 +93,3 @@ 'use strict'; | ||
var arr = new TypedArray(10); | ||
t.equal(typedArray, whichTypedArray(arr), 'new ' + typedArray + '(10) is typed array of type ' + typedArray); | ||
t.equal(whichTypedArray(arr), typedArray, 'new ' + typedArray + '(10) is typed array of type ' + typedArray); | ||
} else { | ||
@@ -96,0 +96,0 @@ t.comment('# SKIP ' + typedArray + ' is not supported'); |
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
33757
5
168
- Removedis-typed-array@^1.1.10
- Removedis-typed-array@1.1.13(transitive)
- Removedwhich-typed-array@1.1.15(transitive)