@augment-vir/common 31.25.0 → 31.26.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.
@@ -1,16 +1,19 @@
1
1
  import { type MaybePromise } from '@augment-vir/core';
2
+ import { type InverseBoolean } from '../type/boolean.js';
3
+ import { type MakePartial } from '../type/partial.js';
2
4
  /**
3
5
  * Polyfill for `Object.groupBy`:
4
6
  * https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/groupBy
5
7
  *
6
8
  * @category Array
7
9
  * @category Object
10
+ * @category Package : @augment-vir/common
8
11
  * @example
9
12
  *
10
13
  * ```ts
11
- * import {arrayToObject} from '@augment-vir/common';
14
+ * import {groupArrayBy} from '@augment-vir/common';
12
15
  *
13
- * const result = arrayToObject(
16
+ * const result = groupArrayBy(
14
17
  * [
15
18
  * 'a',
16
19
  * 'b',
@@ -19,17 +22,125 @@ import { type MaybePromise } from '@augment-vir/core';
19
22
  * );
20
23
  * // result is `{key-a: ['a'], key-b: ['b']}`
21
24
  * ```
25
+ *
26
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
27
+ */
28
+ export declare function groupArrayBy<ElementType, NewKey extends PropertyKey, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey, options?: ArrayToObjectOptions<UseRequired>): MakePartial<Record<NewKey, ElementType[]>, InverseBoolean<UseRequired>>;
29
+ /**
30
+ * Options for {@link groupArrayBy} and {@link arrayToObject}.
31
+ *
32
+ * @category Array
33
+ * @category Object
34
+ * @category Package : @augment-vir/common
35
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
22
36
  */
23
- export declare function groupArrayBy<ElementType, NewKey extends PropertyKey>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => NewKey): Partial<Record<NewKey, ElementType[]>>;
24
- export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
37
+ export type ArrayToObjectOptions<UseRequired extends boolean | undefined> = Partial<{
38
+ /**
39
+ * Set to `true` to make the object return type not use `Partial<>`.
40
+ *
41
+ * @default false
42
+ */
43
+ useRequired: UseRequired;
44
+ }>;
45
+ /**
46
+ * Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
47
+ * `value` outputs from the callback. The resulting object does not have an array of elements
48
+ * (unless the original array itself contains arrays). Automatically handles the case where the
49
+ * callback returns a promise.
50
+ *
51
+ * @category Array
52
+ * @category Object
53
+ * @category Package : @augment-vir/common
54
+ * @example
55
+ *
56
+ * ```ts
57
+ * import {arrayToObject} from '@augment-vir/common';
58
+ *
59
+ * const result = arrayToObject(
60
+ * [
61
+ * 'a',
62
+ * 'b',
63
+ * ],
64
+ * (value) => {
65
+ * return {key: `key-${value}`, value};
66
+ * },
67
+ * );
68
+ * // result is `{key-a: 'a', key-b: 'b'}`
69
+ * ```
70
+ *
71
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
72
+ */
73
+ export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => Promise<{
25
74
  key: NewKey;
26
75
  value: NewValue;
27
- } | undefined>): Promise<Partial<Record<NewKey, NewValue>>>;
28
- export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
76
+ } | undefined>,
77
+ /** Optional. */
78
+ options?: ArrayToObjectOptions<UseRequired>): Promise<MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>>;
79
+ /**
80
+ * Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
81
+ * `value` outputs from the callback. The resulting object does not have an array of elements
82
+ * (unless the original array itself contains arrays). Automatically handles the case where the
83
+ * callback returns a promise.
84
+ *
85
+ * @category Array
86
+ * @category Object
87
+ * @category Package : @augment-vir/common
88
+ * @example
89
+ *
90
+ * ```ts
91
+ * import {arrayToObject} from '@augment-vir/common';
92
+ *
93
+ * const result = arrayToObject(
94
+ * [
95
+ * 'a',
96
+ * 'b',
97
+ * ],
98
+ * (value) => {
99
+ * return {key: `key-${value}`, value};
100
+ * },
101
+ * );
102
+ * // result is `{key-a: 'a', key-b: 'b'}`
103
+ * ```
104
+ *
105
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
106
+ */
107
+ export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => {
29
108
  key: NewKey;
30
109
  value: NewValue;
31
- } | undefined): Partial<Record<NewKey, NewValue>>;
32
- export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
110
+ } | undefined,
111
+ /** Optional. */
112
+ options?: ArrayToObjectOptions<UseRequired>): MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>;
113
+ /**
114
+ * Similar to {@link groupArrayBy} but maps array entries to a single key and requires `key` _and_
115
+ * `value` outputs from the callback. The resulting object does not have an array of elements
116
+ * (unless the original array itself contains arrays). Automatically handles the case where the
117
+ * callback returns a promise.
118
+ *
119
+ * @category Array
120
+ * @category Object
121
+ * @category Package : @augment-vir/common
122
+ * @example
123
+ *
124
+ * ```ts
125
+ * import {arrayToObject} from '@augment-vir/common';
126
+ *
127
+ * const result = arrayToObject(
128
+ * [
129
+ * 'a',
130
+ * 'b',
131
+ * ],
132
+ * (value) => {
133
+ * return {key: `key-${value}`, value};
134
+ * },
135
+ * );
136
+ * // result is `{key-a: 'a', key-b: 'b'}`
137
+ * ```
138
+ *
139
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
140
+ */
141
+ export declare function arrayToObject<ElementType, NewKey extends PropertyKey, NewValue, const UseRequired extends boolean | undefined = undefined>(inputArray: ReadonlyArray<ElementType>, callback: (value: ElementType, index: number, originalArray: ReadonlyArray<ElementType>) => MaybePromise<{
33
142
  key: NewKey;
34
143
  value: NewValue;
35
- } | undefined>): MaybePromise<Partial<Record<NewKey, NewValue>>>;
144
+ } | undefined>,
145
+ /** Optional. */
146
+ options?: ArrayToObjectOptions<UseRequired>): MaybePromise<MakePartial<Record<NewKey, NewValue>, InverseBoolean<UseRequired>>>;
@@ -9,12 +9,13 @@ import { filterMap } from './filter.js';
9
9
  *
10
10
  * @category Array
11
11
  * @category Object
12
+ * @category Package : @augment-vir/common
12
13
  * @example
13
14
  *
14
15
  * ```ts
15
- * import {arrayToObject} from '@augment-vir/common';
16
+ * import {groupArrayBy} from '@augment-vir/common';
16
17
  *
17
- * const result = arrayToObject(
18
+ * const result = groupArrayBy(
18
19
  * [
19
20
  * 'a',
20
21
  * 'b',
@@ -23,8 +24,12 @@ import { filterMap } from './filter.js';
23
24
  * );
24
25
  * // result is `{key-a: ['a'], key-b: ['b']}`
25
26
  * ```
27
+ *
28
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
26
29
  */
27
- export function groupArrayBy(inputArray, callback) {
30
+ export function groupArrayBy(inputArray, callback,
31
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
32
+ options = {}) {
28
33
  return inputArray.reduce((accum, entry, index, originalArray) => {
29
34
  const key = callback(entry, index, originalArray);
30
35
  const entryArray = getOrSet(accum, key, () => []);
@@ -60,7 +65,10 @@ export function groupArrayBy(inputArray, callback) {
60
65
  *
61
66
  * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
62
67
  */
63
- export function arrayToObject(inputArray, callback) {
68
+ export function arrayToObject(inputArray, callback,
69
+ /** Optional. */
70
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
71
+ options = {}) {
64
72
  try {
65
73
  let gotAPromise = false;
66
74
  const mappedEntries = inputArray
@@ -1,2 +1,9 @@
1
1
  import { type AnyObject } from '@augment-vir/core';
2
- export declare function sortObject<const T extends AnyObject>(original: T): T;
2
+ /**
3
+ * Creates as new sorted object copied from the the original given object.
4
+ *
5
+ * @category Object
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export declare function sortObject<const T extends AnyObject>(original: Readonly<T>): T;
@@ -1,3 +1,10 @@
1
+ /**
2
+ * Creates as new sorted object copied from the the original given object.
3
+ *
4
+ * @category Object
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
1
8
  export function sortObject(original) {
2
9
  return Object.fromEntries(Object.entries(original).sort((a, b) => a[0].localeCompare(b[0])));
3
10
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Reverses a boolean (optionally `undefined`). `true` becomes `false`. `false` and `undefined`
3
+ * become `true`.
4
+ *
5
+ * @category Type
6
+ * @category Package : @augment-vir/common
7
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
8
+ */
9
+ export type InverseBoolean<Input extends boolean | undefined> = Input extends true ? false : true;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Sets `T` as partial if `ShouldBePartial` is `true`. Otherwise, `T` is left alone.
3
+ *
4
+ * @category Type
5
+ * @category Package : @augment-vir/common
6
+ * @package [`@augment-vir/common`](https://www.npmjs.com/package/@augment-vir/common)
7
+ */
8
+ export type MakePartial<T extends object, ShouldBePartial extends boolean> = ShouldBePartial extends true ? Partial<T> : T;
@@ -0,0 +1 @@
1
+ export {};
package/dist/index.d.ts CHANGED
@@ -84,7 +84,9 @@ export * from './augments/string/substring-index.js';
84
84
  export * from './augments/string/suffix.js';
85
85
  export * from './augments/string/white-space.js';
86
86
  export * from './augments/string/wrap-string.js';
87
+ export * from './augments/type/boolean.js';
87
88
  export * from './augments/type/ensure-type.js';
89
+ export * from './augments/type/partial.js';
88
90
  export * from './augments/type/readonly.js';
89
91
  export * from './augments/type/type-recursion.js';
90
92
  export * from './augments/type/union.js';
package/dist/index.js CHANGED
@@ -84,7 +84,9 @@ export * from './augments/string/substring-index.js';
84
84
  export * from './augments/string/suffix.js';
85
85
  export * from './augments/string/white-space.js';
86
86
  export * from './augments/string/wrap-string.js';
87
+ export * from './augments/type/boolean.js';
87
88
  export * from './augments/type/ensure-type.js';
89
+ export * from './augments/type/partial.js';
88
90
  export * from './augments/type/readonly.js';
89
91
  export * from './augments/type/type-recursion.js';
90
92
  export * from './augments/type/union.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "31.25.0",
3
+ "version": "31.26.0",
4
4
  "description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
5
5
  "keywords": [
6
6
  "augment",
@@ -40,8 +40,8 @@
40
40
  "test:web": "virmator --no-deps test web"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/assert": "^31.25.0",
44
- "@augment-vir/core": "^31.25.0",
43
+ "@augment-vir/assert": "^31.26.0",
44
+ "@augment-vir/core": "^31.26.0",
45
45
  "@date-vir/duration": "^7.3.1",
46
46
  "ansi-styles": "^6.2.1",
47
47
  "deepcopy-esm": "^2.1.1",