@endo/patterns
The main export from the package is an M
namespace object, for making a variety of Matchers (hence "M"). For example:
import '@endo/init/debug.js';
import { M, mustMatch } from '@endo/patterns';
const specimen = harden({ foo: 3, bar: 4 });
const pattern = M.splitRecord(
{ foo: M.number() }, // required properties
{ bar: M.string(), baz: M.number() }, // optional
);
mustMatch(specimen, pattern); // throws: 'bar?: number 4 - Must be a string'
See PatternMatchers for more on M.splitRecord()
, M.number()
, and other methods.
M
also has GuardMakers methods to make InterfaceGuards that use Patterns to characterize dynamic behavior such as method argument/response signatures and promise awaiting. The @endo/exo! package uses InterfaceGuard
s as the first level of defense for Exo objects against malformed input.
For best rendering, use the Endo reference docs site.
Builds on @endo/pass-style! as described in kindOf
and passStyleOf
levels of abstraction to define higher level data types as individual refinements of Passable CopyTagged records (PassStyle "tagged"):
In support of the above, there is also compareKeys and keyEQ exposing pass-invariant Key comparison, and two concepts with corresponding TypeScript types:
r
may be a child of multiple parents, e.g. { foo: r, bar: [r] }
). A Key is stable and stably comparable with other Keys via keyEQ. Key is the most general data type covering valid contents for CopySets and CopyBags and keys for CopyMaps (the last of which explains the "Key" name).keyEQ
considers identity only for Remotables, where it is shared across all local Presences of the same Remotable), or a Key-like structure in which one or more leaves is a Matcher rather than a primitive or Remotable.