Skip to content

Commit

Permalink
Add .not
Browse files Browse the repository at this point in the history
  • Loading branch information
sttk committed Oct 26, 2017
1 parent 2835620 commit 6edc56f
Show file tree
Hide file tree
Showing 7 changed files with 419 additions and 3 deletions.
23 changes: 22 additions & 1 deletion README.md
Expand Up @@ -14,7 +14,7 @@ To install from npm:
$ npm install --save @fav/type.is-plain-object
```

***NOTE:*** *npm < 2.7.0 does not support scoped package, but even old version Node.js supports it. So when you use such older npm, you should download this package from [github.com][repo-url], and move it in `node_modules/@fav/type.is-plain-object/` direcotry manually.*
***NOTE:*** *npm < 2.7.0 does not support scoped package, but even old version Node.js supports it. So when you use such older npm, you should download this package from [github.com][repo-url], and move it in `node_modules/@fav/type.is-plain-object/` directory manually.*


## Usage
Expand All @@ -24,6 +24,7 @@ For Node.js, when installing `@fav/type.is-plain-object` from npm:
```js
var isPlainObject = require('@fav/type.is-plain-object');
isPlainObject({ a: 1 }); // => true
isPlainObject.not({ a: 1 }); // => false
```

For Web browsers:
Expand All @@ -33,6 +34,7 @@ For Web browsers:
<script>
var isPlainObject = fav.type.isPlainObject;
isPlainObject({ a: 1 }); // => true
isPlainObject.not({ a: 1 }); // => false
</script>
```

Expand All @@ -56,6 +58,25 @@ True, if *value* is a plain object.
**Type:** boolean


### <u>isPlainObject.not(value) : boolean</u>

Checks if *value* is not a plain object.

This function always returns a negative boolean of `isPlainObject(value)`.

#### Parameter:

| Parameter | Type | Description |
|-----------|:------:|--------------------------|
| value | *any* | The value to be checked. |

#### Return:

True, if *value* is not a plain object.

**Type:** boolean


## Checked

### Node.js (4〜8)
Expand Down
9 changes: 9 additions & 0 deletions index.js
Expand Up @@ -22,4 +22,13 @@ function isPlainObject(value) {
}
}

function isNotPlainObject(value) {
return !isPlainObject(value);
}

Object.defineProperty(isPlainObject, 'not', {
enumerable: true,
value: isNotPlainObject,
});

module.exports = isPlainObject;
187 changes: 187 additions & 0 deletions test/is-plain-object-not.test.js
@@ -0,0 +1,187 @@
'use strict';

var chai = require('chai');
var expect = chai.expect;
var semver = require('semver');
var fav = {}; fav.type = {}; fav.type.isPlainObject = require('..');

var isNotPlainObject = fav.type.isPlainObject.not;

describe('fav.type.isPlainObject.not', function() {

it('Should return false when value is a plain object', function() {
expect(isNotPlainObject({})).to.equal(false);
expect(isNotPlainObject({ a: 1 })).to.equal(false);
expect(isNotPlainObject(new Object())).to.equal(false);
expect(isNotPlainObject(Object.create(Object.prototype))).to.equal(false);
expect(isNotPlainObject(Object.create(null))).to.equal(false);
});

it('Should return true when value is not a plain object', function() {
expect(isNotPlainObject(undefined)).to.equal(true);
expect(isNotPlainObject(null)).to.equal(true);
expect(isNotPlainObject(true)).to.equal(true);
expect(isNotPlainObject(false)).to.equal(true);
expect(isNotPlainObject(0)).to.equal(true);
expect(isNotPlainObject(123)).to.equal(true);
expect(isNotPlainObject(NaN)).to.equal(true);
expect(isNotPlainObject(Infinity)).to.equal(true);
expect(isNotPlainObject(new Number(123))).to.equal(true);
expect(isNotPlainObject([])).to.equal(true);
expect(isNotPlainObject([1, 2])).to.equal(true);
expect(isNotPlainObject(new Array(1, 2))).to.equal(true);
expect(isNotPlainObject(/a/g)).to.equal(true);
expect(isNotPlainObject(new RegExp('a', 'g'))).to.equal(true);
expect(isNotPlainObject(function() {})).to.equal(true);
expect(isNotPlainObject(new Date())).to.equal(true);
expect(isNotPlainObject(new Error())).to.equal(true);
expect(isNotPlainObject(new Foo())).to.equal(true);
expect(isNotPlainObject(new FooEx())).to.equal(true);
expect(isNotPlainObject(new SubclassOfPlainObject())).to.equal(true);
expect(isNotPlainObject(Object.create({}))).to.equal(true);
});

it('Should return true when value is a class instance', function() {
if (!isSupportClass()) {
this.skip();
return;
}

var code = codeForClass();
eval(code);
});

it('Should return true when value is a symbol', function() {
if (typeof Symbol !== 'function') {
this.skip();
return;
}

expect(isNotPlainObject(Symbol('foo'))).to.equal(true);
});

});


function Foo() {
this.baz = 'Baz';
return this;
}

function FooEx() {
this.baz = 'Baaaaz';
}
FooEx.prototype = Foo.prototype;

Foo.prototype.bar = function() {
return this.baz;
};

/*
var foo = new Foo();
console.log(foo);
console.log(foo.baz);
console.log(foo.bar());
console.log(typeof foo);
console.log(Object.prototype.toString.call(foo));
console.log(foo.constructor);
console.log(foo.constructor.name);
console.log(foo.prototype);
console.log(Object.getPrototypeOf(foo));
console.log(foo.constructor === Object);
var fooex = new FooEx();
console.log(fooex);
console.log(fooex.baz);
console.log(fooex.bar());
console.log(fooex.constructor === Object);
*/

function SubclassOfPlainObject() {}
SubclassOfPlainObject.prototype = {};

function codeForClass() {
return "\
class Qux {\
constructor(n) {\
this.count = n || 1;\
}\
\
get text() {\
return 'Q' + 'u'.repeat(this.count) + 'x';\
}\
}\
\
class Quux extends Qux {\
constructor(n) {\
super(n);\
}\
\
get text() {\
return 'Q' + 'u'.repeat(this.count * 2) + 'x';\
}\
}\
\
/*\
const qux = new Qux(3);\
console.log(qux);\
console.log(qux.count);\
console.log(qux.text);\
console.log(qux.constructor === Object);\
*/\
\
/*\
const quux = new Quux(3);\
console.log(quux);\
console.log(quux.count);\
console.log(quux.text);\
console.log(quux.constructor === Object);\
*/\
\
expect(isNotPlainObject(new Qux())).to.equal(true);\
expect(isNotPlainObject(new Quux())).to.equal(true);\
";
}

function isSupportClass() {
if (isNode()) {
return semver.gte(process.version, '2.0.0');
}

if (typeof xslet !== 'undefined' && typeof xslet.platform !== 'undefined') {
var ua = xslet.platform.ua;

// Check by latest version
if (ua.CHROME) {
return true;
}
if (ua.FIREFOX) {
return true;
}
if (ua.MSIE) {
return false;
}
if (ua.EDGE) {
return true;
}
if (ua.SAFARI) {
return true;
}
if (ua.VIVALDI) {
return true;
}
if (ua.PHANTOMJS) {
return false;
}
return false;
}
}

function isNode() {
if (typeof process === 'object') {
if (typeof process.kill === 'function') { // exist from v0.0.6
return true;
}
}
return false;
}

0 comments on commit 6edc56f

Please sign in to comment.