Skip to content

Commit

Permalink
feat: object/is
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Mar 25, 2019
1 parent fdf4763 commit d2d7251
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,23 @@
npm install type
```

#### Object

_Object_ is assumed to be any non-primitive JavaScript value

##### `object/is`

```javascript
const isObject = require("type/object/is");

isObject({}); // true
isObject(true); // false
isObject(null); // false
```

#### 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)
_Value_ is assumed to be any JavaScript value that's neither `null` nor `undefined` (_the only primitives which are 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.

Expand Down
9 changes: 9 additions & 0 deletions object/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"use strict";

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

module.exports = function (value) {
if (!isValue(value)) return false;
var type = typeof value;
return type === "object" || type === "function" || type === "undefined"; // document.all
};
31 changes: 31 additions & 0 deletions test/object/is.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"use strict";

var assert = require("chai").assert
, isObject = require("../../object/is");

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

it("Should return false on null", function () { assert.equal(isObject(null), false); });
it("Should return false on undefined", function () { assert.equal(isObject(void 0), false); });
});

0 comments on commit d2d7251

Please sign in to comment.