From b26b0de754944133be7e3be40bcc883fa6127858 Mon Sep 17 00:00:00 2001 From: den1k Date: Mon, 20 Jul 2015 21:55:32 -0400 Subject: [PATCH] convert from cljx to .cljc --- project.clj | 21 +- src/bardo/{ease.cljx => ease.cljc} | 92 +++---- src/bardo/interpolate.cljc | 252 ++++++++++++++++++ src/bardo/interpolate.cljx | 188 ------------- src/bardo/transition.cljc | 91 +++++++ src/bardo/transition.cljx | 93 ------- test/bardo/ease_test.cljc | 42 +++ test/bardo/ease_test.cljx | 4 - ...polate_test.cljx => interpolate_test.cljc} | 0 test/bardo/transition_test.cljc | 79 ++++++ test/bardo/transition_test.cljx | 80 ------ 11 files changed, 513 insertions(+), 429 deletions(-) rename src/bardo/{ease.cljx => ease.cljc} (66%) create mode 100644 src/bardo/interpolate.cljc delete mode 100644 src/bardo/interpolate.cljx create mode 100644 src/bardo/transition.cljc delete mode 100644 src/bardo/transition.cljx create mode 100644 test/bardo/ease_test.cljc delete mode 100644 test/bardo/ease_test.cljx rename test/bardo/{interpolate_test.cljx => interpolate_test.cljc} (100%) create mode 100644 test/bardo/transition_test.cljc delete mode 100644 test/bardo/transition_test.cljx diff --git a/project.clj b/project.clj index 187038a..ec23c11 100644 --- a/project.clj +++ b/project.clj @@ -1,11 +1,10 @@ (defproject bardo "0.1.0" :description "A clojure(script) library to assist with transitions between dimensions" :url "https://github.com/pleasetrythisathome/bardo" - :license {:name "Eclipse Public License - v 1.0" - :url "http://www.eclipse.org/legal/epl-v10.html" + :license {:name "Eclipse Public License - v 1.0" + :url "http://www.eclipse.org/legal/epl-v10.html" :distribution :repo} - :source-paths ["target/src/clj" "target/test/clj"] :resource-paths ["target/src/cljs"] :jar-exclusions [#"\.cljx|\.swp|\.swo|\.DS_Store"] @@ -16,18 +15,4 @@ [org.clojure/core.match "0.3.0-alpha4"] [clj-time "0.8.0"] [com.andrewmcveigh/cljs-time "0.1.1"]] - - :cljx {:builds [{:source-paths ["src"], - :output-path "target/src/clj", - :rules :clj} - {:source-paths ["src"], - :output-path "target/src/cljs", - :rules :cljs} - {:source-paths ["test"], - :output-path "target/test/clj", - :rules :clj} - {:source-paths ["test"], - :output-path "target/test/cljs", - :rules :cljs}]} - :plugins [[com.keminglabs/cljx "0.3.2"]] - :hooks [cljx.hooks]) + ) diff --git a/src/bardo/ease.cljx b/src/bardo/ease.cljc similarity index 66% rename from src/bardo/ease.cljx rename to src/bardo/ease.cljc index d2e902f..45d1aa9 100644 --- a/src/bardo/ease.cljx +++ b/src/bardo/ease.cljc @@ -17,30 +17,28 @@ [f] (fn [t] (f (cond - (< t 0) 0 - (> t 1) 1 - :else t)))) + (< t 0) 0 + (> t 1) 1 + :else t)))) (defn shift "shifts the domain of input from [cmin cmax] to [nmin nmax]" ([f cmin cmax] (shift f cmin cmax 0 1)) ([f cmin cmax nmin nmax] - (fn [t] - (f (-> t - (- cmin) - (/ (- cmax cmin)) - (* (- nmax nmin)) - (+ nmin)))))) + (fn [t] + (f (-> t + (- cmin) + (/ (- cmax cmin)) + (* (- nmax nmin)) + (+ nmin)))))) (defn partition-range "for a range partition into pairs of each number and it's following ex. [0 0.25 0.5 0.75 1] => [[0 0.25] [0.25 0.5] [0.5 0.75] [0.75 1]]" [coll] - (->> coll - ((juxt (partial drop-last 1) - (partial drop 1))) - (apply interleave) + (->> (interleave coll (rest coll)) (partition 2) + ;; needed? (mapv vec))) (defn shift-parts @@ -56,11 +54,11 @@ (mapv (comp vec (partial map-indexed vector) partition-range)))] (fn [t] (cond - (= t 0) (f 0) - (= t 1) (f 1) - :else (let [[idx [istart iend]] (first (filter (comp (fn [[start end]] (<= start t end)) second) input)) - [_ [estart eend]] (get output (int idx))] - ((shift f istart iend estart eend) t)))))) + (= t 0) (f 0) + (= t 1) (f 1) + :else (let [[idx [istart iend]] (first (filter (comp (fn [[start end]] (<= start t end)) second) input)) + [_ [estart eend]] (get output (int idx))] + ((shift f istart iend estart eend) t)))))) (defn flip "reverse" @@ -76,15 +74,16 @@ (f (* 2 t)) (- 2 (f (- 2 (* 2 t)))))))) -(def modes {:in identity - :out flip +(def modes {:in identity + :out flip :in-out reflect :out-in (comp reflect flip)}) ;; adapted from https://github.com/warrenm/AHEasing and ;; and https://github.com/mbostock/d3/blob/master/src/interpolate/ease.js -(def PI #+clj Math/PI #+cljs (.-PI js/Math)) +(def PI #?(:clj Math/PI + :cljs (.-PI js/Math))) (def PI_2 (/ PI 2)) @@ -98,6 +97,7 @@ [t] (* t t t)) + (defn poly "raise t to power e" [e] @@ -119,7 +119,7 @@ [t] (if (= t 0) t - (exp 2 (* 10 (dec t))))) + (Math/pow 2 (* 10 (dec t))))) (defn elastic "Modeled after the damped sine wave y = sin(13PI_2*x)*pow(2, 10 * (x - 1))" @@ -129,7 +129,7 @@ (defn back "Modeled after the overshooting cubic y = x^3-x*sin(x*pi)" - [t ] + [t] (- (* t t t) (* t (Math/sin (* t PI))))) @@ -137,30 +137,30 @@ "Modeled after some fun bouncing stuff" [t] (cond - (< t (/ 1 2.75)) (* 7.5625 t t) - (< t (/ 2 2.75)) (+ (* 7.5625 - (- t (/ 1.5 2.75)) - (- t (/ 1.5 2.75))) - 0.75) - (< t (/ 2.5 2.75)) (+ (* 7.5625 - (- t (/ 2.5 2.75)) - (- t (/ 2.5 2.75))) - 0.9375) - :else (+ (* 7.5625 - (- t (/ 2.625 2.75)) - (- t (/ 2.625 2.75))) - 0.984375))) - -(def ease-fns {:linear (constantly identity) - :quad (constantly quad) - :cubic (constantly cubic) - :poly poly - :sine (constantly sine) - :circle (constantly circle) - :exp (constantly exp) + (< t (/ 1 2.75)) (* 7.5625 t t) + (< t (/ 2 2.75)) (+ (* 7.5625 + (- t (/ 1.5 2.75)) + (- t (/ 1.5 2.75))) + 0.75) + (< t (/ 2.5 2.75)) (+ (* 7.5625 + (- t (/ 2.5 2.75)) + (- t (/ 2.5 2.75))) + 0.9375) + :else (+ (* 7.5625 + (- t (/ 2.625 2.75)) + (- t (/ 2.625 2.75))) + 0.984375))) + +(def ease-fns {:linear (constantly identity) + :quad (constantly quad) + :cubic (constantly cubic) + :poly poly + :sine (constantly sine) + :circle (constantly circle) + :exp (constantly exp) :elastic (constantly elastic) - :back (constantly back) - :bounce (constantly bounce)}) + :back (constantly back) + :bounce (constantly bounce)}) (defn ease "easing function constructor. takes key-mode where mode #{:in :out :in-out} diff --git a/src/bardo/interpolate.cljc b/src/bardo/interpolate.cljc new file mode 100644 index 0000000..27d17c6 --- /dev/null +++ b/src/bardo/interpolate.cljc @@ -0,0 +1,252 @@ +(ns bardo.interpolate + (:require + #?(:cljs [cljs.core.match :refer-macros [match]] + :clj [clojure.core.match :refer [match]]) + [clojure.set :refer [union]] + [bardo.ease :as ease]) + (:import (clojure.lang PersistentArrayMap))) + + +;; a protocol for birthing new values from nil +(defprotocol IFresh + (fresh [x])) + +#?(:clj (extend-protocol IFresh + + java.lang.Number + (fresh [x] + 0) + + clojure.lang.Sequential + (fresh [x] + '()) + + clojure.lang.PersistentArrayMap + (fresh [x] + {})) + + :cljs (extend-protocol IFresh + + Number + (fresh [x] + 0) + + List + (fresh [x] + '()) + + PersistentArrayMap + (fresh [x] + {}))) + +(comment + (extend-protocol IFresh + #?(:clj java.lang.Number + :cljs number) + (fresh [x] + 0) + + #?(:clj clojure.lang.Sequential + :cljs List) + (fresh [x] + '()) + + (#?(:clj clojure.lang.PersistentArrayMap + :cljs PersistentArrayMap)) + (fresh [x] + {}))) + +(def hash-map? (every-pred coll? (complement sequential?))) + +(defn wrap-nil + "if a value is nil, replace it with a fresh value of the other + value if it satisfies IFresh" + [start end] + (match [start end] + [nil nil] nil + [nil end] (if (satisfies? IFresh end) + [(fresh end) end] + [nil end]) + [start nil] (if (satisfies? IFresh start) + [start (fresh start)] + [start nil]) + [start end] [start end])) + +(defn wrap-infinite [x y] + ;;(println :wrap-infinite [x y]) + (if (every? sequential? [x y]) + (match (mapv counted? [x y]) + [false false] (throw + (#?@(:cljs [js/Error js/Exception] + :clj ['Exception.]) + "Cannot interpolate between two uncounted sequences")) + [false _] [(take (count y) x) y] + [_ false] [x (take (count x) y)] + [_ _] [x y]) + [x y])) + +(defn juxt-args [& fns] + (fn [& args] + (map-indexed (fn [idx f] + (f (nth args idx nil))) + fns))) + +(defn symmetrical-error + "calls (f x y) (f y x) and returns [x y] where f is a function (f x y) that returns [x y]" + [s msg f] + (when (or (apply f s) + (apply f (reverse s))) + (throw + (#?(:cljs js/Error + :clj java.lang.Exception) + msg)))) + +(defn pair-pred [pred] + (comp (partial every? identity) + (juxt-args pred (complement pred)))) + +(defn wrap-errors + "throw appropriate errors if you can't interpolate between two values" + [x y] + (let [types {"seq" sequential? + "hash-map" hash-map? + "number" number?}] + (doseq [[type pred] types] + (symmetrical-error [x y] + (str "Cannot interpolate between a " type " and something else") + (pair-pred pred))) + [x y])) + +(defn wrap-size + "removed keys not present in start or end of interpolation" + [start end] + (let [] + (fn [intrpl] + (fn [t] + (let [v (intrpl t)] + (match [t v] + [0 (_ :guard hash-map?)] (select-keys v (keys start)) + [1 (_ :guard hash-map?)] (select-keys v (keys end)) + [0 (_ :guard sequential?)] (vec (take (count start) v)) + [1 (_ :guard sequential?)] (vec (take (count end) v)) + [_ _] v)))))) + +(declare interpolate) + +(defprotocol IInterpolate + (-interpolate [start end])) + +#?(:clj (extend-protocol IInterpolate + + java.lang.Number + (-interpolate [start end] + (fn [t] + (+ start (* t (- end start))))) + + clojure.lang.Sequential + (-interpolate [start end] + (fn [t] + (into [] (for [k (range (Math/max (count start) + (count end)))] + (->> [(nth start k nil) (nth end k nil)] + (apply wrap-nil) + (apply interpolate) + (#(% t))))))) + + clojure.lang.PersistentArrayMap + (-interpolate [start end] + (fn [t] + (into {} (for [k (->> [start end] + (map keys) + (map set) + (apply union))] + [k (->> [start end] + (map k) + (apply interpolate) + (#(% t)))]))))) + :cljs (extend-protocol IInterpolate + number + (-interpolate [start end] + (fn [t] + (+ start (* t (- end start))))) + + + List + (-interpolate [start end] + (fn [t] + (into [] (for [k (range (Math/max (count start) + (count end)))] + (->> [(nth start k nil) (nth end k nil)] + (apply wrap-nil) + (apply interpolate) + (#(% t))))))) + + PersistentArrayMap + (-interpolate [start end] + (fn [t] + (into {} (for [k (->> [start end] + (map keys) + (map set) + (apply union))] + [k (->> [start end] + (map k) + (apply interpolate) + (#(% t)))])))))) + + + +(defn interpolate [start end] + (let [wrapped (some->> [start end] + (apply wrap-nil) + (apply wrap-errors) + (apply wrap-infinite))] + (let [can-interpolate (mapv #(satisfies? IInterpolate %) wrapped)] + (if (apply = true can-interpolate) + ((apply wrap-size wrapped) (apply -interpolate wrapped)) + (do + (throw + (#?(:cljs js/Error + :clj Exception.)) + (str "Cannot interpolate between " start " and " end))))))) + +(defn into-lazy-seq [intrpl vals] + (if (seq (rest vals)) + (cons (intrpl (first vals)) (lazy-seq (into-lazy-seq intrpl (rest vals)))) + (vector (intrpl (first vals))))) + +(defn mix + [start end] + (fn [t] + ((interpolate (start t) (end t)) t))) + +(defn blend + [intrpl end] + (fn [t] + ((interpolate (intrpl t) end) t))) + +(defn chain + ([intrpl end] (chain intrpl end 0.5)) + ([intrpl end mid] + (let [start (ease/shift intrpl 0 mid) + end (-> (intrpl 1) + (interpolate end) + (ease/shift mid 1))] + (fn [t] + (cond + (< t mid) (start t) + (>= t mid) (end t)))))) + +(defn pipeline + ([states] (let [n (count states)] + (pipeline states (->> (range 1 n) + (map (partial * (/ 1 (dec n)))) + (cons 0))))) + ([states input] + (let [n (count states) + [start second & states] states + output (->> (iterate #(/ % 2) 1) + (take (dec n)) + (reverse) + (cons 0))] + (-> (reduce chain (interpolate start second) states) + (ease/shift-parts input output))))) diff --git a/src/bardo/interpolate.cljx b/src/bardo/interpolate.cljx deleted file mode 100644 index f51a406..0000000 --- a/src/bardo/interpolate.cljx +++ /dev/null @@ -1,188 +0,0 @@ -(ns bardo.interpolate - (:require [clojure.set :refer [union]] - #+clj [clojure.core.match :refer [match]] - #+cljs [cljs.core.match :refer-macros [match]] - [bardo.ease :as ease])) - -;; a protocol for birthing new values from nil -(defprotocol IFresh - (fresh [x])) - -(extend-protocol IFresh - - #+clj java.lang.Number - #+cljs number - (fresh [x] - 0) - - #+clj clojure.lang.Sequential - #+cljs List - (fresh [x] - '()) - - #+clj clojure.lang.PersistentArrayMap - #+cljs PersistentArrayMap - (fresh [x] - {})) - -(def hash-map? (every-pred coll? (complement sequential?))) - -(defn wrap-nil - "if a value is nil, replace it with a fresh value of the other - value if it satisfies IFresh" - [start end] - (match [start end] - [nil nil] nil - [nil end] (if (satisfies? IFresh end) - [(fresh end) end] - [nil end]) - [start nil] (if (satisfies? IFresh start) - [start (fresh start)] - [start nil]) - [start end] [start end])) - -(defn wrap-infinite [x y] - ;;(println :wrap-infinite [x y]) - (if (every? sequential? [x y]) - (match (mapv counted? [x y]) - [false false] (throw - (#+cljs js/Error #+clj #+cljs js/Exception #+clj Exception. "Cannot interpolate between two uncounted sequences")) - [false _] [(take (count y) x) y] - [_ false] [x (take (count x) y)] - [_ _] [x y]) - [x y])) - -(defn juxt-args [& fns] - (fn [& args] - (map-indexed (fn [idx f] - (f (nth args idx nil))) - fns))) - -(defn symmetrical-error - "calls (f x y) (f y x) and returns [x y] where f is a function (f x y) that returns [x y]" - [s msg f] - (when (or (apply f s) - (apply f (reverse s))) - (throw - (#+cljs js/Error #+clj Exception. msg)))) - -(defn pair-pred [pred] - (comp (partial every? identity) - (juxt-args pred (complement pred)))) - -(defn wrap-errors - "throw appropriate errors if you can't interpolate between two values" - [x y] - (let [types {"seq" sequential? - "hash-map" hash-map? - "number" number?}] - (doseq [[type pred] types] - (symmetrical-error [x y] - (str "Cannot interpolate between a " type " and something else") - (pair-pred pred))) - [x y])) - -(defn wrap-size - "removed keys not present in start or end of interpolation" - [start end] - (let [] - (fn [intrpl] - (fn [t] - (let [v (intrpl t)] - (match [t v] - [0 (_ :guard hash-map?)] (select-keys v (keys start)) - [1 (_ :guard hash-map?)] (select-keys v (keys end)) - [0 (_ :guard sequential?)] (vec (take (count start) v)) - [1 (_ :guard sequential?)] (vec (take (count end) v)) - [_ _] v)))))) - -(declare interpolate) - -(defprotocol IInterpolate - (-interpolate [start end])) - -(extend-protocol IInterpolate - - #+clj java.lang.Number - #+cljs number - (-interpolate [start end] - (fn [t] - (+ start (* t (- end start))))) - - #+clj clojure.lang.Sequential - #+cljs List - (-interpolate [start end] - (fn [t] - (into [] (for [k (range (Math/max (count start) - (count end)))] - (->> [(nth start k nil) (nth end k nil)] - (apply wrap-nil) - (apply interpolate) - (#(% t))))))) - - #+clj clojure.lang.IPersistentMap - #+cljs PersistentArrayMap - (-interpolate [start end] - (fn [t] - (into {} (for [k (->> [start end] - (map keys) - (map set) - (apply union))] - [k (->> [start end] - (map k) - (apply interpolate) - (#(% t)))]))))) - -(defn interpolate [start end] - (let [wrapped (some->> [start end] - (apply wrap-nil) - (apply wrap-errors) - (apply wrap-infinite))] - (let [can-interpolate (mapv #(satisfies? IInterpolate %) wrapped)] - (if (apply = true can-interpolate) - ((apply wrap-size wrapped) (apply -interpolate wrapped)) - (do - (throw - (#+cljs js/Error #+clj Exception. (str "Cannot interpolate between " start " and " end)))))))) - -(defn into-lazy-seq [intrpl vals] - (if (seq (rest vals)) - (cons (intrpl (first vals)) (lazy-seq (into-lazy-seq intrpl (rest vals)))) - (vector (intrpl (first vals))))) - -(defn mix - [start end] - (fn [t] - ((interpolate (start t) (end t)) t))) - -(defn blend - [intrpl end] - (fn [t] - ((interpolate (intrpl t) end) t))) - -(defn chain - ([intrpl end] (chain intrpl end 0.5)) - ([intrpl end mid] - (let [start (ease/shift intrpl 0 mid) - end (-> (intrpl 1) - (interpolate end) - (ease/shift mid 1))] - (fn [t] - (cond - (< t mid) (start t) - (>= t mid) (end t)))))) - -(defn pipeline - ([states] (let [n (count states)] - (pipeline states (->> (range 1 n) - (map (partial * (/ 1 (dec n)))) - (cons 0))))) - ([states input] - (let [n (count states) - [start second & states] states - output (->> (iterate #(/ % 2) 1) - (take (dec n)) - (reverse) - (cons 0))] - (-> (reduce chain (interpolate start second) states) - (ease/shift-parts input output))))) diff --git a/src/bardo/transition.cljc b/src/bardo/transition.cljc new file mode 100644 index 0000000..00be22f --- /dev/null +++ b/src/bardo/transition.cljc @@ -0,0 +1,91 @@ +(ns bardo.transition + #?(:cljs + (:require-macros [cljs.core.async.macros :refer [go go-loop]])) + (:require [bardo.ease :refer [ease]] + [bardo.interpolate :refer [interpolate]] + #?@(:clj [[clj-time.core :as t] + [clj-time.coerce :as c] + [clojure.core.async + :refer [put! take! ! !! chan timeout sliding-buffer close! go go-loop alts!] + :as async]] + :cljs [[cljs.core.async + :refer [put! take! ! chan timeout sliding-buffer close! alts!] + :as async] + [cljs-time.core :as t] + [cljs-time.coerce :as c]]))) + + +(defn now [] + #?(:clj (c/to-long (t/now)) + :cljs (if-let [now (.-now (.-performance js/window))] + (.call now (.-performance js/window)) + (.now js/Date)))) + +#?(:clj + (defn set-interval + "runs a function intended to produce side effects at a target speed while the function returns truthy" + ([step] (set-interval step {})) + ([step {:keys [target tolerance step] + :or {target 16 + tolerance 1 + step 0.5}}] + (go-loop [last-time (now) + wait target] + + (let [time (now)] + + (when (step time) + + ;; converge wait time on target and recur + (let [last (- time last-time) + new (if (< (- target tolerance) + last + (+ target tolerance)) + wait + ((if (< target last) - +) wait step))] + + (when (< 0 wait) + (> vendors + (map #(aget js/window (str % "requestAnimationFrame"))) + (filter identity) + (first)))] + (let [frame (chan)] + (go-loop [] + (.call native js/window (fn [time] + (put! frame time))) + (let [time ( since duration) + t (if done? + 1 + (ease-fn (/ since duration))) + step (interpolator (ease-fn t))] + (put! out step) + (when done? + (close! out)) + (not done?)))) + out))) diff --git a/src/bardo/transition.cljx b/src/bardo/transition.cljx deleted file mode 100644 index 2ccb309..0000000 --- a/src/bardo/transition.cljx +++ /dev/null @@ -1,93 +0,0 @@ -(ns bardo.transition - #+cljs - (:require-macros [cljs.core.async.macros :refer [go go-loop]]) - (:require [bardo.ease :refer [ease]] - [bardo.interpolate :refer [interpolate]] - #+clj [clj-time.core :as t] - #+clj [clj-time.coerce :as c] - #+clj [clojure.core.async - :refer [put! take! ! !! chan timeout sliding-buffer close! go go-loop alts!] - :as async] - #+cljs [cljs.core.async - :refer [put! take! ! chan timeout sliding-buffer close! alts!] - :as async] - #+cljs [cljs-time.core :as t] - #+cljs [cljs-time.coerce :as c])) - - -(defn now [] - #+clj - (c/to-long (t/now)) - #+cljs - (if-let [now (.-now (.-performance js/window))] - (.call now (.-performance js/window)) - (.now js/Date))) - -#+clj -(defn set-interval - "runs a function intended to produce side effects at a target speed while the function returns truthy" - ([step] (set-interval step {})) - ([step {:keys [target tolerance step] - :or {target 16 - tolerance 1 - step 0.5}}] - (go-loop [last-time (now) - wait target] - - (let [time (now)] - - (when (step time) - - ;; converge wait time on target and recur - (let [last (- time last-time) - new (if (< (- target tolerance) - last - (+ target tolerance)) - wait - ((if (< target last) - +) wait step))] - - (when (< 0 wait) - (> vendors - (map #(aget js/window (str % "requestAnimationFrame"))) - (filter identity) - (first)))] - (let [frame (chan)] - (go-loop [] - (.call native js/window (fn [time] - (put! frame time))) - (let [time ( since duration) - t (if done? - 1 - (ease-fn (/ since duration))) - step (interpolator (ease-fn t))] - (put! out step) - (when done? - (close! out)) - (not done?)))) - out))) diff --git a/test/bardo/ease_test.cljc b/test/bardo/ease_test.cljc new file mode 100644 index 0000000..ba8795c --- /dev/null +++ b/test/bardo/ease_test.cljc @@ -0,0 +1,42 @@ +(ns bardo.ease-test + (:require [clojure.test :refer :all] + [bardo.ease :refer :all])) + + +(deftest ease-test + (is (= 6 ((interpolator inc) 5))) + (is (= 7 ((wrap inc inc) 5))) + (testing "clamp" + (is (= 1.5 ((clamp inc) 0.5))) + (is (= 2 ((clamp inc) 10e4))) + (is (= 1 ((clamp inc) -10e4))) + (is (= 2 ((clamp inc) 1)))) + (testing "shift" + (is (= 0 ((shift identity 5 10) 5))) + (is (= 1 ((shift identity 5 10) 10))) + (is (= 2 ((shift inc 5 10) 10))) + (is (= 5 ((shift identity 5 10 5 10) 5))) + (is (= 10 ((shift identity 5 10 5 10) 10)))) + (testing "partition range" + (is (= [[0 0.25] [0.25 0.5] [0.5 0.75] [0.75 1]] + (partition-range [0 0.25 0.5 0.75 1]))) + (is (= [[1 3] [3 8] [8 11]] + (partition-range [1 3 8 11])))) + (testing "shift parts" + (is (= 0.2 ((shift-parts identity [0.1 1] [0.2 1]) 0.1))) + (is (= 1 ((shift-parts identity [0 0.5 1] [0 0.3 1]) 1))) + (is (= 0 ((shift-parts identity [0 0.5 1] [0 0.3 1]) 0))) + (is (thrown? java.lang.AssertionError + ((shift-parts identity [0] [0 0.1]) 0)))) + (testing "flip" + (is (zero? ((flip (partial + 5)) 5))) + (is (= -5 ((flip (partial + 5)) 0))) + (is (= -3 ((flip (partial + 5)) 2))) + (is (= 10 ((flip identity) 10)) "=> (- 1 (- 1 10))")) + (testing "reflect" + (is (= 10.0 ((reflect identity) 10)) "=> (* 10 2 0.5)") + (is (= 0.0 ((reflect identity) 0))) + (is (= 0.5 ((reflect inc) 0))))) + + + diff --git a/test/bardo/ease_test.cljx b/test/bardo/ease_test.cljx deleted file mode 100644 index c79e206..0000000 --- a/test/bardo/ease_test.cljx +++ /dev/null @@ -1,4 +0,0 @@ -(ns bardo.ease-test - (:require [clojure.test.check.core :as sc] - [clojure.test.check.generators :as gen] - [clojure.test.check.properties :as prop :include-macros true])) diff --git a/test/bardo/interpolate_test.cljx b/test/bardo/interpolate_test.cljc similarity index 100% rename from test/bardo/interpolate_test.cljx rename to test/bardo/interpolate_test.cljc diff --git a/test/bardo/transition_test.cljc b/test/bardo/transition_test.cljc new file mode 100644 index 0000000..be12e75 --- /dev/null +++ b/test/bardo/transition_test.cljc @@ -0,0 +1,79 @@ +(ns bardo.transition-test + #?(:cljs + (:require-macros [cljs.core.async.macros :refer [go go-loop]])) + (:require [bardo.transition :as bardo] + [bardo.ease :as ease] + [bardo.interpolate :as intrpl] + #?@(:clj [[clojure.pprint :refer [pprint]] + [clojure.core.async + :refer [put! take! ! !! chan timeout close! go go-loop] + :as async] + ;[clojure.tools.namespace.repl :refer [refresh refresh-all]] + [clojure.test :refer :all]] + :cljs [[cljs.core.async + :refer [put! take! ! chan timeout close!] + :as async]]) + ;#+cljs [weasel.repl :as repl] + )) + +#?(:cljs + (enable-console-print!)) + +#?(:cljs + (when-not (repl/alive?) + (repl/connect "ws://localhost:9001" :verbose true))) + +#?(:cljs + (defn log + "logs cljs stuff as js stuff for inspection" + [& args] + (.apply (.-log js/console) js/console (clj->js (map clj->js args))))) + +#?(:clj + (def log-chan (chan))) +#?(:clj + (defn log + [& args] + (put! log-chan args))) +#?(:clj + (go-loop [] + (when-let [args (! !! chan timeout close! go go-loop] - :as async] - #+clj [clojure.tools.namespace.repl :refer [refresh refresh-all]] - #+clj [clojure.test :refer :all] - #+cljs [weasel.repl :as repl] - #+cljs [cljs.core.async - :refer [put! take! ! chan timeout close!] - :as async])) - -#+cljs -(enable-console-print!) - -#+cljs -(when-not (repl/alive?) - (repl/connect "ws://localhost:9001" :verbose true)) - -#+cljs -(defn log - "logs cljs stuff as js stuff for inspection" - [& args] - (.apply (.-log js/console) js/console (clj->js (map clj->js args)))) - -#+clj -(def log-chan (chan)) - -#+clj -(defn log - [& args] - (put! log-chan args)) - -#+clj -(go-loop [] - (when-let [args (