Socket
Socket
Sign inDemoInstall

d

Package Overview
Dependencies
8
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

d

Property descriptor factory


Version published
Maintainers
1
Weekly downloads
9,297,801
decreased by-8.15%

Weekly downloads

Package description

What is d?

The 'd' npm package is a utility for defining property descriptors for JavaScript objects. It is often used to create properties with specific characteristics such as being non-enumerable, writable, or configurable, and can be used in object property definition contexts where you want more control over how properties behave.

What are d's main functionalities?

Defining non-enumerable properties

This code sample demonstrates how to define a non-enumerable property 'foo' with the value 'bar'. The property will not show up in for-in loops or Object.keys() calls.

{"foo": d('bar')}

Defining writable and configurable properties

This code sample shows how to define a property 'foo' that is both writable and configurable with the value 'bar'. This means the property value can be changed and the property descriptor can be modified after the initial definition.

{"foo": d(true, 'bar')}

Defining a getter and setter

This code sample illustrates defining a property 'foo' with a getter and setter function. The getter function returns the value of '_foo', and the setter function assigns the new value to '_foo'.

{"foo": d.gs(function() { return this._foo; }, function(value) { this._foo = value; })}

Other packages similar to d

Readme

Source

D

Property descriptor factory

Originally derived from es5-ext package.

Defining properties with descriptors is very verbose:

var Account = function () {};
Object.defineProperties(Account.prototype, {
  deposit: {
    value: function () {
      /* ... */
    },
    configurable: true,
    enumerable: false,
    writable: true
  },
  withdraw: {
    value: function () {
      /* ... */
    },
    configurable: true,
    enumerable: false,
    writable: true
  },
  balance: {
    get: function () {
      /* ... */
    },
    configurable: true,
    enumerable: false
  }
});

D cuts that to:

var d = require("d");

var Account = function () {};
Object.defineProperties(Account.prototype, {
  deposit: d(function () {
    /* ... */
  }),
  withdraw: d(function () {
    /* ... */
  }),
  balance: d.gs(function () {
    /* ... */
  })
});

By default, created descriptor follow characteristics of native ES5 properties, and defines values as:

{ configurable: true, enumerable: false, writable: true }

You can overwrite it by preceding value argument with instruction:

d("c", value); // { configurable: true, enumerable: false, writable: false }
d("ce", value); // { configurable: true, enumerable: true, writable: false }
d("e", value); // { configurable: false, enumerable: true, writable: false }

// Same way for get/set:
d.gs("e", value); // { configurable: false, enumerable: true }

Installation

$ npm install d

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack

Other utilities

autoBind(obj, props) (d/auto-bind)

Define methods which will be automatically bound to its instances

var d = require('d');
var autoBind = require('d/auto-bind');

var Foo = function () { this._count = 0; };
Object.defineProperties(Foo.prototype, autoBind({
  increment: d(function () { ++this._count; });
}));

var foo = new Foo();

// Increment foo counter on each domEl click
domEl.addEventListener('click', foo.increment, false);
lazy(obj, props) (d/lazy)

Define lazy properties, which will be resolved on first access

var d = require("d");
var lazy = require("d/lazy");

var Foo = function () {};
Object.defineProperties(Foo.prototype, lazy({ items: d(function () { return []; }) }));

var foo = new Foo();
foo.items.push(1, 2); // foo.items array created and defined directly on foo

Tests Build Status

$ npm test

Security contact information

To report a security vulnerability, please use the Tidelift security contact. Tidelift will coordinate the fix and disclosure.


Get professional support for d with a Tidelift subscription
Tidelift helps make open source sustainable for maintainers while giving companies
assurances about security, maintenance, and licensing for their dependencies.

Keywords

FAQs

Last updated on 14 Jun 2019

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