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.3-1 to 0.0.3-2

8

dist/index.js

@@ -34,2 +34,10 @@ "use strict";

}
// Handle classes and merge their functions.
var prototype = Object.getPrototypeOf(source);
Object.entries(prototype).forEach(function (_a) {
var _b = __read(_a, 2), key = _b[0], value = _b[1];
if (typeof value === 'function') {
object[key] = value;
}
});
Object.entries(source).forEach(function (_a) {

@@ -36,0 +44,0 @@ var _b = __read(_a, 2), key = _b[0], value = _b[1];

2

package.json
{
"name": "ts-options-defaults",
"version": "0.0.3-1",
"version": "0.0.3-2",
"license": "GPL-3.0",

@@ -5,0 +5,0 @@ "author": "Artur Kurowski <radarsu@gmail.com>",

# 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 (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.
`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, overrides arrays and merges class functions** plus the result remains strongly typed.

@@ -18,3 +18,2 @@ > Options-defaults design pattern implementation for reliable configuration.

logger?: Partial<Console>;
path: string;
}

@@ -28,3 +27,3 @@

constructor(public options: IRatOptions) {
this.options = defaults({}, Rat.defaults, options);
this.options = defaults(Rat.defaults, options);
}

@@ -37,21 +36,25 @@ }

```ts
import { defaults } from 'ts-options-defaults';
import { defaults } from '../src';
class TestLogger {
constructor(public name = `TestLogger`) {}
log() {
console.log(`Call from ${this.name}`);
}
}
const someDefaults = {
some: {
nested: {
property: 'default',
shouldStay: 'default',
array: ['default1', 'default2'],
},
console,
nested: {
property: 'default',
shouldBeDefault: 'default',
array: ['default1', 'default2'],
},
array: ['default'],
};
const someOptionsOne = {
some: {
nested: {
property: 'overriden',
array: ['overriden1'],
},
const someOptions = {
nested: {
property: 'overriden',
array: ['overriden1'],
},

@@ -61,25 +64,35 @@ array: ['overriden'],

const someOptionsTwo = {
justAddingThisOne: true,
};
const options = defaults(
someDefaults,
someOptions,
{
console: {
log: () => {
console.log(`TEST`);
},
},
},
{
console: new TestLogger(),
},
);
const options = defaults(someDefaults, someOptionsOne, someOptionsTwo);
options.console.log(`log`); // "Call from TestLogger"
options.console.debug(`debug`); // "debug"
// options will be:
// {
// "some": {
// "nested": {
// "property": "overriden",
// "shouldStay": "default",
// "array": [
// "overriden1"
// ]
// }
// },
// "array": [
// "overriden"
// ],
// "justAddingThisOne": true
// }
//
{
"nested": {
"property": "overriden",
"shouldBeDefault": "default",
"array": [
"overriden1"
]
},
"array": [
"overriden"
]
}
// someDefaults will not be mutated!
```

@@ -8,2 +8,10 @@ export const merge = (object: any, ...sources: any[]) => {

// Handle classes and merge their functions.
const prototype = Object.getPrototypeOf(source);
Object.entries(prototype).forEach(([key, value]) => {
if (typeof value === 'function') {
object[key] = value;
}
});
Object.entries(source).forEach(([key, value]) => {

@@ -10,0 +18,0 @@ // Handle simple types.

import { defaults } from '../src';
class TestLogger {
constructor(public name = `TestLogger`) {}
log() {
console.log(`Call from ${this.name}`);
}
}
const someDefaults = {
console,
some: {
nested: {
property: 'default',
shouldStay: 'default',
array: ['default1', 'default2'],
},
nested: {
property: 'default',
shouldBeDefault: 'default',
array: ['default1', 'default2'],
},
array: ['default'],
};
const someOptionsOne = {
some: {
nested: {
property: 'overriden',
array: ['overriden1'],
},
const someOptions = {
nested: {
property: 'overriden',
array: ['overriden1'],
},

@@ -25,13 +28,16 @@ array: ['overriden'],

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

@@ -38,0 +44,0 @@ options.console.log(`log`);

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