For helping to create a final options from a defined specification.
npm install @produck/mold
$ npm install @produck/mold
`
Examples
The "options" in fs.appendFile(path, data[, options], callback),
`js
import { S, C, P, Normalizer } from '@produck/mold';
const flags = [
'a', 'ax', 'a+', 'ax+', 'as', 'as+',
'r', 'r+', 'rs+',
'w', 'wx', 'w+', 'wx+'
];
const schema = S.Object({
encoding: P.OrNull(P.String('utf-8')),
mode: P.Integer(0o666),
flag: P.Enum(flags);
});
const normalize = Normalizer(schema);
normalize(); // ok
normalize({ encoding: 'utf-8' }); // ok
normalize({ encoding: 123 }); // throws
normalize({ flag: 'f' }); // throws
`
Usage
$3
As esModule,
`js
import {
Simplex, Simp, S,
Compound, Comp, C,
Preset, Pre, P,
Normalizer, Circular, Custom,
Catcher
} from '@produck/mold';
// Or
import * as Mold from '@produck/mold';
// Your code...
`
As CommonJS,
`js
const {
Simplex, Simp, S,
Compound, Comp, C,
Preset, Pre, P,
Normalizer, Circular, Custom,
Catcher
} = require('@produck/mold');
// Or
const Mold = require('@produck/mold');
// Your code...
`
$3
`js
import { S, P, Normalizer } from '@produck/mold';
const OptionsSchema = S.Object({
foo: P.String('bar')
});
const normalize = Normalizer(OptionsSchema);
const finalOptions = normalize();
`
$3
`js
import { Simplex } from '@produck/mold';
import net from 'node:net';
// A optional schema.
const IPv4 = Simplex.Value(net.isIPv4, 'IP string', () => '0.0.0.0');
// A required schmea
const IPv4 = Simplex.Value(net.isIPv4, 'IP string');
`
$3
$3
API by Levels
$3
`js
const l0 = (_value, _empty) => {
// Some statement...
}
`
$3
`js
import { S, Simp, Simplex } from '@produck/mold';
Simplex.Value();
Simplex.Object();
Simplex.Array();
Simplex.Tuple();
`
$3
`js
import { C, Comp, Compound } from '@produck/mold';
Compound.Not();
Compound.And();
Compound.Or();
Compound.If();
`
$3
`js
import { Circular, Custom } from '@produck/mold';
Circular();
Custom();
`
$3
`js
import { P, Pre, Preset } from '@produck/mold';
Preset.Constant();
Preset.Enum();
Preset.Null;
Preset.NotNull;
Preset.Instance();
Preset.OrNull();
Preset.Number();
...
``