|
| 1 | +// import CoreObject from '@ember/object/core'; |
| 2 | + |
| 3 | +export type TypeName = |
| 4 | + | 'undefined' |
| 5 | + | 'null' |
| 6 | + | 'string' |
| 7 | + | 'number' |
| 8 | + | 'boolean' |
| 9 | + | 'function' |
| 10 | + | 'array' |
| 11 | + | 'regexp' |
| 12 | + | 'date' |
| 13 | + | 'filelist' |
| 14 | + | 'class' |
| 15 | + | 'instance' |
| 16 | + | 'error' |
| 17 | + | 'object'; |
| 18 | + |
| 19 | +// ........................................ |
| 20 | +// TYPING & ARRAY MESSAGING |
| 21 | +// |
| 22 | +const TYPE_MAP: Record<string, TypeName> = { |
| 23 | + '[object Boolean]': 'boolean', |
| 24 | + '[object Number]': 'number', |
| 25 | + '[object String]': 'string', |
| 26 | + '[object Function]': 'function', |
| 27 | + '[object AsyncFunction]': 'function', |
| 28 | + '[object Array]': 'array', |
| 29 | + '[object Date]': 'date', |
| 30 | + '[object RegExp]': 'regexp', |
| 31 | + '[object Object]': 'object', |
| 32 | + '[object FileList]': 'filelist', |
| 33 | +} as const; |
| 34 | + |
| 35 | +const { toString } = Object.prototype; |
| 36 | + |
| 37 | +/** |
| 38 | + @module @ember/utils |
| 39 | +*/ |
| 40 | +/** |
| 41 | + Returns a consistent type for the passed object. |
| 42 | +
|
| 43 | + Use this instead of the built-in `typeof` to get the type of an item. |
| 44 | + It will return the same result across all browsers and includes a bit |
| 45 | + more detail. Here is what will be returned: |
| 46 | +
|
| 47 | + | Return Value | Meaning | |
| 48 | + |---------------|------------------------------------------------------| |
| 49 | + | 'string' | String primitive or String object. | |
| 50 | + | 'number' | Number primitive or Number object. | |
| 51 | + | 'boolean' | Boolean primitive or Boolean object. | |
| 52 | + | 'null' | Null value | |
| 53 | + | 'undefined' | Undefined value | |
| 54 | + | 'function' | A function | |
| 55 | + | 'array' | An instance of Array | |
| 56 | + | 'regexp' | An instance of RegExp | |
| 57 | + | 'date' | An instance of Date | |
| 58 | + | 'filelist' | An instance of FileList | |
| 59 | + | 'class' | An Ember class (created using EmberObject.extend()) | |
| 60 | + | 'instance' | An Ember object instance | |
| 61 | + | 'error' | An instance of the Error object | |
| 62 | + | 'object' | A JavaScript object not inheriting from EmberObject | |
| 63 | +
|
| 64 | + Examples: |
| 65 | +
|
| 66 | + ```javascript |
| 67 | + import { A } from '@ember/array'; |
| 68 | + import { typeOf } from '@ember/utils'; |
| 69 | + import EmberObject from '@ember/object'; |
| 70 | +
|
| 71 | + typeOf(); // 'undefined' |
| 72 | + typeOf(null); // 'null' |
| 73 | + typeOf(undefined); // 'undefined' |
| 74 | + typeOf('michael'); // 'string' |
| 75 | + typeOf(new String('michael')); // 'string' |
| 76 | + typeOf(101); // 'number' |
| 77 | + typeOf(new Number(101)); // 'number' |
| 78 | + typeOf(true); // 'boolean' |
| 79 | + typeOf(new Boolean(true)); // 'boolean' |
| 80 | + typeOf(A); // 'function' |
| 81 | + typeOf(A()); // 'array' |
| 82 | + typeOf([1, 2, 90]); // 'array' |
| 83 | + typeOf(/abc/); // 'regexp' |
| 84 | + typeOf(new Date()); // 'date' |
| 85 | + typeOf(event.target.files); // 'filelist' |
| 86 | + typeOf(EmberObject.extend()); // 'class' |
| 87 | + typeOf(EmberObject.create()); // 'instance' |
| 88 | + typeOf(new Error('teamocil')); // 'error' |
| 89 | +
|
| 90 | + // 'normal' JavaScript object |
| 91 | + typeOf({ a: 'b' }); // 'object' |
| 92 | + ``` |
| 93 | +
|
| 94 | + @method typeOf |
| 95 | + @for @ember/-internals/utils |
| 96 | + @param item the item to check |
| 97 | + @return {String} the type |
| 98 | + @public |
| 99 | + @static |
| 100 | +*/ |
| 101 | +export default function typeOf(item: unknown): TypeName { |
| 102 | + if (item === null) { |
| 103 | + return 'null'; |
| 104 | + } |
| 105 | + if (item === undefined) { |
| 106 | + return 'undefined'; |
| 107 | + } |
| 108 | + let ret = TYPE_MAP[toString.call(item)] || 'object'; |
| 109 | + |
| 110 | + if (ret === 'function') { |
| 111 | + // if (CoreObject.detect(item)) { |
| 112 | + // ret = 'class'; |
| 113 | + // } |
| 114 | + } else if (ret === 'object') { |
| 115 | + if (item instanceof Error) { |
| 116 | + ret = 'error'; |
| 117 | + // } else if (item instanceof CoreObject) { |
| 118 | + // ret = 'instance'; |
| 119 | + } else if (item instanceof Date) { |
| 120 | + ret = 'date'; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + return ret; |
| 125 | +} |
0 commit comments