# options-defaults

> Options-defaults design pattern implementation for reliable configuration. It merges objects deeply, overrides arrays and classes (different than Object) and the result remains strongly typed.

Latest version **2.0.40** (published 2022-06-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install options-defaults
pnpm add options-defaults
yarn add options-defaults
bun add options-defaults
```

## Health

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

Positive: has types; esm support; no vulnerabilities.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.40 |
| Published | 2022-06-17 |
| First published | 2020-09-18 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 24.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Artur Kurowski |
| Maintainers | radarsu |
| Keywords | defaults, design pattern, merge, options |

## Links

- npm: https://www.npmjs.com/package/options-defaults
- Homepage: https://github.com/radarsu/options-defaults#readme
- npm.io page: https://npm.io/package/options-defaults

## Recent versions

- 2.0.40 (latest) — 2022-06-17
- 2.0.39 — 2021-04-09
- 2.0.33 — 2021-04-09
- 2.0.32 — 2021-03-22
- 2.0.31 — 2021-03-17
- 2.0.30 — 2021-03-17
- 2.0.29 — 2021-03-16
- 2.0.28 — 2021-01-11
- 2.0.27 — 2021-01-11
- 2.0.25 — 2021-01-08
- 2.0.24 — 2020-12-21
- 2.0.22 — 2020-12-03
- 2.0.21 — 2020-11-25
- 2.0.18 — 2020-11-23
- 2.0.17 — 2020-11-17
- … 5 more at https://npm.io/package/options-defaults/versions

## README

<p align="center">
    <h1>ts-options-defaults</h1>
    <div>Options-defaults design pattern implementation for reliable configuration. It merges objects deeply, overrides arrays and classes (different than Object) and the result remains strongly typed.</div>
</p>

## Table of contents

1. [Getting Started](#getting-started)

2. [Usage](#usage)

3. [Features](#features)

## Getting Started

`npm i ts-options-defaults`

## Usage

### Design pattern

```ts
import { defaults } from 'ts-options-defaults';

export interface ISomeOptions {
    logger?: Partial<Console>;
}

export class Something {
    static defaults = {
        logger: console,
    };

    options: ISomeOptions & typeof Something.defaults;
    constructor(options?: ISomeOptions) {
        this.options = defaults(Rat.defaults, options);
    }
}
```

### Behavior

```ts
import { defaults } from 'ts-options-defaults';

class TestLogger {
    constructor(public name = `TestLogger`) {}

    log() {
        console.log(`Call from ${this.name}`);
    }
}

const someDefaults = {
    console,
    nested: {
        property: 'default',
        shouldBeDefault: 'default',
        array: ['default1', 'default2'],
    },
};

const someOptions = {
    nested: {
        property: 'overriden',
        array: ['overriden1'],
    },
    array: ['overriden'],
};

const options = defaults(
    someDefaults,
    someOptions,
    {
        console: {
            log: () => {
                console.log(`TEST`);
            },
        },
    },
    {
        console: new TestLogger(),
    },
);

options.console.log(`log`); // "Call from TestLogger"
options.console.debug(`debug`); // "debug"

// options will be:
{
    "nested": {
        "property": "overriden",
        "shouldBeDefault": "default",
        "array": [
            "overriden1"
        ]
    },
    "array": [
        "overriden"
    ]
}

// someDefaults will not be mutated!
```

## Features

**Beats alternatives** - better alternative to `{...defaults, ...options}` destructing and lodash `_.defaults` or `_.merge`
**Secure** - immune to prototype pollution attack
**Simple** - just 40 lines of clean TypeScript code
**Strongly typed** - result remains strongly typed

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