@augment-vir/common 29.0.0 → 29.1.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.
|
@@ -1,2 +1,52 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.selectFrom = selectFrom;
|
|
4
|
+
exports.selectCollapsedFrom = selectCollapsedFrom;
|
|
5
|
+
const run_time_assertions_1 = require("run-time-assertions");
|
|
6
|
+
const tuple_1 = require("../tuple");
|
|
7
|
+
const filter_object_1 = require("./filter-object");
|
|
8
|
+
const map_object_1 = require("./map-object");
|
|
9
|
+
function shouldPreserve(input) {
|
|
10
|
+
return (0, run_time_assertions_1.isPrimitive)(input) || input instanceof RegExp;
|
|
11
|
+
}
|
|
12
|
+
function selectFrom(originalObject, selectionSet) {
|
|
13
|
+
if (Array.isArray(originalObject)) {
|
|
14
|
+
return originalObject.map((originalEntry) => selectFrom(originalEntry, selectionSet));
|
|
15
|
+
}
|
|
16
|
+
const keysToRemove = [];
|
|
17
|
+
return (0, filter_object_1.omitObjectKeys)((0, map_object_1.mapObjectValues)(originalObject, (key, value) => {
|
|
18
|
+
const selection = selectionSet[key];
|
|
19
|
+
if (selection === true) {
|
|
20
|
+
return value;
|
|
21
|
+
}
|
|
22
|
+
else if (!selection) {
|
|
23
|
+
keysToRemove.push(key);
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
return selectFrom(value, selection);
|
|
28
|
+
}
|
|
29
|
+
}), keysToRemove);
|
|
30
|
+
}
|
|
31
|
+
function selectCollapsedFrom(originalObject, selectionSet, collapse) {
|
|
32
|
+
const selected = selectFrom(originalObject, selectionSet);
|
|
33
|
+
return collapseObject(selected);
|
|
34
|
+
}
|
|
35
|
+
function collapseObject(input) {
|
|
36
|
+
if (shouldPreserve(input)) {
|
|
37
|
+
return input;
|
|
38
|
+
}
|
|
39
|
+
const keys = Object.keys(input);
|
|
40
|
+
if (Array.isArray(input)) {
|
|
41
|
+
return input.map((innerInput) => collapseObject(innerInput));
|
|
42
|
+
}
|
|
43
|
+
else if ((0, tuple_1.isLengthAtLeast)(keys, 2)) {
|
|
44
|
+
return input;
|
|
45
|
+
}
|
|
46
|
+
else if ((0, tuple_1.isLengthAtLeast)(keys, 1)) {
|
|
47
|
+
return collapseObject(input[keys[0]]);
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
return input;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1 +1,48 @@
|
|
|
1
|
-
|
|
1
|
+
import { isPrimitive } from 'run-time-assertions';
|
|
2
|
+
import { isLengthAtLeast } from '../tuple';
|
|
3
|
+
import { omitObjectKeys } from './filter-object';
|
|
4
|
+
import { mapObjectValues } from './map-object';
|
|
5
|
+
function shouldPreserve(input) {
|
|
6
|
+
return isPrimitive(input) || input instanceof RegExp;
|
|
7
|
+
}
|
|
8
|
+
export function selectFrom(originalObject, selectionSet) {
|
|
9
|
+
if (Array.isArray(originalObject)) {
|
|
10
|
+
return originalObject.map((originalEntry) => selectFrom(originalEntry, selectionSet));
|
|
11
|
+
}
|
|
12
|
+
const keysToRemove = [];
|
|
13
|
+
return omitObjectKeys(mapObjectValues(originalObject, (key, value) => {
|
|
14
|
+
const selection = selectionSet[key];
|
|
15
|
+
if (selection === true) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
else if (!selection) {
|
|
19
|
+
keysToRemove.push(key);
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
return selectFrom(value, selection);
|
|
24
|
+
}
|
|
25
|
+
}), keysToRemove);
|
|
26
|
+
}
|
|
27
|
+
export function selectCollapsedFrom(originalObject, selectionSet, collapse) {
|
|
28
|
+
const selected = selectFrom(originalObject, selectionSet);
|
|
29
|
+
return collapseObject(selected);
|
|
30
|
+
}
|
|
31
|
+
function collapseObject(input) {
|
|
32
|
+
if (shouldPreserve(input)) {
|
|
33
|
+
return input;
|
|
34
|
+
}
|
|
35
|
+
const keys = Object.keys(input);
|
|
36
|
+
if (Array.isArray(input)) {
|
|
37
|
+
return input.map((innerInput) => collapseObject(innerInput));
|
|
38
|
+
}
|
|
39
|
+
else if (isLengthAtLeast(keys, 2)) {
|
|
40
|
+
return input;
|
|
41
|
+
}
|
|
42
|
+
else if (isLengthAtLeast(keys, 1)) {
|
|
43
|
+
return collapseObject(input[keys[0]]);
|
|
44
|
+
}
|
|
45
|
+
else {
|
|
46
|
+
return input;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IsNever, Primitive } from 'type-fest';
|
|
1
|
+
import { IsAny, IsNever, Primitive } from 'type-fest';
|
|
2
2
|
import { TsRecurse, TsRecursionStart, TsRecursionTracker, TsTooMuchRecursion } from '../type-recursion';
|
|
3
3
|
import { UnionToIntersection } from '../union';
|
|
4
4
|
import { AnyObject } from './any-object';
|
|
@@ -8,11 +8,11 @@ import { PropertyValueType } from './object';
|
|
|
8
8
|
export type SelectionTypesToPreserve = Primitive | RegExp;
|
|
9
9
|
/** A generic selection set without specific keys. */
|
|
10
10
|
export type GenericSelectionSet = {
|
|
11
|
-
[Key in PropertyKey]: boolean | GenericSelectionSet;
|
|
11
|
+
[Key in PropertyKey]: boolean | GenericSelectionSet | unknown;
|
|
12
12
|
};
|
|
13
13
|
/** Masks an object value with the given {@link SelectionSet}. */
|
|
14
14
|
export type PickSelection<Full extends Readonly<AnyObject>, Selection extends GenericSelectionSet, Depth extends TsRecursionTracker = TsRecursionStart> = Depth extends TsTooMuchRecursion ? ['Error: recursive object depth is too deep.'] : Full extends ReadonlyArray<infer Element extends any> ? (PickSelection<Extract<Element, AnyObject>, Selection, TsRecurse<Depth>> | Exclude<Element, AnyObject>)[] : {
|
|
15
|
-
[Key in keyof Selection as Selection[Key] extends false ? never : Key extends keyof Full ? Key : never]: (Selection[Key] extends GenericSelectionSet ? PickSelection<NonNullable<Extract<Full[Key], AnyObject>>, Selection[Key], TsRecurse<Depth>> : Full[Key]) | Exclude<Full[Key], AnyObject>;
|
|
15
|
+
-readonly [Key in keyof Selection as Selection[Key] extends false ? never : Key extends keyof Full ? Key : never]: (Selection[Key] extends GenericSelectionSet ? PickSelection<NonNullable<Extract<Full[Key], AnyObject>>, Selection[Key], TsRecurse<Depth>> : Full[Key]) | Exclude<Full[Key], AnyObject>;
|
|
16
16
|
};
|
|
17
17
|
/**
|
|
18
18
|
* Collapses a selected value to the first part of the selection that contains more than 1 key or
|
|
@@ -20,6 +20,8 @@ export type PickSelection<Full extends Readonly<AnyObject>, Selection extends Ge
|
|
|
20
20
|
*/
|
|
21
21
|
export type FirstSelectedValue<Full extends Readonly<AnyObject>, Selection extends GenericSelectionSet, Depth extends TsRecursionTracker = TsRecursionStart> = Depth extends TsTooMuchRecursion ? 'Error: recursive object depth is too deep.' : KeyCount<PickSelection<Full, Selection, Depth>> extends 1 ? Selection[keyof PickSelection<Full, Selection, Depth>] extends GenericSelectionSet ? FirstSelectedValue<Full[keyof PickSelection<Full, Selection, Depth>], Selection[keyof PickSelection<Full, Selection, Depth>], TsRecurse<Depth>> : PropertyValueType<PickSelection<Full, Selection, Depth>> : PickSelection<Full, Selection, Depth>;
|
|
22
22
|
/** Defines a selection set for the given object. */
|
|
23
|
-
export type SelectionSet<Full extends Readonly<AnyObject>, Depth extends TsRecursionTracker = TsRecursionStart> = Depth extends TsTooMuchRecursion ? ['Error: recursive object depth is too deep.'] : Full extends ReadonlyArray<infer FullChild extends AnyObject> ? SelectionSet<FullChild, TsRecurse<Depth>> : Partial<{
|
|
23
|
+
export type SelectionSet<Full extends Readonly<AnyObject>, Depth extends TsRecursionTracker = TsRecursionStart> = IsAny<Full> extends true ? any : Depth extends TsTooMuchRecursion ? ['Error: recursive object depth is too deep.'] : Full extends ReadonlyArray<infer FullChild extends AnyObject> ? SelectionSet<FullChild, TsRecurse<Depth>> : Partial<{
|
|
24
24
|
[Key in keyof Full]: IsNever<Exclude<Full[Key], SelectionTypesToPreserve>> extends true ? boolean : boolean | UnionToIntersection<SelectionSet<Full[Key], TsRecurse<Depth>>>;
|
|
25
25
|
}>;
|
|
26
|
+
export declare function selectFrom<Full extends AnyObject, const Selection extends SelectionSet<NoInfer<Full>>>(originalObject: Readonly<Full>, selectionSet: Readonly<Selection>): PickSelection<Full, Selection>;
|
|
27
|
+
export declare function selectCollapsedFrom<Full extends AnyObject, const Selection extends SelectionSet<NoInfer<Full>>, const Collapse extends boolean | undefined>(originalObject: Readonly<Full>, selectionSet: Readonly<Selection>, collapse?: Collapse): FirstSelectedValue<Full, Selection>;
|