js.spec icon indicating copy to clipboard operation
js.spec copied to clipboard

Regex operators for sequences

Open prayerslayer opened this issue 9 years ago • 2 comments

From clojure.spec, but we need to rename some as ?, + and * are not valid in javascript identifiers.

  • cat
  • alt
  • ? => qm? atMostOne?
  • + => plus? atLeastOne?
  • * => star? any?

So that we could

import {define, spec} from 'js.spec'

define("ingredient", spec.cat(
  "quantity", spec.number,
  "unit", spec.string))

define("odds-then-maybe-even", spec.cat(
  "odds", spec.plus(spec.odd),
  "even", spec.star(spec.even)))

Check to what extent we can leverage pamatcher.

prayerslayer avatar Nov 30 '16 18:11 prayerslayer

I like the idea of naming them after what they do rather than describing the symbol, even though the symbol names are shorter.

One interesting thought is that all three of those quantifier (which is what the Perl docs call them) operators are really shorthand for {n,m}: ? === {0,1} * === {0,} + === {1,}. I wonder if we just use a single quantify or repeat method that takes three parameters (low, high, spec)? This is similar to the linked pamatcher's repeat.

scotttrinh avatar Dec 02 '16 12:12 scotttrinh

I wonder if we just use a single quantify or repeat method that takes three parameters (low, high, spec)? This is similar to the linked pamatcher's repeat.

I think that's a good idea. We can still expose this function bound to predefined values for +, ?, *.

prayerslayer avatar Dec 02 '16 15:12 prayerslayer