Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
also no longer checks for invalid properties
  • Loading branch information
jonschlinkert committed Dec 28, 2015
1 parent 6d69a34 commit c77b522
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 50 deletions.
47 changes: 23 additions & 24 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

'use strict';

var utils = require('./utils');
var typeOf = require('kind-of');

// data descriptor properties
var data = {
Expand All @@ -16,39 +16,38 @@ var data = {
writable: 'boolean'
};

function isDataDescriptor(obj) {
if (utils.typeOf(obj) !== 'object') {
function isDataDescriptor(obj, prop) {
if (typeOf(obj) !== 'object') {
return false;
}

var dataKeys = Object.keys(data);
var keys = getKeys(obj);
if (typeof prop === 'string') {
var val = Object.getOwnPropertyDescriptor(obj, prop);
return typeof val !== 'undefined';
}

if (obj.hasOwnProperty('value')) {
if (utils.diff(keys, dataKeys).length !== 1) {
return false;
if (!('value' in obj) && !('writable' in obj)) {
return false;
}

for (var key in obj) {
if (key === 'value') continue;

if (!data.hasOwnProperty(key)) {
continue;
}
for (var key in obj) {
if (key === 'value') continue;
if (utils.typeOf(obj[key]) !== data[key]) {
return false;
}

if (typeOf(obj[key]) === data[key]) {
continue;
}

if (typeof obj[key] !== 'undefined') {
return false;
}
}
return true;
}

/**
* Get object keys. `Object.keys()` only gets
* enumerable properties.
*/

function getKeys(obj) {
var keys = [];
for (var key in obj) keys.push(key);
return keys;
}

/**
* Expose `isDataDescriptor`
*/
Expand Down
33 changes: 26 additions & 7 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,41 @@ describe('isDescriptor', function () {
});

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'}));
assert(!isDescriptor({value: 'foo', get: noop}));
assert(!isDescriptor({get: noop, value: noop}));
it('should not be false when the object has invalid properties:', function () {
assert(isDescriptor({value: 'foo', bar: 'baz'}));
assert(isDescriptor({value: 'foo', bar: 'baz'}));
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 be true when the object has valid data-descriptor 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 valid properties are invalid types', function () {
assert(!isDescriptor({value: 'foo', enumerable: 'foo'}));
assert(!isDescriptor({value: 'foo', configurable: 'foo'}));
assert(!isDescriptor({value: 'foo', writable: 'foo'}));
});

it('should be true when a value is a valid data descriptor', function () {
assert(isDescriptor({value: 'foo'}));
assert(isDescriptor({writable: true}));
assert(isDescriptor({value: 'foo', get: 'foo'}));
});

it('should be false when the value is not a valid descriptor', function () {
assert(!isDescriptor('foo'));
assert(!isDescriptor({}));
assert(!isDescriptor({configurable: true}));
assert(!isDescriptor({enumerable: true}));
assert(!isDescriptor({
get: undefined,
set: undefined,
enumerable: true,
configurable: true
}));
});
});
});
19 changes: 0 additions & 19 deletions utils.js

This file was deleted.

0 comments on commit c77b522

Please sign in to comment.