Skip to content

Commit

Permalink
[New] increase support from node 6 down to node 0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 27, 2023
1 parent 5758410 commit aa43b69
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
@@ -1,7 +1,7 @@
{
"root": true,

"extends": "@ljharb/eslint-config/node/6",
"extends": "@ljharb",

"overrides": [
{
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/node-aught.yml
Expand Up @@ -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

Expand Down
40 changes: 25 additions & 15 deletions 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;
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -64,7 +64,7 @@
"tape": "^5.6.3"
},
"engines": {
"node": ">=6"
"node": ">= 0.4"
},
"auto-changelog": {
"output": "CHANGELOG.md",
Expand Down
26 changes: 13 additions & 13 deletions test/index.js
@@ -1,26 +1,26 @@
'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([]));

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 }));
Expand All @@ -29,38 +29,38 @@ 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' }));

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' }));

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));

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 }));
Expand All @@ -69,7 +69,7 @@ test('isDescriptor', (t) => {
get: undefined,
set: undefined,
enumerable: true,
configurable: true,
configurable: true
}));

s2t.end();
Expand Down

0 comments on commit aa43b69

Please sign in to comment.