Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Move *-fuzzy map routines and tests to quoin.

  • Loading branch information...
commit e684039eda8818ad042139590f355696b50b8286 1 parent 2539481
David Santiago authored
Showing with 11 additions and 113 deletions.
  1. +11 −69 src/stencil/utils.clj
  2. +0 −44 test/stencil/test/utils.clj
80 src/stencil/utils.clj
View
@@ -1,65 +1,6 @@
(ns stencil.utils
- (:require [clojure.string :as str]))
-
-;;
-;; Fuzzy map access routines
-;;
-
-(defn contains-fuzzy?
- "Given a map and a key, returns \"true\" if the map contains the key, allowing
- for differences of type between string and keyword. That is, :blah and
- \"blah\" are the same key. The key of the same type is preferred. Returns
- the variant of the found key for true, nil for false."
- ([map key] (contains-fuzzy? map key nil))
- ([map key not-found]
- (if (contains? map key)
- key
- (if (keyword? key)
- (let [str-key (name key)]
- (if (contains? map str-key)
- str-key not-found))
- (let [kw-key (keyword key)]
- (if (contains? map kw-key)
- kw-key not-found))))))
-
-(defn get-fuzzy
- "Given a map and a key, gets the value out of the map, trying various
- permitted combinations of the key. Key can be either a keyword or string,
- and is tried first as it is, before being converted to the other."
- ([map key]
- (get-fuzzy map key nil))
- ([map key not-found]
- (or (get map key)
- (get map (keyword key))
- (get map (name key))
- not-found)))
-
-(defn assoc-fuzzy
- "Just like clojure.core/assoc, except considers keys that are keywords and
- strings equivalent. That is, if you assoc :keyword into a map with a key
- \"keyword\", the latter is replaced."
- ([map key val]
- (let [found-key (contains-fuzzy? map key key)]
- (assoc map found-key val)))
- ([map key val & kvs]
- (let [new-map (assoc-fuzzy map key val)]
- (if kvs
- (recur new-map (first kvs) (second kvs) (nnext kvs))
- new-map))))
-
-(defn dissoc-fuzzy
- "Given a map and key(s), returns a map without the mappings for the keys,
- allowing for the keys to be certain combinations (ie, string/keyword are
- equivalent)."
- ([map] map)
- ([map key]
- (if-let [found-key (contains-fuzzy? map key)]
- (dissoc map found-key)))
- ([map key & ks]
- (let [new-map (dissoc-fuzzy map key)]
- (if ks
- (recur new-map (first ks) (next ks))
- new-map))))
+ (:require [clojure.string :as str]
+ [quoin.map-access :as map]))
;;
;; Context stack access logic
@@ -70,14 +11,15 @@
;;
(defn find-containing-context
- "Given a context stack and a key, walks down the context stack until it
- finds a context that contains the key. The key logic is fuzzy as in
- get-fuzzy/contains-fuzzy?. Returns the context, not the key's value,
- so nil when no context is found that contains the key."
+ "Given a context stack and a key, walks down the context stack until
+ it finds a context that contains the key. The key logic is fuzzy as
+ in get-named/contains-named? in quoin. Returns the context, not the
+ key's value, so nil when no context is found that contains the
+ key."
[context-stack key]
(loop [curr-context-stack context-stack]
(if-let [context-top (peek curr-context-stack)]
- (if (contains-fuzzy? context-top key)
+ (if (map/contains-named? context-top key)
context-top
;; Didn't have the key, so walk down the stack.
(recur (next curr-context-stack)))
@@ -104,12 +46,12 @@
;; key left, we repeat the process using only the matching context as
;; the context stack.
(if (next key)
- (recur (list (get-fuzzy matching-context
- (first key))) ;; Singleton ctx stack.
+ (recur (list (map/get-named matching-context
+ (first key))) ;; Singleton ctx stack.
(next key)
not-found)
;; Otherwise, we found the item!
- (get-fuzzy matching-context (first key)))
+ (map/get-named matching-context (first key)))
;; Didn't find a matching context.
not-found))))
44 test/stencil/test/utils.clj
View
@@ -2,50 +2,6 @@
(:use clojure.test
stencil.utils))
-(deftest test-contains-fuzzy?
- (is (= :a
- (contains-fuzzy? {:a 1} :a)))
- (is (= :a
- (contains-fuzzy? {:a 1} "a")))
- (is (= "a"
- (contains-fuzzy? {"a" 1} :a)))
- (is (= "a"
- (contains-fuzzy? {"a" 1} "a")))
- (is (= 1
- (contains-fuzzy? {:a 2} "b" 1))))
-
-(deftest test-get-fuzzy
- (is (= "success"
- (get-fuzzy {"test" "success"} "test")))
- (is (= "success"
- (get-fuzzy {"test" "success"} :test)))
- (is (= "success"
- (get-fuzzy {:test "success"} :test)))
- (is (= "success"
- (get-fuzzy {:test "success"} "test")))
- (is (= "failure"
- (get-fuzzy {:test "success"} "TEST" "failure"))))
-
-(deftest test-assoc-fuzzy
- (is (= {:a 1}
- (assoc-fuzzy {:a 0} :a 1)))
- (is (= {:a 1}
- (assoc-fuzzy {:a 0} "a" 1)))
- (is (= {"a" 1}
- (assoc-fuzzy {"a" 0} :a 1)))
- (is (= {"a" 1}
- (assoc-fuzzy {"a" 0} "a" 1)))
- (is (= {:a 1 :b 2}
- (assoc-fuzzy {:b 0} :a 1 "b" 2))))
-
-(deftest test-dissoc-fuzzy
- (is (= {}
- (dissoc-fuzzy {"test" 1} "test")))
- (is (= {}
- (dissoc-fuzzy {"test" 1} :test)))
- (is (= {}
- (dissoc-fuzzy {"test1" 1 :test2 2} :test1 "test2"))))
-
(deftest test-find-containing-context
(is (= {:a 1}
(find-containing-context '({:a 1}) :a)))
Please sign in to comment.
Something went wrong with that request. Please try again.