
TypeScript Options-object "Currying"
Curry a function's default options.
Since working with TypeScript I've moved to mostly having functions that only accepts 1 options argument as then the args get named, order of args doesn't matter, and you can add new options without breaking the API.
Sometimes it's nice to compose a new function with default values set. This lib does that.
How to use
npm i options-curry --save
yarn add options-curry
Examples
Happy path
import { withDefaults } from 'options-curry';
function fn({ foo, bar }: { foo: string; bar: string }) {
return { foo, bar };
}
const fnWithDefaults = withDefaults(fn, {
foo: 'foo',
});
console.log(fnWithDefaults({ bar: 'bar' }));
console.log(fnWithDefaults({ bar: 'bar', foo: 'x' }));
const composedFn = withDefaults(fnWithDefaults, { bar: 'bar' });
console.log(composedFn({}));
Type safety showcase
import { withDefaults } from 'options-curry';
function fn({ foo, bar }: { foo: string; bar: string }) {
return { foo, bar };
}
const fnWithDefaults = withDefaults(fn, {
foo: 'foo',
});
fnWithDefaults({
bar: 1,
});
const ret = fnWithDefaults({ bar: 'x' });
console.log(ret.nope);
Local Development
(This project was bootstrapped with TSDX.)
Below is a list of commands you will probably find useful.
yarn start
Runs the project in development/watch mode. Your project will be rebuilt upon changes. TSDX has a special logger for you convenience. Error messages are pretty printed and formatted for compatibility VS Code's Problems tab.
yarn build
Bundles the package to the dist folder.
The package is optimized and bundled with Rollup into multiple formats (CommonJS, UMD, and ES Module).
yarn test
Runs the test watcher (Jest) in an interactive mode.
By default, runs tests related to files changed since the last commit.