@thi.ng/date 2.4.25 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2023-10-30T14:31:56Z
3
+ - **Last updated**: 2023-11-04T22:20:11Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,13 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [2.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/date@2.5.0) (2023-11-04)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add durationAs() & asXXX() duration helpers ([ad17cee](https://github.com/thi-ng/umbrella/commit/ad17cee))
17
+ - add absDifference(), add/update docs ([9b54f6c](https://github.com/thi-ng/umbrella/commit/9b54f6c))
18
+
12
19
  ### [2.4.10](https://github.com/thi-ng/umbrella/tree/@thi.ng/date@2.4.10) (2023-03-27)
13
20
 
14
21
  #### ♻️ Refactoring
package/README.md CHANGED
@@ -58,7 +58,7 @@ For Node.js REPL:
58
58
  const date = await import("@thi.ng/date");
59
59
  ```
60
60
 
61
- Package sizes (brotli'd, pre-treeshake): ESM: 5.12 KB
61
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.25 KB
62
62
 
63
63
  ## Dependencies
64
64
 
@@ -72,11 +72,12 @@ Several projects in this repo's
72
72
  [/examples](https://github.com/thi-ng/umbrella/tree/develop/examples)
73
73
  directory are using this package:
74
74
 
75
- | Screenshot | Description | Live demo | Source |
76
- |:----------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------|:----------------------------------------------------|:----------------------------------------------------------------------------------|
77
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/commit-heatmap.png" width="240"/> | Heatmap visualization of this mono-repo's commits | | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/commit-heatmap) |
78
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/mastodon-feed.jpg" width="240"/> | Mastodon API feed reader with support for different media types, fullscreen media modal, HTML rewriting | [Demo](https://demo.thi.ng/umbrella/mastodon-feed/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/mastodon-feed) |
79
- | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-todo-list.png" width="240"/> | Zig-based To-Do list, DOM creation, local storage task persistence | [Demo](https://demo.thi.ng/umbrella/zig-todo-list/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-todo-list) |
75
+ | Screenshot | Description | Live demo | Source |
76
+ |:----------------------------------------------------------------------------------------------------------------------|:--------------------------------------------------------------------------------------------------------|:-----------------------------------------------------|:----------------------------------------------------------------------------------|
77
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/commit-heatmap.png" width="240"/> | Heatmap visualization of this mono-repo's commits | | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/commit-heatmap) |
78
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/mastodon-feed.jpg" width="240"/> | Mastodon API feed reader with support for different media types, fullscreen media modal, HTML rewriting | [Demo](https://demo.thi.ng/umbrella/mastodon-feed/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/mastodon-feed) |
79
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/pointfree-geom.jpg" width="240"/> | Live coding playground for 2D geometry generation using @thi.ng/pointfree-lang | [Demo](https://demo.thi.ng/umbrella/pointfree-geom/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/pointfree-geom) |
80
+ | <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-todo-list.png" width="240"/> | Zig-based To-Do list, DOM creation, local storage task persistence | [Demo](https://demo.thi.ng/umbrella/zig-todo-list/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-todo-list) |
80
81
 
81
82
  ## API
82
83
 
@@ -175,14 +176,20 @@ a.eqDelta(a.add(99, "t"), 100)
175
176
  a.compare(a.add(1, "s"))
176
177
  // -1000
177
178
 
178
- // compute difference between dates (in milliseconds)
179
+ // compute (signed) difference between dates (in milliseconds)
179
180
  difference(a, "1970-01-01") === a.getTime()
180
181
  // true
181
182
 
182
- difference("2021-02", "2020-02")
183
+ // difference = a - b
184
+ difference("2020-02", "2021-02")
185
+ // -31622400000
186
+
187
+ // always produces unsigned result
188
+ absDifference("2020-02", "2021-02")
183
189
  // 31622400000
184
190
 
185
- difference("2021-02", "2020-02") / DAY
191
+ // compute abs difference in days
192
+ asDays(absDifference("2020-02", "2021-02"))
186
193
  // 366 (because 2020 was a leap year)
187
194
  ```
188
195
 
package/api.d.ts CHANGED
@@ -158,6 +158,11 @@ export type EpochIterator = IterableIterator<number>;
158
158
  */
159
159
  export type Precision = "y" | "M" | "d" | "h" | "m" | "s" | "t";
160
160
  export type Period = Precision | "w" | "q";
161
+ /**
162
+ * Object which maps {@link Period} IDs to their respective values (in
163
+ * milliseconds).
164
+ */
165
+ export declare const PERIODS: Record<Period, number>;
161
166
  export type FormatFn = Fn2<Date, boolean, string>;
162
167
  export type MaybeDate = DateTime | Date | number | string;
163
168
  /**
package/api.js CHANGED
@@ -40,3 +40,18 @@ export const YEAR = (DAYS_IN_400YEARS / 400) * DAY;
40
40
  * Mean month duration (30.436875 days) in milliseconds
41
41
  */
42
42
  export const MONTH = YEAR / 12;
43
+ /**
44
+ * Object which maps {@link Period} IDs to their respective values (in
45
+ * milliseconds).
46
+ */
47
+ export const PERIODS = {
48
+ y: YEAR,
49
+ M: MONTH,
50
+ d: DAY,
51
+ h: HOUR,
52
+ m: MINUTE,
53
+ s: SECOND,
54
+ t: 1,
55
+ w: WEEK,
56
+ q: 3 * MONTH,
57
+ };
package/duration.d.ts CHANGED
@@ -19,4 +19,59 @@ export declare const decomposeDuration: (dur: number) => number[];
19
19
  * @param parts
20
20
  */
21
21
  export declare const composeDuration: (parts: Partial<Record<Precision, number>>) => number;
22
+ /**
23
+ * Calculates the given duration in units of given `prec`ision.
24
+ *
25
+ * @example
26
+ * ```ts
27
+ * durationAs("d", difference("2023-02-01T12:00:00Z", "2023-01-01"))
28
+ * // 31.5
29
+ * ```
30
+ *
31
+ * @param prec
32
+ * @param dur
33
+ */
34
+ export declare const durationAs: (prec: Precision, dur: number) => number;
35
+ /**
36
+ * Returns duration in seconds.
37
+ *
38
+ * @param dur
39
+ */
40
+ export declare const asSeconds: (dur: number) => number;
41
+ /**
42
+ * Returns duration in minutes.
43
+ *
44
+ * @param dur
45
+ */
46
+ export declare const asMinutes: (dur: number) => number;
47
+ /**
48
+ * Returns duration in hours.
49
+ *
50
+ * @param dur
51
+ */
52
+ export declare const asHours: (dur: number) => number;
53
+ /**
54
+ * Returns duration in days.
55
+ *
56
+ * @param dur
57
+ */
58
+ export declare const asDays: (dur: number) => number;
59
+ /**
60
+ * Returns duration in weeks.
61
+ *
62
+ * @param dur
63
+ */
64
+ export declare const asWeeks: (dur: number) => number;
65
+ /**
66
+ * Returns duration in months (as defined by {@link MONTH})..
67
+ *
68
+ * @param dur
69
+ */
70
+ export declare const asMonths: (dur: number) => number;
71
+ /**
72
+ * Returns duration in years (as defined by {@link YEAR}).
73
+ *
74
+ * @param dur
75
+ */
76
+ export declare const asYears: (dur: number) => number;
22
77
  //# sourceMappingURL=duration.d.ts.map
package/duration.js CHANGED
@@ -1,4 +1,4 @@
1
- import { YEAR, MONTH, DAY, HOUR, MINUTE, SECOND, } from "./api.js";
1
+ import { DAY, HOUR, MINUTE, MONTH, PERIODS, SECOND, WEEK, YEAR, } from "./api.js";
2
2
  /**
3
3
  * Decomposes given duration (in milliseconds) into a tuple of: `[year, month,
4
4
  * day, hour, minute, second, millis]`.
@@ -42,3 +42,58 @@ export const composeDuration = (parts) => {
42
42
  dur += parts.t || 0;
43
43
  return dur;
44
44
  };
45
+ /**
46
+ * Calculates the given duration in units of given `prec`ision.
47
+ *
48
+ * @example
49
+ * ```ts
50
+ * durationAs("d", difference("2023-02-01T12:00:00Z", "2023-01-01"))
51
+ * // 31.5
52
+ * ```
53
+ *
54
+ * @param prec
55
+ * @param dur
56
+ */
57
+ export const durationAs = (prec, dur) => dur / PERIODS[prec];
58
+ /**
59
+ * Returns duration in seconds.
60
+ *
61
+ * @param dur
62
+ */
63
+ export const asSeconds = (dur) => dur / SECOND;
64
+ /**
65
+ * Returns duration in minutes.
66
+ *
67
+ * @param dur
68
+ */
69
+ export const asMinutes = (dur) => dur / MINUTE;
70
+ /**
71
+ * Returns duration in hours.
72
+ *
73
+ * @param dur
74
+ */
75
+ export const asHours = (dur) => dur / HOUR;
76
+ /**
77
+ * Returns duration in days.
78
+ *
79
+ * @param dur
80
+ */
81
+ export const asDays = (dur) => dur / DAY;
82
+ /**
83
+ * Returns duration in weeks.
84
+ *
85
+ * @param dur
86
+ */
87
+ export const asWeeks = (dur) => dur / WEEK;
88
+ /**
89
+ * Returns duration in months (as defined by {@link MONTH})..
90
+ *
91
+ * @param dur
92
+ */
93
+ export const asMonths = (dur) => dur / MONTH;
94
+ /**
95
+ * Returns duration in years (as defined by {@link YEAR}).
96
+ *
97
+ * @param dur
98
+ */
99
+ export const asYears = (dur) => dur / YEAR;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/date",
3
- "version": "2.4.25",
3
+ "version": "2.5.0",
4
4
  "description": "Datetime types, relative dates, math, iterators, composable formatters, locales",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -36,7 +36,7 @@
36
36
  "dependencies": {
37
37
  "@thi.ng/api": "^8.9.6",
38
38
  "@thi.ng/checks": "^3.4.6",
39
- "@thi.ng/strings": "^3.6.3"
39
+ "@thi.ng/strings": "^3.6.4"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@microsoft/api-extractor": "^7.38.0",
@@ -132,5 +132,5 @@
132
132
  "thi.ng": {
133
133
  "year": 2020
134
134
  },
135
- "gitHead": "bfa16829786146bd24df3cdbd44649a45a603e44\n"
135
+ "gitHead": "336bd1bf95825b3c318a3ab49c54451c94aaa883\n"
136
136
  }
package/relative.d.ts CHANGED
@@ -50,12 +50,25 @@ export declare const parseRelative: (offset: string, base?: MaybeDate) => DateTi
50
50
  export declare const relative: (num: number, period: Period, base?: MaybeDate) => DateTime;
51
51
  /**
52
52
  * Returns the signed difference in milliseconds between given two dates `a` and
53
- * `b`.
53
+ * `b` (as `diff = a - b`).
54
+ *
55
+ * @remarks
56
+ * Also see {@link absDifference}.
54
57
  *
55
58
  * @param a -
56
59
  * @param b -
57
60
  */
58
61
  export declare const difference: (a: MaybeDate, b: MaybeDate) => number;
62
+ /**
63
+ * Returns the unsigned difference in milliseconds between given dates.
64
+ *
65
+ * @remarks
66
+ * Also see {@link difference} for signed difference.
67
+ *
68
+ * @param a
69
+ * @param b
70
+ */
71
+ export declare const absDifference: (a: MaybeDate, b: MaybeDate) => number;
59
72
  /**
60
73
  * Computes and decomposes difference between given dates. Returns tuple of:
61
74
  * `[sign, years, months, days, hours, mins, secs, millis]`. The `sign` is used
package/relative.js CHANGED
@@ -106,12 +106,25 @@ const parsePeriod = (x) => {
106
106
  export const relative = (num, period, base = dateTime()) => dateTime(base).add(num, period);
107
107
  /**
108
108
  * Returns the signed difference in milliseconds between given two dates `a` and
109
- * `b`.
109
+ * `b` (as `diff = a - b`).
110
+ *
111
+ * @remarks
112
+ * Also see {@link absDifference}.
110
113
  *
111
114
  * @param a -
112
115
  * @param b -
113
116
  */
114
117
  export const difference = (a, b) => ensureEpoch(a) - ensureEpoch(b);
118
+ /**
119
+ * Returns the unsigned difference in milliseconds between given dates.
120
+ *
121
+ * @remarks
122
+ * Also see {@link difference} for signed difference.
123
+ *
124
+ * @param a
125
+ * @param b
126
+ */
127
+ export const absDifference = (a, b) => Math.abs(difference(a, b));
115
128
  /**
116
129
  * Computes and decomposes difference between given dates. Returns tuple of:
117
130
  * `[sign, years, months, days, hours, mins, secs, millis]`. The `sign` is used