|
1 | 1 | (ns ^{:doc "Contains functions to be exposed to library users."
|
2 |
| - :todo {1 "Possibility of making it available for both clj and cljs."}} |
| 2 | + :todo {1 "Possibility of making it available for both `clj` and `cljs`."}} |
3 | 3 | humane-time.core
|
4 | 4 | (:require [humane-time.ops :as ops]))
|
5 | 5 |
|
6 | 6 | (defn readable-date
|
7 |
| - "Accepts date-string only in the form of 'DD-MM-YYYY' and 'YYYY-MM-DD' formats. DD and MM could just be D or M. |
| 7 | + "Accepts date-string only in the form of `DD-MM-YYYY` and `YYYY-MM-DD` formats. `DD` and `MM` could just be `D` or `M`. |
8 | 8 | Returns a string similar to:
|
9 |
| - April 27, 2020 or Apr 27, 2020 or Monday, April 27, 2020 or Mon, April 27, 2020 or Mon, Apr 27, 2020. |
| 9 | + `April 27, 2020` or `Apr 27, 2020` or `Monday, April 27, 2020` or `Mon, April 27, 2020` or `Mon, Apr 27, 2020`. |
10 | 10 | The optional options map dictates the return format. It includes the following keys:
|
11 |
| - :day-name? - defaults to true. |
12 |
| - :short-names? - defaults to false." |
| 11 | + - `:day-name?` - defaults to `true`. |
| 12 | + - `:short-names?` - defaults to `false`." |
13 | 13 | [date-string & opts-map]
|
14 | 14 | (let [dt (ops/datetime-descriptor date-string)]
|
15 | 15 | (str (if (or (nil? (:day-name? (first opts-map))) (:day-name? (first opts-map)))
|
|
60 | 60 | :else "less than an hour")))
|
61 | 61 |
|
62 | 62 | (defn
|
63 |
| - ^{:doc "Accepts start and end values (strings) only in the form of 'DD-MM-YYYY' and 'YYYY-MM-DD' formats. DD and MM could just be D or M. |
| 63 | + ^{:doc "Accepts `start` and `end` values (strings) only in the form of `DD-MM-YYYY` and `YYYY-MM-DD` formats. `DD` and `MM` could just be `D` or `M`. |
64 | 64 | Returns a readable duration, but only in the highest unit, with lower bound of the value.
|
65 |
| - Example: if the duration is between 1 and 2 years (ex: 1 year 10 months), then it will return '1 year'. |
66 |
| - Similarly, if the duration is between 10 to 11 months, then it will return '10 months'. |
67 |
| - Takes an optional approximation-string, defaults to 'about'." |
68 |
| - :todo {1 "Use error-margin to compute approx timeline duration, rather than simply taking the lower bounds. |
69 |
| - Maybe, an alternate return value can be offered with better approximation, with an 'about' string attached in each return value." |
70 |
| - 2 "Return a string that can describe the duration as: 5 years, 10 months, 2 weeks, 4 days, and 2 hours."}} |
| 65 | + Example: if the duration is between 1 and 2 years (ex: 1 year 10 months), then it will return `1 year`. |
| 66 | + Similarly, if the duration is between 10 to 11 months, then it will return `10 months`. |
| 67 | + Takes an optional `approximation-string`, defaults to `about`." |
| 68 | + :todo {1 "Use `error-margin` to compute approx timeline duration, rather than simply taking the lower bounds. |
| 69 | + Maybe, an alternate return value can be offered with better approximation, with an `about` string attached in each return value." |
| 70 | + 2 "Return a string that can describe the duration as: `5 years, 10 months, 2 weeks, 4 days, and 2 hours.`"}} |
71 | 71 | readable-duration
|
72 | 72 | [{:keys [start end approximation-string]}]
|
73 | 73 | (cond
|
|
83 | 83 | :else (throw (ExceptionInfo. (str "Invalid inputs - :start " start ", :end " end)))))
|
84 | 84 |
|
85 | 85 | (defn readable-moment
|
86 |
| - "Accepts date-string only in the form of 'DD-MM-YYYY' and 'YYYY-MM-DD' formats. DD and MM could just be D or M. |
| 86 | + "Accepts `date-string` only in the form of `DD-MM-YYYY` and `YYYY-MM-DD` formats. `DD` and `MM` could just be `D` or `M`. |
87 | 87 | Describes a moment in histry. Useful for one-time events.
|
88 | 88 | Takes an optional moment descriptor map with the following keys:
|
89 |
| - :prefix - defaults to 'Happened'. |
90 |
| - :suffix - defaults to 'ago'. |
| 89 | + - `:prefix` - defaults to `Happened`. |
| 90 | + - `:suffix` - defaults to `ago`. |
91 | 91 | One or both the keys may be provided in the map."
|
92 | 92 | [date-string & moement-desc-map]
|
93 | 93 | (str (or (:prefix (first moement-desc-map)) "Happened")
|
|
127 | 127 | (period-helper-end end period-desc-map)))
|
128 | 128 |
|
129 | 129 | (defn readable-period
|
130 |
| - "Accepts start and end values (strings) only in the form of 'DD-MM-YYYY' and 'YYYY-MM-DD' formats. DD and MM could just be D or M. |
| 130 | + "Accepts `start` and `end` values (strings) only in the form of `DD-MM-YYYY` and `YYYY-MM-DD` formats. `DD` and `MM` could just be `D` or `M`. |
131 | 131 | Takes an optional period description map with the following keys:
|
132 |
| - :start-desc - defaults to 'Started'. |
133 |
| - :end-desc - defaults to 'Ended'. |
134 |
| - :period-desc - defaults to 'Went on for'. |
135 |
| - :approximation-string - defaults to 'about'. |
136 |
| - :past-indicator - defaults to 'ago'. |
137 |
| - :separator - defaults to ' | '. Note that there are spaces before and after the separator." |
| 132 | + - `:start-desc` - defaults to 'Started'. |
| 133 | + - `:end-desc` - defaults to 'Ended'. |
| 134 | + - `:period-desc` - defaults to 'Went on for'. |
| 135 | + - `:approximation-string` - defaults to 'about'. |
| 136 | + - `:past-indicator` - defaults to 'ago'. |
| 137 | + - `:separator` - defaults to ' | '. Note that there are spaces before and after the separator." |
138 | 138 | [{:keys [start end period-desc]}]
|
139 | 139 | (cond
|
140 | 140 | (and start (nil? end)) (period-helper-start start period-desc)
|
|
0 commit comments