ts-options-defaults
Advanced tools
Comparing version 0.0.0 to 0.0.1
@@ -30,2 +30,6 @@ "use strict"; | ||
sources.forEach(function (source) { | ||
// Ignore invalid source objects. | ||
if (!source || typeof source !== 'object') { | ||
return; | ||
} | ||
Object.entries(source).forEach(function (_a) { | ||
@@ -32,0 +36,0 @@ var _b = __read(_a, 2), key = _b[0], value = _b[1]; |
{ | ||
"name": "ts-options-defaults", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"license": "GPL-3.0", | ||
"author": "Artur Kurowski <radarsu@gmail.com>", | ||
"homepage": "https://github.com/radarsu/ts-options-defaults#readme", | ||
"main": "dist/index.js", | ||
@@ -7,0 +8,0 @@ "types": "dist/index.d.ts", |
# ts-options-defaults | ||
`Object.assign({}, defaults, options)` and equivalent with destructing `{...defaults, ...options}` come with a pitfall of creating only a _shallow copy_. Lodash \_.merge works on deep properties, but it merges arrays and that usually makes no sense in the context of default options. This package fixes that problem - **it merges objects deeply and overrides arrays**. Also result remains strongly typed. | ||
`Object.assign({}, defaults, options)` and equivalent with destructing `{...defaults, ...options}` come with a pitfall of creating only a _shallow copy_. Lodash `_.merge` works on deep properties, but it merges arrays and that usually makes no sense in the context of default options (also it mutates first element; this package doesn't). This package fixes that problem - **it merges objects deeply and overrides arrays**. Also result remains strongly typed. | ||
@@ -11,2 +11,4 @@ > Options-defaults design pattern implementation for reliable configuration. | ||
# Design pattern | ||
```ts | ||
@@ -30,1 +32,52 @@ import { defaults } from 'ts-options-defaults'; | ||
``` | ||
# Behavior | ||
```ts | ||
import { defaults } from 'ts-options-defaults'; | ||
const someDefaults = { | ||
some: { | ||
nested: { | ||
property: 'default', | ||
shouldStay: 'default', | ||
array: ['default1', 'default2'], | ||
}, | ||
}, | ||
array: ['default'], | ||
}; | ||
const someOptionsOne = { | ||
some: { | ||
nested: { | ||
property: 'overriden', | ||
array: ['overriden1'], | ||
}, | ||
}, | ||
array: ['overriden'], | ||
}; | ||
const someOptionsTwo = { | ||
justAddingThisOne: true, | ||
}; | ||
const options = defaults(someDefaults, someOptionsOne, someOptionsTwo); | ||
// options will be: | ||
// { | ||
// "some": { | ||
// "nested": { | ||
// "property": "overriden", | ||
// "shouldStay": "default", | ||
// "array": [ | ||
// "overriden1" | ||
// ] | ||
// } | ||
// }, | ||
// "array": [ | ||
// "overriden" | ||
// ], | ||
// "justAddingThisOne": true | ||
// } | ||
// | ||
// someDefaults will not be mutated! | ||
``` |
export const merge = (object: any, ...sources: any[]) => { | ||
sources.forEach((source) => { | ||
// Ignore invalid source objects. | ||
if (!source || typeof source !== 'object') { | ||
return; | ||
} | ||
Object.entries(source).forEach(([key, value]) => { | ||
@@ -4,0 +9,0 @@ // Handle simple types. |
@@ -11,3 +11,3 @@ { | ||
}, | ||
"exclude": ["node_modules", "dist", "scripts", "scripts.ts"] | ||
"exclude": ["node_modules", "dist", "scripts", "scripts.ts", "tests"] | ||
} |
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
9288
12
213
1
82