Skip to content

Commit 1fa0959

Browse files
authored
chore: add typeof utility (#1648)
1 parent cf403d0 commit 1fa0959

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

src/lib/util/typeOf.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
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 = /\[object (.*)]/g;
8+
const type = typeOfRegex.exec(rawObject)[1];
9+
return type;
10+
}

test/util.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
});

0 commit comments

Comments
 (0)