Skip to content

Commit

Permalink
feat: value/is
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 25, 2019
1 parent f160fb8 commit fdf4763
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
# type

## Runtime validation and processing of JavaScript types

### Installation

```sh
npm install type
```

#### Value

_Value_ is assumed to be any JavaScript value that's neither `null` nor `undefined` (the only primitives not accompanied with object representation. Hence any property access on them (as e.g. `null.foo`) results with an exception)

`undefined` usually means that given variable or property was never defined, while `null` reflects declared intention of having given property or variable empty.

##### `value/is`

```javascript
const isValue = require("type/value/is");

isValue({}); // true
isValue(null); // false
```
29 changes: 29 additions & 0 deletions test/value/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
"use strict";

var assert = require("chai").assert
, isValue = require("../../value/is");

describe("value/is", function () {
it("Should return true on object", function () { assert.equal(isValue({}), true); });
it("Should return true on function", function () {
assert.equal(isValue(function () { return true; }), true);
});
it("Should return true on array", function () { assert.equal(isValue([]), true); });
if (typeof Object.create === "function") {
it("Should return true on object with no prototype", function () {
assert.equal(isValue(Object.create(null)), true);
});
}
it("Should return true on string", function () { assert.equal(isValue("foo"), true); });
it("Should return true on empty string", function () { assert.equal(isValue(""), true); });
it("Should return true on number", function () { assert.equal(isValue(123), true); });
it("Should return true on NaN", function () { assert.equal(isValue(NaN), true); });
it("Should return true on boolean", function () { assert.equal(isValue(false), true); });
if (typeof Symbol === "function") {
// eslint-disable-next-line no-undef
it("Should return true on symbol", function () { assert.equal(isValue(Symbol()), true); });
}

it("Should return false on null", function () { assert.equal(isValue(null), false); });
it("Should return false on undefined", function () { assert.equal(isValue(void 0), false); });
});
6 changes: 6 additions & 0 deletions value/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"use strict";

// ES3 safe
var _undefined = void 0;

module.exports = function (value) { return value !== _undefined && value !== null; };

0 comments on commit fdf4763

Please sign in to comment.