包详细信息

is-descriptor

inspect-js174.8mMIT3.1.1

Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for data descriptors and accessor descriptors.

accessor, check, data, descriptor

自述文件

is-descriptor Version Badge

github actions coverage License Downloads

npm badge

Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for fully completed data descriptors and accessor descriptors.

Usage

const isDescriptor = require('is-descriptor');
const assert = require('assert');

const defaults = { configurable: false, enumerable: false };
const dataDefaults = { ...defaults, writable: false};

assert.ok(isDescriptor({ ...dataDefaults, value: 'foo' }));
assert.ok(isDescriptor({ ...defaults, get() {}, set() {} }));
assert.ok(!isDescriptor({ ...defaults, get: 'foo', set() {} }));

You may also check for a descriptor by passing an object as the first argument and property name (string) as the second argument.

const obj = { foo: 'abc' };

Object.defineProperty(obj, 'bar', { value: 'xyz' });
Reflect.defineProperty(obj, 'baz', { value: 'xyz' });

assert.equal(isDescriptor(obj, 'foo'), true);
assert.equal(isDescriptor(obj, 'bar'), true);
assert.equal(isDescriptor(obj, 'baz'), true);

Examples

value type

Returns false when not an object

assert.equal(isDescriptor('a'), false);
assert.equal(isDescriptor(null), false);
assert.equal(isDescriptor([]), false);

data descriptor

Returns true when the object has valid properties with valid values.

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo' }), true);
assert.equal(isDescriptor({ ...dataDefaults, value() {} }), true);

Returns false when the object has invalid properties

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'baz' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', get() {} }), false);
assert.equal(isDescriptor({ ...dataDefaults, get() {}, value() {} }), false);

false when a value is not the correct type

assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...dataDefaults, value: 'foo', writable: 'foo' }), false);

accessor descriptor

true when the object has valid properties with valid values.

assert.equal(isDescriptor({ ...defaults, get() {}, set() {} }), true);
assert.equal(isDescriptor({ ...defaults, get() {} }), true);
assert.equal(isDescriptor({ ...defaults, set() {} }), true);

false when the object has invalid properties

assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, writable: true }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, value: true }), false);

Returns false when an accessor is not a function

assert.equal(isDescriptor({ ...defaults, get() {}, set: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set() {} }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', bar: 'baz' }), false);
assert.equal(isDescriptor({ ...defaults, get: 'foo', set: 'baz' }), false);

Returns false when a value is not the correct type

assert.equal(isDescriptor({ ...defaults, get() {}, set() {}, enumerable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, set() {}, configurable: 'foo' }), false);
assert.equal(isDescriptor({ ...defaults, get() {}, configurable: 'foo' }), false);

Related projects

You might also be interested in these projects:

  • is-accessor-descriptor: Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.
  • is-data-descriptor: Returns true if a value has the characteristics of a valid JavaScript data descriptor.
  • is-object: Returns true if the value is an object and not an array or null.

Tests

Simply clone the repo, npm install, and run npm test

更新日志

Changelog

All notable changes to this project will be documented in this file.

The format is based on Keep a Changelog and this project adheres to Semantic Versioning.

v3.1.1 - 2023-10-27

Commits

v3.1.0 - 2023-05-01

Commits

  • [eslint] cleanup 1f4e8cd
  • [Tests] travis -> Github Actions; add safe-publish-latest, npmignore, auto-changelog, evalmd, aud 5993285
  • [readme] clean up docs, URLs, package.json, etc 8807164
  • [Docs] remove verb 0bc26a3
  • [Tests] convert from mocha to tape 1604d7f
  • [New] increase support from node 6 down to node 0.4 7893404
  • [Tests] add coverage 1dcc45e
  • [Fix] when an object/key pair is provided, check arguments.length instead of key truthiness d1edefe
  • [meta] switch from files field to npmignore; add exports c64d3d3

v3.0.0 - 2018-12-13

Commits

v2.0.0 - 2017-12-28

Commits

v1.0.3 - 2023-10-26

Commits

  • [eslint] actually use eslint 8bcf028
  • [meta] update package.json, gitignore from main 544cdfe
  • [readme] update readme from main 1130f79
  • [Tests] switch to tape 3f8f094
  • [Docs] remove verb 92ee1bf
  • [Tests] migrate from travis to github actions 8da3a3c
  • [Fix] a descriptor with set and not get is still an accessor descriptor 269fb53
  • [patch] switch from files to exports 41b2d61
  • [Fix] allow any non-primitive; arrays and functions are objects too 9fd1ac8
  • [Deps] update is-accessor-descriptor, is-data-descriptor f4dbc73
  • [Tests] make a test dir 9eaa17c

v1.0.2 - 2017-12-28

Merged

  • Update dependencies #5

v1.0.1 - 2017-07-22

Commits

v1.0.0 - 2017-02-25

v0.1.7 - 2023-10-26

Merged

  • Update dependencies #5

Commits

  • [eslint] actually use eslint 8bcf028
  • [meta] update package.json, gitignore from main 544cdfe
  • [readme] update readme from main 1130f79
  • [Tests] switch to tape 3f8f094
  • [Docs] remove verb 92ee1bf
  • [Tests] migrate from travis to github actions 8da3a3c
  • run update, lint 754cc73
  • [Fix] a descriptor with set and not get is still an accessor descriptor 269fb53
  • [patch] switch from files to exports 41b2d61
  • [Fix] allow any non-primitive; arrays and functions are objects too 9fd1ac8
  • update deps 2b58af6
  • [Deps] update is-accessor-descriptor, is-data-descriptor f4dbc73
  • v0.x line: v1 and v0 are the same, so, branch v0 from 1.x 91be723
  • [Tests] make a test dir 9eaa17c

v0.1.6 - 2017-07-22

v0.1.5 - 2017-02-25

Merged

  • Bump lazy-cache. #4

Commits

v0.1.4 - 2015-12-28

Commits

v0.1.3 - 2015-12-20

Commits

v0.1.2 - 2015-10-04

Commits

v0.1.1 - 2015-10-04

Merged

  • Update .verb.md #1

Commits

v0.1.0 - 2015-08-31

Commits