Skip to content

Commit

Permalink
@3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
davalapar committed Mar 24, 2019
1 parent 0b3bd4b commit d543507
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
24 changes: 20 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class User {
this.name = name;
}
}
vartype(User); // 'function', transpiler-safe
const alice = new User('alice');
vartype(alice); // 'user'

Expand All @@ -84,6 +85,10 @@ vartypeof(1.4, 'integer', 'float', 'double'); // true
vartypeof(1.5, 'integer', 'float', 'double'); // true
```

## Usage notes

- If using minifier like Terser, use `--keep-classnames`

## How does it work?

Short and sweet, since we're targeting modern environments.
Expand Down Expand Up @@ -121,14 +126,25 @@ const vartype = (value) => {
return 'symbol';
case 'bigint':
return 'bigint';
case 'object':
case 'object': {
if (value === null) return 'null';
if (typeof value.constructor === 'function') {
if (typeof value.constructor.name === 'string') {
return value.constructor.name.toLowerCase();

const prototype = Object.getPrototypeOf(value);

// Because: Object.getPrototypeOf(Object.create(null)); === null
// https://github.com/lodash/lodash/blob/master/isPlainObject.js
if (prototype === null) {
return 'object';
}

if (typeof prototype.constructor === 'function') {
if (typeof prototype.constructor.name === 'string') {
return prototype.constructor.name.toLowerCase();
}
}

return 'unknown';
}
default:
return 'unknown';
}
Expand Down
18 changes: 14 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ const vartype = (value) => {
return 'symbol';
case 'bigint':
return 'bigint';
case 'object':
case 'object': {
if (value === null) return 'null';
if (typeof value.constructor === 'function') {
if (typeof value.constructor.name === 'string') {
return value.constructor.name.toLowerCase();

const prototype = Object.getPrototypeOf(value);

// Because: Object.getPrototypeOf(Object.create(null)); === null
if (prototype === null) {
return 'object';
}

if (typeof prototype.constructor === 'function') {
if (typeof prototype.constructor.name === 'string') {
return prototype.constructor.name.toLowerCase();
}
}

return 'unknown';
}
default:
return 'unknown';
}
Expand Down
11 changes: 10 additions & 1 deletion index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,21 @@ test('uint8array', () => {
expect(vartype(new Uint8Array())).toBe('uint8array');
});

test('user (Class-based)', () => {
test('user (Class-itself)', () => {
class User {}
expect(vartype(User)).toBe('function');
});

test('user (Class-instance)', () => {
class User {}
const alice = new User();
expect(vartype(alice)).toBe('user');
});

test('plain object with constructor property', () => {
expect(vartype({ constructor: null })).toBe('object');
});

test('vartypeof', () => {
expect(vartypeof(1.4, 'string')).toBe(false);
expect(vartypeof(1.4, 'integer', 'float', 'double')).toBe(true);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@davalapar/vartype",
"version": "2.0.0",
"version": "3.0.0",
"description": "Classify JS types with precision.",
"main": "index.js",
"author": "davalapar <[email protected]>",
Expand Down

0 comments on commit d543507

Please sign in to comment.