Skip to content

Commit

Permalink
chore: add typeof utility (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezkemboi authored Apr 18, 2021
1 parent cf403d0 commit 1fa0959
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/util/typeOf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Better way to handle type checking
* null, {}, array and date are objects, which confuses
*/
export default function typeOf(input) {
const rawObject = Object.prototype.toString.call(input).toLowerCase();
const typeOfRegex = /\[object (.*)]/g;
const type = typeOfRegex.exec(rawObject)[1];
return type;
}
20 changes: 20 additions & 0 deletions test/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* All tests that tests any utility.
* Prevent any breaking of functionality
*/
import assert from 'assert';
import typeOf from '../src/lib/util/typeOf';

describe('Util', () => {
it('should validate different typeOf', () => {
assert.strictEqual(typeOf([]), 'array');
assert.strictEqual(typeOf(null), 'null');
assert.strictEqual(typeOf({}), 'object');
assert.strictEqual(typeOf(new Date()), 'date');
assert.strictEqual(typeOf('ezkemboi'), 'string');
assert.strictEqual(typeOf(String('kemboi')), 'string');
assert.strictEqual(typeOf(undefined), 'undefined');
assert.strictEqual(typeOf(2021), 'number');
assert.notStrictEqual(typeOf([]), 'object');
});
});

0 comments on commit 1fa0959

Please sign in to comment.