Skip to content

Commit

Permalink
Add functionality to stop at interval
Browse files Browse the repository at this point in the history
  • Loading branch information
luciodale committed Nov 4, 2020
1 parent 831efbb commit 1452304
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 24 deletions.
12 changes: 7 additions & 5 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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.
Expand All @@ -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.
2 changes: 1 addition & 1 deletion 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"}
Expand Down
22 changes: 12 additions & 10 deletions src/arco/core.cljc
Expand Up @@ -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)
Expand All @@ -32,18 +33,19 @@
(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]]
(let [vocabulary (merge-with #(or %2 %1)
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
Expand All @@ -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})))))
18 changes: 14 additions & 4 deletions src/arco/utils.cljc
Expand Up @@ -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."
Expand Down
8 changes: 4 additions & 4 deletions test/core.clj
Expand Up @@ -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))))))

Expand Down

0 comments on commit 1452304

Please sign in to comment.