main.js (2416B)
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 INT8 = require( '@stdlib/constants/int8/num-bytes' ); 24 var UINT8 = require( '@stdlib/constants/uint8/num-bytes' ); 25 var INT16 = require( '@stdlib/constants/int16/num-bytes' ); 26 var UINT16 = require( '@stdlib/constants/uint16/num-bytes' ); 27 var INT32 = require( '@stdlib/constants/int32/num-bytes' ); 28 var UINT32 = require( '@stdlib/constants/uint32/num-bytes' ); 29 var FLOAT16 = require( '@stdlib/constants/float16/num-bytes' ); 30 var FLOAT32 = require( '@stdlib/constants/float32/num-bytes' ); 31 var FLOAT64 = require( '@stdlib/constants/float64/num-bytes' ); 32 var COMPLEX64 = require( '@stdlib/constants/complex64/num-bytes' ); 33 var COMPLEX128 = require( '@stdlib/constants/complex128/num-bytes' ); 34 35 36 // MAIN // 37 38 /** 39 * Returns the size (in bytes) of the canonical binary representation of a specified numeric type. 40 * 41 * @param {string} dtype - numeric type 42 * @throws {TypeError} must provide a recognized numeric type 43 * @returns {integer} size in bytes 44 * 45 * @example 46 * var s = sizeOf( 'int8' ); 47 * // returns 1 48 * 49 * @example 50 * var s = sizeOf( 'uint8' ); 51 * // returns 1 52 * 53 * @example 54 * var s = sizeOf( 'int16' ); 55 * // returns 2 56 */ 57 function sizeOf( dtype ) { 58 switch ( dtype ) { 59 case 'float64': 60 return FLOAT64; 61 case 'int32': 62 return INT32; 63 case 'uint32': 64 return UINT32; 65 case 'float32': 66 return FLOAT32; 67 case 'int8': 68 return INT8; 69 case 'uint8': 70 // Fall-through... 71 case 'uint8c': // eslint-disable-line no-fallthrough 72 return UINT8; 73 case 'int16': 74 return INT16; 75 case 'uint16': 76 return UINT16; 77 case 'float16': 78 return FLOAT16; 79 case 'complex128': 80 return COMPLEX128; 81 case 'complex64': 82 return COMPLEX64; 83 default: 84 throw new TypeError( 'invalid argument. Must provide a recognized type. Value: `'+dtype+'`.' ); 85 } 86 } 87 88 89 // EXPORTS // 90 91 module.exports = sizeOf;