Created
September 9, 2015 10:58
-
-
Save postspectacular/051a325685a90984eb71 to your computer and use it in GitHub Desktop.
Parse accept header into prioritized seq
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[thi.ng.strf.core :as strf]) | |
(require '[clojure.string :as str]) | |
(defn parse-accept-header | |
[accept] | |
(->> accept | |
(re-seq #"((([\w\*\-]+/[\w\*\-\+]+,?)+)(;q=(\d+\.\d+))?)") | |
(mapcat | |
(fn [[_ _ a _ _ q]] | |
(let [q (if q (strf/parse-float q 0) 1)] | |
(map vector (str/split a #",") (repeat q))))) | |
(sort-by peek) | |
reverse)) | |
(parse-accept-header | |
"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8,application/json") | |
;; (["application/json" 1] ["application/xml" 0.9] ["application/xhtml+xml" 0.9] ["text/html" 0.9] ["*/*" 0.8]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment