Liking cljdoc? Tell your friends :D

shape-shifter

Clojars Project

Transforms a string pattern into clojure spec.

Patterns

Inspired by grape pattern system.

Inspired by grasp spec utilization.

Wildcards

WildcardDefinition
$matches one element of any kind
$fnmatches one function element
$macro-keywordmatches one macro keyword element
$symbolmatches one symbol element
$stringmatches one string element
$setmatches one set element
$charmatches one char element
$keywordmatches one keyword element
$mapmatches one map element
$numbermatches one number element
$listmatches one list element
$vectormatches one vector element
$regexmatches one regex element

If an & is provided in the end of a wildcard it'll match 0 or n elements of the specified kind.

Examples

Simple form

(require '[shape-shifter.core :refer [pattern->spec]])
(def spec (pattern->spec "($& read-string $&)"))
(s/valid? spec '(-> "1" read-string inc)) => true

Nested forms

(require '[shape-shifter.core :refer [pattern->spec]])
(def spec (pattern->spec "(-> $string read-string (+ $number))"))
(s/valid? spec '(-> "1" read-string (+ 10))) => true

Expand patterns

(require '[shape-shifter.core :refer [pattern->spec *wildcards*]])

(def spec (binding [*wildcards* (merge *wildcards* {"$banana" #{"banana"}})]
                 (pattern->spec "$banana")))
(s/valid? spec "banana")

Regex evaluation

(require '[shape-shifter.core :refer [pattern->spec *wildcards*]])

(def spec (binding [*config* (assoc *config* :interpret-regex? true)]
                 (pattern->spec "#\"bana.*\" ")))
(s/valid? spec "banana")

Can you improve this documentation?Edit on GitHub

cljdoc is a website building & hosting documentation for Clojure/Script libraries

× close