Utilities for transforming raw input into safe and well-typed values and objects.
npm install @vbarbarosh/type-helpers
describe
section in test a .test.js
file).const types = {
tab: {
name: 'str',
label: 'str',
active: 'bool',
disabled: 'bool',
},
tabs: {type: 'array', of: 'tab'}
};
const tabs = make('tabs', body?.card, types);
Creating basic types:
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
assert.strictEqual(make('int'), 0);
assert.strictEqual(make('int', -0), 0);
assert.strictEqual(make('int', '15.55'), 15);
assert.strictEqual(make('int', '15.999'), 15);
assert.strictEqual(make('float'), 0);
assert.strictEqual(make('float', -0), 0);
assert.strictEqual(make('float', '15.55'), 15.55);
assert.strictEqual(make('float', '15.999'), 15.999);
assert.strictEqual(make('bool', ''), false);
assert.strictEqual(make('bool', '1'), true);
assert.strictEqual(make('bool', 'x'), true);
assert.strictEqual(make('str', 1), '1');
assert.strictEqual(make('str', true), 'true');
assert.strictEqual(make('str', false), 'false');
Creating enum:
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
assert.strictEqual(make({type: 'enum', options: ['foo', 'bar', 'baz']}), 'foo');
assert.strictEqual(make({type: 'enum', options: ['foo', 'bar', 'baz']}, 'x'), 'foo');
assert.strictEqual(make({type: 'enum', options: ['foo', 'bar', 'baz']}, 'bar'), 'bar');
Creating uniform arrays (all values have the same type):
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
assert.deepStrictEqual(make({type: 'array', of: 'str'}), []);
assert.deepStrictEqual(make({type: 'array', of: 'str', min: 2}, 'x'), ['x', '']);
assert.deepStrictEqual(make({type: 'array', of: 'int', min: 2}, ['1']), [1, 0]);
Creating tuples (an array with fixed number of elements and predefined types):
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
assert.deepStrictEqual(make({type: 'tuple', items: ['str', 'str']}), ['', '']);
assert.deepStrictEqual(make({type: 'tuple', items: ['str', 'str']}, ['a']), ['a', '']);
Creating objects:
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
const types = {
rect: {
width: {type: 'int', min: 0},
height: {type: 'int', min: 0},
},
};
assert.deepStrictEqual(make('rect', null, types), {width: 0, height: 0});
assert.deepStrictEqual(make('rect', {}, types), {width: 0, height: 0});
assert.deepStrictEqual(make('rect', {width: -100}, types), {width: 0, height: 0});
assert.deepStrictEqual(make('rect', {width: 15, height: 25}, types), {width: 15, height: 25});
Creating object unions (an object which shape is determined by value from a property):
const assert = require('assert');
const make = require('@vbarbarosh/type-helpers/src/make');
const types = {
widget: {
type: 'union',
prop: 'kind',
default: 'text',
options: {
text: {
value: 'str',
},
number: {
value: 'float',
min: 'float',
max: 'float',
step: {type: 'float', min: 0.001, default: 1},
},
submit: {
label: 'str',
name: 'str',
value: 'str',
},
},
},
};
assert.deepStrictEqual(make('widget', null, types), {kind: 'text', value: ''});
assert.deepStrictEqual(make('widget', {kind: 'submit'}, types), {kind: 'submit', label: '', name: '', value: ''});
{type: 'raw', nullable: false, before: input => input, after: out => out}
{type: 'any', default: undefined, nullable: false, before: input => input, after: out => out}
{type: 'null', nullable: false, before: input => input, after: out => out}
{type: 'const', value: 123, nullable: false, before: input => input, after: out => out}
{type: 'bool', default: false, nullable: false, before: input => input, after: out => out}
{type: 'int', min: 0, max: 100, default: 0, nullable: false, before: input => input, after: out => out}
{type: 'float', min: 0, max: 100, default: 0, nullable: false, before: input => input, after: out => out}
{type: 'str', default: 'foo', nullable: false, before: input => input, after: out => out}
{type: 'array', of: __type__, min: 0, nullable: false, before: input => input, after: out => out}
{type: 'tuple', items: [], nullable: false, before: input => input, after: out => out}
{type: 'enum', options: [], transform: v => v, nullable: false, before: input => input, after: out => out}
{type: 'tags', options: ['foo', 'bar', 'baz'], nullable: false, before: input => input, after: out => out}
{type: 'obj', props: {...}, transform: v => v, finish: v => v, nullable: false, before: input => input, after: out => out}
{type: 'union', prop: 'kind', options: {...}, nullable: false, before: input => input, after: out => out}