UNPKG

3.99 kBMarkdownView Raw
1# object.getownpropertydescriptors <sup>[![Version Badge][npm-version-svg]][package-url]</sup>
2
3[![github actions][actions-image]][actions-url]
4[![coverage][codecov-image]][codecov-url]
5[![dependency status][deps-svg]][deps-url]
6[![dev dependency status][dev-deps-svg]][dev-deps-url]
7[![License][license-image]][license-url]
8[![Downloads][downloads-image]][downloads-url]
9
10[![npm badge][npm-badge-png]][package-url]
11
12An ES2017 spec-compliant shim for `Object.getOwnPropertyDescriptors` that works in ES5.
13Invoke its "shim" method to shim `Object.getOwnPropertyDescriptors` if it is unavailable, and if `Object.getOwnPropertyDescriptor` is available.
14
15This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](https://github.com/tc39/ecma262/pull/582).
16
17## Example
18
19```js
20var getDescriptors = require('object.getownpropertydescriptors');
21var assert = require('assert');
22var obj = { normal: Infinity };
23var enumDescriptor = {
24 enumerable: false,
25 writable: false,
26 configurable: true,
27 value: true
28};
29var writableDescriptor = {
30 enumerable: true,
31 writable: true,
32 configurable: true,
33 value: 42
34};
35var symbol = Symbol();
36var symDescriptor = {
37 enumerable: true,
38 writable: true,
39 configurable: false,
40 value: [symbol]
41};
42
43Object.defineProperty(obj, 'enumerable', enumDescriptor);
44Object.defineProperty(obj, 'writable', writableDescriptor);
45Object.defineProperty(obj, 'symbol', symDescriptor);
46
47var descriptors = getDescriptors(obj);
48
49assert.deepEqual(descriptors, {
50 normal: {
51 enumerable: true,
52 writable: true,
53 configurable: true,
54 value: Infinity
55 },
56 enumerable: enumDescriptor,
57 writable: writableDescriptor,
58 symbol: symDescriptor
59});
60```
61
62```js
63var getDescriptors = require('object.getownpropertydescriptors');
64var assert = require('assert');
65/* when Object.getOwnPropertyDescriptors is not present */
66delete Object.getOwnPropertyDescriptors;
67var shimmedDescriptors = getDescriptors.shim();
68assert.equal(shimmedDescriptors, getDescriptors);
69assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));
70```
71
72```js
73var getDescriptors = require('object.getownpropertydescriptors');
74var assert = require('assert');
75/* when Object.getOwnPropertyDescriptors is present */
76var shimmedDescriptors = getDescriptors.shim();
77assert.notEqual(shimmedDescriptors, getDescriptors);
78assert.deepEqual(shimmedDescriptors(obj), getDescriptors(obj));
79```
80
81## Tests
82Simply clone the repo, `npm install`, and run `npm test`
83
84[package-url]: https://npmjs.org/package/object.getownpropertydescriptors
85[npm-version-svg]: http://versionbadg.es/es-shims/Object.getOwnPropertyDescriptors.svg
86[travis-svg]: https://travis-ci.org/es-shims/Object.getOwnPropertyDescriptors.svg
87[travis-url]: https://travis-ci.org/es-shims/Object.getOwnPropertyDescriptors
88[deps-svg]: https://david-dm.org/es-shims/Object.getOwnPropertyDescriptors.svg
89[deps-url]: https://david-dm.org/es-shims/Object.getOwnPropertyDescriptors
90[dev-deps-svg]: https://david-dm.org/es-shims/Object.getOwnPropertyDescriptors/dev-status.svg
91[dev-deps-url]: https://david-dm.org/es-shims/Object.getOwnPropertyDescriptors#info=devDependencies
92[npm-badge-png]: https://nodei.co/npm/object.getownpropertydescriptors.png?downloads=true&stars=true
93[license-image]: http://img.shields.io/npm/l/object.getownpropertydescriptors.svg
94[license-url]: LICENSE
95[downloads-image]: http://img.shields.io/npm/dm/object.getownpropertydescriptors.svg
96[downloads-url]: http://npm-stat.com/charts.html?package=object.getownpropertydescriptors
97[codecov-image]: https://codecov.io/gh/es-shims/Object.getOwnPropertyDescriptors/branch/main/graphs/badge.svg
98[codecov-url]: https://app.codecov.io/gh/es-shims/Object.getOwnPropertyDescriptors/
99[actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/es-shims/Object.getOwnPropertyDescriptors
100[actions-url]: https://github.com/es-shims/Object.getOwnPropertyDescriptors/actions