[d565449] | 1 | # define-properties <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 |
|
---|
| 12 | Define multiple non-enumerable properties at once. Uses `Object.defineProperty` when available; falls back to standard assignment in older engines.
|
---|
| 13 | Existing properties are not overridden. Accepts a map of property names to a predicate that, when true, force-overrides.
|
---|
| 14 |
|
---|
| 15 | ## Example
|
---|
| 16 |
|
---|
| 17 | ```js
|
---|
| 18 | var define = require('define-properties');
|
---|
| 19 | var assert = require('assert');
|
---|
| 20 |
|
---|
| 21 | var obj = define({ a: 1, b: 2 }, {
|
---|
| 22 | a: 10,
|
---|
| 23 | b: 20,
|
---|
| 24 | c: 30
|
---|
| 25 | });
|
---|
| 26 | assert(obj.a === 1);
|
---|
| 27 | assert(obj.b === 2);
|
---|
| 28 | assert(obj.c === 30);
|
---|
| 29 | if (define.supportsDescriptors) {
|
---|
| 30 | assert.deepEqual(Object.keys(obj), ['a', 'b']);
|
---|
| 31 | assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'c'), {
|
---|
| 32 | configurable: true,
|
---|
| 33 | enumerable: false,
|
---|
| 34 | value: 30,
|
---|
| 35 | writable: false
|
---|
| 36 | });
|
---|
| 37 | }
|
---|
| 38 | ```
|
---|
| 39 |
|
---|
| 40 | Then, with predicates:
|
---|
| 41 | ```js
|
---|
| 42 | var define = require('define-properties');
|
---|
| 43 | var assert = require('assert');
|
---|
| 44 |
|
---|
| 45 | var obj = define({ a: 1, b: 2, c: 3 }, {
|
---|
| 46 | a: 10,
|
---|
| 47 | b: 20,
|
---|
| 48 | c: 30
|
---|
| 49 | }, {
|
---|
| 50 | a: function () { return false; },
|
---|
| 51 | b: function () { return true; }
|
---|
| 52 | });
|
---|
| 53 | assert(obj.a === 1);
|
---|
| 54 | assert(obj.b === 20);
|
---|
| 55 | assert(obj.c === 3);
|
---|
| 56 | if (define.supportsDescriptors) {
|
---|
| 57 | assert.deepEqual(Object.keys(obj), ['a', 'c']);
|
---|
| 58 | assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'b'), {
|
---|
| 59 | configurable: true,
|
---|
| 60 | enumerable: false,
|
---|
| 61 | value: 20,
|
---|
| 62 | writable: false
|
---|
| 63 | });
|
---|
| 64 | }
|
---|
| 65 | ```
|
---|
| 66 |
|
---|
| 67 | ## Tests
|
---|
| 68 | Simply clone the repo, `npm install`, and run `npm test`
|
---|
| 69 |
|
---|
| 70 | [package-url]: https://npmjs.org/package/define-properties
|
---|
| 71 | [npm-version-svg]: https://versionbadg.es/ljharb/define-properties.svg
|
---|
| 72 | [deps-svg]: https://david-dm.org/ljharb/define-properties.svg
|
---|
| 73 | [deps-url]: https://david-dm.org/ljharb/define-properties
|
---|
| 74 | [dev-deps-svg]: https://david-dm.org/ljharb/define-properties/dev-status.svg
|
---|
| 75 | [dev-deps-url]: https://david-dm.org/ljharb/define-properties#info=devDependencies
|
---|
| 76 | [npm-badge-png]: https://nodei.co/npm/define-properties.png?downloads=true&stars=true
|
---|
| 77 | [license-image]: https://img.shields.io/npm/l/define-properties.svg
|
---|
| 78 | [license-url]: LICENSE
|
---|
| 79 | [downloads-image]: https://img.shields.io/npm/dm/define-properties.svg
|
---|
| 80 | [downloads-url]: https://npm-stat.com/charts.html?package=define-properties
|
---|
| 81 | [codecov-image]: https://codecov.io/gh/ljharb/define-properties/branch/main/graphs/badge.svg
|
---|
| 82 | [codecov-url]: https://app.codecov.io/gh/ljharb/define-properties/
|
---|
| 83 | [actions-image]: https://img.shields.io/endpoint?url=https://github-actions-badge-u3jn4tfpocch.runkit.sh/ljharb/define-properties
|
---|
| 84 | [actions-url]: https://github.com/ljharb/define-properties/actions
|
---|