Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add typeof utility #1648

Merged
merged 1 commit into from
Apr 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, could you add one more test for String('kemboi') ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will add

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the test @profnandaa

assert.strictEqual(typeOf(String('kemboi')), 'string');
assert.strictEqual(typeOf(undefined), 'undefined');
assert.strictEqual(typeOf(2021), 'number');
assert.notStrictEqual(typeOf([]), 'object');
});
});