README.md (9986B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Constructor Name 22 23 > Determine the name of a value's constructor. 24 25 <section class="usage"> 26 27 ## Usage 28 29 ```javascript 30 var constructorName = require( '@stdlib/utils/constructor-name' ); 31 ``` 32 33 #### constructorName( value ) 34 35 Returns the name of a value's constructor. 36 37 ```javascript 38 var v = constructorName( 'a' ); 39 // returns 'String' 40 41 v = constructorName( 5 ); 42 // returns 'Number' 43 44 function Beep() { 45 return this; 46 } 47 v = constructorName( new Beep() ); 48 // returns 'Beep' 49 ``` 50 51 | description | value | constructor | notes | 52 | --------------------- | ----------------------------------- | --------------------- | ------------ | 53 | string | `'beep'` | `'String'` | | 54 | number | `5` | `'Number'` | | 55 | NaN | `NaN` | `'Number'` | | 56 | infinity | `+infinity`/`-infinity` | `'Number'` | | 57 | boolean | `true`/`false` | `'Boolean'` | | 58 | null | `null` | `'Null'` | | 59 | undefined | `undefined` | `'Undefined'` | | 60 | array | `['beep', 5]` | `'Array'` | | 61 | object | `{'foo': 'bar'}` | `'Object'` | | 62 | function | `function (){}` | `'Function'` | | 63 | symbol | `Symbol()` | `'Symbol'` | ES2015 | 64 | regexp | `/./` | `'RegExp'` | Android 4.1+ | 65 | String | `new String('beep')` | `'String'` | | 66 | Number | `new Number(5)` | `'Number'` | | 67 | Boolean | `new Boolean(false)` | `'Boolean'` | | 68 | Object | `new Object()` | `'Object'` | | 69 | Array | `new Array()` | `'Array'` | | 70 | Int8Array | `new Int8Array()` | `'Int8Array'` | | 71 | Uint8Array | `new Uint8Array()` | `'Uint8Array'` | | 72 | Uint8ClampedArray | `new Uint8ClampedArray()` | `'Uint8ClampedArray'` | | 73 | Int16Array | `new Int16Array()` | `'Int16Array'` | | 74 | Uint16Array | `new Uint16Array()` | `'Uint16Array'` | | 75 | Int32Array | `new Int32Array()` | `'Int32Array'` | | 76 | Uint32Array | `new Uint32Array()` | `'Uint32Array'` | | 77 | Float32Array | `new Float32Array()` | `'Float32Array'` | | 78 | Float64Array | `new Float64Array()` | `'Float64Array'` | | 79 | ArrayBuffer | `new ArrayBuffer()` | `'ArrayBuffer'` | | 80 | Buffer | `new Buffer()` | `'Buffer'` | Node.js | 81 | Date | `new Date()` | `'Date'` | | 82 | RegExp | `new RegExp('.')` | `'RegExp'` | Android 4.1+ | 83 | Function | `new Function('x', 'return x')` | `'Function'` | | 84 | Map | `new Map()` | `'Map'` | ES2015 | 85 | WeakMap | `new WeakMap()` | `'WeakMap'` | ES2015 | 86 | Set | `new Set()` | `'Set'` | ES2015 | 87 | WeakSet | `new WeakSet()` | `'WeakSet'` | ES2015 | 88 | Error | `new Error()` | `'Error'` | | 89 | TypeError | `new TypeError()` | `'TypeError'` | | 90 | SyntaxError | `new SyntaxError()` | `'SyntaxError'` | | 91 | ReferenceError | `new ReferenceError()` | `'ReferenceError'` | | 92 | URIError | `new URIError()` | `'URIError'` | | 93 | RangeError | `new RangeError()` | `'RangeError'` | | 94 | EvalError | `new EvalError()` | `'EvalError'` | | 95 | Math | `Math` | `'Math'` | | 96 | JSON | `JSON` | `'JSON'` | IE8+ | 97 | arguments | `(function(){return arguments;})()` | `'Arguments'` | IE9+ | 98 | custom constructor | `new Beep()` | `'Beep'` | | 99 | anonymous constructor | `new (function(){})()` | `''` | | 100 101 </section> 102 103 <!-- /.usage --> 104 105 <section class="notes"> 106 107 ## Notes 108 109 - If a value's constructor is an anonymous `function`, the implementation returns an empty `string`. 110 111 <!-- eslint-disable no-restricted-syntax, func-style, func-names --> 112 113 ```javascript 114 var Beep = function () { 115 return this; 116 }; 117 118 var v = constructorName( new Beep() ); 119 // returns '' 120 ``` 121 122 </section> 123 124 <!-- /.notes --> 125 126 <section class="examples"> 127 128 ## Examples 129 130 <!-- TODO: update once have Buffer wrapper --> 131 132 <!-- eslint no-undef: "error" --> 133 134 <!-- eslint-disable no-restricted-syntax, no-buffer-constructor, func-style, func-names --> 135 136 ```javascript 137 var Float32Array = require( '@stdlib/array/float32' ); 138 var Float64Array = require( '@stdlib/array/float64' ); 139 var Int8Array = require( '@stdlib/array/int8' ); 140 var Int16Array = require( '@stdlib/array/int16' ); 141 var Int32Array = require( '@stdlib/array/int32' ); 142 var Uint8Array = require( '@stdlib/array/uint8' ); 143 var Uint8ClampedArray = require( '@stdlib/array/uint8c' ); 144 var Uint16Array = require( '@stdlib/array/uint16' ); 145 var Uint32Array = require( '@stdlib/array/uint32' ); 146 var ArrayBuffer = require( '@stdlib/array/buffer' ); 147 var Buffer = require( '@stdlib/buffer/ctor' ); 148 var Symbol = require( '@stdlib/symbol/ctor' ); 149 var constructorName = require( '@stdlib/utils/constructor-name' ); 150 151 function noop() { 152 // Do nothing... 153 } 154 155 var v = constructorName( 'a' ); 156 // returns 'String' 157 158 v = constructorName( 5 ); 159 // returns 'Number' 160 161 v = constructorName( NaN ); 162 // returns 'Number' 163 164 v = constructorName( null ); 165 // returns 'Null' 166 167 v = constructorName( void 0 ); 168 // returns 'Undefined' 169 170 v = constructorName( true ); 171 // returns 'Boolean' 172 173 v = constructorName( false ); 174 // returns 'Boolean' 175 176 v = constructorName( {} ); 177 // returns 'Object' 178 179 v = constructorName( [] ); 180 // returns 'Array' 181 182 v = constructorName( noop ); 183 // returns 'Function' 184 185 v = constructorName( /./ ); 186 // returns 'RegExp' 187 188 v = constructorName( new Date() ); 189 // returns 'Date' 190 191 v = constructorName( new Map() ); 192 // returns 'Map' 193 194 v = constructorName( new WeakMap() ); 195 // returns 'WeakMap' 196 197 v = constructorName( new Set() ); 198 // returns 'Set' 199 200 v = constructorName( new WeakSet() ); 201 // returns 'WeakSet' 202 203 v = constructorName( Symbol( 'beep' ) ); 204 // returns 'Symbol' 205 206 v = constructorName( new Error() ); 207 // returns 'Error' 208 209 v = constructorName( new TypeError() ); 210 // returns 'TypeError' 211 212 v = constructorName( new SyntaxError() ); 213 // returns 'SyntaxError' 214 215 v = constructorName( new URIError() ); 216 // returns 'URIError' 217 218 v = constructorName( new RangeError() ); 219 // returns 'RangeError' 220 221 v = constructorName( new ReferenceError() ); 222 // returns 'ReferenceError' 223 224 v = constructorName( new EvalError() ); 225 // returns 'EvalError' 226 227 v = constructorName( new Int8Array() ); 228 // returns 'Int8Array' 229 230 v = constructorName( new Uint8Array() ); 231 // returns 'Uint8Array' 232 233 v = constructorName( new Uint8ClampedArray() ); 234 // returns 'Uint8ClampedArray' 235 236 v = constructorName( new Int16Array() ); 237 // returns 'Int16Array' 238 239 v = constructorName( new Uint16Array() ); 240 // returns 'Uint16Array' 241 242 v = constructorName( new Int32Array() ); 243 // returns 'Int32Array' 244 245 v = constructorName( new Uint32Array() ); 246 // returns 'Uint32Array' 247 248 v = constructorName( new Float32Array() ); 249 // returns 'Float32Array' 250 251 v = constructorName( new Float64Array() ); 252 // returns 'Float64Array' 253 254 v = constructorName( new ArrayBuffer() ); 255 // returns 'ArrayBuffer' 256 257 v = constructorName( new Buffer( 'beep' ) ); 258 // returns 'Buffer' 259 260 v = constructorName( Math ); 261 // returns 'Math' 262 263 v = constructorName( JSON ); 264 // returns 'JSON' 265 266 function Person1() { 267 return this; 268 } 269 v = constructorName( new Person1() ); 270 // returns 'Person1' 271 272 var Person2 = function () { 273 return this; 274 }; 275 v = constructorName( new Person2() ); 276 // returns '' 277 ``` 278 279 </section> 280 281 <!-- /.examples --> 282 283 <section class="links"> 284 285 </section> 286 287 <!-- /.links -->