New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

ts-options-defaults

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ts-options-defaults - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

dist/src/index.d.ts

4

dist/index.js

@@ -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];

3

package.json
{
"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"]
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc