Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

reduce-configs

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

reduce-configs

Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
50K
decreased by-4.71%
Maintainers
0
Weekly downloads
 
Created
Source

reduce-configs

Merge an initial configuration object with one or more configuration objects, functions, or arrays of configuration objects/functions.

npm version license

Install

npm add reduce-configs -D

reduceConfigs

The reduceConfigs function merges one or more configuration objects into a final configuration. It also allows modification of the configuration object via functions.

  • Type:
type OneOrMany<T> = T | T[];
type ConfigChain<T> = OneOrMany<T | ((config: T) => T | void)>;

function reduceConfigs<T>(options: {
  /**
   * Initial configuration object.
   */
  initial: T;
  /**
   * The configuration object, function, or array of configuration objects/functions
   * to be merged into the initial configuration
   */
  config?: ConfigChain<T> | undefined;
  /**
   * The function used to merge configuration objects.
   * @default Object.assign
   */
  mergeFn?: typeof Object.assign;
}): T;
  • Example:
import { reduceConfigs } from "@rsbuild/core";

const initial = { a: 1, b: 2 };

// Merging an object
const finalConfig1 = reduceConfigs({
  initial: initial,
  config: { b: 3, c: 4 },
});
// -> { a: 1, b: 3, c: 4 }

// Using a function to modify the config
const finalConfig2 = reduceConfigs({
  initial: initial,
  config: (config) => ({ ...config, b: 5, d: 6 }),
});
// -> { a: 1, b: 5, d: 6 }

// Merging an array of objects/functions
const finalConfig3 = reduceConfigs({
  initial: initial,
  config: [
    { b: 7 },
    (config) => ({ ...config, c: 8 }),
    (config) => ({ ...config, d: 9 }),
  ],
});
// -> { a: 1, b: 7, c: 8, d: 9 }

reduceConfigsWithContext

The reduceConfigsWithContext function is similar to reduceConfigs, which allows you to pass an additional context object to the configuration function.

  • Type:
type OneOrMany<T> = T | T[];
type ConfigChainWithContext<T, Ctx> = OneOrMany<
  T | ((config: T, ctx: Ctx) => T | void)
>;

function reduceConfigsWithContext<T, Ctx>(options: {
  /**
   * Initial configuration object.
   */
  initial: T;
  /**
   * The configuration object, function, or array of configuration objects/functions
   * to be merged into the initial configuration
   */
  config?: ConfigChain<T> | undefined;
  /**
   * Context object that can be used within the configuration functions.
   */
  ctx?: Ctx;
  /**
   * The function used to merge configuration objects.
   * @default Object.assign
   */
  mergeFn?: typeof Object.assign;
}): T;
  • Example:
import { reduceConfigsWithContext } from "@rsbuild/core";

const initial = { a: 1, b: 2 };
const context = { user: "admin" };

const finalConfig = reduceConfigsWithContext({
  initial,
  config: [
    { b: 3 },
    (config, ctx) => ({ ...config, c: ctx.user === "admin" ? 99 : 4 }),
  ],
  ctx: context,
});
// -> { a: 1, b: 3, c: 99 }

License

MIT.

FAQs

Package last updated on 22 Nov 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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