├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test.js /LICENSE.md: -------------------------------------------------------------------------------- 1 | This software is released under the MIT license: 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of 4 | this software and associated documentation files (the "Software"), to deal in 5 | the Software without restriction, including without limitation the rights to 6 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 7 | the Software, and to permit persons to whom the Software is furnished to do so, 8 | subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in all 11 | copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 15 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 16 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 17 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 18 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 19 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # is-typedarray [![locked](http://badges.github.io/stability-badges/dist/locked.svg)](http://github.com/badges/stability-badges) 2 | 3 | Detect whether or not an object is a 4 | [Typed Array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays). 5 | 6 | ## Usage 7 | 8 | [![NPM](https://nodei.co/npm/is-typedarray.png)](https://nodei.co/npm/is-typedarray/) 9 | 10 | ### isTypedArray(array) 11 | 12 | Returns `true` when array is a Typed Array, and `false` when it is not. 13 | 14 | ## License 15 | 16 | MIT. See [LICENSE.md](http://github.com/hughsk/is-typedarray/blob/master/LICENSE.md) for details. 17 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = isTypedArray 2 | isTypedArray.strict = isStrictTypedArray 3 | isTypedArray.loose = isLooseTypedArray 4 | 5 | var toString = Object.prototype.toString 6 | var names = { 7 | '[object Int8Array]': true 8 | , '[object Int16Array]': true 9 | , '[object Int32Array]': true 10 | , '[object Uint8Array]': true 11 | , '[object Uint8ClampedArray]': true 12 | , '[object Uint16Array]': true 13 | , '[object Uint32Array]': true 14 | , '[object Float32Array]': true 15 | , '[object Float64Array]': true 16 | } 17 | 18 | function isTypedArray(arr) { 19 | return ( 20 | isStrictTypedArray(arr) 21 | || isLooseTypedArray(arr) 22 | ) 23 | } 24 | 25 | function isStrictTypedArray(arr) { 26 | return ( 27 | arr instanceof Int8Array 28 | || arr instanceof Int16Array 29 | || arr instanceof Int32Array 30 | || arr instanceof Uint8Array 31 | || arr instanceof Uint8ClampedArray 32 | || arr instanceof Uint16Array 33 | || arr instanceof Uint32Array 34 | || arr instanceof Float32Array 35 | || arr instanceof Float64Array 36 | ) 37 | } 38 | 39 | function isLooseTypedArray(arr) { 40 | return names[toString.call(arr)] 41 | } 42 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "is-typedarray", 3 | "version": "1.0.0", 4 | "description": "Detect whether or not an object is a Typed Array", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "node test" 8 | }, 9 | "author": "Hugh Kennedy (http://hughsk.io/)", 10 | "license": "MIT", 11 | "dependencies": {}, 12 | "devDependencies": { 13 | "tape": "^2.13.1" 14 | }, 15 | "repository": { 16 | "type": "git", 17 | "url": "git://github.com/hughsk/is-typedarray.git" 18 | }, 19 | "keywords": [ 20 | "typed", 21 | "array", 22 | "detect", 23 | "is", 24 | "util" 25 | ], 26 | "bugs": { 27 | "url": "https://github.com/hughsk/is-typedarray/issues" 28 | }, 29 | "homepage": "https://github.com/hughsk/is-typedarray" 30 | } 31 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape') 2 | var ista = require('./') 3 | 4 | test('strict', function(t) { 5 | t.ok(ista.strict(new Int8Array), 'Int8Array') 6 | t.ok(ista.strict(new Int16Array), 'Int16Array') 7 | t.ok(ista.strict(new Int32Array), 'Int32Array') 8 | t.ok(ista.strict(new Uint8Array), 'Uint8Array') 9 | t.ok(ista.strict(new Uint16Array), 'Uint16Array') 10 | t.ok(ista.strict(new Uint32Array), 'Uint32Array') 11 | t.ok(ista.strict(new Float32Array), 'Float32Array') 12 | t.ok(ista.strict(new Float64Array), 'Float64Array') 13 | 14 | t.ok(!ista.strict(new Array), 'Array') 15 | t.ok(!ista.strict([]), '[]') 16 | 17 | t.end() 18 | }) 19 | 20 | test('loose', function(t) { 21 | t.ok(ista.loose(new Int8Array), 'Int8Array') 22 | t.ok(ista.loose(new Int16Array), 'Int16Array') 23 | t.ok(ista.loose(new Int32Array), 'Int32Array') 24 | t.ok(ista.loose(new Uint8Array), 'Uint8Array') 25 | t.ok(ista.loose(new Uint16Array), 'Uint16Array') 26 | t.ok(ista.loose(new Uint32Array), 'Uint32Array') 27 | t.ok(ista.loose(new Float32Array), 'Float32Array') 28 | t.ok(ista.loose(new Float64Array), 'Float64Array') 29 | 30 | t.ok(!ista.loose(new Array), 'Array') 31 | t.ok(!ista.loose([]), '[]') 32 | 33 | t.end() 34 | }) 35 | --------------------------------------------------------------------------------