flextag-pattern
v0.1.0
Published
Basic tools to match & fill flextag patterns
Downloads
1
Readme
flextag-pattern
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
matcha 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.