Skip to content

Instantly share code, notes, and snippets.

@cemerick
Last active June 18, 2016 01:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cemerick/fd824dea77c9736225531a5cc97affee to your computer and use it in GitHub Desktop.
Save cemerick/fd824dea77c9736225531a5cc97affee to your computer and use it in GitHub Desktop.
(def percentage-spec (s/conformer #(or (some-> (re-matches #"(-?\d+(\.\d+)?)%" %) (nth 1) Double/parseDouble)
::s/invalid)
#(str % "%")))
(s/def ::absolute-coordinate number?)
(s/def ::pct-coordinate percentage-spec)
(s/def ::coordinate (s/or :abs ::absolute-coordinate
:pct ::pct-coordinate))
(def specrec (s/cat :lx ::coordinate
:by ::coordinate
:rx ::coordinate
:ty ::coordinate))
pdfdata.ops> (s/conform ::coordinate 123)
[:abs 123]
pdfdata.ops> (s/unform ::coordinate *1)
123
pdfdata.ops> (s/conform ::coordinate "-50.2%")
[:pct -50.2]
pdfdata.ops> (s/unform ::coordinate *1)
-50.2%
pdfdata.ops> (s/conform specrec [12 "-50.2%" -99.1 "2%"])
{:lx [:abs 12], :by [:pct -50.2], :rx [:abs -99.1], :ty [:pct 2.0]}
pdfdata.ops> (s/unform specrec *1)
(12 "-50.2%" -99.1 "2%")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment