ClojureScript

keyword fn in single-arg form produces nil namespace kw for str and symbols

Details

  • Type: Defect Defect
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Completed
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Environment:
    cljs r1978

Description

In Clojurescript, keyword called with a single non-keyword argument always produces a nil-namespace keyword. This is inconsistent with Clojure (although the clojure docs don't mention anything about parsing strings for namespaces).

(-> 'a.b keyword ((juxt namespace name)))
;=> [nil "a.b"]
(-> 'a.b/c keyword ((juxt namespace name)))
;=> ["a.b" "c"]
(-> "a.b" keyword ((juxt namespace name)))
;=> [nil "a.b"]
(-> "a.b/c" keyword ((juxt namespace name)))
;=> ["a.b" "c"]
(-> "/c" keyword ((juxt namespace name)))
;=> ["" "c"]
(-> (symbol "/c") keyword ((juxt namespace name)))
;=> ["" "c"]

Compare with cljs:

(-> 'a.b keyword ((juxt namespace name)))
;=> [nil "a.b"]
(-> 'a.b/c keyword ((juxt namespace name)))
;=> [nil "c"]
(-> "a.b" keyword ((juxt namespace name)))
;=> [nil "a.b"]
(-> "a.b/c" keyword ((juxt namespace name)))
;=> [nil "a.b/c"]
(-> "/c" keyword ((juxt namespace name)))
;=> [nil "/c"]
(-> (symbol "/c") keyword ((juxt namespace name)))
;=> [nil "/c"]

Suggested (untested) patch:

cljs/cljs/core.cljs
(defn keyword
  "Returns a Keyword with the given namespace and name.  Do not use :
  in the keyword strings, it will be added automatically."
  ([name] (cond
            (keyword? name) name
            (symbol? name) (Keyword. (namespace name) (cljs.core/name name)
                                     (str name) nil)
            (string? name) (let [idx (.indexOf name "/")
                                 [ns n] (if (= -1 idx)
                                          [nil name]
                                          [(subs name 0 idx) (subs name idx)])]
                             (Keyword. ns n name nil))
            :else (Keyword. nil name name nil)))
  ([ns name] (Keyword. ns name (str (when ns (str ns "/")) name) nil)))

Activity

Hide
Francis Avila added a comment -

[(subs name 0 idx) (subs name idx)] above should be [(subs name 0 idx) (subs name (inc idx))]

Show
Francis Avila added a comment - [(subs name 0 idx) (subs name idx)] above should be [(subs name 0 idx) (subs name (inc idx))]
Hide
David Nolen added a comment -

Real patch welcome with tests included, thanks!

Show
David Nolen added a comment - Real patch welcome with tests included, thanks!
Hide
David Nolen added a comment -

I don't consider the /foo cases to be valid.

Show
David Nolen added a comment - I don't consider the /foo cases to be valid.

People

Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: