diff --git a/.eslintrc b/.eslintrc index 9ad7ef5..758ff12 100644 --- a/.eslintrc +++ b/.eslintrc @@ -1,7 +1,7 @@ { "root": true, - "extends": "@ljharb/eslint-config/node/6", + "extends": "@ljharb", "overrides": [ { diff --git a/.github/workflows/node-aught.yml b/.github/workflows/node-aught.yml index ee6cbc2..f3cddd8 100644 --- a/.github/workflows/node-aught.yml +++ b/.github/workflows/node-aught.yml @@ -6,7 +6,7 @@ jobs: tests: uses: ljharb/actions/.github/workflows/node.yml@main with: - range: '>= 6 < 10' + range: '< 10' type: minors command: npm run tests-only diff --git a/index.js b/index.js index 54f0f67..65c159a 100644 --- a/index.js +++ b/index.js @@ -1,31 +1,41 @@ 'use strict'; -const hasOwn = (obj, key) => Object.prototype.hasOwnProperty.call(obj, key); -const isObject = (val) => val !== null && typeof val === 'object' && !Array.isArray(val); +var hasOwn = function (obj, key) { + return Object.prototype.hasOwnProperty.call(obj, key); +}; +var isObject = function (val) { + return val !== null && typeof val === 'object' && !Array.isArray(val); +}; -const isDescriptor = (obj, key) => { +module.exports = function isDataDescriptor(obj, key) { if (!isObject(obj)) { return false; } - const desc = key ? Object.getOwnPropertyDescriptor(obj, key) : obj; + var desc = key ? Object.getOwnPropertyDescriptor(obj, key) : obj; if (isObject(desc)) { - const booleans = [ - 'configurable', 'enumerable', 'writable', - ]; - if (!hasOwn(desc, 'value') || hasOwn(desc, 'get') || hasOwn(desc, 'set')) { + if ( + !hasOwn(desc, 'value') + || hasOwn(desc, 'get') + || hasOwn(desc, 'set') + || (hasOwn(desc, 'configurable') && typeof desc.configurable !== 'boolean') + || (hasOwn(desc, 'enumerable') && typeof desc.enumerable !== 'boolean') + || (hasOwn(desc, 'writable') && typeof desc.writable !== 'boolean') + ) { return false; } - for (const descKey of Object.keys(desc)) { - if (booleans.includes(descKey) && typeof desc[descKey] !== 'boolean') { - return false; - } - if (!booleans.includes(descKey) && descKey !== 'value') { + for (var descKey in desc) { // eslint-disable-line no-restricted-syntax + if ( + hasOwn(desc, descKey) + && descKey !== 'configurable' + && descKey !== 'enumerable' + && descKey !== 'writable' + && descKey !== 'value' + ) { return false; } } + return true; } return false; }; - -module.exports = isDescriptor; diff --git a/package.json b/package.json index 03ad64d..c9e8460 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,7 @@ "tape": "^5.6.3" }, "engines": { - "node": ">=6" + "node": ">= 0.4" }, "auto-changelog": { "output": "CHANGELOG.md", diff --git a/test/index.js b/test/index.js index b431593..fa0c04e 100644 --- a/test/index.js +++ b/test/index.js @@ -1,12 +1,12 @@ 'use strict'; -const test = require('tape'); +var test = require('tape'); -const isDescriptor = require('../'); -const noop = () => {}; +var isDescriptor = require('../'); +var noop = function () {}; -test('isDescriptor', (t) => { - t.test('value type: is false when not an object', (st) => { +test('isDescriptor', function (t) { + t.test('value type: is false when not an object', function (st) { st.notOk(isDescriptor('a')); st.notOk(isDescriptor(null)); st.notOk(isDescriptor([])); @@ -14,13 +14,13 @@ test('isDescriptor', (t) => { st.end(); }); - t.test('data descriptor:', (st) => { + t.test('data descriptor:', function (st) { st.notOk(isDescriptor({ value: 'foo', bar: 'baz' }), 'is false when the object has invalid properties'); st.notOk(isDescriptor({ value: 'foo', get: noop }), 'is false when the object has get or set properties'); st.notOk(isDescriptor({ get: noop, value: noop }), 'is false when the object has get or set properties'); - st.test('is false when the object has invalid properties and strict is true', (s2t) => { + st.test('is false when the object has invalid properties and strict is true', function (s2t) { s2t.notOk(isDescriptor({ value: 'foo', bar: 'baz' })); s2t.notOk(isDescriptor({ value: 'foo', bar: 'baz' })); s2t.notOk(isDescriptor({ value: 'foo', get: noop })); @@ -29,14 +29,14 @@ test('isDescriptor', (t) => { s2t.end(); }); - st.test('is true when the object has valid data-descriptor properties', (s2t) => { + st.test('is true when the object has valid data-descriptor properties', function (s2t) { s2t.ok(isDescriptor({ value: 'foo' })); s2t.ok(isDescriptor({ value: noop })); s2t.end(); }); - st.test('is false when valid properties are invalid types', (s2t) => { + st.test('is false when valid properties are invalid types', function (s2t) { s2t.notOk(isDescriptor({ value: 'foo', enumerable: 'foo' })); s2t.notOk(isDescriptor({ value: 'foo', configurable: 'foo' })); s2t.notOk(isDescriptor({ value: 'foo', writable: 'foo' })); @@ -44,7 +44,7 @@ test('isDescriptor', (t) => { s2t.end(); }); - st.test('should be true when a value is a valid data descriptor', (s2t) => { + st.test('should be true when a value is a valid data descriptor', function (s2t) { s2t.ok(isDescriptor({ value: 'foo' })); s2t.notOk(isDescriptor({ writable: true })); s2t.notOk(isDescriptor({ value: 'foo', get: 'foo' })); @@ -52,7 +52,7 @@ test('isDescriptor', (t) => { s2t.end(); }); - st.test('is false when descriptor has an in-valid propery and "strict" is true', (s2t) => { + st.test('is false when descriptor has an in-valid propery and "strict" is true', function (s2t) { s2t.ok(isDescriptor({ value: 'foo' })); s2t.notOk(isDescriptor({ writable: true }, void undefined, true)); s2t.notOk(isDescriptor({ value: 'foo', get: 'foo' }, void undefined, true)); @@ -60,7 +60,7 @@ test('isDescriptor', (t) => { s2t.end(); }); - st.test('should be false when the value is not a valid descriptor', (s2t) => { + st.test('should be false when the value is not a valid descriptor', function (s2t) { s2t.notOk(isDescriptor('foo')); s2t.notOk(isDescriptor({})); s2t.notOk(isDescriptor({ configurable: true })); @@ -69,7 +69,7 @@ test('isDescriptor', (t) => { get: undefined, set: undefined, enumerable: true, - configurable: true, + configurable: true })); s2t.end();