@bytebury/toolkit 1.0.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.
Files changed (129) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +49 -0
  3. package/esm/_dnt.test_polyfills.d.ts.map +1 -0
  4. package/esm/_dnt.test_shims.d.ts.map +1 -0
  5. package/esm/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
  6. package/esm/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
  7. package/esm/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
  8. package/esm/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
  9. package/esm/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
  10. package/esm/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
  11. package/esm/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
  12. package/esm/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
  13. package/esm/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
  14. package/esm/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
  15. package/esm/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
  16. package/esm/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
  17. package/esm/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
  18. package/esm/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
  19. package/esm/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
  20. package/esm/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
  21. package/esm/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
  22. package/esm/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
  23. package/esm/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
  24. package/esm/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
  25. package/esm/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
  26. package/esm/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
  27. package/esm/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
  28. package/esm/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
  29. package/esm/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
  30. package/esm/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
  31. package/esm/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
  32. package/esm/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
  33. package/esm/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
  34. package/esm/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
  35. package/esm/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
  36. package/esm/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
  37. package/esm/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
  38. package/esm/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
  39. package/esm/mod.d.ts +7 -0
  40. package/esm/mod.d.ts.map +1 -0
  41. package/esm/mod.js +6 -0
  42. package/esm/package.json +3 -0
  43. package/esm/src/core.d.ts +297 -0
  44. package/esm/src/core.d.ts.map +1 -0
  45. package/esm/src/core.js +256 -0
  46. package/esm/src/core_test.d.ts.map +1 -0
  47. package/esm/src/dates.d.ts +113 -0
  48. package/esm/src/dates.d.ts.map +1 -0
  49. package/esm/src/dates.js +197 -0
  50. package/esm/src/dates_test.d.ts.map +1 -0
  51. package/esm/src/duration.d.ts +102 -0
  52. package/esm/src/duration.d.ts.map +1 -0
  53. package/esm/src/duration.js +110 -0
  54. package/esm/src/duration_test.d.ts.map +1 -0
  55. package/esm/src/numbers.d.ts +58 -0
  56. package/esm/src/numbers.d.ts.map +1 -0
  57. package/esm/src/numbers.js +76 -0
  58. package/esm/src/numbers_test.d.ts.map +1 -0
  59. package/esm/src/strings.d.ts +164 -0
  60. package/esm/src/strings.d.ts.map +1 -0
  61. package/esm/src/strings.js +196 -0
  62. package/esm/src/strings_test.d.ts.map +1 -0
  63. package/esm/src/utility_types.d.ts +117 -0
  64. package/esm/src/utility_types.d.ts.map +1 -0
  65. package/esm/src/utility_types.js +1 -0
  66. package/package.json +28 -0
  67. package/script/_dnt.test_polyfills.d.ts.map +1 -0
  68. package/script/_dnt.test_shims.d.ts.map +1 -0
  69. package/script/deps/jsr.io/@std/assert/1.0.16/almost_equals.d.ts.map +1 -0
  70. package/script/deps/jsr.io/@std/assert/1.0.16/array_includes.d.ts.map +1 -0
  71. package/script/deps/jsr.io/@std/assert/1.0.16/assert.d.ts.map +1 -0
  72. package/script/deps/jsr.io/@std/assert/1.0.16/assertion_error.d.ts.map +1 -0
  73. package/script/deps/jsr.io/@std/assert/1.0.16/equal.d.ts.map +1 -0
  74. package/script/deps/jsr.io/@std/assert/1.0.16/equals.d.ts.map +1 -0
  75. package/script/deps/jsr.io/@std/assert/1.0.16/exists.d.ts.map +1 -0
  76. package/script/deps/jsr.io/@std/assert/1.0.16/fail.d.ts.map +1 -0
  77. package/script/deps/jsr.io/@std/assert/1.0.16/false.d.ts.map +1 -0
  78. package/script/deps/jsr.io/@std/assert/1.0.16/greater.d.ts.map +1 -0
  79. package/script/deps/jsr.io/@std/assert/1.0.16/greater_or_equal.d.ts.map +1 -0
  80. package/script/deps/jsr.io/@std/assert/1.0.16/instance_of.d.ts.map +1 -0
  81. package/script/deps/jsr.io/@std/assert/1.0.16/is_error.d.ts.map +1 -0
  82. package/script/deps/jsr.io/@std/assert/1.0.16/less.d.ts.map +1 -0
  83. package/script/deps/jsr.io/@std/assert/1.0.16/less_or_equal.d.ts.map +1 -0
  84. package/script/deps/jsr.io/@std/assert/1.0.16/match.d.ts.map +1 -0
  85. package/script/deps/jsr.io/@std/assert/1.0.16/mod.d.ts.map +1 -0
  86. package/script/deps/jsr.io/@std/assert/1.0.16/not_equals.d.ts.map +1 -0
  87. package/script/deps/jsr.io/@std/assert/1.0.16/not_instance_of.d.ts.map +1 -0
  88. package/script/deps/jsr.io/@std/assert/1.0.16/not_match.d.ts.map +1 -0
  89. package/script/deps/jsr.io/@std/assert/1.0.16/not_strict_equals.d.ts.map +1 -0
  90. package/script/deps/jsr.io/@std/assert/1.0.16/object_match.d.ts.map +1 -0
  91. package/script/deps/jsr.io/@std/assert/1.0.16/rejects.d.ts.map +1 -0
  92. package/script/deps/jsr.io/@std/assert/1.0.16/strict_equals.d.ts.map +1 -0
  93. package/script/deps/jsr.io/@std/assert/1.0.16/string_includes.d.ts.map +1 -0
  94. package/script/deps/jsr.io/@std/assert/1.0.16/throws.d.ts.map +1 -0
  95. package/script/deps/jsr.io/@std/assert/1.0.16/unimplemented.d.ts.map +1 -0
  96. package/script/deps/jsr.io/@std/assert/1.0.16/unreachable.d.ts.map +1 -0
  97. package/script/deps/jsr.io/@std/internal/1.0.12/build_message.d.ts.map +1 -0
  98. package/script/deps/jsr.io/@std/internal/1.0.12/diff.d.ts.map +1 -0
  99. package/script/deps/jsr.io/@std/internal/1.0.12/diff_str.d.ts.map +1 -0
  100. package/script/deps/jsr.io/@std/internal/1.0.12/format.d.ts.map +1 -0
  101. package/script/deps/jsr.io/@std/internal/1.0.12/styles.d.ts.map +1 -0
  102. package/script/deps/jsr.io/@std/internal/1.0.12/types.d.ts.map +1 -0
  103. package/script/mod.d.ts +7 -0
  104. package/script/mod.d.ts.map +1 -0
  105. package/script/mod.js +22 -0
  106. package/script/package.json +3 -0
  107. package/script/src/core.d.ts +297 -0
  108. package/script/src/core.d.ts.map +1 -0
  109. package/script/src/core.js +279 -0
  110. package/script/src/core_test.d.ts.map +1 -0
  111. package/script/src/dates.d.ts +113 -0
  112. package/script/src/dates.d.ts.map +1 -0
  113. package/script/src/dates.js +224 -0
  114. package/script/src/dates_test.d.ts.map +1 -0
  115. package/script/src/duration.d.ts +102 -0
  116. package/script/src/duration.d.ts.map +1 -0
  117. package/script/src/duration.js +114 -0
  118. package/script/src/duration_test.d.ts.map +1 -0
  119. package/script/src/numbers.d.ts +58 -0
  120. package/script/src/numbers.d.ts.map +1 -0
  121. package/script/src/numbers.js +81 -0
  122. package/script/src/numbers_test.d.ts.map +1 -0
  123. package/script/src/strings.d.ts +164 -0
  124. package/script/src/strings.d.ts.map +1 -0
  125. package/script/src/strings.js +209 -0
  126. package/script/src/strings_test.d.ts.map +1 -0
  127. package/script/src/utility_types.d.ts +117 -0
  128. package/script/src/utility_types.d.ts.map +1 -0
  129. package/script/src/utility_types.js +2 -0
@@ -0,0 +1,256 @@
1
+ import { isWhitespace, lower } from "./strings.js";
2
+ /**
3
+ * Clone an object using structuredClone.
4
+ */
5
+ export function clone(obj) {
6
+ return structuredClone(obj);
7
+ }
8
+ /**
9
+ * Compares two things by turning them into strings,
10
+ * and comparing them by their string value.
11
+ *
12
+ * @example
13
+ * ```ts
14
+ * isEqual("1", 1); // true
15
+ * isEqual({foo: "bar"}, {foo: "bar"}); // true
16
+ * isEqual([], []); // true
17
+ * isEqual([0], [1]); // false
18
+ * isEqual(false, " false "); // false
19
+ * isEqual(false, "FALSE"); // false
20
+ * ````
21
+ */
22
+ export function isEqual(thing1, thing2) {
23
+ return stringify(thing1) === stringify(thing2);
24
+ }
25
+ export function first(value) {
26
+ return value[0];
27
+ }
28
+ export function last(value) {
29
+ return value[value.length - 1];
30
+ }
31
+ /**
32
+ * Compares two things by turning them into strings,
33
+ * and comparing them by their string value.
34
+ *
35
+ * @remarks this is the inverse of `isEqual`.
36
+ *
37
+ * @example
38
+ * ```ts
39
+ * isNotEqual("1", 1); // true
40
+ * isNotEqual({foo: "bar"}, {foo: "bar"}); // true
41
+ * isNotEqual([], []); // true
42
+ * isNotEqual([0], [1]); // false
43
+ * isNotEqual(false, " false "); // true
44
+ * isNotEqual(false, "FALSE"); // false
45
+ * ```
46
+ */
47
+ export function isNotEqual(thing1, thing2) {
48
+ return !isEqual(thing1, thing2);
49
+ }
50
+ /**
51
+ * Compares two things by turning them into strings and lowercasing
52
+ * them, and comparing the string values. Works exactly like `isEqual` except
53
+ * will lowercase both things before comparing.
54
+ *
55
+ * @example
56
+ * ```ts
57
+ * isEqualIgnoreCase("1", 1); // true
58
+ * isEqualIgnoreCase({foo: "bar"}, {foo: "bar"}); // true
59
+ * isEqualIgnoreCase([], []); // true
60
+ * isEqualIgnoreCase([0], [1]); // false
61
+ * isEqualIgnoreCase(false, " false "); // false
62
+ * isEqualIgnoreCase(false, "FALSE"); // true
63
+ * ```
64
+ */
65
+ export function isEqualIgnoreCase(thing1, thing2) {
66
+ thing1 = lower(stringify(thing1));
67
+ thing2 = lower(stringify(thing2));
68
+ return thing1 === thing2;
69
+ }
70
+ /**
71
+ * Compares two things by turning them into strings, trimming and lowercasing
72
+ * them, and comparing the string values. Works exactly like `isEqual` except
73
+ * will lowercase both things before comparing.
74
+ *
75
+ * @example
76
+ * ```ts
77
+ * isNotEqualIgnoreCase("1", 1); // false
78
+ * isNotEqualIgnoreCase({foo: "bar"}, {foo: "bar"}); // false
79
+ * isNotEqualIgnoreCase([], []); // false
80
+ * isNotEqualIgnoreCase([0], [1]); // true
81
+ * isNotEqualIgnoreCase(false, " false "); // true
82
+ * isNotEqualIgnoreCase(false, "FALSE"); // false
83
+ * ```
84
+ */
85
+ export function isNotEqualIgnoreCase(thing1, thing2) {
86
+ return !isEqualIgnoreCase(thing1, thing2);
87
+ }
88
+ /**
89
+ * Converts the given parameter into the string equivalent.
90
+ *
91
+ * If the thing provided has the type of "object", then this function
92
+ * returns `JSON.stringify(thing)`. Otherwise, it will wrap the `thing`
93
+ * in a String and convert it to it's string representation.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * stringify({ foo: "bar" }); // "{ "foo": "bar" }"
98
+ * stringify([1, 2, 3]); // "[1, 2, 3]"
99
+ * stringify(1); // "1"
100
+ * ```
101
+ */
102
+ export function stringify(thing) {
103
+ return typeof thing === "object" ? JSON.stringify(thing) : String(thing);
104
+ }
105
+ export function reverse(thing) {
106
+ if (typeof thing === "string")
107
+ return thing.split("").reverse().join("");
108
+ if (thing instanceof Set)
109
+ return new Set([...thing].reverse());
110
+ return thing.reverse();
111
+ }
112
+ export function isEmpty(thing) {
113
+ if (isNone(thing))
114
+ return true;
115
+ if (typeof thing === "string")
116
+ return isWhitespace(thing);
117
+ if (Array.isArray(thing))
118
+ return thing.length === 0;
119
+ if (thing instanceof Map || thing instanceof Set)
120
+ return thing.size === 0;
121
+ if (typeof thing === "object")
122
+ return Object.keys(thing).length === 0;
123
+ return false;
124
+ }
125
+ export function isNotEmpty(thing) {
126
+ return !isEmpty(thing);
127
+ }
128
+ /**
129
+ * Returns the distinct values from a list.
130
+ *
131
+ * @remarks same as `distinct`.
132
+ *
133
+ * @example
134
+ * ```ts
135
+ * const myList = [1, 2, 3, 3];
136
+ * unique(myList); // [1, 2, 3];
137
+ * ```
138
+ */
139
+ export function unique(list) {
140
+ return [...new Set(list)];
141
+ }
142
+ /**
143
+ * Returns the distinct values from a list.
144
+ *
145
+ * @remarks alias for `unique`.
146
+ *
147
+ * @example
148
+ * ```ts
149
+ * const myList = [1, 2, 3, 3];
150
+ * distinct(myList); // [1, 2, 3];
151
+ * ```
152
+ */
153
+ export function distinct(list) {
154
+ return unique(list);
155
+ }
156
+ /**
157
+ * Pick a random item from an array.
158
+ *
159
+ * @remarks
160
+ * If you pass an empty list, then this function will return `undefined`.
161
+ *
162
+ * @example
163
+ * ```ts
164
+ * const myList = [1, 2, 3, 4];
165
+ * const randomItem = sample(myList);
166
+ *
167
+ * console.log(randomItem); // could be 1, 2, 3, or 4
168
+ * console.log(sample([])); // undefined
169
+ * ```
170
+ */
171
+ export function sample(list) {
172
+ return list[Math.floor(Math.random() * list.length)];
173
+ }
174
+ /**
175
+ * Gives a random number in the given range. The first parameter is inclusive
176
+ * and the second one is exclusive. Therefore, it will work with lists out of
177
+ * the box.
178
+ *
179
+ * @example
180
+ * ```ts
181
+ * rand(0, 10); // 0 -> 9
182
+ * rand(3, 7); // 3 -> 6
183
+ * ```
184
+ */
185
+ export function rand(start, end) {
186
+ return Math.floor(Math.random() * (end - start)) + start;
187
+ }
188
+ /**
189
+ * Determines if the given value is truthy.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * truthy(true); // true
194
+ * truthy(false); // false
195
+ * truthy(''); // false
196
+ * ```
197
+ */
198
+ export function truthy(thing) {
199
+ return Boolean(thing) === true;
200
+ }
201
+ /**
202
+ * Determines if the given value is falsy.
203
+ *
204
+ * @example
205
+ * ```ts
206
+ * falsy(true); // false
207
+ * falsy(false); // true
208
+ * falsy(''); // true
209
+ * ```
210
+ */
211
+ export function falsy(thing) {
212
+ return !truthy(thing);
213
+ }
214
+ /**
215
+ * Returns true if the given value is not null or undefined.
216
+ *
217
+ * @example
218
+ * ```ts
219
+ * isSome(null); // false
220
+ * isSome(undefined); // false
221
+ * isSome(0); // true
222
+ * isSome({}); // true
223
+ * ```
224
+ */
225
+ export function isSome(thing) {
226
+ return !isNone(thing);
227
+ }
228
+ /**
229
+ * Returns true if the given value is null or undefined.
230
+ *
231
+ * @example
232
+ * ```ts
233
+ * isNone(null); // true
234
+ * isNone(undefined); // true
235
+ * isNone(true); // false
236
+ * isNone(0); // false
237
+ * ```
238
+ */
239
+ export function isNone(thing) {
240
+ return thing === null || thing === undefined;
241
+ }
242
+ // deno-lint-ignore no-explicit-any
243
+ export function sum(list, key) {
244
+ if (list.length === 0)
245
+ return 0;
246
+ if (typeof list[0] === "number") {
247
+ return list.reduce((a, v) => a + v, 0);
248
+ }
249
+ return list.reduce(
250
+ // deno-lint-ignore no-explicit-any
251
+ (a, v) => a + v[key], 0);
252
+ }
253
+ // deno-lint-ignore no-explicit-any
254
+ export function average(list, key) {
255
+ return (sum(list, key) / list.length) || 0;
256
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"core_test.d.ts","sourceRoot":"","sources":["../../src/src/core_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}
@@ -0,0 +1,113 @@
1
+ /**
2
+ * Right now. This is an alias for `new Date()`.
3
+ */
4
+ export declare function now(): Date;
5
+ /**
6
+ * Today's date at midnight.
7
+ */
8
+ export declare function today(): Date;
9
+ /**
10
+ * Tomorrow's date at midnight.
11
+ */
12
+ export declare function tomorrow(): Date;
13
+ /**
14
+ * Yesterday's date at midnight.
15
+ */
16
+ export declare function yesterday(): Date;
17
+ /**
18
+ * Adds the given amount of days to the specified date.
19
+ */
20
+ export declare function addDays(date: Date, days: number): Date;
21
+ /**
22
+ * Subtracts the given amount of days from the specified date.
23
+ */
24
+ export declare function subtractDays(date: Date, days: number): Date;
25
+ /**
26
+ * Adds the given amount of months to the specified date.
27
+ */
28
+ export declare function addMonths(date: Date, months: number): Date;
29
+ /**
30
+ * Subtracts the given amount of months from the specified date.
31
+ */
32
+ export declare function subtractMonths(date: Date, months: number): Date;
33
+ /**
34
+ * Adds the given amount of years to the specified date.
35
+ */
36
+ export declare function addYears(date: Date, years: number): Date;
37
+ /**
38
+ * Subtracts the given amount of yers from the specified date.
39
+ */
40
+ export declare function subtractYears(date: Date, years: number): Date;
41
+ /**
42
+ * Calculates the days between two dates.
43
+ */
44
+ export declare function daysBetween(start: Date, end: Date): number;
45
+ /**
46
+ * Calculates the months between two dates.
47
+ */
48
+ export declare function monthsBetween(start: Date, end: Date): number;
49
+ /**
50
+ * Calculates the years between two dates.
51
+ * The date must be equal or past for it to count as a full year.
52
+ *
53
+ * @example
54
+ * ```ts
55
+ * yearsBetween(new Date('2007-08-01', '2008-07-31')); // 0
56
+ * yearsBetween(new Date('2007-08-01', '2008-08-01')); // 1
57
+ * ```
58
+ */
59
+ export declare function yearsBetween(start: Date, end: Date): number;
60
+ /**
61
+ * Checks if a date is a Sunday.
62
+ */
63
+ export declare function isSunday(date: Date): boolean;
64
+ /**
65
+ * Checks if a date is a Monday.
66
+ */
67
+ export declare function isMonday(date: Date): boolean;
68
+ /**
69
+ * Checks if a date is a Tuesday.
70
+ */
71
+ export declare function isTuesday(date: Date): boolean;
72
+ /**
73
+ * Checks if a date is a Wednesday.
74
+ */
75
+ export declare function isWednesday(date: Date): boolean;
76
+ /**
77
+ * Checks if a date is a Thursday.
78
+ */
79
+ export declare function isThursday(date: Date): boolean;
80
+ /**
81
+ * Checks if a date is a Friday.
82
+ */
83
+ export declare function isFriday(date: Date): boolean;
84
+ /**
85
+ * Checks if a date is a Saturday.
86
+ */
87
+ export declare function isSaturday(date: Date): boolean;
88
+ /**
89
+ * Checks if a date is a weekend.
90
+ */
91
+ export declare function isWeekend(date: Date): boolean;
92
+ /**
93
+ * Checks if a date is a weekday.
94
+ */
95
+ export declare function isWeekday(date: Date): boolean;
96
+ /**
97
+ * Determines if the date is in the past.
98
+ */
99
+ export declare function isInPast(date: Date): boolean;
100
+ /**
101
+ * Determines if the date is in the future.
102
+ *
103
+ * @remarks
104
+ * this is date specific. So, if it is 1:00 PM and you pass
105
+ * a date that is today at 2:00 PM, it will return false, because
106
+ * the "date" is the same.
107
+ */
108
+ export declare function isInFuture(date: Date): boolean;
109
+ /**
110
+ * Determines if the date is today.
111
+ */
112
+ export declare function isToday(date: Date): boolean;
113
+ //# sourceMappingURL=dates.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dates.d.ts","sourceRoot":"","sources":["../../src/src/dates.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,wBAAgB,GAAG,IAAI,IAAI,CAE1B;AAED;;GAEG;AACH,wBAAgB,KAAK,IAAI,IAAI,CAI5B;AAED;;GAEG;AACH,wBAAgB,QAAQ,IAAI,IAAI,CAI/B;AAED;;GAEG;AACH,wBAAgB,SAAS,IAAI,IAAI,CAEhC;AAED;;GAEG;AACH,wBAAgB,OAAO,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CAE9C;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE5C;AAED;;GAEG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE9C;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE7C;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE7C;AAED;;GAEG;AACH,wBAAgB,QAAQ,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE5C;AAED;;;;;;;GAOG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE9C;AAED;;GAEG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAE3C"}
@@ -0,0 +1,197 @@
1
+ import { Duration } from "./duration.js";
2
+ /**
3
+ * Right now. This is an alias for `new Date()`.
4
+ */
5
+ export function now() {
6
+ return new Date();
7
+ }
8
+ /**
9
+ * Today's date at midnight.
10
+ */
11
+ export function today() {
12
+ const d = new Date(now());
13
+ d.setHours(0, 0, 0, 0);
14
+ return d;
15
+ }
16
+ /**
17
+ * Tomorrow's date at midnight.
18
+ */
19
+ export function tomorrow() {
20
+ const d = new Date(now());
21
+ d.setHours(0, 0, 0, 0);
22
+ return addDays(d, 1);
23
+ }
24
+ /**
25
+ * Yesterday's date at midnight.
26
+ */
27
+ export function yesterday() {
28
+ return subtractDays(today(), 1);
29
+ }
30
+ /**
31
+ * Adds the given amount of days to the specified date.
32
+ */
33
+ export function addDays(date, days) {
34
+ const result = new Date(date);
35
+ result.setDate(result.getDate() + days);
36
+ return result;
37
+ }
38
+ /**
39
+ * Subtracts the given amount of days from the specified date.
40
+ */
41
+ export function subtractDays(date, days) {
42
+ return addDays(new Date(date), -days);
43
+ }
44
+ /**
45
+ * Adds the given amount of months to the specified date.
46
+ */
47
+ export function addMonths(date, months) {
48
+ const result = new Date(date);
49
+ const targetMonth = result.getMonth() + months;
50
+ result.setMonth(targetMonth);
51
+ // Fix for month overflow (e.g., Jan 31 → Feb)
52
+ while (result.getMonth() !== ((targetMonth % 12 + 12) % 12)) {
53
+ result.setDate(result.getDate() - 1);
54
+ }
55
+ return result;
56
+ }
57
+ /**
58
+ * Subtracts the given amount of months from the specified date.
59
+ */
60
+ export function subtractMonths(date, months) {
61
+ return addMonths(new Date(date), -months);
62
+ }
63
+ /**
64
+ * Adds the given amount of years to the specified date.
65
+ */
66
+ export function addYears(date, years) {
67
+ return addMonths(new Date(date), years * 12);
68
+ }
69
+ /**
70
+ * Subtracts the given amount of yers from the specified date.
71
+ */
72
+ export function subtractYears(date, years) {
73
+ return addYears(new Date(date), -years);
74
+ }
75
+ /**
76
+ * Calculates the days between two dates.
77
+ */
78
+ export function daysBetween(start, end) {
79
+ return Math.abs(Math.floor((new Date(end).getTime() - new Date(start).getTime()) /
80
+ Duration.days(1).toMilliseconds()));
81
+ }
82
+ /**
83
+ * Calculates the months between two dates.
84
+ */
85
+ export function monthsBetween(start, end) {
86
+ const startDate = new Date(start);
87
+ const endDate = new Date(end);
88
+ const years = endDate.getFullYear() - startDate.getFullYear();
89
+ const months = endDate.getMonth() - startDate.getMonth();
90
+ return Math.abs(years * 12 + months);
91
+ }
92
+ /**
93
+ * Calculates the years between two dates.
94
+ * The date must be equal or past for it to count as a full year.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * yearsBetween(new Date('2007-08-01', '2008-07-31')); // 0
99
+ * yearsBetween(new Date('2007-08-01', '2008-08-01')); // 1
100
+ * ```
101
+ */
102
+ export function yearsBetween(start, end) {
103
+ const startDate = new Date(start);
104
+ const endDate = new Date(end);
105
+ const years = endDate.getFullYear() - startDate.getFullYear();
106
+ if (years === 0) {
107
+ return 0;
108
+ }
109
+ // Check if the end date is before the start date
110
+ const hasDayPassed = endDate.getMonth() > startDate.getMonth() ||
111
+ (endDate.getMonth() === startDate.getMonth() &&
112
+ endDate.getDate() >= startDate.getDate());
113
+ if (!hasDayPassed) {
114
+ if (years < 0) {
115
+ return Math.abs(years);
116
+ }
117
+ return Math.abs(years - 1);
118
+ }
119
+ return Math.abs(years);
120
+ }
121
+ /**
122
+ * Checks if a date is a Sunday.
123
+ */
124
+ export function isSunday(date) {
125
+ return new Date(date).getDay() === 0;
126
+ }
127
+ /**
128
+ * Checks if a date is a Monday.
129
+ */
130
+ export function isMonday(date) {
131
+ return new Date(date).getDay() === 1;
132
+ }
133
+ /**
134
+ * Checks if a date is a Tuesday.
135
+ */
136
+ export function isTuesday(date) {
137
+ return new Date(date).getDay() === 2;
138
+ }
139
+ /**
140
+ * Checks if a date is a Wednesday.
141
+ */
142
+ export function isWednesday(date) {
143
+ return new Date(date).getDay() === 3;
144
+ }
145
+ /**
146
+ * Checks if a date is a Thursday.
147
+ */
148
+ export function isThursday(date) {
149
+ return new Date(date).getDay() === 4;
150
+ }
151
+ /**
152
+ * Checks if a date is a Friday.
153
+ */
154
+ export function isFriday(date) {
155
+ return new Date(date).getDay() === 5;
156
+ }
157
+ /**
158
+ * Checks if a date is a Saturday.
159
+ */
160
+ export function isSaturday(date) {
161
+ return new Date(date).getDay() === 6;
162
+ }
163
+ /**
164
+ * Checks if a date is a weekend.
165
+ */
166
+ export function isWeekend(date) {
167
+ return isSaturday(date) || isSunday(date);
168
+ }
169
+ /**
170
+ * Checks if a date is a weekday.
171
+ */
172
+ export function isWeekday(date) {
173
+ return !isWeekend(date);
174
+ }
175
+ /**
176
+ * Determines if the date is in the past.
177
+ */
178
+ export function isInPast(date) {
179
+ return date < today();
180
+ }
181
+ /**
182
+ * Determines if the date is in the future.
183
+ *
184
+ * @remarks
185
+ * this is date specific. So, if it is 1:00 PM and you pass
186
+ * a date that is today at 2:00 PM, it will return false, because
187
+ * the "date" is the same.
188
+ */
189
+ export function isInFuture(date) {
190
+ return date >= tomorrow();
191
+ }
192
+ /**
193
+ * Determines if the date is today.
194
+ */
195
+ export function isToday(date) {
196
+ return !isInFuture(date) && !isInPast(date);
197
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dates_test.d.ts","sourceRoot":"","sources":["../../src/src/dates_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}
@@ -0,0 +1,102 @@
1
+ import type { Brand } from "./utility_types.js";
2
+ /**
3
+ * Represents a duration of time in milliseconds. You should
4
+ * always use this type when working with durations of time in milliseconds.
5
+ * It helps ensure that you're working with durations of time in milliseconds,
6
+ * and not accidentally using a different unit of time.
7
+ *
8
+ * @example
9
+ * ```ts
10
+ * setTimeout(() => {}, 1_000 as Milliseconds);
11
+ * ```
12
+ */
13
+ export type Milliseconds = Brand<number, "milliseconds">;
14
+ /** Represents a duration of time in seconds. */
15
+ export type Seconds = Brand<number, "seconds">;
16
+ /** Represents a duration of time in minutes. */
17
+ export type Minutes = Brand<number, "minutes">;
18
+ /** Represents a duration of time in hours. */
19
+ export type Hours = Brand<number, "hours">;
20
+ /** Represents a duration of time in days. */
21
+ export type Days = Brand<number, "days">;
22
+ /** Represents a duration of time in weeks. */
23
+ export type Weeks = Brand<number, "weeks">;
24
+ /** Represents a duration of time in years. */
25
+ export type Years = Brand<number, "years">;
26
+ /**
27
+ * A representation of a duration of time within a codebase.
28
+ * This class provides a way to work with durations of time in a type-safe manner.
29
+ *
30
+ * @see milliseconds to quickly create a millisecond duration.
31
+ *
32
+ * @example Converting seconds to milliseconds
33
+ * ```ts
34
+ * setTimeout(() => {}, Duration.seconds(1 as Seconds).toMilliseconds());
35
+ * ```
36
+ * @example Converting minutes to milliseconds
37
+ * ```ts
38
+ * setTimeout(() => {}, Duration.minutes(1 as Minutes).toMilliseconds());
39
+ * ```
40
+ * @example Converting to milliseconds
41
+ */
42
+ export declare class Duration {
43
+ private readonly milliseconds;
44
+ private constructor();
45
+ /**
46
+ * Creates a Duration from milliseconds.
47
+ */
48
+ static milliseconds(milliseconds: Milliseconds): Duration;
49
+ /**
50
+ * Creates a Duration from seconds.
51
+ */
52
+ static seconds(seconds: Seconds): Duration;
53
+ /**
54
+ * Creates a Duration from minutes.
55
+ */
56
+ static minutes(minutes: Minutes): Duration;
57
+ /**
58
+ * Creates a Duration from hours.
59
+ */
60
+ static hours(hours: Hours): Duration;
61
+ /**
62
+ * Creates a Duration from days.
63
+ */
64
+ static days(days: Days): Duration;
65
+ /**
66
+ * Creates a Duration from weeks.
67
+ */
68
+ static weeks(weeks: Weeks): Duration;
69
+ /**
70
+ * Creates a Duration from years.
71
+ */
72
+ static years(years: Years): Duration;
73
+ /**
74
+ * Converts the duration to milliseconds.
75
+ */
76
+ toMilliseconds(): Milliseconds;
77
+ /**
78
+ * Converts the duration to seconds.
79
+ */
80
+ toSeconds(): Seconds;
81
+ /**
82
+ * Converts the duration to minutes.
83
+ */
84
+ toMinutes(): Minutes;
85
+ /**
86
+ * Converts the duration to hours.
87
+ */
88
+ toHours(): Hours;
89
+ /**
90
+ * Converts the duration to days.
91
+ */
92
+ toDays(): Days;
93
+ /**
94
+ * Converts the duration to weeks.
95
+ */
96
+ toWeeks(): Weeks;
97
+ /**
98
+ * Converts the duration to years.
99
+ */
100
+ toYears(): Years;
101
+ }
102
+ //# sourceMappingURL=duration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"duration.d.ts","sourceRoot":"","sources":["../../src/src/duration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAEhD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;AACzD,gDAAgD;AAChD,MAAM,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAC/C,gDAAgD;AAChD,MAAM,MAAM,OAAO,GAAG,KAAK,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;AAC/C,8CAA8C;AAC9C,MAAM,MAAM,KAAK,GAAG,K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