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
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';
function createUser(options: {
name: string;
age?: number;
isAdmin?: boolean;
}) {
console.log('Creating user with options:', options);
}
const createUserWithDefaults = bindDefaults(createUser, {
age: 25,
isAdmin: false,
});
createUserWithDefaults({ name: 'Alice' });
Similar projects
License
MIT License (c) 2025