@augment-vir/common 30.5.1 → 30.6.1

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.
@@ -0,0 +1,30 @@
1
+ import type { Tuple } from '@augment-vir/core';
2
+ /**
3
+ * Creates an array of size `size` and calls the given `callback` for each entry in the array and
4
+ * fills the array with the results. The returned array is typed to exactly fit the given size.
5
+ *
6
+ * This function automatically awaits async callbacks.
7
+ *
8
+ * @category Array
9
+ * @category Package : @augment-vir/common
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {createArray} from '@augment-vir/common';
14
+ *
15
+ * const result = createArray(5, (index) => {
16
+ * return `hi ${index}`;
17
+ * });
18
+ * // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`
19
+ *
20
+ * const asyncResult = await createArray(5, async (index) => {
21
+ * return Promise.resolve(`hi ${index}`);
22
+ * });
23
+ * // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`
24
+ * ```
25
+ *
26
+ * @returns A new array filled with the results of the given `callback` typed as a Tuple,
27
+ * automatically wrapping the return type in a `Promise` if the callback returns one.
28
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
29
+ */
30
+ export declare function createArray<Size extends number, T>(size: Size, callback: (index: number) => T): Extract<T, Promise<any>> extends never ? Tuple<T, Size> : Promise<Tuple<Awaited<T>, Size>>;
@@ -0,0 +1,41 @@
1
+ import { check } from '@augment-vir/assert';
2
+ /**
3
+ * Creates an array of size `size` and calls the given `callback` for each entry in the array and
4
+ * fills the array with the results. The returned array is typed to exactly fit the given size.
5
+ *
6
+ * This function automatically awaits async callbacks.
7
+ *
8
+ * @category Array
9
+ * @category Package : @augment-vir/common
10
+ * @example
11
+ *
12
+ * ```ts
13
+ * import {createArray} from '@augment-vir/common';
14
+ *
15
+ * const result = createArray(5, (index) => {
16
+ * return `hi ${index}`;
17
+ * });
18
+ * // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`
19
+ *
20
+ * const asyncResult = await createArray(5, async (index) => {
21
+ * return Promise.resolve(`hi ${index}`);
22
+ * });
23
+ * // result is `['hi 0', 'hi 1', 'hi 2', 'hi 3', 'hi 4']`
24
+ * ```
25
+ *
26
+ * @returns A new array filled with the results of the given `callback` typed as a Tuple,
27
+ * automatically wrapping the return type in a `Promise` if the callback returns one.
28
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
29
+ */
30
+ export function createArray(size, callback) {
31
+ const array = [];
32
+ let gotPromise = false;
33
+ for (let i = 0; i < size; i++) {
34
+ const result = callback(i);
35
+ if (check.isPromise(result)) {
36
+ gotPromise = true;
37
+ }
38
+ array.push(result);
39
+ }
40
+ return (gotPromise ? Promise.all(array) : array);
41
+ }
@@ -1,4 +1,4 @@
1
- import { MinMax } from './min-max.js';
1
+ import { MinMax } from '@augment-vir/core';
2
2
  /**
3
3
  * Clamp's the given value to within the min and max bounds, inclusive.
4
4
  *
@@ -1,7 +1,7 @@
1
- import { MinMax } from './min-max.js';
1
+ import { MinMax } from '@augment-vir/core';
2
2
  /**
3
3
  * If the given value is outside the given min/max bounds, instead of clamping the number (as the
4
- * {@link clamp} function does), this function wraps the value around to the next bound (inclusive).
4
+ * `clamp` function does), this function wraps the value around to the next bound (inclusive).
5
5
  *
6
6
  * @category Number
7
7
  * @category Package : @augment-vir/common
@@ -1,7 +1,7 @@
1
- import { ensureMinMax } from './min-max.js';
1
+ import { ensureMinMax } from '@augment-vir/core';
2
2
  /**
3
3
  * If the given value is outside the given min/max bounds, instead of clamping the number (as the
4
- * {@link clamp} function does), this function wraps the value around to the next bound (inclusive).
4
+ * `clamp` function does), this function wraps the value around to the next bound (inclusive).
5
5
  *
6
6
  * @category Number
7
7
  * @category Package : @augment-vir/common
@@ -1,4 +1,4 @@
1
- import { ensureMinMax } from '../number/min-max.js';
1
+ import { ensureMinMax } from '@augment-vir/core';
2
2
  /**
3
3
  * Creates a random integer (no decimal points are included) between the given min and max values
4
4
  * (inclusive).
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export * from './augments/array/array-to-object.js';
3
3
  export * from './augments/array/awaited/awaited-filter.js';
4
4
  export * from './augments/array/awaited/awaited-for-each.js';
5
5
  export * from './augments/array/awaited/awaited-map.js';
6
+ export * from './augments/array/create-array.js';
6
7
  export * from './augments/array/filter.js';
7
8
  export * from './augments/array/remove-duplicates.js';
8
9
  export * from './augments/array/repeat-array.js';
@@ -29,7 +30,6 @@ export * from './augments/number/clamp.js';
29
30
  export * from './augments/number/coords.js';
30
31
  export * from './augments/number/digit.js';
31
32
  export * from './augments/number/dimensions.js';
32
- export * from './augments/number/min-max.js';
33
33
  export * from './augments/number/number-conversion.js';
34
34
  export * from './augments/number/round.js';
35
35
  export * from './augments/number/scientific.js';
package/dist/index.js CHANGED
@@ -3,6 +3,7 @@ export * from './augments/array/array-to-object.js';
3
3
  export * from './augments/array/awaited/awaited-filter.js';
4
4
  export * from './augments/array/awaited/awaited-for-each.js';
5
5
  export * from './augments/array/awaited/awaited-map.js';
6
+ export * from './augments/array/create-array.js';
6
7
  export * from './augments/array/filter.js';
7
8
  export * from './augments/array/remove-duplicates.js';
8
9
  export * from './augments/array/repeat-array.js';
@@ -29,7 +30,6 @@ export * from './augments/number/clamp.js';
29
30
  export * from './augments/number/coords.js';
30
31
  export * from './augments/number/digit.js';
31
32
  export * from './augments/number/dimensions.js';
32
- export * from './augments/number/min-max.js';
33
33
  export * from './augments/number/number-conversion.js';
34
34
  export * from './augments/number/round.js';
35
35
  export * from './augments/number/scientific.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "30.5.1",
3
+ "version": "30.6.1",
4
4
  "description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
5
5
  "keywords": [
6
6
  "augment",
@@ -39,8 +39,8 @@
39
39
  "test:web": "virmator --no-deps test web"
40
40
  },
41
41
  "dependencies": {
42
- "@augment-vir/assert": "^30.5.1",
43
- "@augment-vir/core": "^30.5.1",
42
+ "@augment-vir/assert": "^30.6.1",
43
+ "@augment-vir/core": "^30.6.1",
44
44
  "@date-vir/duration": "^6.0.1",
45
45
  "ansi-styles": "^6.2.1",
46
46
  "json5": "^2.2.3",
@@ -1,18 +0,0 @@
1
- /**
2
- * @category Number
3
- * @category Package : @augment-vir/common
4
- * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
5
- */
6
- export type MinMax = {
7
- min: number;
8
- max: number;
9
- };
10
- /**
11
- * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
12
- * that is not the case, values are swapped.
13
- *
14
- * @category Number
15
- * @category Package : @augment-vir/common
16
- * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
17
- */
18
- export declare function ensureMinMax({ min, max }: MinMax): MinMax;
@@ -1,16 +0,0 @@
1
- /**
2
- * Given a min and max, ensures that they are in correct order. Meaning, min is less than max. If
3
- * that is not the case, values are swapped.
4
- *
5
- * @category Number
6
- * @category Package : @augment-vir/common
7
- * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
- */
9
- export function ensureMinMax({ min, max }) {
10
- if (min > max) {
11
- return { min: max, max: min };
12
- }
13
- else {
14
- return { min, max };
15
- }
16
- }