Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Dec 14, 2018
1 parent 6024f52 commit 8dcc492
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 76 deletions.
3 changes: 1 addition & 2 deletions .editorconfig
Expand Up @@ -9,6 +9,5 @@ indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[{**/{actual,fixtures,expected,templates}/**,*.md}]
[*.md]
trim_trailing_whitespace = false
insert_final_newline = false
13 changes: 13 additions & 0 deletions .eslintrc.json
@@ -1,11 +1,24 @@
{
"extends": [
"eslint:recommended"
],

"env": {
"browser": false,
"es6": true,
"node": true,
"mocha": true
},

"parserOptions":{
"ecmaVersion": 9,
"sourceType": "module",
"ecmaFeatures": {
"modules": true,
"experimentalObjectRestSpread": true
}
},

"globals": {
"document": false,
"navigator": false,
Expand Down
1 change: 1 addition & 0 deletions .npmrc
@@ -0,0 +1 @@
package-lock=false
7 changes: 3 additions & 4 deletions .travis.yml
Expand Up @@ -2,14 +2,13 @@ sudo: false
os:
- linux
- osx
- windows
language: node_js
node_js:
- node
- '11'
- '10'
- '9'
- '8'
- '7'
- '6'
- '5'
- '4'
- '0.12'
- '0.10'
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2015-2017, Jon Schlinkert.
Copyright (c) 2015-present, Jon Schlinkert.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
64 changes: 22 additions & 42 deletions index.js
@@ -1,49 +1,29 @@
/*!
* is-data-descriptor <https://github.com/jonschlinkert/is-data-descriptor>
*
* Copyright (c) 2015-2017, Jon Schlinkert.
* Released under the MIT License.
*/

'use strict';

var typeOf = require('kind-of');

module.exports = function isDataDescriptor(obj, prop) {
// data descriptor properties
var data = {
configurable: 'boolean',
enumerable: 'boolean',
writable: 'boolean'
};

if (typeOf(obj) !== 'object') {
return false;
}

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

if (!('value' in obj) && !('writable' in obj)) {
return false;
}

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

if (!data.hasOwnProperty(key)) {
continue;
}

if (typeOf(obj[key]) === data[key]) {
continue;
}
const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key);
const isObject = val => {
return val !== null && typeof val === 'object' && !Array.isArray(val);
};

if (typeof obj[key] !== 'undefined') {
const isDescriptor = (obj, key) => {
if (!isObject(obj)) return false;
let desc = key ? Object.getOwnPropertyDescriptor(obj, key) : obj;
if (isObject(desc)) {
let booleans = ['configurable', 'enumerable', 'writable'];
if (!hasOwn(desc, 'value') || hasOwn(desc, 'get') || hasOwn(desc, 'set')) {
return false;
}
for (let key of Object.keys(desc)) {
if (booleans.includes(key) && typeof desc[key] !== 'boolean') {
return false;
}
if (!booleans.includes(key) && key !== 'value') {
return false;
}
}
return true;
}
return true;
return false;
};

module.exports = isDescriptor;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -18,7 +18,7 @@
],
"main": "index.js",
"engines": {
"node": ">=0.10.0"
"node": ">=6"
},
"scripts": {
"test": "mocha"
Expand Down
68 changes: 42 additions & 26 deletions test.js
@@ -1,49 +1,65 @@
'use strict';

require('mocha');
var assert = require('assert');
var isDescriptor = require('./');
var noop = function() {};
const assert = require('assert');
const isDescriptor = require('./');
const noop = () => {};

describe('isDescriptor', function() {
describe('value type', function() {
it('should be false when not an object:', function() {
describe('isDescriptor', () => {
describe('value type', () => {
it('should be false when not an object:', () => {
assert(!isDescriptor('a'));
assert(!isDescriptor(null));
assert(!isDescriptor([]));
});
});

describe('data descriptor:', function() {
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}));
describe('data descriptor:', () => {
it('should be false when the object has invalid properties:', () => {
assert(!isDescriptor({ value: 'foo', bar: 'baz' }));
assert(!isDescriptor({ value: 'foo', bar: 'baz' }));
});

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 the object has get or set properties', () => {
assert(!isDescriptor({ value: 'foo', get: noop }));
assert(!isDescriptor({ get: noop, value: noop }));
});

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 false when the object has invalid properties and strict is true', () => {
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 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 true when the object has valid data-descriptor properties', () => {
assert(isDescriptor({ value: 'foo' }));
assert(isDescriptor({ value: noop }));
});

it('should be false when the value is not a valid descriptor', function() {
it('should be false when valid properties are invalid types', () => {
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', () => {
assert(isDescriptor({ value: 'foo' }));
assert(!isDescriptor({ writable: true }));
assert(!isDescriptor({ value: 'foo', get: 'foo' }));
});

it('should be false when descriptor has an in-valid propery and "strict" is true', () => {
assert(isDescriptor({ value: 'foo' }));
assert(!isDescriptor({ writable: true }, void 0, true));
assert(!isDescriptor({ value: 'foo', get: 'foo' }, void 0, true));
});

it('should be false when the value is not a valid descriptor', () => {
assert(!isDescriptor('foo'));
assert(!isDescriptor({}));
assert(!isDescriptor({configurable: true}));
assert(!isDescriptor({enumerable: true}));
assert(!isDescriptor({ configurable: true }));
assert(!isDescriptor({ enumerable: true }));
assert(!isDescriptor({
get: undefined,
set: undefined,
Expand Down

0 comments on commit 8dcc492

Please sign in to comment.