@augment-vir/common 31.21.1 → 31.23.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.
@@ -0,0 +1,72 @@
1
+ import { type RequireExactlyOne } from 'type-fest';
2
+ /**
3
+ * Options for {@link getArrayPage}.
4
+ *
5
+ * @category Internal
6
+ */
7
+ export type ArrayPaginationOptions = {
8
+ /** Split the array into pages of this size. */
9
+ countPerPage: number;
10
+ /** Get this page number. This is 0 indexed. */
11
+ getPage: number;
12
+ };
13
+ /**
14
+ * Split an array into pages of size `countPerPage` and then get the `getPage` page from that split.
15
+ * `getPage` is `0` indexed.
16
+ *
17
+ * @category Array
18
+ * @example
19
+ *
20
+ * ```ts
21
+ * import {getArrayPage} from '@augment-vir/common';
22
+ *
23
+ * const result = getArrayPage(
24
+ * [
25
+ * 'a',
26
+ * 'b',
27
+ * 'c',
28
+ * ],
29
+ * {
30
+ * countPerPage: 2,
31
+ * getPage: 1,
32
+ * },
33
+ * );
34
+ * // result is `['c']`
35
+ * ```
36
+ */
37
+ export declare function getArrayPage<ArrayEntry>(originalArray: ReadonlyArray<ArrayEntry>, options: Readonly<ArrayPaginationOptions>): ArrayEntry[] | undefined;
38
+ /**
39
+ * Options for {@link chunkArray}. Only set one property (`chunkCount` or `chunkSize`). If the value
40
+ * given is `0`, the array will not be chunked at all.
41
+ *
42
+ * @category Internal
43
+ */
44
+ export type ChunkArrayOptions = RequireExactlyOne<{
45
+ /** Split the array into this many chunks. */
46
+ chunkCount: number;
47
+ /** Split the array into chunks of this size. */
48
+ chunkSize: number;
49
+ }>;
50
+ /**
51
+ * Split an array into multiple sub array "chunks" based on the given options.
52
+ *
53
+ * @category Array
54
+ * @example
55
+ *
56
+ * ```ts
57
+ * import {chunkArray} from '@augment-vir/common';
58
+ *
59
+ * const result = chunkArray(
60
+ * [
61
+ * 'a',
62
+ * 'b',
63
+ * 'c',
64
+ * ],
65
+ * {
66
+ * chunkSize: 2,
67
+ * },
68
+ * );
69
+ * // result is `[['a', 'b'], ['c']]`
70
+ * ```
71
+ */
72
+ export declare function chunkArray<ArrayEntry>(originalArray: ReadonlyArray<ArrayEntry>, options: Readonly<ChunkArrayOptions>): ArrayEntry[][];
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Split an array into pages of size `countPerPage` and then get the `getPage` page from that split.
3
+ * `getPage` is `0` indexed.
4
+ *
5
+ * @category Array
6
+ * @example
7
+ *
8
+ * ```ts
9
+ * import {getArrayPage} from '@augment-vir/common';
10
+ *
11
+ * const result = getArrayPage(
12
+ * [
13
+ * 'a',
14
+ * 'b',
15
+ * 'c',
16
+ * ],
17
+ * {
18
+ * countPerPage: 2,
19
+ * getPage: 1,
20
+ * },
21
+ * );
22
+ * // result is `['c']`
23
+ * ```
24
+ */
25
+ export function getArrayPage(originalArray, options) {
26
+ const chunks = chunkArray(originalArray, { chunkSize: options.countPerPage });
27
+ return chunks[options.getPage];
28
+ }
29
+ /**
30
+ * Split an array into multiple sub array "chunks" based on the given options.
31
+ *
32
+ * @category Array
33
+ * @example
34
+ *
35
+ * ```ts
36
+ * import {chunkArray} from '@augment-vir/common';
37
+ *
38
+ * const result = chunkArray(
39
+ * [
40
+ * 'a',
41
+ * 'b',
42
+ * 'c',
43
+ * ],
44
+ * {
45
+ * chunkSize: 2,
46
+ * },
47
+ * );
48
+ * // result is `[['a', 'b'], ['c']]`
49
+ * ```
50
+ */
51
+ export function chunkArray(originalArray, options) {
52
+ const chunkSize = options.chunkSize ||
53
+ (options.chunkCount ? Math.ceil(originalArray.length / options.chunkCount) : 0);
54
+ if (!chunkSize) {
55
+ return [[...originalArray]];
56
+ }
57
+ const chunkedArray = [];
58
+ for (let i = 0; i < originalArray.length; i += chunkSize) {
59
+ chunkedArray.push(originalArray.slice(i, i + chunkSize));
60
+ }
61
+ return chunkedArray;
62
+ }
@@ -0,0 +1 @@
1
+ export { deepCopy, type CustomCopy, type DeepCopyOptions } from 'deepcopy-esm';
@@ -0,0 +1 @@
1
+ export { deepCopy } from 'deepcopy-esm';
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/array/array-map.js';
2
+ export * from './augments/array/array-pagination.js';
2
3
  export * from './augments/array/array-to-object.js';
3
4
  export * from './augments/array/awaited/awaited-filter.js';
4
5
  export * from './augments/array/awaited/awaited-for-each.js';
@@ -36,6 +37,7 @@ export * from './augments/number/round.js';
36
37
  export * from './augments/number/scientific.js';
37
38
  export * from './augments/number/truncate-number.js';
38
39
  export * from './augments/number/wrap-number.js';
40
+ export * from './augments/object/deep-copy.js';
39
41
  export * from './augments/object/diff.js';
40
42
  export * from './augments/object/empty.js';
41
43
  export * from './augments/object/get-or-set.js';
package/dist/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export * from './augments/array/array-map.js';
2
+ export * from './augments/array/array-pagination.js';
2
3
  export * from './augments/array/array-to-object.js';
3
4
  export * from './augments/array/awaited/awaited-filter.js';
4
5
  export * from './augments/array/awaited/awaited-for-each.js';
@@ -36,6 +37,7 @@ export * from './augments/number/round.js';
36
37
  export * from './augments/number/scientific.js';
37
38
  export * from './augments/number/truncate-number.js';
38
39
  export * from './augments/number/wrap-number.js';
40
+ export * from './augments/object/deep-copy.js';
39
41
  export * from './augments/object/diff.js';
40
42
  export * from './augments/object/empty.js';
41
43
  export * from './augments/object/get-or-set.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@augment-vir/common",
3
- "version": "31.21.1",
3
+ "version": "31.23.0",
4
4
  "description": "A collection of augments, helpers types, functions, and classes for any JavaScript environment.",
5
5
  "keywords": [
6
6
  "augment",
@@ -40,10 +40,12 @@
40
40
  "test:web": "virmator --no-deps test web"
41
41
  },
42
42
  "dependencies": {
43
- "@augment-vir/assert": "^31.21.1",
44
- "@augment-vir/core": "^31.21.1",
43
+ "@augment-vir/assert": "^31.23.0",
44
+ "@augment-vir/core": "^31.23.0",
45
45
  "@date-vir/duration": "^7.3.1",
46
46
  "ansi-styles": "^6.2.1",
47
+ "deepcopy": "^2.1.0",
48
+ "deepcopy-esm": "^2.1.0",
47
49
  "json5": "^2.2.3",
48
50
  "type-fest": "^4.41.0",
49
51
  "typed-event-target": "^4.1.0"