File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * Better way to handle type checking
3+ * null, {}, array and date are objects, which confuses
4+ */
5+ export default function typeOf ( input ) {
6+ const rawObject = Object . prototype . toString . call ( input ) . toLowerCase ( ) ;
7+ const typeOfRegex = / \[ o b j e c t ( .* ) ] / g;
8+ const type = typeOfRegex . exec ( rawObject ) [ 1 ] ;
9+ return type ;
10+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * All tests that tests any utility.
3+ * Prevent any breaking of functionality
4+ */
5+ import assert from 'assert' ;
6+ import typeOf from '../src/lib/util/typeOf' ;
7+
8+ describe ( 'Util' , ( ) => {
9+ it ( 'should validate different typeOf' , ( ) => {
10+ assert . strictEqual ( typeOf ( [ ] ) , 'array' ) ;
11+ assert . strictEqual ( typeOf ( null ) , 'null' ) ;
12+ assert . strictEqual ( typeOf ( { } ) , 'object' ) ;
13+ assert . strictEqual ( typeOf ( new Date ( ) ) , 'date' ) ;
14+ assert . strictEqual ( typeOf ( 'ezkemboi' ) , 'string' ) ;
15+ assert . strictEqual ( typeOf ( String ( 'kemboi' ) ) , 'string' ) ;
16+ assert . strictEqual ( typeOf ( undefined ) , 'undefined' ) ;
17+ assert . strictEqual ( typeOf ( 2021 ) , 'number' ) ;
18+ assert . notStrictEqual ( typeOf ( [ ] ) , 'object' ) ;
19+ } ) ;
20+ } ) ;
You can’t perform that action at this time.
0 commit comments