kuvio
v2.0.0
Published
Create string patterns and derive things from them, such as regexes
Downloads
2,684
Maintainers
Readme
kuvio
kuvio
is a tool to construct composable string patterns, from which you can
derive things like regular expressions or fast-check Arbitrary
s. As of
v1.4.0, it requires no dependencies.
kuvio
is specifically for string-like patterns. If you want to extend this
concept to more complicated data types, check out schemata-ts! kuvio
was
originally developed as part of schemata-ts
but was extracted as it seems
useful independently.
Usage
kuvio
comes with several useful patterns built-in, but you can also create
your own.
import * as k from 'kuvio';
// Use built-in patterns
const creditCardRegex = k.regexFromPattern(k.patterns.creditCard);
// Create your own patterns.
const areaCode = k.exactly(3)(k.digit)
const exchangeCode = k.exactly(3)(k.digit)
const lineNumber = k.exactly(4)(k.digit)
// Create pattern functions
const parenthesize = (p: k.Pattern) => k.subgroup(
k.sequence(k.char('('), p, k.char(')'))
)
// Compose patterns to make more complex patterns
const phoneNumberPattern = k.sequence(
parenthesize(areaCode),
k.char(' '),
k.subgroup(exchangeCode),
k.char('-'),
k.subgroup(lineNumber),
)
See the patterns directory for the built-in patterns, which can also be useful examples for creating your own.
Note regarding fast-check
usage
Arbitraries are intended primarily for use in test code; in order to
help keep fast-check
out of your production code, kuvio
does not include
fast-check
in the main export. If you want to use the Arbitrary
functions,
you'll need to import them separately from kuvio/arbitrary
.
kuvio
also exports a version of the Arbitrary
functions that take the
fast-check
library as an argument, to prevent any possible accidental
inclusion of fast-check
in production code. This shouldn't be an issue, but
there are many bundlers and we cannot test them all. You can import these from
kuvio/arbitrary-deferred
.