@produck/es-abstract-member
v0.4.1
Published
Ready-made parsers for declaring the members of an abstract constructor.
Readme
@produck/es-abstract-member
Ready-made parsers for declaring the members of an abstract constructor built
with @produck/es-abstract-token.
Installation
npm install @produck/es-abstract-memberUsage
import Abstract from '@produck/es-abstract-token';
import * as _ from '@produck/es-abstract-member';
const AbstractSample = Abstract(
class Sample {},
...[
Abstract({
foo: _.String,
count: _.Number,
createdAt: _.Instance(Date),
check: _.Method().args(_.Number).returns(_.Boolean),
pending: _.OrPromise(_.Number),
}),
Abstract.Static('qux', _.Boolean),
],
);
class SubSample extends AbstractSample {
foo = 'implemented';
count = 1;
createdAt = new Date();
pending = Promise.resolve(1);
static qux = true;
check(value) {
return value > 10;
}
}
const sample = new SubSample();
sample.foo; // 'implemented'
sample.check(25); // true
await sample.pending; // 1API
Primitives
Each of them asserts the type of the member value.
Undefined— Asserts value isundefined.Object— Asserts value is of typeobject(includingnull).Boolean— Asserts value is of typeboolean.Number— Asserts value is of typenumber.BigInt— Asserts value is of typebigint.String— Asserts value is of typestring.Symbol— Asserts value is of typesymbol.Function— Asserts value is of typefunction.
Instance(constructor)
Asserts value is an instance of constructor.
_.Instance(Date);Method()
Creates a member parser for a function member, with chainable operators:
.args(...parsers)— a parser for each positional argument..rest(parser)— the parser applied to every extra argument..returns(parser)— the parser applied to the return value.
Each operator may be called once, and none of them may be called after the member has been used.
_.Method().args(_.Number).returns(_.Boolean);Promise(parser?) / OrPromise(parser?)
Promise(parser?)— asserts the value is aPromise, then appliesparserto the resolved value.OrPromise(parser?)— accepts aPromiseor a plain value, and appliesparseraccordingly.
PromiseLike(parser?) / OrPromiseLike(parser?)
The same as Promise / OrPromise, but accepting any thenable instead of a
Promise instance.
Iterable / AsyncIterable
Iterable— asserts the value is a synchronous iterable (Symbol.iterator) and returns it unchanged.AsyncIterable— asserts the value is an async iterable (Symbol.asyncIterator) and returns it unchanged.
Strings are iterable, so _.Iterable('ab') passes; a bare next object does
not.
Iterator / WellFormedIterator
Iterator— asserts the value exposes a callablenext, and returns it unchanged.WellFormedIterator— additionally requiresSymbol.iteratorto return the iterator itself, which is whatfor...ofdemands.
An async generator is an Iterator but not a WellFormedIterator.
Generator / AsyncGenerator
Generator— asserts the value is a generator function (function*).AsyncGenerator— asserts the value is an async generator function (async function*).
Both check the function itself, not the iterator it produces.
Any / Unknown
A parser that passes any value through without validation. It is also the default parser when a declared member has none.
License
MIT
Author
Produck
