0.1.0 • Published 5 years ago

flextag-pattern v0.1.0

Weekly downloads
2
License
MIT
Repository
github
Last release
5 years ago

flextag-pattern

NPM version

Basic tools to match & fill flextag patterns

Pattern.match

const { Pattern } = new require('flextag-pattern')

const pat = new Pattern('hello, ?thing.')
console.log(pat.match('Hello, World!'))
// => { thing: 'World' }

Pattern.fill

const { Pattern } = new require('flextag-pattern')

const pat = new Pattern('hello, ?thing.')
console.log(pat.fill({ thing: 'Universe' }))
// => hello, Universe

Conformant matching

Conforms to the anticipated draft spec for flextag pattern matching.

  • case insenstive matching
  • greedy matching of bare strings, reducing need for quoting, so pattern a ?x z match a 1 2 3 z will result in { x: '1 2 3' }
  • sequence marking the end of a greedy match is multiple tokens, some of which may be variables bound by earlier matches in that statement
  • TODO hidden variables
  • TODO alternative path syntax

Match Options

incomingBindings

You can pass in an object of var bindings, basically filling the variables before the match occurs. Match will fail if they align with some different value.

Reason for failure

You can pass a "reason" function. It will be called with an English explaination of where/why the match failed. If there are branches, it will be called once for each branch that fails, possibly resulting in many calls before branch matches and returns successfully.