Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle nil values similar to how Clojure does for #63 #64

Closed

Conversation

vincentjames501
Copy link

This potentially addresses #63. The goal is to make the behavior on nil the exact same as nil would be handled with Clojure equivalent functions.

The above behavior works like the following:

CSK CLJ Nil input behavior
-><case>-keyword keyword nil
-><case>-string str ""
-><case>-symbol symbol IllegalArgumentException
-><case> identity nil

I could also see the argument for simply returning nil for all of the above instead or if you truly feel nil is just a bad input, throwing IllegalArgumentException/NullPointerException would be fine and still better than an Error. Let me know if you prefer one more than the other and I'm happy to adjust the PR.

(deftest nils-test
  (testing "returns idiomatic response for type converting functions on nil input"
    (is (nil? (csk/->kebab-case-keyword nil)))
    (is (= "" (csk/->kebab-case-string nil)))
    (is (thrown? IllegalArgumentException (csk/->kebab-case-symbol nil))))
  (testing "returns idiomatic response for non-type converting functions on nil input"
    (is (nil? (csk/->kebab-case nil)))))

For reference:

keyword

user=> (keyword nil)
nil
user=> (keyword "foo")
:foo
user=> (keyword :foo)
:foo
user=> (keyword 'foo)
:foo

symbol

user=> (symbol nil)
Execution error (IllegalArgumentException) at user/eval2101 (REPL:1).
no conversion to symbol

user=> (symbol "foo")
foo
user=> (symbol :foo)
foo
user=> (symbol 'foo)
foo

str

user=> (str nil)
""
user=> (str "foo")
"foo"
user=> (str :foo)
":foo"
user=> (str 'foo)
"foo"

name

user=> (name nil)
Execution error (NullPointerException) at user/eval2091 (REPL:1).
null

user=> (name "foo")
"foo"
user=> (name :foo)
"foo"
user=> (name 'foo)
"foo"

@qerub
Copy link
Collaborator

qerub commented Aug 9, 2020

After much deliberation and opinion collection from #clj-commons, I decided that I think the least bad way to handle nil inputs is to throw an Exception (instead of an Error). I've changed the library to do so with #70 so I'm closing this PR. But I still think there's room for discussion so I'm leaving #63 open.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants