From 14523049774d08542e5d91d4cf875e21ca279a4a Mon Sep 17 00:00:00 2001 From: Lucio D'Alessandro Date: Wed, 4 Nov 2020 15:58:59 +0000 Subject: [PATCH] Add functionality to stop at interval --- README.md | 12 +++++++----- project.clj | 2 +- src/arco/core.cljc | 22 ++++++++++++---------- src/arco/utils.cljc | 18 ++++++++++++++---- test/core.clj | 8 ++++---- 5 files changed, 38 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 2fbb973..860d637 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Also, this library is side effect free, which makes it 100% testable, and works #### In Deps ```clojure -arco {:mvn/version "0.3.2"} +arco {:mvn/version "0.3.3"} tick {:mvn/version "0.4.27-alpha"} ``` #### In Namespace @@ -67,7 +67,7 @@ To add a custom language, you can pass a map with a `:vocabulary` key and an opt (arco/time-since ["2019-12-27T11:00:20Z" "2019-12-28T11:00:20Z"] {:vocabulary {:ago "fa" :now "ora" - ;; singular plural + ;; singular plural :second ["secondo" "secondi"] :minute ["minuto" "minuti"] :hour ["ora" "ore"] @@ -155,6 +155,8 @@ Let's add a new interval unit, being `:century`. As you can see, we provide the vocabulary for the new interval, reduce the default `:year` limit, and add a new `:century` interval by setting the `:limit` and `:seconds` keys. +If you want to stop at a specific interval i.e. 385 seconds, you can pass a `:stop-at-interval :second` key value pair in the config to remove the uneeded intervals. + ### Different Timezone? No worries! Just format the instant value before passing it to the function. @@ -181,9 +183,9 @@ There is an `arco.react` namespace that provides a reagent component to help wit [:span "Live time passed: "] [ar/time-since {:times ["2020-05-31T00:18:38.112Z"] :config {:refresh-rate 2000 - :vocabulary ...}} - (fn [formatted-t] - [:div "my formatted-t: " formatted-t])]]) + :vocabulary ...}} + (fn [formatted-t] + [:div "my formatted-t: " formatted-t])]]) ``` Note that `ar/time-to` and `ar/time-sice` accept a `:refresh-rate` key, which indicates the rate at which the react component is re-rendered expressed in milliseconds. diff --git a/project.clj b/project.clj index 9e98d0a..1ef24b4 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject arco "0.3.2" +(defproject arco "0.3.3" :description "Instant formatter to render time that has passed or is left since/to a certain event" :url "https://github.com/luciodale/arco" :license {:name "MIT"} diff --git a/src/arco/core.cljc b/src/arco/core.cljc index 6a36855..3913e94 100644 --- a/src/arco/core.cljc +++ b/src/arco/core.cljc @@ -9,7 +9,8 @@ utils/default-vocabulary (:vocabulary config)) intervals (utils/generate-intervals utils/default-intervals - (:intervals config)) + (:intervals config) + (:stop-at-interval config)) inst-now (when t-now (t/instant t-now)) seconds-from-event (utils/diff-in-seconds (t/instant t) @@ -32,10 +33,10 @@ (when (or (pos? seconds-from-event) (zero? seconds-from-event)) (utils/format-output order - (:stringify? config true) - (merge ago - {:time time-value - :interval interval-name}))))) + (:stringify? config true) + (merge ago + {:time time-value + :interval interval-name}))))) (defn time-to [[t t-now] & [config]] @@ -43,7 +44,8 @@ utils/default-vocabulary (:vocabulary config)) intervals (utils/generate-intervals utils/default-intervals - (:intervals config)) + (:intervals config) + (:stop-at-interval config)) inst-now (when t-now (t/instant t-now)) seconds-from-event (utils/diff-in-seconds @@ -68,7 +70,7 @@ (when (or (pos? seconds-from-event) (zero? seconds-from-event)) (utils/format-output order - (:stringify? config true) - (merge in - {:time time-value - :interval interval-name}))))) + (:stringify? config true) + (merge in + {:time time-value + :interval interval-name}))))) diff --git a/src/arco/utils.cljc b/src/arco/utils.cljc index 445779d..94cb496 100644 --- a/src/arco/utils.cljc +++ b/src/arco/utils.cljc @@ -28,10 +28,20 @@ (defn generate-intervals "To generate intervals in crescent order, after merging any user specific default interval." - [default-intervals intervals] - (->> intervals - (merge-with merge default-intervals) - (sort-by (comp :limit second) <))) + [default-intervals intervals stop-at-interval] + (let [merged-intervals (->> intervals + (merge-with merge default-intervals)) + stop-at-limit (:limit (get merged-intervals stop-at-interval)) + updated-intervals (if stop-at-limit + (assoc-in + (into {} + (remove (fn [[_ {:keys [limit]}]] + (> limit stop-at-limit)) + merged-intervals)) + [stop-at-interval :limit] #?(:clj Long/MAX_VALUE + :cljs js/Number.MAX_SAFE_INTEGER)) + merged-intervals)] + (sort-by (comp :limit second) < updated-intervals))) (defn diff-in-seconds "To convert time difference into raw seconds." diff --git a/test/core.clj b/test/core.clj index 6421413..855c9a8 100644 --- a/test/core.clj +++ b/test/core.clj @@ -66,19 +66,19 @@ [:month {:limit 31556926 :seconds 2629743}] [:year {:limit Long/MAX_VALUE :seconds 31556926}]] - (utils/generate-intervals utils/default-intervals {}))) + (utils/generate-intervals utils/default-intervals {} nil))) (let [intervals (utils/generate-intervals utils/default-intervals - {:hour {:limit 90000}})] + {:hour {:limit 90000}} nil)] (is (= [:hour {:limit 90000 :seconds 3600}] (nth intervals 3)))) (let [intervals (utils/generate-intervals utils/default-intervals - {:second {:limit 80 :seconds 2}})] + {:second {:limit 80 :seconds 2}} nil)] (is (= [:second {:limit 80 :seconds 2}] (second intervals)))) (let [intervals (utils/generate-intervals utils/default-intervals - {:now {:limit 30 :seconds 1}})] + {:now {:limit 30 :seconds 1}} nil)] (is (= [:now {:limit 30 :seconds 1}] (first intervals))))))