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

bind-defaults

Package Overview
Dependencies
Maintainers
0
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bind-defaults

A utility for enhancing functions by binding default values to their first parameter

  • 1.0.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source

bind-defaults

A utility for enhancing functions by binding default values to their first parameter. It's perfect for simplifying APIs, reducing boilerplate code, and ensuring consistent configurations across function calls.

Features

  • Default Binding: Bind default values to the first parameter of a function.
  • Option Merging: Automatically merge user-provided options with defaults using Object.assign.
  • Function Identity: Preserves the original function's name for better debugging.
  • Context preservation through proper this handling
  • TypeScript-Friendly: Built with TypeScript in mind, providing full type inference and safety.
  • Lightweight: Zero dependencies and minimal overhead.

Installation

npm install bind-defaults
# or
yarn add bind-defaults

API

bindDefaults<F>(originalFunction: F, defaults: FirstParameter<F>)
  • originalFunction: The function to which defaults will be bound.
  • defaults: An object containing default values for the first parameter of originalFunction.
  • Returns: A new function with the defaults bound to its first parameter.

Usage

import { bindDefaults } from 'bind-defaults';

// Example function
function createUser(options: {
  name: string;
  age?: number;
  isAdmin?: boolean;
}) {
  console.log('Creating user with options:', options);
}

// Bind defaults
const createUserWithDefaults = bindDefaults(createUser, {
  age: 25,
  isAdmin: false,
});

// Call the bound function
createUserWithDefaults({ name: 'Alice' });
// Output: Creating user with options: { name: 'Alice', age: 25, isAdmin: false }

Similar projects

  • https://github.com/sgtlambda/merge-bind
  • https://github.com/balderdashy/merge-context

License

MIT License (c) 2025

Keywords

FAQs

Package last updated on 16 Feb 2025

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