Set of functions helpful for safe parsing. Safe parsing functions always return desired type or nil, or default value. They never throw error for any kind of input.
Add [district0x/district-parsers "1.0.0"]
into your project.clj
Include [district.parsers]
in your CLJS file
Parses int. If unparsable, returns nil or default.
(parsers/parse-int "1")
;; => 1
(parsers/parse-int "abc")
;; => nil
(parsers/parse-int "a" 1)
;; => 1
(parsers/parse-int [1])
;; => nil
Parses float. If unparsable, returns nil or default.
(parsers/parse-float "1.1")
;; => 1.1
(parsers/parse-float "1,1")
;; => 1.1
(parsers/parse-float "a" 1)
;; => 1
Parses keyword. If unparsable, returns nil or default.
(parsers/parse-keyword ":a")
;; => :a
(parsers/parse-keyword ":a/b")
;; => :a/b
(parsers/parse-keyword "a")
;; => :a
(parsers/parse-keyword "a/b")
;; => :a/b
Parses boolean string. If unparsable, returns nil or default.
(parsers/parse-boolean "true")
;; => true
(parsers/parse-boolean "FALSE")
;; => false
(parsers/parse-boolean "a")
;; => nil
Parses non empty string. If unparsable or empty string given, returns nil or default.
(parsers/parse-non-empty-str "")
;; => nil
(parsers/parse-non-empty-str [1])
;; => nil
(parsers/parse-non-empty-str "a")
;; => "a"
(parsers/parse-non-empty-str 1)
;; => "1"
Parses web3.js address and transforms it to lowercase. If invalid address is given, returns nil or default.
(parsers/parse-web3-address "0x0B73EEc3b1C5A2c555799B7FfC500082606DFf15")
;; => "0x0b73eec3b1c5a2c555799b7ffc500082606dff15"
(parsers/parse-web3-address "0x0")
;; => nil
Parses sequence of ints. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-int-seq ["1,1" "a"] 10)
;; => [1 10]
(parsers/parse-int-seq "1")
;; => [1]
Parses sequence of floats. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-float-seq ["1.1" "1,1"])
;; => [1.1 1.1]
Parses sequence of keywords. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-keyword-seq ["a" ":a"])
;; => [:a :a]
Parses sequence of booleans. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-boolean-seq ["true" "false"])
;; => [true false]
Parses sequence of non empty string. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-non-empty-str-seq ["" "a"])
;; => [nil "a"]
Parses sequence of web3-addresss. Unparsable ones will be replaced by nil or default. Ensures sequence if not passed.
(parsers/parse-web3-address-seq ["0x0B73EEc3b1C5A2c555799B7FfC500082606DFf15" "a"] "0x0")
;; => ["0x0b73eec3b1c5a2c555799b7ffc500082606dff15" "0x0"]
Creates a parsing function for sequences.
(parse-seq-fn parsers/parse-int)
;; is the same as
parsers/parse-int-seq
- Build:
npx shadow-cljs compile test-node
- Tests:
node out/node-tests.js
To release (happens automatically on CI at merge to master)
- Build:
clj -T:build jar
- Release:
clj -T:build deploy