Add the parser to parse the string as an integer if it's needed.

dev
KKlochko 2 years ago
parent 553f4788e4
commit d357e82b54

@ -3,7 +3,21 @@
(:gen-class))
(defn unsigned-int?
"Return true if the string is an unsigned integer."
[str]
(if (re-matches #"\d+" str)
true false))
(defn parse-unsigned-int
"Return an integer if the argument is an integer."
[str-or-int]
(cond
(int? str-or-int)
str-or-int
(unsigned-int? str-or-int)
(Integer/parseInt str-or-int)
:else
nil))

@ -4,7 +4,7 @@
[cipher-analytical-machine.parsers.parsers :refer :all]))
(deftest unsigned-int?-test
(testing ""
(testing "The function return true only for an unsingned integer as a string."
(are [str expected]
(= expected
(unsigned-int? str))
@ -13,3 +13,13 @@
"-10" false
"abc" false)))
(deftest parse-unsigned-int-test
(testing "The function parse the integer if the string is an integer."
(are [str-or-int expected]
(= expected
(parse-unsigned-int str-or-int))
"9" 9
9 9
"10" 10
"-10" nil
"abc" nil)))

Loading…
Cancel
Save