@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 @@
1
+ {"version":3,"file":"core.d.ts","sourceRoot":"","sources":["../../src/src/core.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAa,MAAM,oBAAoB,CAAC;AAElE;;GAEG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAElC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,GAAG,OAAO,CAEjE;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AACnD,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAO7C;;;;;;;;;;;;GAYG;AACH,wBAAgB,IAAI,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;AAClD,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAAC;AAO5C;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AG,CAAC,EAAE,CAE1C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,MAAM,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,CAAC,CAElD;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,IAAI,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,CAEvD;AAED;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE7C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,MAAM,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;;;;;GASG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAC5C,wBAAgB,GAAG,CAAC,CAAC,SAAS,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EACvD,IAAI,EAAE,CAAC,EAAE,EACT,GAAG,EAAE,MAAM,CAAC,GACX,MAAM,CAAC;AAcV;;;;;;;;;;;GAWG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAAC;AAChD,wBAAgB,OAAO,CAAC,CAAC,SAAS,MAAM,CAAC,WAAW,EAAE,MAAM,CAAC,EAC3D,IAAI,EAAE,CAAC,EAAE,EACT,GAAG,EAAE,MAAM,CAAC,GACX,MAAM,CAAC"}
@@ -0,0 +1,279 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.clone = clone;
4
+ exports.isEqual = isEqual;
5
+ exports.first = first;
6
+ exports.last = last;
7
+ exports.isNotEqual = isNotEqual;
8
+ exports.isEqualIgnoreCase = isEqualIgnoreCase;
9
+ exports.isNotEqualIgnoreCase = isNotEqualIgnoreCase;
10
+ exports.stringify = stringify;
11
+ exports.reverse = reverse;
12
+ exports.isEmpty = isEmpty;
13
+ exports.isNotEmpty = isNotEmpty;
14
+ exports.unique = unique;
15
+ exports.distinct = distinct;
16
+ exports.sample = sample;
17
+ exports.rand = rand;
18
+ exports.truthy = truthy;
19
+ exports.falsy = falsy;
20
+ exports.isSome = isSome;
21
+ exports.isNone = isNone;
22
+ exports.sum = sum;
23
+ exports.average = average;
24
+ const strings_js_1 = require("./strings.js");
25
+ /**
26
+ * Clone an object using structuredClone.
27
+ */
28
+ function clone(obj) {
29
+ return structuredClone(obj);
30
+ }
31
+ /**
32
+ * Compares two things by turning them into strings,
33
+ * and comparing them by their string value.
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * isEqual("1", 1); // true
38
+ * isEqual({foo: "bar"}, {foo: "bar"}); // true
39
+ * isEqual([], []); // true
40
+ * isEqual([0], [1]); // false
41
+ * isEqual(false, " false "); // false
42
+ * isEqual(false, "FALSE"); // false
43
+ * ````
44
+ */
45
+ function isEqual(thing1, thing2) {
46
+ return stringify(thing1) === stringify(thing2);
47
+ }
48
+ function first(value) {
49
+ return value[0];
50
+ }
51
+ function last(value) {
52
+ return value[value.length - 1];
53
+ }
54
+ /**
55
+ * Compares two things by turning them into strings,
56
+ * and comparing them by their string value.
57
+ *
58
+ * @remarks this is the inverse of `isEqual`.
59
+ *
60
+ * @example
61
+ * ```ts
62
+ * isNotEqual("1", 1); // true
63
+ * isNotEqual({foo: "bar"}, {foo: "bar"}); // true
64
+ * isNotEqual([], []); // true
65
+ * isNotEqual([0], [1]); // false
66
+ * isNotEqual(false, " false "); // true
67
+ * isNotEqual(false, "FALSE"); // false
68
+ * ```
69
+ */
70
+ function isNotEqual(thing1, thing2) {
71
+ return !isEqual(thing1, thing2);
72
+ }
73
+ /**
74
+ * Compares two things by turning them into strings and lowercasing
75
+ * them, and comparing the string values. Works exactly like `isEqual` except
76
+ * will lowercase both things before comparing.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * isEqualIgnoreCase("1", 1); // true
81
+ * isEqualIgnoreCase({foo: "bar"}, {foo: "bar"}); // true
82
+ * isEqualIgnoreCase([], []); // true
83
+ * isEqualIgnoreCase([0], [1]); // false
84
+ * isEqualIgnoreCase(false, " false "); // false
85
+ * isEqualIgnoreCase(false, "FALSE"); // true
86
+ * ```
87
+ */
88
+ function isEqualIgnoreCase(thing1, thing2) {
89
+ thing1 = (0, strings_js_1.lower)(stringify(thing1));
90
+ thing2 = (0, strings_js_1.lower)(stringify(thing2));
91
+ return thing1 === thing2;
92
+ }
93
+ /**
94
+ * Compares two things by turning them into strings, trimming and lowercasing
95
+ * them, and comparing the string values. Works exactly like `isEqual` except
96
+ * will lowercase both things before comparing.
97
+ *
98
+ * @example
99
+ * ```ts
100
+ * isNotEqualIgnoreCase("1", 1); // false
101
+ * isNotEqualIgnoreCase({foo: "bar"}, {foo: "bar"}); // false
102
+ * isNotEqualIgnoreCase([], []); // false
103
+ * isNotEqualIgnoreCase([0], [1]); // true
104
+ * isNotEqualIgnoreCase(false, " false "); // true
105
+ * isNotEqualIgnoreCase(false, "FALSE"); // false
106
+ * ```
107
+ */
108
+ function isNotEqualIgnoreCase(thing1, thing2) {
109
+ return !isEqualIgnoreCase(thing1, thing2);
110
+ }
111
+ /**
112
+ * Converts the given parameter into the string equivalent.
113
+ *
114
+ * If the thing provided has the type of "object", then this function
115
+ * returns `JSON.stringify(thing)`. Otherwise, it will wrap the `thing`
116
+ * in a String and convert it to it's string representation.
117
+ *
118
+ * @example
119
+ * ```ts
120
+ * stringify({ foo: "bar" }); // "{ "foo": "bar" }"
121
+ * stringify([1, 2, 3]); // "[1, 2, 3]"
122
+ * stringify(1); // "1"
123
+ * ```
124
+ */
125
+ function stringify(thing) {
126
+ return typeof thing === "object" ? JSON.stringify(thing) : String(thing);
127
+ }
128
+ function reverse(thing) {
129
+ if (typeof thing === "string")
130
+ return thing.split("").reverse().join("");
131
+ if (thing instanceof Set)
132
+ return new Set([...thing].reverse());
133
+ return thing.reverse();
134
+ }
135
+ function isEmpty(thing) {
136
+ if (isNone(thing))
137
+ return true;
138
+ if (typeof thing === "string")
139
+ return (0, strings_js_1.isWhitespace)(thing);
140
+ if (Array.isArray(thing))
141
+ return thing.length === 0;
142
+ if (thing instanceof Map || thing instanceof Set)
143
+ return thing.size === 0;
144
+ if (typeof thing === "object")
145
+ return Object.keys(thing).length === 0;
146
+ return false;
147
+ }
148
+ function isNotEmpty(thing) {
149
+ return !isEmpty(thing);
150
+ }
151
+ /**
152
+ * Returns the distinct values from a list.
153
+ *
154
+ * @remarks same as `distinct`.
155
+ *
156
+ * @example
157
+ * ```ts
158
+ * const myList = [1, 2, 3, 3];
159
+ * unique(myList); // [1, 2, 3];
160
+ * ```
161
+ */
162
+ function unique(list) {
163
+ return [...new Set(list)];
164
+ }
165
+ /**
166
+ * Returns the distinct values from a list.
167
+ *
168
+ * @remarks alias for `unique`.
169
+ *
170
+ * @example
171
+ * ```ts
172
+ * const myList = [1, 2, 3, 3];
173
+ * distinct(myList); // [1, 2, 3];
174
+ * ```
175
+ */
176
+ function distinct(list) {
177
+ return unique(list);
178
+ }
179
+ /**
180
+ * Pick a random item from an array.
181
+ *
182
+ * @remarks
183
+ * If you pass an empty list, then this function will return `undefined`.
184
+ *
185
+ * @example
186
+ * ```ts
187
+ * const myList = [1, 2, 3, 4];
188
+ * const randomItem = sample(myList);
189
+ *
190
+ * console.log(randomItem); // could be 1, 2, 3, or 4
191
+ * console.log(sample([])); // undefined
192
+ * ```
193
+ */
194
+ function sample(list) {
195
+ return list[Math.floor(Math.random() * list.length)];
196
+ }
197
+ /**
198
+ * Gives a random number in the given range. The first parameter is inclusive
199
+ * and the second one is exclusive. Therefore, it will work with lists out of
200
+ * the box.
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * rand(0, 10); // 0 -> 9
205
+ * rand(3, 7); // 3 -> 6
206
+ * ```
207
+ */
208
+ function rand(start, end) {
209
+ return Math.floor(Math.random() * (end - start)) + start;
210
+ }
211
+ /**
212
+ * Determines if the given value is truthy.
213
+ *
214
+ * @example
215
+ * ```ts
216
+ * truthy(true); // true
217
+ * truthy(false); // false
218
+ * truthy(''); // false
219
+ * ```
220
+ */
221
+ function truthy(thing) {
222
+ return Boolean(thing) === true;
223
+ }
224
+ /**
225
+ * Determines if the given value is falsy.
226
+ *
227
+ * @example
228
+ * ```ts
229
+ * falsy(true); // false
230
+ * falsy(false); // true
231
+ * falsy(''); // true
232
+ * ```
233
+ */
234
+ function falsy(thing) {
235
+ return !truthy(thing);
236
+ }
237
+ /**
238
+ * Returns true if the given value is not null or undefined.
239
+ *
240
+ * @example
241
+ * ```ts
242
+ * isSome(null); // false
243
+ * isSome(undefined); // false
244
+ * isSome(0); // true
245
+ * isSome({}); // true
246
+ * ```
247
+ */
248
+ function isSome(thing) {
249
+ return !isNone(thing);
250
+ }
251
+ /**
252
+ * Returns true if the given value is null or undefined.
253
+ *
254
+ * @example
255
+ * ```ts
256
+ * isNone(null); // true
257
+ * isNone(undefined); // true
258
+ * isNone(true); // false
259
+ * isNone(0); // false
260
+ * ```
261
+ */
262
+ function isNone(thing) {
263
+ return thing === null || thing === undefined;
264
+ }
265
+ // deno-lint-ignore no-explicit-any
266
+ function sum(list, key) {
267
+ if (list.length === 0)
268
+ return 0;
269
+ if (typeof list[0] === "number") {
270
+ return list.reduce((a, v) => a + v, 0);
271
+ }
272
+ return list.reduce(
273
+ // deno-lint-ignore no-explicit-any
274
+ (a, v) => a + v[key], 0);
275
+ }
276
+ // deno-lint-ignore no-explicit-any
277
+ function average(list, key) {
278
+ return (sum(list, key) / list.length) || 0;
279
+ }
@@ -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,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAItD;AAED;;GAEG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,GAAG,IAAI,CAE3D;AAED;;GAEG;AACH,wBAAgB,SA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C,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,224 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.now = now;
4
+ exports.today = today;
5
+ exports.tomorrow = tomorrow;
6
+ exports.yesterday = yesterday;
7
+ exports.addDays = addDays;
8
+ exports.subtractDays = subtractDays;
9
+ exports.addMonths = addMonths;
10
+ exports.subtractMonths = subtractMonths;
11
+ exports.addYears = addYears;
12
+ exports.subtractYears = subtractYears;
13
+ exports.daysBetween = daysBetween;
14
+ exports.monthsBetween = monthsBetween;
15
+ exports.yearsBetween = yearsBetween;
16
+ exports.isSunday = isSunday;
17
+ exports.isMonday = isMonday;
18
+ exports.isTuesday = isTuesday;
19
+ exports.isWednesday = isWednesday;
20
+ exports.isThursday = isThursday;
21
+ exports.isFriday = isFriday;
22
+ exports.isSaturday = isSaturday;
23
+ exports.isWeekend = isWeekend;
24
+ exports.isWeekday = isWeekday;
25
+ exports.isInPast = isInPast;
26
+ exports.isInFuture = isInFuture;
27
+ exports.isToday = isToday;
28
+ const duration_js_1 = require("./duration.js");
29
+ /**
30
+ * Right now. This is an alias for `new Date()`.
31
+ */
32
+ function now() {
33
+ return new Date();
34
+ }
35
+ /**
36
+ * Today's date at midnight.
37
+ */
38
+ function today() {
39
+ const d = new Date(now());
40
+ d.setHours(0, 0, 0, 0);
41
+ return d;
42
+ }
43
+ /**
44
+ * Tomorrow's date at midnight.
45
+ */
46
+ function tomorrow() {
47
+ const d = new Date(now());
48
+ d.setHours(0, 0, 0, 0);
49
+ return addDays(d, 1);
50
+ }
51
+ /**
52
+ * Yesterday's date at midnight.
53
+ */
54
+ function yesterday() {
55
+ return subtractDays(today(), 1);
56
+ }
57
+ /**
58
+ * Adds the given amount of days to the specified date.
59
+ */
60
+ function addDays(date, days) {
61
+ const result = new Date(date);
62
+ result.setDate(result.getDate() + days);
63
+ return result;
64
+ }
65
+ /**
66
+ * Subtracts the given amount of days from the specified date.
67
+ */
68
+ function subtractDays(date, days) {
69
+ return addDays(new Date(date), -days);
70
+ }
71
+ /**
72
+ * Adds the given amount of months to the specified date.
73
+ */
74
+ function addMonths(date, months) {
75
+ const result = new Date(date);
76
+ const targetMonth = result.getMonth() + months;
77
+ result.setMonth(targetMonth);
78
+ // Fix for month overflow (e.g., Jan 31 → Feb)
79
+ while (result.getMonth() !== ((targetMonth % 12 + 12) % 12)) {
80
+ result.setDate(result.getDate() - 1);
81
+ }
82
+ return result;
83
+ }
84
+ /**
85
+ * Subtracts the given amount of months from the specified date.
86
+ */
87
+ function subtractMonths(date, months) {
88
+ return addMonths(new Date(date), -months);
89
+ }
90
+ /**
91
+ * Adds the given amount of years to the specified date.
92
+ */
93
+ function addYears(date, years) {
94
+ return addMonths(new Date(date), years * 12);
95
+ }
96
+ /**
97
+ * Subtracts the given amount of yers from the specified date.
98
+ */
99
+ function subtractYears(date, years) {
100
+ return addYears(new Date(date), -years);
101
+ }
102
+ /**
103
+ * Calculates the days between two dates.
104
+ */
105
+ function daysBetween(start, end) {
106
+ return Math.abs(Math.floor((new Date(end).getTime() - new Date(start).getTime()) /
107
+ duration_js_1.Duration.days(1).toMilliseconds()));
108
+ }
109
+ /**
110
+ * Calculates the months between two dates.
111
+ */
112
+ function monthsBetween(start, end) {
113
+ const startDate = new Date(start);
114
+ const endDate = new Date(end);
115
+ const years = endDate.getFullYear() - startDate.getFullYear();
116
+ const months = endDate.getMonth() - startDate.getMonth();
117
+ return Math.abs(years * 12 + months);
118
+ }
119
+ /**
120
+ * Calculates the years between two dates.
121
+ * The date must be equal or past for it to count as a full year.
122
+ *
123
+ * @example
124
+ * ```ts
125
+ * yearsBetween(new Date('2007-08-01', '2008-07-31')); // 0
126
+ * yearsBetween(new Date('2007-08-01', '2008-08-01')); // 1
127
+ * ```
128
+ */
129
+ function yearsBetween(start, end) {
130
+ const startDate = new Date(start);
131
+ const endDate = new Date(end);
132
+ const years = endDate.getFullYear() - startDate.getFullYear();
133
+ if (years === 0) {
134
+ return 0;
135
+ }
136
+ // Check if the end date is before the start date
137
+ const hasDayPassed = endDate.getMonth() > startDate.getMonth() ||
138
+ (endDate.getMonth() === startDate.getMonth() &&
139
+ endDate.getDate() >= startDate.getDate());
140
+ if (!hasDayPassed) {
141
+ if (years < 0) {
142
+ return Math.abs(years);
143
+ }
144
+ return Math.abs(years - 1);
145
+ }
146
+ return Math.abs(years);
147
+ }
148
+ /**
149
+ * Checks if a date is a Sunday.
150
+ */
151
+ function isSunday(date) {
152
+ return new Date(date).getDay() === 0;
153
+ }
154
+ /**
155
+ * Checks if a date is a Monday.
156
+ */
157
+ function isMonday(date) {
158
+ return new Date(date).getDay() === 1;
159
+ }
160
+ /**
161
+ * Checks if a date is a Tuesday.
162
+ */
163
+ function isTuesday(date) {
164
+ return new Date(date).getDay() === 2;
165
+ }
166
+ /**
167
+ * Checks if a date is a Wednesday.
168
+ */
169
+ function isWednesday(date) {
170
+ return new Date(date).getDay() === 3;
171
+ }
172
+ /**
173
+ * Checks if a date is a Thursday.
174
+ */
175
+ function isThursday(date) {
176
+ return new Date(date).getDay() === 4;
177
+ }
178
+ /**
179
+ * Checks if a date is a Friday.
180
+ */
181
+ function isFriday(date) {
182
+ return new Date(date).getDay() === 5;
183
+ }
184
+ /**
185
+ * Checks if a date is a Saturday.
186
+ */
187
+ function isSaturday(date) {
188
+ return new Date(date).getDay() === 6;
189
+ }
190
+ /**
191
+ * Checks if a date is a weekend.
192
+ */
193
+ function isWeekend(date) {
194
+ return isSaturday(date) || isSunday(date);
195
+ }
196
+ /**
197
+ * Checks if a date is a weekday.
198
+ */
199
+ function isWeekday(date) {
200
+ return !isWeekend(date);
201
+ }
202
+ /**
203
+ * Determines if the date is in the past.
204
+ */
205
+ function isInPast(date) {
206
+ return date < today();
207
+ }
208
+ /**
209
+ * Determines if the date is in the future.
210
+ *
211
+ * @remarks
212
+ * this is date specific. So, if it is 1:00 PM and you pass
213
+ * a date that is today at 2:00 PM, it will return false, because
214
+ * the "date" is the same.
215
+ */
216
+ function isInFuture(date) {
217
+ return date >= tomorrow();
218
+ }
219
+ /**
220
+ * Determines if the date is today.
221
+ */
222
+ function isToday(date) {
223
+ return !isInFuture(date) && !isInPast(date);
224
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dates_test.d.ts","sourceRoot":"","sources":["../../src/src/dates_test.ts"],"names":[],"mappings":"AAAA,OAAO,2BAA2B,CAAC"}