@bytebury/toolkit 1.0.0 → 1.1.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/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  <div align="center">
2
2
 
3
- <img src="./assets/logo.png" alt="Toolkit Logo" width="256" height="256" /><br />
3
+ <img src="./assets/logo.png" alt="Toolkit Logo" width="348" height="256" /><br />
4
4
 
5
5
  # toolkit
6
6
 
@@ -44,6 +44,6 @@ function getAverageAge(): number {
44
44
  { name: "Carly", age: 8 },
45
45
  { name: "Jenny", age: 5 }
46
46
  ];
47
- return average(people, "age"); // 5
47
+ return average(people.map(({ age }) => age)); // 5
48
48
  }
49
49
  ```
@@ -0,0 +1,10 @@
1
+ import { Deno } from "@deno/shim-deno";
2
+ export { Deno } from "@deno/shim-deno";
3
+ import { setInterval, setTimeout } from "@deno/shim-timers";
4
+ export { setInterval, setTimeout } from "@deno/shim-timers";
5
+ export declare const dntGlobalThis: Omit<typeof globalThis, "setInterval" | "setTimeout" | "Deno"> & {
6
+ Deno: typeof Deno;
7
+ setInterval: typeof setInterval;
8
+ setTimeout: typeof setTimeout;
9
+ };
10
+ //# sourceMappingURL=_dnt.shims.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAO5D,eAAO,MAAM,aAAa;;;;CAA2C,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { Deno } from "@deno/shim-deno";
2
+ export { Deno } from "@deno/shim-deno";
3
+ import { setInterval, setTimeout } from "@deno/shim-timers";
4
+ export { setInterval, setTimeout } from "@deno/shim-timers";
5
+ const dntGlobals = {
6
+ Deno,
7
+ setInterval,
8
+ setTimeout,
9
+ };
10
+ export const dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
11
+ function createMergeProxy(baseObj, extObj) {
12
+ return new Proxy(baseObj, {
13
+ get(_target, prop, _receiver) {
14
+ if (prop in extObj) {
15
+ return extObj[prop];
16
+ }
17
+ else {
18
+ return baseObj[prop];
19
+ }
20
+ },
21
+ set(_target, prop, value) {
22
+ if (prop in extObj) {
23
+ delete extObj[prop];
24
+ }
25
+ baseObj[prop] = value;
26
+ return true;
27
+ },
28
+ deleteProperty(_target, prop) {
29
+ let success = false;
30
+ if (prop in extObj) {
31
+ delete extObj[prop];
32
+ success = true;
33
+ }
34
+ if (prop in baseObj) {
35
+ delete baseObj[prop];
36
+ success = true;
37
+ }
38
+ return success;
39
+ },
40
+ ownKeys(_target) {
41
+ const baseKeys = Reflect.ownKeys(baseObj);
42
+ const extKeys = Reflect.ownKeys(extObj);
43
+ const extKeysSet = new Set(extKeys);
44
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
45
+ },
46
+ defineProperty(_target, prop, desc) {
47
+ if (prop in extObj) {
48
+ delete extObj[prop];
49
+ }
50
+ Reflect.defineProperty(baseObj, prop, desc);
51
+ return true;
52
+ },
53
+ getOwnPropertyDescriptor(_target, prop) {
54
+ if (prop in extObj) {
55
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
56
+ }
57
+ else {
58
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
59
+ }
60
+ },
61
+ has(_target, prop) {
62
+ return prop in extObj || prop in baseObj;
63
+ },
64
+ });
65
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"_dnt.test_shims.d.ts","sourceRoot":"","sources":["../src/_dnt.test_shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAKvC,eAAO,MAAM,aAAa;;CAA2C,CAAC"}
1
+ {"version":3,"file":"_dnt.test_shims.d.ts","sourceRoot":"","sources":["../src/_dnt.test_shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAO5D,eAAO,MAAM,aAAa;;;;CAA2C,CAAC"}
package/esm/src/core.d.ts CHANGED
@@ -221,7 +221,7 @@ export declare function sample<T>(list: NonEmptyList<T>): T;
221
221
  * rand(3, 7); // 3 -> 6
222
222
  * ```
223
223
  */
224
- export declare function rand(start: number, end: number): number;
224
+ export declare function random(start: number, end: number): number;
225
225
  /**
226
226
  * Determines if the given value is truthy.
227
227
  *
@@ -269,29 +269,33 @@ export declare function isSome(thing: unknown): boolean;
269
269
  */
270
270
  export declare function isNone(thing: unknown): boolean;
271
271
  /**
272
- * Sums the elements in the given list. If you pass a list of numbers, it sums
273
- * them directly. If you pass a list of objects, provide the key to sum.
272
+ * Returns a function that does nothing.
274
273
  *
275
274
  * @example
276
275
  * ```ts
277
- * sum([1, 2, 3]); // 6
278
- * sum([{ x: 1 }, { x: 2 }], "x"); // 3
276
+ * const doNothing = noop();
277
+ * doNothing(); // does nothing
279
278
  * ```
280
279
  */
281
- export declare function sum(list: number[]): number;
282
- export declare function sum<T extends Record<PropertyKey, number>>(list: T[], key: keyof T): number;
280
+ export declare function noop(): void;
283
281
  /**
284
- * Calculates the average of the elements in the given list.
285
- * If you pass a list of numbers, it calculates the average directly.
286
- * If you pass a list of objects, provide the key to calculate the average.
282
+ * Returns true if the given value is within the given range.
287
283
  *
288
284
  * @example
289
285
  * ```ts
290
- * average([1, 2, 3]); // 2
291
- * average([{ x: 1 }, { x: 2 }], "x"); // 1.5
292
- * average([]); // 0
286
+ * inRange(5, 0, 10); // true
287
+ * inRange(0, 0, 10); // true
288
+ * inRange(10, 0, 10); // true
289
+ * inRange(11, 0, 10); // false
293
290
  * ```
294
291
  */
295
- export declare function average(list: number[]): number;
296
- export declare function average<T extends Record<PropertyKey, number>>(list: T[], key: keyof T): number;
292
+ export declare function inRange(value: number, min: number, max: number): boolean;
293
+ /**
294
+ * Splits an array into chunks of a fixed size.
295
+ *
296
+ * @example
297
+ * chunk([1,2,3,4,5,6,7], 3); // [[1,2,3],[4,5,6],[7]]
298
+ * chunk(['a','b','c','d'], 2); // [['a','b'], ['c','d']]
299
+ */
300
+ export declare function chunk<T>(arr: T[], size: number): T[][];
297
301
  //# sourceMappingURL=core.d.ts.map
@@ -1 +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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MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExE;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,CAQtD"}
package/esm/src/core.js CHANGED
@@ -169,7 +169,7 @@ export function distinct(list) {
169
169
  * ```
170
170
  */
171
171
  export function sample(list) {
172
- return list[Math.floor(Math.random() * list.length)];
172
+ return list[random(0, list.length)];
173
173
  }
174
174
  /**
175
175
  * Gives a random number in the given range. The first parameter is inclusive
@@ -182,7 +182,7 @@ export function sample(list) {
182
182
  * rand(3, 7); // 3 -> 6
183
183
  * ```
184
184
  */
185
- export function rand(start, end) {
185
+ export function random(start, end) {
186
186
  return Math.floor(Math.random() * (end - start)) + start;
187
187
  }
188
188
  /**
@@ -239,18 +239,45 @@ export function isSome(thing) {
239
239
  export function isNone(thing) {
240
240
  return thing === null || thing === undefined;
241
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);
242
+ /**
243
+ * Returns a function that does nothing.
244
+ *
245
+ * @example
246
+ * ```ts
247
+ * const doNothing = noop();
248
+ * doNothing(); // does nothing
249
+ * ```
250
+ */
251
+ export function noop() {
252
+ // Do nothing
252
253
  }
253
- // deno-lint-ignore no-explicit-any
254
- export function average(list, key) {
255
- return (sum(list, key) / list.length) || 0;
254
+ /**
255
+ * Returns true if the given value is within the given range.
256
+ *
257
+ * @example
258
+ * ```ts
259
+ * inRange(5, 0, 10); // true
260
+ * inRange(0, 0, 10); // true
261
+ * inRange(10, 0, 10); // true
262
+ * inRange(11, 0, 10); // false
263
+ * ```
264
+ */
265
+ export function inRange(value, min, max) {
266
+ return value >= min && value <= max;
267
+ }
268
+ /**
269
+ * Splits an array into chunks of a fixed size.
270
+ *
271
+ * @example
272
+ * chunk([1,2,3,4,5,6,7], 3); // [[1,2,3],[4,5,6],[7]]
273
+ * chunk(['a','b','c','d'], 2); // [['a','b'], ['c','d']]
274
+ */
275
+ export function chunk(arr, size) {
276
+ if (size <= 0)
277
+ throw new Error("chunk size must be > 0");
278
+ const result = [];
279
+ for (let i = 0; i < arr.length; i += size) {
280
+ result.push(arr.slice(i, i + size));
281
+ }
282
+ return result;
256
283
  }
@@ -23,6 +23,19 @@ export type Days = Brand<number, "days">;
23
23
  export type Weeks = Brand<number, "weeks">;
24
24
  /** Represents a duration of time in years. */
25
25
  export type Years = Brand<number, "years">;
26
+ /**
27
+ * Sleeps for the given duration of milliseconds.
28
+ *
29
+ * @example Using `sleep` directly (for milliseconds).
30
+ * ```ts
31
+ * await sleep(1_000 as Milliseconds);
32
+ * ```
33
+ * @example Using `Duration.sleep` (for non-milliseconds).
34
+ * ```ts
35
+ * await Duration.seconds(1 as Seconds).sleep();
36
+ * ```
37
+ */
38
+ export declare const sleep: (ms: Milliseconds) => Promise<void>;
26
39
  /**
27
40
  * A representation of a duration of time within a codebase.
28
41
  * This class provides a way to work with durations of time in a type-safe manner.
@@ -98,5 +111,15 @@ export declare class Duration {
98
111
  * Converts the duration to years.
99
112
  */
100
113
  toYears(): Years;
114
+ /**
115
+ * Sleeps for the current duration.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const duration = Duration.seconds(5);
120
+ * await duration.sleep();
121
+ * ```
122
+ */
123
+ sleep(): Promise<void>;
101
124
  }
102
125
  //# sourceMappingURL=duration.d.ts.map
@@ -1 +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,E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;IAIzD;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,QAAQ;IAI1C;;OAEG;IACH,MAAM,CAAC,OAAO,CAAC,OAAO,EAAE,OAAO,GAAG,QAAQ;IAI1C;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ;IAIpC;;OAEG;IACH,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,IAAI,GAAG,QAAQ;IAIjC;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ;IAIpC;;OAEG;IACH,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,KAAK,GAAG,QAAQ;IAIpC;;OAEG;IACH,cAAc,IAAI,YAAY;IAI9B;;OAEG;IACH,SAAS,IAAI,OAAO;IAIpB;;OAEG;IACH,SAAS,IAAI,OAAO;IAIpB;;OAEG;IACH,OAAO,IAAI,KAAK;IAIhB;;OAEG;IACH,MAAM,IAAI,IAAI;IAId;;OAEG;IACH,OAAO,IAAI,KAAK;IAIhB;;OAEG;IACH,OAAO,IAAI,KAAK;IAIhB;;;;;;;;OAQG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
@@ -1,3 +1,19 @@
1
+ import * as dntShim from "../_dnt.shims.js";
2
+ /**
3
+ * Sleeps for the given duration of milliseconds.
4
+ *
5
+ * @example Using `sleep` directly (for milliseconds).
6
+ * ```ts
7
+ * await sleep(1_000 as Milliseconds);
8
+ * ```
9
+ * @example Using `Duration.sleep` (for non-milliseconds).
10
+ * ```ts
11
+ * await Duration.seconds(1 as Seconds).sleep();
12
+ * ```
13
+ */
14
+ export const sleep = async (ms) => {
15
+ await Duration.milliseconds(ms).sleep();
16
+ };
1
17
  /**
2
18
  * A representation of a duration of time within a codebase.
3
19
  * This class provides a way to work with durations of time in a type-safe manner.
@@ -107,4 +123,16 @@ export class Duration {
107
123
  toYears() {
108
124
  return this.milliseconds / (365 * 24 * 60 * 60 * 1000);
109
125
  }
126
+ /**
127
+ * Sleeps for the current duration.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * const duration = Duration.seconds(5);
132
+ * await duration.sleep();
133
+ * ```
134
+ */
135
+ async sleep() {
136
+ await new Promise((resolve) => dntShim.setTimeout(resolve, this.toMilliseconds()));
137
+ }
110
138
  }
@@ -55,4 +55,88 @@ export declare function isOdd(num: number): boolean;
55
55
  * ```
56
56
  */
57
57
  export declare function ordinalize(num: number): string;
58
+ /**
59
+ * Returns the maximum number from a list of numbers.
60
+ *
61
+ * @remarks
62
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * max([]); // 0
67
+ * max([1]); // 1
68
+ * max([2, 3]); // 3
69
+ * max([4, 5, 6]); // 6
70
+ * max([11, 21, 112]); // 112
71
+ * ```
72
+ */
73
+ export declare function max(nums: number[]): number;
74
+ /**
75
+ * Returns the minimum number from a list of numbers.
76
+ *
77
+ * @remarks
78
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * min([]); // 0
83
+ * min([1]); // 1
84
+ * min([2, 3]); // 2
85
+ * min([4, 5, 6]); // 4
86
+ * min([11, 21, 112]); // 11
87
+ * ```
88
+ */
89
+ export declare function min(nums: number[]): number;
90
+ /**
91
+ * Rounds a number to the nearest integer.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * round(null) // 0
96
+ * round(1.2); // 1
97
+ * round(1.5); // 2
98
+ * round(1.8); // 2
99
+ * round(2.2); // 2
100
+ * round(2.5); // 3
101
+ * round(2.8); // 3
102
+ * ```
103
+ */
104
+ export declare function round(num: number): number;
105
+ /**
106
+ * Returns the sum of a list of numbers.
107
+ *
108
+ * @remarks
109
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * sum([1]); // 1
114
+ * sum([2, 3]); // 5
115
+ * sum([4, 5, 6]); // 15
116
+ * sum([11, 21, 112]); // 144
117
+ * ```
118
+ */
119
+ export declare function sum(nums: number[]): number;
120
+ /**
121
+ * Returns the average number from a list of numbers.
122
+ *
123
+ * @remarks
124
+ * This will include decimals, so you may want to use `Math.round`,
125
+ * `Math.floor` or `Math.ceil`.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * average([1]); // 1
130
+ * average([2, 3]); // 2.5
131
+ * average([4, 5, 6]); // 5
132
+ * average([11, 21, 112]); 48
133
+ * ```
134
+ * @example With a list of objects
135
+ * ```ts
136
+ * const people = [{ age: 20 }, { age: 18 }, { age: 19 }];
137
+ * const averageAge = average(people.map(({ age }) => age));
138
+ * console.log(averageAge); // 19
139
+ * ```
140
+ */
141
+ export declare function average(nums: number[]): number;
58
142
  //# sourceMappingURL=numbers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/src/numbers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE1C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C"}
1
+ {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/src/numbers.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE1C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG9C"}
@@ -1,3 +1,4 @@
1
+ import { isEmpty } from "./core.js";
1
2
  /**
2
3
  * Determines if a number is even.
3
4
  *
@@ -74,3 +75,105 @@ export function ordinalize(num) {
74
75
  return num + "th";
75
76
  }
76
77
  }
78
+ /**
79
+ * Returns the maximum number from a list of numbers.
80
+ *
81
+ * @remarks
82
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
83
+ *
84
+ * @example
85
+ * ```ts
86
+ * max([]); // 0
87
+ * max([1]); // 1
88
+ * max([2, 3]); // 3
89
+ * max([4, 5, 6]); // 6
90
+ * max([11, 21, 112]); // 112
91
+ * ```
92
+ */
93
+ export function max(nums) {
94
+ if (isEmpty(nums))
95
+ return 0;
96
+ return Math.max(...nums);
97
+ }
98
+ /**
99
+ * Returns the minimum number from a list of numbers.
100
+ *
101
+ * @remarks
102
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
103
+ *
104
+ * @example
105
+ * ```ts
106
+ * min([]); // 0
107
+ * min([1]); // 1
108
+ * min([2, 3]); // 2
109
+ * min([4, 5, 6]); // 4
110
+ * min([11, 21, 112]); // 11
111
+ * ```
112
+ */
113
+ export function min(nums) {
114
+ if (isEmpty(nums))
115
+ return 0;
116
+ return Math.min(...nums);
117
+ }
118
+ /**
119
+ * Rounds a number to the nearest integer.
120
+ *
121
+ * @example
122
+ * ```ts
123
+ * round(null) // 0
124
+ * round(1.2); // 1
125
+ * round(1.5); // 2
126
+ * round(1.8); // 2
127
+ * round(2.2); // 2
128
+ * round(2.5); // 3
129
+ * round(2.8); // 3
130
+ * ```
131
+ */
132
+ export function round(num) {
133
+ return Math.round(num || 0);
134
+ }
135
+ /**
136
+ * Returns the sum of a list of numbers.
137
+ *
138
+ * @remarks
139
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
140
+ *
141
+ * @example
142
+ * ```ts
143
+ * sum([1]); // 1
144
+ * sum([2, 3]); // 5
145
+ * sum([4, 5, 6]); // 15
146
+ * sum([11, 21, 112]); // 144
147
+ * ```
148
+ */
149
+ export function sum(nums) {
150
+ if (isEmpty(nums))
151
+ return 0;
152
+ return nums.reduce((acc, num) => acc + num, 0);
153
+ }
154
+ /**
155
+ * Returns the average number from a list of numbers.
156
+ *
157
+ * @remarks
158
+ * This will include decimals, so you may want to use `Math.round`,
159
+ * `Math.floor` or `Math.ceil`.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * average([1]); // 1
164
+ * average([2, 3]); // 2.5
165
+ * average([4, 5, 6]); // 5
166
+ * average([11, 21, 112]); 48
167
+ * ```
168
+ * @example With a list of objects
169
+ * ```ts
170
+ * const people = [{ age: 20 }, { age: 18 }, { age: 19 }];
171
+ * const averageAge = average(people.map(({ age }) => age));
172
+ * console.log(averageAge); // 19
173
+ * ```
174
+ */
175
+ export function average(nums) {
176
+ if (isEmpty(nums))
177
+ return 0;
178
+ return sum(nums) / nums.length;
179
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bytebury/toolkit",
3
- "version": "1.0.0",
3
+ "version": "1.1.2",
4
4
  "description": "TypeScript utility library to help energize your projects with useful functions for any size project. Save yourself some time and focus on shipping features.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -19,10 +19,13 @@
19
19
  "test": "node test_runner.js"
20
20
  },
21
21
  "private": false,
22
+ "dependencies": {
23
+ "@deno/shim-deno": "~0.18.0",
24
+ "@deno/shim-timers": "~0.1.0"
25
+ },
22
26
  "devDependencies": {
23
27
  "@types/node": "^20.9.0",
24
- "picocolors": "^1.0.0",
25
- "@deno/shim-deno": "~0.18.0"
28
+ "picocolors": "^1.0.0"
26
29
  },
27
30
  "_generatedBy": "dnt@dev"
28
31
  }
@@ -0,0 +1,10 @@
1
+ import { Deno } from "@deno/shim-deno";
2
+ export { Deno } from "@deno/shim-deno";
3
+ import { setInterval, setTimeout } from "@deno/shim-timers";
4
+ export { setInterval, setTimeout } from "@deno/shim-timers";
5
+ export declare const dntGlobalThis: Omit<typeof globalThis, "setInterval" | "setTimeout" | "Deno"> & {
6
+ Deno: typeof Deno;
7
+ setInterval: typeof setInterval;
8
+ setTimeout: typeof setTimeout;
9
+ };
10
+ //# sourceMappingURL=_dnt.shims.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"_dnt.shims.d.ts","sourceRoot":"","sources":["../src/_dnt.shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAO5D,eAAO,MAAM,aAAa;;;;CAA2C,CAAC"}
@@ -0,0 +1,71 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.dntGlobalThis = exports.setTimeout = exports.setInterval = exports.Deno = void 0;
4
+ const shim_deno_1 = require("@deno/shim-deno");
5
+ var shim_deno_2 = require("@deno/shim-deno");
6
+ Object.defineProperty(exports, "Deno", { enumerable: true, get: function () { return shim_deno_2.Deno; } });
7
+ const shim_timers_1 = require("@deno/shim-timers");
8
+ var shim_timers_2 = require("@deno/shim-timers");
9
+ Object.defineProperty(exports, "setInterval", { enumerable: true, get: function () { return shim_timers_2.setInterval; } });
10
+ Object.defineProperty(exports, "setTimeout", { enumerable: true, get: function () { return shim_timers_2.setTimeout; } });
11
+ const dntGlobals = {
12
+ Deno: shim_deno_1.Deno,
13
+ setInterval: shim_timers_1.setInterval,
14
+ setTimeout: shim_timers_1.setTimeout,
15
+ };
16
+ exports.dntGlobalThis = createMergeProxy(globalThis, dntGlobals);
17
+ function createMergeProxy(baseObj, extObj) {
18
+ return new Proxy(baseObj, {
19
+ get(_target, prop, _receiver) {
20
+ if (prop in extObj) {
21
+ return extObj[prop];
22
+ }
23
+ else {
24
+ return baseObj[prop];
25
+ }
26
+ },
27
+ set(_target, prop, value) {
28
+ if (prop in extObj) {
29
+ delete extObj[prop];
30
+ }
31
+ baseObj[prop] = value;
32
+ return true;
33
+ },
34
+ deleteProperty(_target, prop) {
35
+ let success = false;
36
+ if (prop in extObj) {
37
+ delete extObj[prop];
38
+ success = true;
39
+ }
40
+ if (prop in baseObj) {
41
+ delete baseObj[prop];
42
+ success = true;
43
+ }
44
+ return success;
45
+ },
46
+ ownKeys(_target) {
47
+ const baseKeys = Reflect.ownKeys(baseObj);
48
+ const extKeys = Reflect.ownKeys(extObj);
49
+ const extKeysSet = new Set(extKeys);
50
+ return [...baseKeys.filter((k) => !extKeysSet.has(k)), ...extKeys];
51
+ },
52
+ defineProperty(_target, prop, desc) {
53
+ if (prop in extObj) {
54
+ delete extObj[prop];
55
+ }
56
+ Reflect.defineProperty(baseObj, prop, desc);
57
+ return true;
58
+ },
59
+ getOwnPropertyDescriptor(_target, prop) {
60
+ if (prop in extObj) {
61
+ return Reflect.getOwnPropertyDescriptor(extObj, prop);
62
+ }
63
+ else {
64
+ return Reflect.getOwnPropertyDescriptor(baseObj, prop);
65
+ }
66
+ },
67
+ has(_target, prop) {
68
+ return prop in extObj || prop in baseObj;
69
+ },
70
+ });
71
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"_dnt.test_shims.d.ts","sourceRoot":"","sources":["../src/_dnt.test_shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AAKvC,eAAO,MAAM,aAAa;;CAA2C,CAAC"}
1
+ {"version":3,"file":"_dnt.test_shims.d.ts","sourceRoot":"","sources":["../src/_dnt.test_shims.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,IAAI,EAAE,MAAM,iBAAiB,CAAC;AACvC,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC5D,OAAO,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAO5D,eAAO,MAAM,aAAa;;;;CAA2C,CAAC"}
@@ -221,7 +221,7 @@ export declare function sample<T>(list: NonEmptyList<T>): T;
221
221
  * rand(3, 7); // 3 -> 6
222
222
  * ```
223
223
  */
224
- export declare function rand(start: number, end: number): number;
224
+ export declare function random(start: number, end: number): number;
225
225
  /**
226
226
  * Determines if the given value is truthy.
227
227
  *
@@ -269,29 +269,33 @@ export declare function isSome(thing: unknown): boolean;
269
269
  */
270
270
  export declare function isNone(thing: unknown): boolean;
271
271
  /**
272
- * Sums the elements in the given list. If you pass a list of numbers, it sums
273
- * them directly. If you pass a list of objects, provide the key to sum.
272
+ * Returns a function that does nothing.
274
273
  *
275
274
  * @example
276
275
  * ```ts
277
- * sum([1, 2, 3]); // 6
278
- * sum([{ x: 1 }, { x: 2 }], "x"); // 3
276
+ * const doNothing = noop();
277
+ * doNothing(); // does nothing
279
278
  * ```
280
279
  */
281
- export declare function sum(list: number[]): number;
282
- export declare function sum<T extends Record<PropertyKey, number>>(list: T[], key: keyof T): number;
280
+ export declare function noop(): void;
283
281
  /**
284
- * Calculates the average of the elements in the given list.
285
- * If you pass a list of numbers, it calculates the average directly.
286
- * If you pass a list of objects, provide the key to calculate the average.
282
+ * Returns true if the given value is within the given range.
287
283
  *
288
284
  * @example
289
285
  * ```ts
290
- * average([1, 2, 3]); // 2
291
- * average([{ x: 1 }, { x: 2 }], "x"); // 1.5
292
- * average([]); // 0
286
+ * inRange(5, 0, 10); // true
287
+ * inRange(0, 0, 10); // true
288
+ * inRange(10, 0, 10); // true
289
+ * inRange(11, 0, 10); // false
293
290
  * ```
294
291
  */
295
- export declare function average(list: number[]): number;
296
- export declare function average<T extends Record<PropertyKey, number>>(list: T[], key: keyof T): number;
292
+ export declare function inRange(value: number, min: number, max: number): boolean;
293
+ /**
294
+ * Splits an array into chunks of a fixed size.
295
+ *
296
+ * @example
297
+ * chunk([1,2,3,4,5,6,7], 3); // [[1,2,3],[4,5,6],[7]]
298
+ * chunk(['a','b','c','d'], 2); // [['a','b'], ['c','d']]
299
+ */
300
+ export declare function chunk<T>(arr: T[], size: number): T[][];
297
301
  //# sourceMappingURL=core.d.ts.map
@@ -1 +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;;;;;;;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CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAE9C;AAED;;;;;;;;GAQG;AACH,wBAAgB,IAAI,IAAI,IAAI,CAE3B;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAExE;AAED;;;;;;GAMG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,EAAE,IAAI,EAAE,MAAM,GAAG,CAAC,EAAE,EAAE,CAQtD"}
@@ -14,13 +14,14 @@ exports.isNotEmpty = isNotEmpty;
14
14
  exports.unique = unique;
15
15
  exports.distinct = distinct;
16
16
  exports.sample = sample;
17
- exports.rand = rand;
17
+ exports.random = random;
18
18
  exports.truthy = truthy;
19
19
  exports.falsy = falsy;
20
20
  exports.isSome = isSome;
21
21
  exports.isNone = isNone;
22
- exports.sum = sum;
23
- exports.average = average;
22
+ exports.noop = noop;
23
+ exports.inRange = inRange;
24
+ exports.chunk = chunk;
24
25
  const strings_js_1 = require("./strings.js");
25
26
  /**
26
27
  * Clone an object using structuredClone.
@@ -192,7 +193,7 @@ function distinct(list) {
192
193
  * ```
193
194
  */
194
195
  function sample(list) {
195
- return list[Math.floor(Math.random() * list.length)];
196
+ return list[random(0, list.length)];
196
197
  }
197
198
  /**
198
199
  * Gives a random number in the given range. The first parameter is inclusive
@@ -205,7 +206,7 @@ function sample(list) {
205
206
  * rand(3, 7); // 3 -> 6
206
207
  * ```
207
208
  */
208
- function rand(start, end) {
209
+ function random(start, end) {
209
210
  return Math.floor(Math.random() * (end - start)) + start;
210
211
  }
211
212
  /**
@@ -262,18 +263,45 @@ function isSome(thing) {
262
263
  function isNone(thing) {
263
264
  return thing === null || thing === undefined;
264
265
  }
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);
266
+ /**
267
+ * Returns a function that does nothing.
268
+ *
269
+ * @example
270
+ * ```ts
271
+ * const doNothing = noop();
272
+ * doNothing(); // does nothing
273
+ * ```
274
+ */
275
+ function noop() {
276
+ // Do nothing
275
277
  }
276
- // deno-lint-ignore no-explicit-any
277
- function average(list, key) {
278
- return (sum(list, key) / list.length) || 0;
278
+ /**
279
+ * Returns true if the given value is within the given range.
280
+ *
281
+ * @example
282
+ * ```ts
283
+ * inRange(5, 0, 10); // true
284
+ * inRange(0, 0, 10); // true
285
+ * inRange(10, 0, 10); // true
286
+ * inRange(11, 0, 10); // false
287
+ * ```
288
+ */
289
+ function inRange(value, min, max) {
290
+ return value >= min && value <= max;
291
+ }
292
+ /**
293
+ * Splits an array into chunks of a fixed size.
294
+ *
295
+ * @example
296
+ * chunk([1,2,3,4,5,6,7], 3); // [[1,2,3],[4,5,6],[7]]
297
+ * chunk(['a','b','c','d'], 2); // [['a','b'], ['c','d']]
298
+ */
299
+ function chunk(arr, size) {
300
+ if (size <= 0)
301
+ throw new Error("chunk size must be > 0");
302
+ const result = [];
303
+ for (let i = 0; i < arr.length; i += size) {
304
+ result.push(arr.slice(i, i + size));
305
+ }
306
+ return result;
279
307
  }
@@ -23,6 +23,19 @@ export type Days = Brand<number, "days">;
23
23
  export type Weeks = Brand<number, "weeks">;
24
24
  /** Represents a duration of time in years. */
25
25
  export type Years = Brand<number, "years">;
26
+ /**
27
+ * Sleeps for the given duration of milliseconds.
28
+ *
29
+ * @example Using `sleep` directly (for milliseconds).
30
+ * ```ts
31
+ * await sleep(1_000 as Milliseconds);
32
+ * ```
33
+ * @example Using `Duration.sleep` (for non-milliseconds).
34
+ * ```ts
35
+ * await Duration.seconds(1 as Seconds).sleep();
36
+ * ```
37
+ */
38
+ export declare const sleep: (ms: Milliseconds) => Promise<void>;
26
39
  /**
27
40
  * A representation of a duration of time within a codebase.
28
41
  * This class provides a way to work with durations of time in a type-safe manner.
@@ -98,5 +111,15 @@ export declare class Duration {
98
111
  * Converts the duration to years.
99
112
  */
100
113
  toYears(): Years;
114
+ /**
115
+ * Sleeps for the current duration.
116
+ *
117
+ * @example
118
+ * ```ts
119
+ * const duration = Duration.seconds(5);
120
+ * await duration.sleep();
121
+ * ```
122
+ */
123
+ sleep(): Promise<void>;
101
124
  }
102
125
  //# sourceMappingURL=duration.d.ts.map
@@ -1 +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,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3C,6CAA6C;AAC7C,MAAM,MAAM,IAAI,GAAG,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AACzC,8CAA8C;AAC9C,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAC3C,8CAA8C;AAC9C,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE3C;;;;;;;;;;;;;;;GAeG;AACH,qBAAa,QAAQ;IACC,OAAO,CAAC,QAAQ,CAAC,YAAY;IAAjD,OAAO;IAEP;;OAEG;IACH,MAAM,CAAC,YAAY,CAAC,YAAY,EAAE,YAAY,GAAG,QAAQ;I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QG;IACG,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;CAG7B"}
@@ -1,6 +1,56 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Duration = void 0;
36
+ exports.Duration = exports.sleep = void 0;
37
+ const dntShim = __importStar(require("../_dnt.shims.js"));
38
+ /**
39
+ * Sleeps for the given duration of milliseconds.
40
+ *
41
+ * @example Using `sleep` directly (for milliseconds).
42
+ * ```ts
43
+ * await sleep(1_000 as Milliseconds);
44
+ * ```
45
+ * @example Using `Duration.sleep` (for non-milliseconds).
46
+ * ```ts
47
+ * await Duration.seconds(1 as Seconds).sleep();
48
+ * ```
49
+ */
50
+ const sleep = async (ms) => {
51
+ await Duration.milliseconds(ms).sleep();
52
+ };
53
+ exports.sleep = sleep;
4
54
  /**
5
55
  * A representation of a duration of time within a codebase.
6
56
  * This class provides a way to work with durations of time in a type-safe manner.
@@ -110,5 +160,17 @@ class Duration {
110
160
  toYears() {
111
161
  return this.milliseconds / (365 * 24 * 60 * 60 * 1000);
112
162
  }
163
+ /**
164
+ * Sleeps for the current duration.
165
+ *
166
+ * @example
167
+ * ```ts
168
+ * const duration = Duration.seconds(5);
169
+ * await duration.sleep();
170
+ * ```
171
+ */
172
+ async sleep() {
173
+ await new Promise((resolve) => dntShim.setTimeout(resolve, this.toMilliseconds()));
174
+ }
113
175
  }
114
176
  exports.Duration = Duration;
@@ -55,4 +55,88 @@ export declare function isOdd(num: number): boolean;
55
55
  * ```
56
56
  */
57
57
  export declare function ordinalize(num: number): string;
58
+ /**
59
+ * Returns the maximum number from a list of numbers.
60
+ *
61
+ * @remarks
62
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * max([]); // 0
67
+ * max([1]); // 1
68
+ * max([2, 3]); // 3
69
+ * max([4, 5, 6]); // 6
70
+ * max([11, 21, 112]); // 112
71
+ * ```
72
+ */
73
+ export declare function max(nums: number[]): number;
74
+ /**
75
+ * Returns the minimum number from a list of numbers.
76
+ *
77
+ * @remarks
78
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * min([]); // 0
83
+ * min([1]); // 1
84
+ * min([2, 3]); // 2
85
+ * min([4, 5, 6]); // 4
86
+ * min([11, 21, 112]); // 11
87
+ * ```
88
+ */
89
+ export declare function min(nums: number[]): number;
90
+ /**
91
+ * Rounds a number to the nearest integer.
92
+ *
93
+ * @example
94
+ * ```ts
95
+ * round(null) // 0
96
+ * round(1.2); // 1
97
+ * round(1.5); // 2
98
+ * round(1.8); // 2
99
+ * round(2.2); // 2
100
+ * round(2.5); // 3
101
+ * round(2.8); // 3
102
+ * ```
103
+ */
104
+ export declare function round(num: number): number;
105
+ /**
106
+ * Returns the sum of a list of numbers.
107
+ *
108
+ * @remarks
109
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
110
+ *
111
+ * @example
112
+ * ```ts
113
+ * sum([1]); // 1
114
+ * sum([2, 3]); // 5
115
+ * sum([4, 5, 6]); // 15
116
+ * sum([11, 21, 112]); // 144
117
+ * ```
118
+ */
119
+ export declare function sum(nums: number[]): number;
120
+ /**
121
+ * Returns the average number from a list of numbers.
122
+ *
123
+ * @remarks
124
+ * This will include decimals, so you may want to use `Math.round`,
125
+ * `Math.floor` or `Math.ceil`.
126
+ *
127
+ * @example
128
+ * ```ts
129
+ * average([1]); // 1
130
+ * average([2, 3]); // 2.5
131
+ * average([4, 5, 6]); // 5
132
+ * average([11, 21, 112]); 48
133
+ * ```
134
+ * @example With a list of objects
135
+ * ```ts
136
+ * const people = [{ age: 20 }, { age: 18 }, { age: 19 }];
137
+ * const averageAge = average(people.map(({ age }) => age));
138
+ * console.log(averageAge); // 19
139
+ * ```
140
+ */
141
+ export declare function average(nums: number[]): number;
58
142
  //# sourceMappingURL=numbers.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/src/numbers.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE1C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C"}
1
+ {"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../src/src/numbers.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE3C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAE1C;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAiB9C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,KAAK,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,GAAG,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG1C;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,CAG9C"}
@@ -3,6 +3,12 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.isEven = isEven;
4
4
  exports.isOdd = isOdd;
5
5
  exports.ordinalize = ordinalize;
6
+ exports.max = max;
7
+ exports.min = min;
8
+ exports.round = round;
9
+ exports.sum = sum;
10
+ exports.average = average;
11
+ const core_js_1 = require("./core.js");
6
12
  /**
7
13
  * Determines if a number is even.
8
14
  *
@@ -79,3 +85,105 @@ function ordinalize(num) {
79
85
  return num + "th";
80
86
  }
81
87
  }
88
+ /**
89
+ * Returns the maximum number from a list of numbers.
90
+ *
91
+ * @remarks
92
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
93
+ *
94
+ * @example
95
+ * ```ts
96
+ * max([]); // 0
97
+ * max([1]); // 1
98
+ * max([2, 3]); // 3
99
+ * max([4, 5, 6]); // 6
100
+ * max([11, 21, 112]); // 112
101
+ * ```
102
+ */
103
+ function max(nums) {
104
+ if ((0, core_js_1.isEmpty)(nums))
105
+ return 0;
106
+ return Math.max(...nums);
107
+ }
108
+ /**
109
+ * Returns the minimum number from a list of numbers.
110
+ *
111
+ * @remarks
112
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * min([]); // 0
117
+ * min([1]); // 1
118
+ * min([2, 3]); // 2
119
+ * min([4, 5, 6]); // 4
120
+ * min([11, 21, 112]); // 11
121
+ * ```
122
+ */
123
+ function min(nums) {
124
+ if ((0, core_js_1.isEmpty)(nums))
125
+ return 0;
126
+ return Math.min(...nums);
127
+ }
128
+ /**
129
+ * Rounds a number to the nearest integer.
130
+ *
131
+ * @example
132
+ * ```ts
133
+ * round(null) // 0
134
+ * round(1.2); // 1
135
+ * round(1.5); // 2
136
+ * round(1.8); // 2
137
+ * round(2.2); // 2
138
+ * round(2.5); // 3
139
+ * round(2.8); // 3
140
+ * ```
141
+ */
142
+ function round(num) {
143
+ return Math.round(num || 0);
144
+ }
145
+ /**
146
+ * Returns the sum of a list of numbers.
147
+ *
148
+ * @remarks
149
+ * This is `null | undefined` safe. If you pass `null | undefined` this will return `0`.
150
+ *
151
+ * @example
152
+ * ```ts
153
+ * sum([1]); // 1
154
+ * sum([2, 3]); // 5
155
+ * sum([4, 5, 6]); // 15
156
+ * sum([11, 21, 112]); // 144
157
+ * ```
158
+ */
159
+ function sum(nums) {
160
+ if ((0, core_js_1.isEmpty)(nums))
161
+ return 0;
162
+ return nums.reduce((acc, num) => acc + num, 0);
163
+ }
164
+ /**
165
+ * Returns the average number from a list of numbers.
166
+ *
167
+ * @remarks
168
+ * This will include decimals, so you may want to use `Math.round`,
169
+ * `Math.floor` or `Math.ceil`.
170
+ *
171
+ * @example
172
+ * ```ts
173
+ * average([1]); // 1
174
+ * average([2, 3]); // 2.5
175
+ * average([4, 5, 6]); // 5
176
+ * average([11, 21, 112]); 48
177
+ * ```
178
+ * @example With a list of objects
179
+ * ```ts
180
+ * const people = [{ age: 20 }, { age: 18 }, { age: 19 }];
181
+ * const averageAge = average(people.map(({ age }) => age));
182
+ * console.log(averageAge); // 19
183
+ * ```
184
+ */
185
+ function average(nums) {
186
+ if ((0, core_js_1.isEmpty)(nums))
187
+ return 0;
188
+ return sum(nums) / nums.length;
189
+ }