diff --git a/README.md b/README.md index 656c64d..e934d42 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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: @@ -33,6 +34,7 @@ For Web browsers: ``` @@ -56,6 +58,25 @@ True, if *value* is a plain object. **Type:** boolean +### isPlainObject.not(value) : boolean + +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) diff --git a/index.js b/index.js index 7d6780f..5ef656f 100644 --- a/index.js +++ b/index.js @@ -22,4 +22,13 @@ function isPlainObject(value) { } } +function isNotPlainObject(value) { + return !isPlainObject(value); +} + +Object.defineProperty(isPlainObject, 'not', { + enumerable: true, + value: isNotPlainObject, +}); + module.exports = isPlainObject; diff --git a/test/is-plain-object-not.test.js b/test/is-plain-object-not.test.js new file mode 100644 index 0000000..4724086 --- /dev/null +++ b/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; +} diff --git a/test/web/browser-test.js b/test/web/browser-test.js index 6fdfda0..50870f0 100644 --- a/test/web/browser-test.js +++ b/test/web/browser-test.js @@ -6,6 +6,196 @@ var expect = chai.expect; +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; +} + +})(); +(function(){ +'use strict'; + + +var expect = chai.expect; + + + var isPlainObject = fav.type.isPlainObject; describe('fav.type.isPlainObject', function() { diff --git a/web/fav.type.is-plain-object.js b/web/fav.type.is-plain-object.js index 939e897..7ace4ae 100644 --- a/web/fav.type.is-plain-object.js +++ b/web/fav.type.is-plain-object.js @@ -23,6 +23,15 @@ function isPlainObject(value) { } } +function isNotPlainObject(value) { + return !isPlainObject(value); +} + +Object.defineProperty(isPlainObject, 'not', { + enumerable: true, + value: isNotPlainObject, +}); + module.exports = isPlainObject; },{}]},{},[1])(1) diff --git a/web/fav.type.is-plain-object.min.js b/web/fav.type.is-plain-object.min.js index 5814d15..ff14599 100644 --- a/web/fav.type.is-plain-object.min.js +++ b/web/fav.type.is-plain-object.min.js @@ -1,2 +1,2 @@ -!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var t;(t=(t=(t="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).fav||(t.fav={})).type||(t.type={})).isPlainObject=e()}}(function(){return function e(t,n,r){function o(i,u){if(!n[i]){if(!t[i]){var c="function"==typeof require&&require;if(!u&&c)return c(i,!0);if(f)return f(i,!0);var p=new Error("Cannot find module '"+i+"'");throw p.code="MODULE_NOT_FOUND",p}var d=n[i]={exports:{}};t[i][0].call(d.exports,function(e){var n=t[i][1][e];return o(n||e)},d,d.exports,e,t,n,r)}return n[i].exports}for(var f="function"==typeof require&&require,i=0;i