|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2018 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +'use strict'; |
| 20 | + |
| 21 | +// MODULES // |
| 22 | + |
| 23 | +var isBuffer = require( '@stdlib/assert/is-buffer' ); |
| 24 | +var isArray = require( '@stdlib/assert/is-array' ); |
| 25 | +var Float64Array = require( '@stdlib/array/float64' ); |
| 26 | +var Float32Array = require( '@stdlib/array/float32' ); |
| 27 | +var Uint32Array = require( '@stdlib/array/uint32' ); |
| 28 | +var Int32Array = require( '@stdlib/array/int32' ); |
| 29 | +var Uint16Array = require( '@stdlib/array/uint16' ); |
| 30 | +var Int16Array = require( '@stdlib/array/int16' ); |
| 31 | +var Uint8Array = require( '@stdlib/array/uint8' ); |
| 32 | +var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); |
| 33 | +var Int8Array = require( '@stdlib/array/int8' ); |
| 34 | +var getType = require( '@stdlib/ndarray/base/buffer-dtype' ); |
| 35 | + |
| 36 | + |
| 37 | +// VARIABLES // |
| 38 | + |
| 39 | +var TYPES = [ |
| 40 | + [ Float64Array, 'float64' ], |
| 41 | + [ Float32Array, 'float32' ], |
| 42 | + [ Uint32Array, 'uint32' ], |
| 43 | + [ Int32Array, 'int32' ], |
| 44 | + [ Uint16Array, 'uint16' ], |
| 45 | + [ Int16Array, 'int16' ], |
| 46 | + [ Uint8Array, 'uint8' ], |
| 47 | + [ Uint8ClampedArray, 'uint8c' ], |
| 48 | + [ Int8Array, 'int8' ] |
| 49 | +]; |
| 50 | +var NTYPES = TYPES.length; |
| 51 | + |
| 52 | + |
| 53 | +// MAIN // |
| 54 | + |
| 55 | +/** |
| 56 | +* Resolves a data source underlying data type. |
| 57 | +* |
| 58 | +* @private |
| 59 | +* @param {(Array|TypedArray|Buffer)} buffer - data source |
| 60 | +* @returns {string} data type |
| 61 | +*/ |
| 62 | +function dtype( buffer ) { |
| 63 | + var i; |
| 64 | + if ( isArray( buffer ) ) { |
| 65 | + return 'generic'; |
| 66 | + } |
| 67 | + if ( isBuffer( buffer ) ) { |
| 68 | + return 'binary'; |
| 69 | + } |
| 70 | + // By process of elimination, left with typed arrays... |
| 71 | + for ( i = 0; i < NTYPES; i++ ) { |
| 72 | + if ( buffer instanceof TYPES[ i ][ 0 ] ) { |
| 73 | + return TYPES[ i ][ 1 ]; |
| 74 | + } |
| 75 | + } |
| 76 | + // If the above failed, fall back to a more robust (and significantly slower) means for resolving underlying data types: |
| 77 | + return getType( buffer ); |
| 78 | +} |
| 79 | + |
| 80 | + |
| 81 | +// EXPORTS // |
| 82 | + |
| 83 | +module.exports = dtype; |
0 commit comments