Socket
Socket
Sign inDemoInstall

define-properties

Package Overview
Dependencies
9
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Install Socket

Protect your apps from supply chain attacks

Install

define-properties

Define multiple non-enumerable properties at once. Uses `Object.defineProperty` when available; falls back to standard assignment in older engines.

    1.2.0latest
    GitHub
    npm

Version published
Maintainers
1
Weekly downloads
37,480,183
increased by3.4%

Weekly downloads

Package description

What is define-properties?

The define-properties npm package is used to define new properties on an object, including any necessary descriptors. It provides a simple API to define multiple properties at once while ensuring compatibility with older JavaScript engines that may not fully support ES5.

What are define-properties's main functionalities?

Defining multiple properties

This feature allows you to define multiple properties on an object with their descriptors. The 'enumerable' predicate ensures that the property will only be defined if the value is truthy.

{"object": {}, "properties": {"prop1": {"value": 42, "enumerable": true}, "prop2": {"value": 'hello', "enumerable": false}}, "predicates": {"enumerable": function(val) { return !!val; }}}

Defining properties with shared descriptors

This feature allows you to define properties with a shared descriptor. The predicate function can be used to conditionally define properties based on the key or value.

{"object": {}, "propertyNames": ["prop1", "prop2"], "descriptor": {"enumerable": true}, "predicate": function(key, value) { return key === 'prop1' || value > 10; }}

Other packages similar to define-properties

Changelog

Source

v1.2.0 - 2023-02-10

Commits

  • [New] if the predicate is boolean true, it compares the existing value with === as the predicate d8dd6fc
  • [meta] add auto-changelog 7ebe2b0
  • [meta] use npmignore to autogenerate an npmignore file 647478a
  • [Dev Deps] update @ljharb/eslint-config, aud, tape e620d70
  • [Dev Deps] update aud, tape f1e5072
  • [actions] update checkout action 628b3af
<!-- auto-changelog-above -->

1.1.4 / 2022-04-14

  • [Refactor] use has-property-descriptors
  • [readme] add github actions/codecov badges
  • [Docs] fix header parsing; remove testling
  • [Deps] update object-keys
  • [meta] use prepublishOnly script for npm 7+
  • [meta] add funding field; create FUNDING.yml
  • [actions] add "Allow Edits" workflow; automatic rebasing / merge commit blocking
  • [actions] reuse common workflows
  • [actions] update codecov uploader
  • [actions] use node/install instead of node/run; use codecov action
  • [Tests] migrate tests to Github Actions
  • [Tests] run nyc on all tests; use tape runner
  • [Tests] use shared travis-ci config
  • [Tests] use npx aud instead of nsp or npm audit with hoops
  • [Tests] remove jscs
  • [Dev Deps] update eslint, @ljharb/eslint-config, safe-publish-latest, tape; add aud, safe-publish-latest

1.1.3 / 2018-08-14

  • [Refactor] use a for loop instead of foreach to make for smaller bundle sizes
  • [Robustness] cache Array.prototype.concat and Object.defineProperty
  • [Deps] update object-keys
  • [Dev Deps] update eslint, @ljharb/eslint-config, nsp, tape, jscs; remove unused eccheck script + dep
  • [Tests] use pretest/posttest for linting/security
  • [Tests] fix npm upgrades on older nodes

1.1.2 / 2015-10-14

  • [Docs] Switch from vb.teelaun.ch to versionbadg.es for the npm version badge SVG
  • [Deps] Update object-keys
  • [Dev Deps] update jscs, tape, eslint, @ljharb/eslint-config, nsp
  • [Tests] up to io.js v3.3, node v4.2

1.1.1 / 2015-07-21

  • [Deps] Update object-keys
  • [Dev Deps] Update tape, eslint
  • [Tests] Test on io.js v2.4

1.1.0 / 2015-07-01

  • [New] Add support for symbol-valued properties.
  • [Dev Deps] Update nsp, eslint
  • [Tests] Test up to io.js v2.3

1.0.3 / 2015-05-30

  • Using a more reliable check for supported property descriptors.

1.0.2 / 2015-05-23

  • Test up to io.js v2.0
  • Update tape, jscs, nsp, eslint, object-keys, editorconfig-tools, covert

1.0.1 / 2015-01-06

  • Update object-keys to fix ES3 support

1.0.0 / 2015-01-04

  • v1.0.0

Readme

Source

define-properties Version Badge

github actions coverage dependency status dev dependency status License Downloads

npm badge

Define multiple non-enumerable properties at once. Uses Object.defineProperty when available; falls back to standard assignment in older engines. Existing properties are not overridden. Accepts a map of property names to a predicate that, when true, force-overrides.

Example

var define = require('define-properties');
var assert = require('assert');

var obj = define({ a: 1, b: 2 }, {
	a: 10,
	b: 20,
	c: 30
});
assert(obj.a === 1);
assert(obj.b === 2);
assert(obj.c === 30);
if (define.supportsDescriptors) {
	assert.deepEqual(Object.keys(obj), ['a', 'b']);
	assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'c'), {
		configurable: true,
		enumerable: false,
		value: 30,
		writable: false
	});
}

Then, with predicates:

var define = require('define-properties');
var assert = require('assert');

var obj = define({ a: 1, b: 2, c: 3 }, {
	a: 10,
	b: 20,
	c: 30
}, {
	a: function () { return false; },
	b: function () { return true; }
});
assert(obj.a === 1);
assert(obj.b === 20);
assert(obj.c === 3);
if (define.supportsDescriptors) {
	assert.deepEqual(Object.keys(obj), ['a', 'c']);
	assert.deepEqual(Object.getOwnPropertyDescriptor(obj, 'b'), {
		configurable: true,
		enumerable: false,
		value: 20,
		writable: false
	});
}

Tests

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

Keywords

FAQs

Last updated on 10 Feb 2023

Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the 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