Skip to content

Commit

Permalink
added instanceof and primitive to check API
Browse files Browse the repository at this point in the history
  • Loading branch information
StickyCube committed Nov 26, 2015
1 parent ce06b21 commit 146f6ba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ Inspired by chaijs, the assertion supports chainable `.is` and `.not` getters to

it.is.typeof('string'); // typeof operator
it.is.kindof('array'); // same as typeof but distinguises null and array
it.is.instanceof(String);

it.equals(123); // ===
it.is.gt(Infinity); // >
Expand All @@ -115,6 +116,7 @@ Inspired by chaijs, the assertion supports chainable `.is` and `.not` getters to
it.is.defined; // !== undefined
it.is.null; // === null
it.is.value; // != null
it.is.primitive; // null|undefined|String|Boolean|Number

// NOTE: negate the assertion using `.not` before the endpoint.
// NOTE: `.is` is merely aesthetic, and can be safely omitted.
Expand Down Expand Up @@ -145,3 +147,4 @@ Please submit any issues or feature requests to the repository's github [issue t

## Version History
- v0.1.0 - Initial release
- v0.2.0 - Added `instanceof` and `.primitive` to check API
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"gulp-rimraf": "~0.2.0"
},
"name": "ok-js",
"version": "0.1.0",
"version": "0.2.0",
"description": "An object utility library",
"main": "index.js",
"directories": {
Expand Down
18 changes: 18 additions & 0 deletions src/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ Assertion.prototype = {
return utils.kindof(this._data) === type;
},

instanceof: function (Klass) {
return this._data instanceof Klass;
},

lt: function (v) {
return this._data < v;
},
Expand Down Expand Up @@ -119,6 +123,20 @@ Object.defineProperty(Assertion.prototype, 'value', {
}
});

Object.defineProperty(Assertion.prototype, 'primitive', {
get: function () {
var result;

if (!utils.isValue(this._data)) {
return !this._negate;
}

result = this.typeof('string') || this.typeof('number') || this.typeof('boolean');

return this._negate ? !result : result;
}
});

module.exports = function (context, property) {
var value;

Expand Down

0 comments on commit 146f6ba

Please sign in to comment.