Socket
Socket
Sign inDemoInstall

is-accessor-descriptor

Package Overview
Dependencies
7
Maintainers
2
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    is-accessor-descriptor

Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.


Version published
Weekly downloads
27M
decreased by-10.92%
Maintainers
2
Install size
147 kB
Created
Weekly downloads
 

Package description

What is is-accessor-descriptor?

The is-accessor-descriptor npm package is used to check if an object property descriptor defines an accessor descriptor. An accessor descriptor is one that includes getter and/or setter functions, as opposed to a data descriptor which contains a value and is writable.

What are is-accessor-descriptor's main functionalities?

Check if a descriptor is an accessor descriptor

This feature allows you to verify if a given property descriptor from an object is an accessor descriptor. It returns true if the descriptor has a get or set key, and false otherwise.

{"isAccessorDescriptor": require('is-accessor-descriptor');
var descriptor = Object.getOwnPropertyDescriptor({get foo() {}}, 'foo');
console.log(isAccessorDescriptor(descriptor)); //=> true
}

Check if an object is an accessor descriptor

This feature allows you to check if a plain object mimics the structure of an accessor descriptor. It is useful for validation purposes when you have an object that should represent a descriptor and you want to ensure it is an accessor descriptor.

{"isAccessorDescriptor": require('is-accessor-descriptor');
console.log(isAccessorDescriptor({get: function() {}})); //=> true
console.log(isAccessorDescriptor({set: function() {}})); //=> true
console.log(isAccessorDescriptor({value: 123})); //=> false
}

Other packages similar to is-accessor-descriptor

Changelog

Source

v3.0.4 - 2023-10-26

Commits

  • Merge tag v0.1.7, v1.0.1 a5ae905
  • [eslint] actually use eslint d0b9c94
  • [eslint] actually use eslint a05c057
  • [meta] update package.json, etc from main c4fab21
  • [readme] clean up docs, URLs, package.json, etc 6648dcd
  • temp a6392d4
  • [meta] update .gitignore cba0ea0
  • [Tests] switch to tape 9a0ab35
  • [readme] remove verb 2f07da4
  • [Tests] switch to tape 7e39202
  • [Tests] migrate from travis to github actions aa436b0
  • [Tests] migrate from travis to github actions 6e9c4f1
  • [readme] remove verb 967137d
  • [readme] use evalmd 471d3b9
  • [Fix] only one of get and set are required to be an accessor c8de5e7
  • [Fix] properly handle an accessor descriptor with only a setter 04647f4
  • [Refactor] use hasown 6151f01
  • [Refactor] properly guard for-in loop a0454cc
  • [Fix] allow any non-primitive; arrays and functions are objects too 853db5a
  • [Fix] allow any non-primitive; arrays and functions are objects too 123e3c3
  • Only apps should have lockfiles 87402b1
  • [Refactor] use hasown 7ad36a0
  • [Fix] use correct logic for two-arg form 3c1729e
  • [Fix] properly guard for-in loop 442daa8
  • [Tests] move tests to test dir 18313a7
  • [readme] fix incorrect example 3ee754a
  • [Tests] move tests to test dir 5d70880
  • [Dev Deps] add missing npmignore 97ce4bc
  • [Robustness] use a null object just in case 675af5b

Readme

Source

is-accessor-descriptor Version Badge

<<<<<<< .merge_file_oXHkzP github actions coverage License Downloads

Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.

Install

Install with npm:

$ npm i is-accessor-descriptor --save

.merge_file_OTGJy6

npm badge

<<<<<<< .merge_file_oXHkzP

Returns true if a value has the characteristics of a valid JavaScript accessor descriptor.

Examples

const isAccessorDescriptor = require('is-accessor-descriptor');
const assert = require('assert');

const obj = {
	get foo() {},
	bar: { get: function() {} }
};

assert.equal(true, isAccessorDescriptor(obj, 'foo'));
assert.equal(false, isAccessorDescriptor(obj, 'bar'));

// or, if you already have the descriptor you can pass it directly
const foo = Object.getOwnPropertyDescriptor(obj, 'foo');
assert.equal(true, isAccessorDescriptor(foo));

const bar = Object.getOwnPropertyDescriptor(obj, 'bar');
assert.equal(false, isAccessorDescriptor(bar));

You might also be interested in these projects:

  • is-data-descriptor: Returns true if a value has the characteristics of a valid JavaScript data descriptor.
  • is-descriptor: Returns true if a value has the characteristics of a valid JavaScript descriptor. Works for… more
  • 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

=======

var isAccessor = require('is-accessor-descriptor');
var assert = require('assert');

assert.equal(isAccessor({ get() {} }), true);

You may also pass an object and property name to check if the property is an accessor:

assert.equal(isAccessor({ bar: 'foo' }, 'bar'), false);

Examples

false when not an object

assert.equal(isAccessor('a'), false);
assert.equal(isAccessor(null), false);

true when the object has valid properties

and the properties all have the correct JavaScript types:

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

false when the object has invalid properties

assert.equal(isAccessor({ get() {}, set() {}, enumerable: 'baz' }), false);
assert.equal(isAccessor({ get() {}, writable: true }), false);
assert.equal(isAccessor({ get() {}, value: true }), false);
//=> false

false when an accessor is not a function

assert.equal(isAccessor({ get() {}, set: 'baz' }), false);
assert.equal(isAccessor({ get: 'foo', set() {} }), false);
assert.equal(isAccessor({ get: 'foo', bar: 'baz' }), false);
assert.equal(isAccessor({ get: 'foo', set: 'baz' }), false);
//=> false

false when a value is not the correct type

assert.equal(isAccessor({ get() {}, set() {}, enumerable: 'foo' }), false);
assert.equal(isAccessor({ set() {}, configurable: 'foo' }), false);
assert.equal(isAccessor({ get() {}, configurable: 'foo' }), false);
//=> false

Running tests

Install dev dependencies:

$ npm i -d && npm test

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Author

Jon Schlinkert

License

Copyright © 2015 Jon Schlinkert Released under the MIT license.

.merge_file_OTGJy6

Keywords

FAQs

Last updated on 27 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc