From 202062b56735525e7def35c8453505778ce9de03 Mon Sep 17 00:00:00 2001 From: jonschlinkert Date: Mon, 28 Dec 2015 04:43:59 -0500 Subject: [PATCH] allow a key to be passed --- index.js | 12 ++++++---- test.js | 68 ++++++++++++++++++++++++++++++++++++++++---------------- utils.js | 5 +++++ 3 files changed, 62 insertions(+), 23 deletions(-) diff --git a/index.js b/index.js index 71bd58d..dae8678 100644 --- a/index.js +++ b/index.js @@ -9,8 +9,12 @@ var utils = require('./utils'); -module.exports = function isDescriptor(obj) { - if (utils.typeOf(obj) !== 'object') return false; - if ('value' in obj) return utils.isData(obj); - return utils.isAccessor(obj); +module.exports = function isDescriptor(obj, key) { + if (utils.typeOf(obj) !== 'object') { + return false; + } + if ('get' in obj) { + return utils.isAccessor(obj, key); + } + return utils.isData(obj, key); }; diff --git a/test.js b/test.js index fc7c33c..d1ed857 100644 --- a/test.js +++ b/test.js @@ -1,61 +1,91 @@ 'use strict'; -/* deps: mocha */ +require('mocha'); var assert = require('assert'); -var should = require('should'); var isDescriptor = require('./'); -var noop = function(){}; +var noop = function() {}; -describe('isDescriptor', function () { - describe('value type', function () { - it('should be false when not an object:', function () { +describe('isDescriptor', function() { + describe('value type', function() { + it('should be false when not an object:', function() { assert(!isDescriptor('a')); assert(!isDescriptor(null)); assert(!isDescriptor([])); }); }); - describe('data descriptor:', function () { - it('should be false when the object has invalid properties:', function () { - assert(!isDescriptor({value: 'foo', bar: 'baz'})); - assert(!isDescriptor({value: 'foo', bar: 'baz'})); + describe('check for descriptor', function() { + it('should return true if the property exists', function() { + var obj = {}; + obj.foo = null; + + Object.defineProperty(obj, 'bar', { + value: 'xyz' + }); + + Object.defineProperty(obj, 'baz', { + get: function() { + return 'aaa'; + } + }); + + assert(isDescriptor(obj, 'foo')); + assert(isDescriptor(obj, 'bar')); + assert(isDescriptor(obj, 'baz')); + }); + }); + + describe('data descriptor:', function() { + it('should be false when the object has invalid properties:', function() { assert(!isDescriptor({value: 'foo', get: noop})); assert(!isDescriptor({get: noop, value: noop})); }); - it('should be true when the object has valid properties:', function () { + it('should not be false when the object has unrecognize properties:', function() { + assert(isDescriptor({value: 'foo', bar: 'baz'})); + assert(isDescriptor({value: 'foo', bar: 'baz'})); + }); + + it('should be true when the object has valid properties:', function() { assert(isDescriptor({value: 'foo'})); assert(isDescriptor({value: noop})); }); - it('should be false when a value is not the correct type:', function () { + it('should be false when a value is not the correct type:', function() { assert(!isDescriptor({value: 'foo', enumerable: 'foo'})); assert(!isDescriptor({value: 'foo', configurable: 'foo'})); assert(!isDescriptor({value: 'foo', writable: 'foo'})); }); }); - describe('accessor descriptor:', function () { - it('should be false when the object has invalid properties:', function () { - assert(!isDescriptor({get: noop, set: noop, bar: 'baz'})); + describe('accessor descriptor:', function() { + it('should be false when the object has invalid properties:', function() { assert(!isDescriptor({get: noop, writable: true})); assert(!isDescriptor({get: noop, value: true})); }); - it('should be false when an accessor is not a function:', function () { + it('should not be false when the object has unrecognize properties:', function() { + assert(isDescriptor({get: noop, set: noop, bar: 'baz'})); + }); + + it('should be false when an accessor is not a function:', function() { assert(!isDescriptor({get: noop, set: 'baz'})); assert(!isDescriptor({get: 'foo', set: noop})); assert(!isDescriptor({get: 'foo', bar: 'baz'})); assert(!isDescriptor({get: 'foo', set: 'baz'})); }); - it('should be true when the object has valid properties:', function () { + it('should be false when "get" is not a function', function() { + assert(!isDescriptor({set: noop})); + assert(!isDescriptor({get: 'foo'})); + }); + + it('should be true when the object has valid properties:', function() { assert(isDescriptor({get: noop, set: noop})); assert(isDescriptor({get: noop})); - assert(isDescriptor({set: noop})); }); - it('should be false when a value is not the correct type:', function () { + it('should be false when a value is not the correct type:', function() { assert(!isDescriptor({get: noop, set: noop, enumerable: 'foo'})); assert(!isDescriptor({set: noop, configurable: 'foo'})); assert(!isDescriptor({get: noop, configurable: 'foo'})); diff --git a/utils.js b/utils.js index 212bc2c..c85e9b9 100644 --- a/utils.js +++ b/utils.js @@ -7,6 +7,11 @@ var utils = require('lazy-cache')(require); var fn = require; require = utils; + +/** + * Utils + */ + require('kind-of', 'typeOf'); require('is-accessor-descriptor', 'isAccessor'); require('is-data-descriptor', 'isData');