Skip to content

Commit

Permalink
Add "just now" built-in support
Browse files Browse the repository at this point in the history
  • Loading branch information
luciodale committed Dec 29, 2019
1 parent fc51f42 commit f10783d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 20 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ It's also worth to point out that *Inst* is side effect free and works both on y
#### In Deps

```clojure
inst {:mvn/version "0.1.5"}
inst {:mvn/version "0.1.6"}
```

or
Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject inst "0.1.5"
(defproject inst "0.1.6"
:description "Instant formatter for time since an event occurred"
:url "https://github.com/luciodale/hint"
:license {:name "MIT"}
Expand Down
32 changes: 18 additions & 14 deletions src/inst/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
(->> intervals
(merge-with
merge
{:second {:limit 60 :seconds 1}
{:now {:limit 5 :seconds 1}
:second {:limit 60 :seconds 1}
:minute {:limit 3600 :seconds 60}
:hour {:limit 86400 :seconds 3600}
:day {:limit 604800 :seconds 86400}
Expand Down Expand Up @@ -54,30 +55,33 @@

(defn format-output
[order stringify? data]
(let []
(if stringify?
(->> order
(map #(get data %))
(string/join " "))
data)))
(if stringify?
(->> order
(map #(get data %))
(string/join " "))
data))

(defn time-since
([ts]
(time-since ts {}))
([[t t-now] config]
(let [intervals (generate-intervals (:intervals config))
(let [vocabulary (merge {:now ["just now" "just now"]}
(:vocabulary config))
intervals (generate-intervals (:intervals config))
inst-now (when t-now (t/instant t-now))
seconds-from-event (diff-in-seconds (t/instant t) (or inst-now (t/instant)))
abs-seconds (Math/abs seconds-from-event)
interval (find-interval intervals abs-seconds)
time-value (time-value abs-seconds interval)
interval-name (interval-name (:vocabulary config) interval time-value)
interval-name (interval-name vocabulary interval time-value)
past? (pos? seconds-from-event)
adverb-preposition-map (adverb-preposition-map (:vocabulary config) past?)]
(format-output (if past?
(:past config [:time :interval :ago])
(:future config [:in :time :interval]))
now? (= :now (first interval))
adverb-preposition-map (adverb-preposition-map vocabulary past?)]
(format-output (cond
now? [:interval]
past? (:past config [:time :interval :ago])
:else (:future config [:in :time :interval]))
(:stringify? config true)
(merge {:time time-value
:interval interval-name}
adverb-preposition-map)))))
(when-not now? adverb-preposition-map))))))
12 changes: 8 additions & 4 deletions test/core.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@

(deftest find-interval
(testing "Find interval based on limit value"
(let [intervals [[:second {:limit 60 :seconds 1}]
(let [intervals [[:now {:limit 5 :seconds 1}]
[:second {:limit 60 :seconds 1}]
[:minute {:limit 3600 :seconds 60}]
[:hour {:limit 86400 :seconds 3600}]
[:day {:limit 604800 :seconds 86400}]
Expand All @@ -53,7 +54,8 @@

(deftest generate-intervals
(testing "Sorted intervals"
(let [intervals [[:second {:limit 60 :seconds 1}]
(let [intervals [[:now {:limit 5 :seconds 1}]
[:second {:limit 60 :seconds 1}]
[:minute {:limit 3600 :seconds 60}]
[:hour {:limit 86400 :seconds 3600}]
[:day {:limit 604800 :seconds 86400}]
Expand All @@ -64,9 +66,11 @@
:seconds 31556926}]]]
(is (= intervals (inst/generate-intervals {}))))
(let [intervals (inst/generate-intervals {:hour {:limit 90000}})]
(is (= [:hour {:limit 90000 :seconds 3600}] (nth intervals 2))))
(is (= [:hour {:limit 90000 :seconds 3600}] (nth intervals 3))))
(let [intervals (inst/generate-intervals {:second {:limit 80 :seconds 2}})]
(is (= [:second {:limit 80 :seconds 2}] (first intervals))))))
(is (= [:second {:limit 80 :seconds 2}] (second intervals))))
(let [intervals (inst/generate-intervals {:now {:limit 30 :seconds 1}})]
(is (= [:now {:limit 30 :seconds 1}] (first intervals))))))

(deftest format-output
(testing "Test different formattings"
Expand Down

0 comments on commit f10783d

Please sign in to comment.