# d

> Property descriptor factory

Latest version **1.0.2** (published 2024-03-01) · ISC license · 0 weekly downloads

## Install

```sh
npm install d
pnpm add d
yarn add d
bun add d
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.2 |
| Published | 2024-03-01 |
| First published | 2013-06-20 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | separate (@types/d) |
| Module format | CommonJS |
| Node | >=0.12 |
| Dependencies | 2 |
| Unpacked size | 13.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 44 |
| Author | Mariusz Nowak |
| Maintainers | medikoo |
| Keywords | descriptor, es, ecmascript, ecma, property, descriptors, meta, properties |

## Links

- npm: https://www.npmjs.com/package/d
- Repository: https://github.com/medikoo/d
- Homepage: https://github.com/medikoo/d#readme
- Issues: https://github.com/medikoo/d/issues
- npm.io page: https://npm.io/package/d

## Dependencies (2)

- [type](https://npm.io/package/type.md) ^2.7.2
- [es5-ext](https://npm.io/package/es5-ext.md) ^0.10.64

## Alternatives

- [duck](https://npm.io/package/duck.md) — 4.2M weekly downloads
- [ava](https://npm.io/package/ava.md) — 560.2K weekly downloads
- [storybook-addon-module-mock](https://npm.io/package/storybook-addon-module-mock.md) — 71.7K weekly downloads
- [vest](https://npm.io/package/vest.md) — 50.1K weekly downloads
- [@ethereum-waffle/mock-contract](https://npm.io/package/@ethereum-waffle/mock-contract.md) — 40.0K weekly downloads

## Recent versions

- 1.0.2 (latest) — 2024-03-01
- 1.0.1 — 2019-06-14
- 1.0.0 — 2015-12-04
- 0.1.1 — 2014-04-24
- 0.1.0 — 2013-06-20

## README

[![Build status][build-image]][build-url]
[![Tests coverage][cov-image]][cov-url]
[![npm version][npm-image]][npm-url]

# d

## Property descriptor factory

_Originally derived from [d](https://github.com/medikoo/d) package._

Defining properties with descriptors is very verbose:

```javascript
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:

```javascript
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:

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

You can overwrite it by preceding _value_ argument with instruction:

```javascript
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](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/)

### Other utilities

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

Define methods which will be automatically bound to its instances

```javascript
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

```javascript
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

    $ npm test

## Security contact information

To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.

---

<div align="center">
	<b>
		<a href="https://tidelift.com/subscription/pkg/npm-d?utm_source=npm-d&utm_medium=referral&utm_campaign=readme">Get professional support for d with a Tidelift subscription</a>
	</b>
	<br>
	<sub>
		Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
	</sub>
</div>

[build-image]: https://github.com/medikoo/d/workflows/Integrate/badge.svg
[build-url]: https://github.com/medikoo/d/actions?query=workflow%3AIntegrate
[cov-image]: https://img.shields.io/codecov/c/github/medikoo/d.svg
[cov-url]: https://codecov.io/gh/medikoo/d
[npm-image]: https://img.shields.io/npm/v/d.svg
[npm-url]: https://www.npmjs.com/package/d

---
_Source: https://npm.io/package/d · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
