@thi.ng/date 2.4.26 → 2.5.2

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-11-01T11:05:34Z
3
+ - **Last updated**: 2023-11-09T10:28:18Z
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.34 KB
62
62
 
63
63
  ## Dependencies
64
64
 
@@ -176,14 +176,20 @@ a.eqDelta(a.add(99, "t"), 100)
176
176
  a.compare(a.add(1, "s"))
177
177
  // -1000
178
178
 
179
- // compute difference between dates (in milliseconds)
179
+ // compute (signed) difference between dates (in milliseconds)
180
180
  difference(a, "1970-01-01") === a.getTime()
181
181
  // true
182
182
 
183
- 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")
184
189
  // 31622400000
185
190
 
186
- difference("2021-02", "2020-02") / DAY
191
+ // compute abs difference in days
192
+ asDays(absDifference("2020-02", "2021-02"))
187
193
  // 366 (because 2020 was a leap year)
188
194
  ```
189
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/datetime.js CHANGED
@@ -13,6 +13,13 @@ export const dateTime = (epoch, prec) => new DateTime(epoch, prec);
13
13
  * UTC only.
14
14
  */
15
15
  export class DateTime {
16
+ t;
17
+ s;
18
+ m;
19
+ h;
20
+ d;
21
+ M;
22
+ y;
16
23
  constructor(epoch = Date.now(), prec = "t") {
17
24
  const x = ensureDate(epoch);
18
25
  const id = __precisionToID(prec);
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.26",
3
+ "version": "2.5.2",
4
4
  "description": "Datetime types, relative dates, math, iterators, composable formatters, locales",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -28,22 +28,21 @@
28
28
  "clean": "rimraf --glob '*.js' '*.d.ts' '*.map' doc internal i18n",
29
29
  "doc": "typedoc --excludePrivate --excludeInternal --out doc src/index.ts",
30
30
  "doc:ae": "mkdir -p .ae/doc .ae/temp && api-extractor run --local --verbose",
31
- "doc:readme": "yarn doc:stats && tools:readme",
32
- "doc:stats": "tools:module-stats",
31
+ "doc:readme": "bun ../../tools/src/module-stats.ts && bun ../../tools/src/readme.ts",
33
32
  "pub": "yarn npm publish --access public",
34
- "test": "testament test"
33
+ "test": "bun test"
35
34
  },
36
35
  "dependencies": {
37
- "@thi.ng/api": "^8.9.6",
38
- "@thi.ng/checks": "^3.4.6",
39
- "@thi.ng/strings": "^3.6.3"
36
+ "@thi.ng/api": "^8.9.8",
37
+ "@thi.ng/checks": "^3.4.8",
38
+ "@thi.ng/strings": "^3.6.6"
40
39
  },
41
40
  "devDependencies": {
42
- "@microsoft/api-extractor": "^7.38.0",
43
- "@thi.ng/testament": "^0.3.24",
41
+ "@microsoft/api-extractor": "^7.38.2",
42
+ "@thi.ng/testament": "^0.4.1",
44
43
  "rimraf": "^5.0.5",
45
44
  "tools": "^0.0.1",
46
- "typedoc": "^0.25.2",
45
+ "typedoc": "^0.25.3",
47
46
  "typescript": "^5.2.2"
48
47
  },
49
48
  "keywords": [
@@ -132,5 +131,5 @@
132
131
  "thi.ng": {
133
132
  "year": 2020
134
133
  },
135
- "gitHead": "351014d081cee85803950eb6751c5a02dca2efbd\n"
134
+ "gitHead": "669a3151e4302480244fe3e60eff5e732ea5b7a7\n"
136
135
  }
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