option-defaults
Build a configuration object by filling in missing values, keeping known keys, and converting or validating supplied values. The same rules can apply at nested levels.
Installation
npm install @rsh-codedev/defaultsBasic usage
ES modules:
import { defaults } from "@rsh-codedev/defaults";
const options = defaults({ port: 80, extra: true }, { port: 8080 });
console.log(options); // { port: 80 }CommonJS:
const { defaults } = require("@rsh-codedev/defaults");
const options = defaults({ port: 80, extra: true }, { port: 8080 });
console.log(options); // { port: 80 }Nested options with conversion and validation
import { defaults } from "@rsh-codedev/defaults";
const fallback = { server: { host: "localhost", port: 8080 } };
const rules = {
server: {
port: {
__DigestFn: Number,
__ConditionFn: (port) => Number.isInteger(port) && port >= 1 && port <= 65535,
},
},
};
console.log(defaults({ server: { port: "3000", extra: true } }, fallback, rules));
// { server: { host: 'localhost', port: 3000 } }
console.log(defaults({ server: { port: "70000" } }, fallback, rules));
// { server: { host: 'localhost', port: 8080 } }Keep extra keys for a plugin
import { defaults } from "@rsh-codedev/defaults";
const supplied = { enabled: false, tags: ["preview"], label: "" };
const fallback = { enabled: true, tags: ["stable"] };
const settings = { globalRestricted: false };
console.log(defaults(supplied, fallback, {}, settings));
// { enabled: false, tags: [ 'preview' ], label: '' }API
defaults(options, defaultOptions, optionManipulations, settings)
| Argument | Default | Purpose |
|---|---|---|
options |
{} |
Caller-supplied option values. |
defaultOptions |
{} |
Fallback values and the allowed keys at each restricted level. |
optionManipulations |
{} |
Rules arranged by option key. Rules can contain nested rules, callbacks, and overrides. |
settings |
{} |
Global defaults for globalDigestState (beforeCondition), canGoDeeper (true), and globalRestricted (true). |
| Supplied value | Behavior |
|---|---|
Missing or undefined |
Uses the fallback without calling either callback. An undefined fallback becomes null. |
null, false, 0, or an empty string |
Treats it as a supplied value and applies any callbacks. |
| Array | Treats the array as one value. An unchanged array retains its reference. |
| Object with a matching object fallback | Processes nested keys by default. Disable this globally with canGoDeeper: false or for one key with __Deeper: false. |
- Set
globalRestricted: falseto retain additional enumerable string keys, including inherited enumerable keys. Set__Restrictedat the root or on a nested rule to override restriction at that level. - The function creates a result object and leaves input objects and the settings object untouched. Retained values can share references with the inputs.
Callback order
Set __DigestState on a rule to override settings.globalDigestState.
| Mode | Order | Result when accepted |
|---|---|---|
beforeCondition |
Digest, then validate the converted value. | Converted value |
afterCondition |
Validate the supplied value, then digest on acceptance. | Converted value |
onlyForCondition |
Digest, then validate the converted value. | Supplied value |
A rejected condition selects the fallback. The package exports the TypeScript types GlobalOptions, Manipulation, DigestState, DigestFn, and ConditionFn.
Development
corepack pnpm install --frozen-lockfile
corepack pnpm build
corepack pnpm test
npm run test:packThe pack test installs the tarball in a temporary consumer and checks ES modules, CommonJS, and strict TypeScript. The examples and tests use only this package and Node.js.
License
MIT. See LICENSE.