1.4.0 • Published 1 month ago

kuvio v1.4.0

Weekly downloads
-
License
MIT
Repository
-
Last release
1 month ago

kuvio

build Codacy
Badge codecov dependencies size downloads version

kuvio is a tool to construct composable string patterns, from which you can derive things like regular expressions or fast-check Arbitrarys. 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.

1.4.0

1 month ago

1.4.0-beta.1

3 months ago

1.3.2

3 months ago

1.3.1

10 months ago

1.3.0

11 months ago

1.2.1

11 months ago

1.2.0

11 months ago

1.1.1

11 months ago

1.1.0

1 year ago

1.0.0

1 year ago