Skip to content

Commit

Permalink
utils tweaks; many more utils tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrew committed Jun 14, 2016
1 parent f0967b8 commit b5760e2
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 34 deletions.
6 changes: 3 additions & 3 deletions src/cljs/ulysses/handlers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
parse-int-id
str->int
json->clj
to-id-map
by-id
find-by-id
remove-by-id
merge-by-id
Expand Down Expand Up @@ -162,7 +162,7 @@

(register-handler
:request-grant-ops
debounce-request-grant-ops)
debounce-request-grant-ops)

(register-handler
:request-grant-ops-start
Expand Down Expand Up @@ -202,7 +202,7 @@
:faculty
parse-int-id
list
to-id-map)))))))
by-id)))))))

;; ----------------------------------------------------------------------------
;; user data
Expand Down
70 changes: 40 additions & 30 deletions src/cljs/ulysses/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,26 @@
(string/trimr)
(str ellip)))))

(defn into-maybe
"if target is a list or lazy sequence, return coll.
otherwise, convert coll into the same type as target"
[target coll]
(let [list-or-lzs? ((some-fn list? lazy-seq?) target)
f (if list-or-lzs? identity (partial into (empty target)))]
(f coll)))

(defn by-id
"convert a seqable of maps with :id into a
hashmap with id keys and original map values.
if there are multiple with the same :id,
then it takes the last
ex. (by-id [{:id 1} {:id 2}])
=> {1 {:id 1}, 2 {:id 2}}"
[seqable]
(->> seqable
(group-by :id)
(remap last)))

(defn find-by-id
"find a map by :id within a seqable (of maps)"
[id seqable]
Expand All @@ -127,31 +147,18 @@
the rightmost having the highest preference;
the result taking on the type of the first"
[a & more]
(let [list-or-lzs? ((some-fn list? lazy-seq?) a)
into-maybe (if list-or-lzs? identity (partial into (empty a)))
all (apply (partial concat a) more)]
(->> all
(group-by :id)
(vals)
(map last)
(into-maybe))))
(->> (apply (partial concat a) more)
(by-id)
(vals)
(into-maybe a)))

(defn remove-by-id
"remove a map by :id within a seqable (of maps)"
[id seqable]
(let [id-int (str->int id)]
(remove
#(= id-int (:id %))
seqable)))

(defn to-id-map
"convert a list of maps with :id into a hashmap with :id keys"
[seqable]
(reduce
(fn [a e]
(assoc a (:id e) e))
(hash-map)
seqable))
(->> seqable
(remove #(= id-int (:id %)))
(into-maybe seqable))))

(defn new-id
"generate a new integer id, given a list of maps
Expand All @@ -169,17 +176,20 @@
[m ids]
(->> ids
(map (partial get m))
(remove nil?)))
(remove nil?)
(into-maybe ids)))

(defn valid-request-id?
"determines if either positive integer
or positive integer string"
[id]
(->> id
(str)
(re-find #"^\d+$")
(nil?)
(not)))
(if (= (str id) "0")
false
(->> id
(str)
(re-find #"^\d+$")
(nil?)
(not))))

(defn throttle [ms f]
(let [c (chan (sliding-buffer 1))]
Expand Down Expand Up @@ -221,7 +231,7 @@
"when obj is a javascript object, get property"
[obj prop-name]
(when (object? obj)
(aget obj prop-name)))
(aget obj (name prop-name))))

;; ----------------------------------------------------------------------------
;; classes
Expand Down Expand Up @@ -295,8 +305,8 @@
[]
(or
(when-let [d js/document]
(let [body (wop d "body")
de (wop d "documentElement")
(let [body (wop d :body)
de (wop d :documentElement)
wb (partial wop body)
wde (partial wop de)]
(when (or body de)
Expand Down Expand Up @@ -324,7 +334,7 @@
"mechanism to contextual (as keyword) helper
ex. :nsf => :info"
[mechanism]
(mechanism config/mechanism-contextuals))
(get config/mechanism-contextuals mechanism))

(defn update-db-pagination
"update db :page-current and :page-last given paginated response"
Expand Down
2 changes: 1 addition & 1 deletion test/cljs/ulysses/runner.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
[ulysses.db-test]))

;; run all tests in all namespaces matching ulysses.*
(doo-all-tests #"ulysses.*")
(doo-all-tests #"ulysses.*-test")
133 changes: 133 additions & 0 deletions test/cljs/ulysses/utils_test.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@
false ""
false 0))

(deftest lazy-seq?
(are [x y] (= x (utils/lazy-seq? y))
true (lazy-seq)
true (map integer? [1 2 3])
false (list)
false (vector)
false (hash-map)
false nil
false ""))

; missing: ptr

; missing: ptr-first
Expand Down Expand Up @@ -119,6 +129,19 @@
(testing "custom ellipsis"
(is (= "foo[..]" (utils/truncate-with-ellipsis "foo bar" 3 "[..]")))))

(deftest into-maybe
(is (= (list 1) (utils/into-maybe (list) [1])))
(is (= (lazy-seq (list 1 2 3)) (utils/into-maybe (list) [1 2 3])))
(is (= #{1} (utils/into-maybe #{1} [1])))
(is (= #{1 2} (utils/into-maybe #{3} #{1 2}))))

(deftest by-id
(are [x y] (= x (utils/by-id y))
{} []
{1 {:id 1}} [{:id 1}]
{1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2}]
{1 {:id 1} 2 {:id 2}} [{:id 1} {:id 2 :old true} {:id 2}]))

(deftest find-by-id
(let [ms [{:id 1} {:id 2} {:id 94}]]
(testing "found (type int)"
Expand Down Expand Up @@ -156,3 +179,113 @@
#{{:id 0} {:id 1} {:id 2}} [#{0} #{1 2} #{0 2 1}]
#{{:id 0} {:id 1} {:id 2 :a true}} [#{0} [1 2] #{{:id 2 :a true}}]
(list {:id 0} {:id 1} {:id 2 :a true} {:id 3}) [(list 0 1 2) [1 {:id 2 :a true} 3] []]))))

(deftest remove-by-id
(are [x id y] (= x (utils/remove-by-id id y))
[{:id 1} {:id 2} {:id 3}] 85 [{:id 1} {:id 2} {:id 3}]
#{{:id 1} {:id 2} {:id 3}} 85 #{{:id 1} {:id 2} {:id 3}}
[{:id 1} {:id 3}] 2 [{:id 1} {:id 2} {:id 3}]
[{:id 1} {:id 3}] "2" [{:id 1} {:id 2} {:id 3}]))

(deftest new-id
(are [x y] (= x (utils/new-id y))
1 (list)
2 (list {:id 1})
3 (list {:id 1} {:id 2})
40 (list {:id 1} {:id 39} {:id 4})))

(deftest map-lookup
(are [x m ids] (= x (utils/map-lookup m ids))
(list) {} (list)
(vector) {} (vector)
[] {2 {:id 2}} [1]
[{:id 1}] {1 {:id 1} 2 {:id 2}} [1]
[{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2]
[{:id 1} {:id 2}] {1 {:id 1} 2 {:id 2}} [1 2 3]))

(deftest valid-request-id?
(testing "yes"
(are [x] (utils/valid-request-id? x)
1 5 "1" "5" "12345"))
(testing "no"
(are [x] (not (utils/valid-request-id? x))
0 -5 "0" "-5" nil [] "")))

; missing: throttle

; missing: debounce

; missing: debounce-standard

(deftest wop
(testing "is object"
(are [x y prop] (= x (utils/wop y prop))
1 #js {"ok" 1} "ok"
1 #js {"ok" 1} :ok
nil #js {"ok" 1} "missing"
nil #js {"ok" 1} :missing))
(testing "is not object"
(are [y prop] (nil? (utils/wop y prop))
#js [1 2 3] :foo
nil :foo
{:foo 3} :foo)))

; missing: classes-vector

; missing: classes

; missing: classes-attr

; missing: btn-classes

; missing: d-p

; missing: window-size

; missing: document-size

; missing: window-scroll-position

(deftest mechanism-to-contextual
(are [x y] (= x (utils/mechanism-to-contextual y))
:info :nsf
:success :nih
nil :not-found))

; missing: update-db-pagination

; missing: faculties-pool-params

; missing: metric-maxes

; missing: verify-user-poll-response

; missing: validate-builder-filters

(deftest parse-int-id
(are [x y] (= x (utils/parse-int-id y))
{:id 3} {:id "3"}
{:id 123} {:id "123"}
{:id 3} {:id 3}
{:id 123} {:id 123}
{:id nil} {:id "*"}))

; missing: clean-incoming-grant-op

; missing: clean-incoming-grant-ops

; missing: request

; missing: request-grant-ops-channel

; missing: debounce-request-grant-ops

; missing: request-faculties-pool-channel

; missing: debounce-request-faculties-pool

; missing: request-workspace-save-channel

; missing: debounce-request-workspace-save

; missing: scroll-to

0 comments on commit b5760e2

Please sign in to comment.