@bessemer/cornerstone 0.5.83 → 0.5.84
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/dist/error.d.ts +2 -1
- package/dist/error.d.ts.map +1 -1
- package/dist/error.js +17 -9
- package/dist/error.js.map +1 -1
- package/dist/map.d.ts +3 -0
- package/dist/map.d.ts.map +1 -1
- package/dist/map.js +8 -0
- package/dist/map.js.map +1 -1
- package/dist/object/type-path.d.ts +2 -0
- package/dist/object/type-path.d.ts.map +1 -1
- package/dist/object/type-path.js +22 -22
- package/dist/object/type-path.js.map +1 -1
- package/package.json +1 -1
package/dist/error.d.ts
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
-
export declare const isError: (value:
|
1
|
+
export declare const isError: (value: unknown) => value is Error;
|
2
|
+
export declare const getCausalChain: (error: Error) => Array<Error>;
|
2
3
|
export declare const findInCausalChain: (error: Error, predicate: (error: Error) => boolean) => Error | undefined;
|
3
4
|
//# sourceMappingURL=error.d.ts.map
|
package/dist/error.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"
|
1
|
+
{"version":3,"file":"error.d.ts","sourceRoot":"","sources":["../src/error.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,OAAO,GAAI,OAAO,OAAO,KAAG,KAAK,IAAI,KAEjD,CAAA;AAED,eAAO,MAAM,cAAc,GAAI,OAAO,KAAK,KAAG,KAAK,CAAC,KAAK,CAexD,CAAA;AAED,eAAO,MAAM,iBAAiB,GAAI,OAAO,KAAK,EAAE,WAAW,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,KAAG,KAAK,GAAG,SAE9F,CAAA"}
|
package/dist/error.js
CHANGED
@@ -1,18 +1,26 @@
|
|
1
1
|
// src/error.ts
|
2
|
-
import { isError as _isError } from "lodash-es";
|
3
2
|
import { isPresent } from "@bessemer/cornerstone/object";
|
4
|
-
var isError =
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
3
|
+
var isError = (value) => {
|
4
|
+
return value instanceof Error;
|
5
|
+
};
|
6
|
+
var getCausalChain = (error) => {
|
7
|
+
const chain = [];
|
8
|
+
let current = error;
|
9
|
+
while (true) {
|
10
|
+
chain.push(current);
|
11
|
+
if (!isPresent(current.cause) || !isError(current.cause)) {
|
12
|
+
break;
|
13
|
+
}
|
14
|
+
current = current.cause;
|
11
15
|
}
|
12
|
-
return
|
16
|
+
return chain;
|
17
|
+
};
|
18
|
+
var findInCausalChain = (error, predicate) => {
|
19
|
+
return getCausalChain(error).find(predicate);
|
13
20
|
};
|
14
21
|
export {
|
15
22
|
findInCausalChain,
|
23
|
+
getCausalChain,
|
16
24
|
isError
|
17
25
|
};
|
18
26
|
//# sourceMappingURL=error.js.map
|
package/dist/error.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/error.ts"],"sourcesContent":["import {
|
1
|
+
{"version":3,"sources":["../src/error.ts"],"sourcesContent":["import { isPresent } from '@bessemer/cornerstone/object'\nimport { Throwable } from '@bessemer/cornerstone/types'\n\nexport const isError = (value: unknown): value is Error => {\n return value instanceof Error\n}\n\nexport const getCausalChain = (error: Error): Array<Error> => {\n const chain: Array<Error> = []\n let current: Error = error\n\n while (true) {\n chain.push(current)\n\n if (!isPresent(current.cause) || !isError(current.cause)) {\n break\n }\n\n current = current.cause\n }\n\n return chain\n}\n\nexport const findInCausalChain = (error: Error, predicate: (error: Error) => boolean): Error | undefined => {\n return getCausalChain(error).find(predicate)\n}\n\nconst serializeError = (error: Throwable): any => {\n if (!isPresent(error)) {\n return undefined\n }\n\n if (!isError(error)) {\n return error\n }\n\n const cause = isPresent(error.cause) ? serializeError(error.cause) : undefined\n\n const serialized = {\n name: error.name,\n message: error.message,\n stack: error.stack,\n cause,\n }\n\n return serialized\n}\n"],"mappings":";AAAA,SAAS,iBAAiB;AAGnB,IAAM,UAAU,CAAC,UAAmC;AACzD,SAAO,iBAAiB;AAC1B;AAEO,IAAM,iBAAiB,CAAC,UAA+B;AAC5D,QAAM,QAAsB,CAAC;AAC7B,MAAI,UAAiB;AAErB,SAAO,MAAM;AACX,UAAM,KAAK,OAAO;AAElB,QAAI,CAAC,UAAU,QAAQ,KAAK,KAAK,CAAC,QAAQ,QAAQ,KAAK,GAAG;AACxD;AAAA,IACF;AAEA,cAAU,QAAQ;AAAA,EACpB;AAEA,SAAO;AACT;AAEO,IAAM,oBAAoB,CAAC,OAAc,cAA4D;AAC1G,SAAO,eAAe,KAAK,EAAE,KAAK,SAAS;AAC7C;","names":[]}
|
package/dist/map.d.ts
CHANGED
@@ -1,4 +1,7 @@
|
|
1
1
|
import { Entry } from '@bessemer/cornerstone/entry';
|
2
|
+
import { Dictionary } from '@bessemer/cornerstone/types';
|
3
|
+
export declare const from: <ValueType>(record: Dictionary<ValueType>) => Map<string, ValueType>;
|
4
|
+
export declare const fromEntries: <KeyType extends string | number | symbol, ValueType>(entries: Array<Entry<KeyType, ValueType>>) => Map<KeyType, ValueType>;
|
2
5
|
export declare const groupBy: <CollectionType, KeyType>(iterable: Iterable<CollectionType>, mapper: (it: CollectionType) => KeyType) => Map<KeyType, Array<CollectionType>>;
|
3
6
|
export declare const fuse: <KeyType, FuseType, ValueType>(target: Map<KeyType, FuseType>, valuesMap: Map<FuseType, ValueType>) => Map<KeyType, ValueType>;
|
4
7
|
export declare const mapKeys: <KeyType, ValueType, NewKeyType>(target: Map<KeyType, ValueType>, mapper: (key: KeyType) => NewKeyType) => Map<NewKeyType, ValueType>;
|
package/dist/map.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,6BAA6B,CAAA;
|
1
|
+
{"version":3,"file":"map.d.ts","sourceRoot":"","sources":["../src/map.ts"],"names":[],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AG,IAMvI,CAAA"}
|
package/dist/map.js
CHANGED
@@ -2,6 +2,12 @@
|
|
2
2
|
import { concatenate as arrayConcatenate } from "@bessemer/cornerstone/array";
|
3
3
|
import { assert, assertPresent } from "@bessemer/cornerstone/assertion";
|
4
4
|
import { Objects } from "@bessemer/cornerstone/index";
|
5
|
+
var from = (record) => {
|
6
|
+
return fromEntries(Object.entries(record));
|
7
|
+
};
|
8
|
+
var fromEntries = (entries) => {
|
9
|
+
return collect(entries, (it) => it);
|
10
|
+
};
|
5
11
|
var groupBy = (iterable, mapper) => {
|
6
12
|
return collect(
|
7
13
|
iterable,
|
@@ -108,6 +114,8 @@ export {
|
|
108
114
|
concatenateInto,
|
109
115
|
filter,
|
110
116
|
filterFrom,
|
117
|
+
from,
|
118
|
+
fromEntries,
|
111
119
|
fuse,
|
112
120
|
groupBy,
|
113
121
|
map,
|
package/dist/map.js.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/map.ts"],"sourcesContent":["import { Entry } from '@bessemer/cornerstone/entry'\nimport { concatenate as arrayConcatenate } from '@bessemer/cornerstone/array'\nimport { assert, assertPresent } from '@bessemer/cornerstone/assertion'\nimport { Objects } from '@bessemer/cornerstone/index'\n\nexport const groupBy = <CollectionType, KeyType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => KeyType\n): Map<KeyType, Array<CollectionType>> => {\n return collect(\n iterable,\n (it) => [mapper(it), [it]],\n (_, first, second) => arrayConcatenate(first, second)\n )\n}\n\nexport const fuse = <KeyType, FuseType, ValueType>(target: Map<KeyType, FuseType>, valuesMap: Map<FuseType, ValueType>): Map<KeyType, ValueType> => {\n return mapValues(target, (value) => {\n const newValue = valuesMap.get(value)\n assertPresent(newValue, () => `Maps.fuse - Encountered missing linkage: ${value}`)\n return newValue\n })\n}\n\nexport const mapKeys = <KeyType, ValueType, NewKeyType>(\n target: Map<KeyType, ValueType>,\n mapper: (key: KeyType) => NewKeyType\n): Map<NewKeyType, ValueType> => {\n return map(target, ([key, value]) => [mapper(key), value])\n}\n\nexport const mapValues = <KeyType, ValueType, NewValueType>(\n target: Map<KeyType, ValueType>,\n mapper: (value: ValueType) => NewValueType\n): Map<KeyType, NewValueType> => {\n return map(target, ([key, value]) => [key, mapper(value)])\n}\n\nexport const map = <KeyType, ValueType, NewKeyType, NewValueType>(\n target: Map<KeyType, ValueType>,\n mapper: (entry: Entry<KeyType, ValueType>) => Entry<NewKeyType, NewValueType>\n): Map<NewKeyType, NewValueType> => {\n return collect(target.entries(), mapper, (key) => {\n throw new Error(`Maps.transform - Encountered illegal duplicate collection value: ${key}`)\n })\n}\n\nexport const collectKeys = <CollectionType, KeyType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => KeyType\n): Map<KeyType, CollectionType> => {\n return collect(\n iterable,\n (it) => [mapper(it), it],\n (key) => {\n throw new Error(`Maps.collectKeys - Encountered illegal duplicate collection value: ${key}`)\n }\n )\n}\n\nexport const collectValues = <CollectionType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => ValueType\n): Map<CollectionType, ValueType> => {\n return collect(\n iterable,\n (it) => [it, mapper(it)],\n (key) => {\n throw new Error(`Maps.mapValues - Encountered illegal duplicate collection value: ${key}`)\n }\n )\n}\n\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>\n): Map<KeyType, ValueType>\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>,\n reducer: (key: KeyType, first: ValueType, second: ValueType) => ValueType\n): Map<KeyType, ValueType>\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>,\n reducer?: (key: KeyType, first: ValueType, second: ValueType) => ValueType\n): Map<KeyType, ValueType> {\n const result = new Map<KeyType, ValueType>()\n\n for (const item of iterable) {\n const [key, value] = mapEntry(item)\n\n if (result.has(key)) {\n if (Objects.isNil(reducer)) {\n throw new Error(`Maps.collect - Encountered illegal duplicate collection key: ${key}`)\n }\n\n const existingValue = result.get(key)!\n const reducedValue = reducer(key, existingValue, value)\n result.set(key, reducedValue)\n } else {\n result.set(key, value)\n }\n }\n\n return result\n}\n\nexport const append = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Entry<KeyType, ValueType>>): Map<KeyType, ValueType> => {\n const result = new Map(map.entries())\n appendInto(result, ...values)\n return result\n}\n\nexport const appendInto = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Entry<KeyType, ValueType>>): void => {\n for (const [key, value] of values) {\n assert(!map.has(key), () => `Maps.append - Encountered illegal duplicate key: ${key}`)\n map.set(key, value)\n }\n}\n\nexport const concatenate = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Map<KeyType, ValueType>>): Map<KeyType, ValueType> => {\n const result = new Map(map)\n concatenateInto(result, ...values)\n return result\n}\n\nexport const concatenateInto = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Map<KeyType, ValueType>>): void => {\n for (const otherMap of values) {\n for (const [key, value] of otherMap) {\n assert(!map.has(key), () => `Maps.concatenate - Encountered illegal duplicate key: ${key}`)\n map.set(key, value)\n }\n }\n}\n\nexport const filter = <KeyType, ValueType>(\n map: Map<KeyType, ValueType>,\n predicate: (entry: Entry<KeyType, ValueType>) => boolean\n): Map<KeyType, ValueType> => {\n const result = new Map(map)\n filterFrom(result, predicate)\n return result\n}\n\nexport const filterFrom = <KeyType, ValueType>(map: Map<KeyType, ValueType>, predicate: (entry: Entry<KeyType, ValueType>) => boolean): void => {\n for (const entry of map) {\n if (!predicate(entry)) {\n map.delete(entry[0])\n }\n }\n}\n"],"mappings":";AACA,SAAS,eAAe,wBAAwB;AAChD,SAAS,QAAQ,qBAAqB;AACtC,SAAS,eAAe;
|
1
|
+
{"version":3,"sources":["../src/map.ts"],"sourcesContent":["import { Entry } from '@bessemer/cornerstone/entry'\nimport { concatenate as arrayConcatenate } from '@bessemer/cornerstone/array'\nimport { assert, assertPresent } from '@bessemer/cornerstone/assertion'\nimport { Objects } from '@bessemer/cornerstone/index'\nimport { Dictionary } from '@bessemer/cornerstone/types'\n\nexport const from = <ValueType>(record: Dictionary<ValueType>): Map<string, ValueType> => {\n return fromEntries(Object.entries(record))\n}\n\nexport const fromEntries = <KeyType extends string | number | symbol, ValueType>(\n entries: Array<Entry<KeyType, ValueType>>\n): Map<KeyType, ValueType> => {\n return collect(entries, (it) => it)\n}\n\nexport const groupBy = <CollectionType, KeyType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => KeyType\n): Map<KeyType, Array<CollectionType>> => {\n return collect(\n iterable,\n (it) => [mapper(it), [it]],\n (_, first, second) => arrayConcatenate(first, second)\n )\n}\n\nexport const fuse = <KeyType, FuseType, ValueType>(target: Map<KeyType, FuseType>, valuesMap: Map<FuseType, ValueType>): Map<KeyType, ValueType> => {\n return mapValues(target, (value) => {\n const newValue = valuesMap.get(value)\n assertPresent(newValue, () => `Maps.fuse - Encountered missing linkage: ${value}`)\n return newValue\n })\n}\n\nexport const mapKeys = <KeyType, ValueType, NewKeyType>(\n target: Map<KeyType, ValueType>,\n mapper: (key: KeyType) => NewKeyType\n): Map<NewKeyType, ValueType> => {\n return map(target, ([key, value]) => [mapper(key), value])\n}\n\nexport const mapValues = <KeyType, ValueType, NewValueType>(\n target: Map<KeyType, ValueType>,\n mapper: (value: ValueType) => NewValueType\n): Map<KeyType, NewValueType> => {\n return map(target, ([key, value]) => [key, mapper(value)])\n}\n\nexport const map = <KeyType, ValueType, NewKeyType, NewValueType>(\n target: Map<KeyType, ValueType>,\n mapper: (entry: Entry<KeyType, ValueType>) => Entry<NewKeyType, NewValueType>\n): Map<NewKeyType, NewValueType> => {\n return collect(target.entries(), mapper, (key) => {\n throw new Error(`Maps.transform - Encountered illegal duplicate collection value: ${key}`)\n })\n}\n\nexport const collectKeys = <CollectionType, KeyType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => KeyType\n): Map<KeyType, CollectionType> => {\n return collect(\n iterable,\n (it) => [mapper(it), it],\n (key) => {\n throw new Error(`Maps.collectKeys - Encountered illegal duplicate collection value: ${key}`)\n }\n )\n}\n\nexport const collectValues = <CollectionType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapper: (it: CollectionType) => ValueType\n): Map<CollectionType, ValueType> => {\n return collect(\n iterable,\n (it) => [it, mapper(it)],\n (key) => {\n throw new Error(`Maps.mapValues - Encountered illegal duplicate collection value: ${key}`)\n }\n )\n}\n\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>\n): Map<KeyType, ValueType>\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>,\n reducer: (key: KeyType, first: ValueType, second: ValueType) => ValueType\n): Map<KeyType, ValueType>\nexport function collect<CollectionType, KeyType, ValueType>(\n iterable: Iterable<CollectionType>,\n mapEntry: (it: CollectionType) => Entry<KeyType, ValueType>,\n reducer?: (key: KeyType, first: ValueType, second: ValueType) => ValueType\n): Map<KeyType, ValueType> {\n const result = new Map<KeyType, ValueType>()\n\n for (const item of iterable) {\n const [key, value] = mapEntry(item)\n\n if (result.has(key)) {\n if (Objects.isNil(reducer)) {\n throw new Error(`Maps.collect - Encountered illegal duplicate collection key: ${key}`)\n }\n\n const existingValue = result.get(key)!\n const reducedValue = reducer(key, existingValue, value)\n result.set(key, reducedValue)\n } else {\n result.set(key, value)\n }\n }\n\n return result\n}\n\nexport const append = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Entry<KeyType, ValueType>>): Map<KeyType, ValueType> => {\n const result = new Map(map.entries())\n appendInto(result, ...values)\n return result\n}\n\nexport const appendInto = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Entry<KeyType, ValueType>>): void => {\n for (const [key, value] of values) {\n assert(!map.has(key), () => `Maps.append - Encountered illegal duplicate key: ${key}`)\n map.set(key, value)\n }\n}\n\nexport const concatenate = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Map<KeyType, ValueType>>): Map<KeyType, ValueType> => {\n const result = new Map(map)\n concatenateInto(result, ...values)\n return result\n}\n\nexport const concatenateInto = <KeyType, ValueType>(map: Map<KeyType, ValueType>, ...values: Array<Map<KeyType, ValueType>>): void => {\n for (const otherMap of values) {\n for (const [key, value] of otherMap) {\n assert(!map.has(key), () => `Maps.concatenate - Encountered illegal duplicate key: ${key}`)\n map.set(key, value)\n }\n }\n}\n\nexport const filter = <KeyType, ValueType>(\n map: Map<KeyType, ValueType>,\n predicate: (entry: Entry<KeyType, ValueType>) => boolean\n): Map<KeyType, ValueType> => {\n const result = new Map(map)\n filterFrom(result, predicate)\n return result\n}\n\nexport const filterFrom = <KeyType, ValueType>(map: Map<KeyType, ValueType>, predicate: (entry: Entry<KeyType, ValueType>) => boolean): void => {\n for (const entry of map) {\n if (!predicate(entry)) {\n map.delete(entry[0])\n }\n }\n}\n"],"mappings":";AACA,SAAS,eAAe,wBAAwB;AAChD,SAAS,QAAQ,qBAAqB;AACtC,SAAS,eAAe;AAGjB,IAAM,OAAO,CAAY,WAA0D;AACxF,SAAO,YAAY,OAAO,QAAQ,MAAM,CAAC;AAC3C;AAEO,IAAM,cAAc,CACzB,YAC4B;AAC5B,SAAO,QAAQ,SAAS,CAAC,OAAO,EAAE;AACpC;AAEO,IAAM,UAAU,CACrB,UACA,WACwC;AACxC,SAAO;AAAA,IACL;AAAA,IACA,CAAC,OAAO,CAAC,OAAO,EAAE,GAAG,CAAC,EAAE,CAAC;AAAA,IACzB,CAAC,GAAG,OAAO,WAAW,iBAAiB,OAAO,MAAM;AAAA,EACtD;AACF;AAEO,IAAM,OAAO,CAA+B,QAAgC,cAAiE;A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,CAAC;AAAA,IACrB;AAAA,EACF;AACF;","names":["map"]}
|
@@ -1,6 +1,7 @@
|
|
1
1
|
import { NominalType } from '@bessemer/cornerstone/types';
|
2
2
|
import Zod from 'zod';
|
3
3
|
import { InferTypePath, ParseTypePath, TypePathConcreteType, TypePathGet, TypePathSelector, TypePathType, WildcardSelector } from '@bessemer/cornerstone/object/type-path-type';
|
4
|
+
import { Result } from '@bessemer/cornerstone/result';
|
4
5
|
export type TypePath<T extends TypePathType = TypePathType> = NominalType<TypePathConcreteType, ['TypePath', T]>;
|
5
6
|
export declare const of: <T extends TypePathConcreteType>(value: T) => TypePath<InferTypePath<T>>;
|
6
7
|
export declare const fromString: <T extends string>(path: T) => TypePath<ParseTypePath<T>>;
|
@@ -8,5 +9,6 @@ export declare const Schema: Zod.ZodPipe<Zod.ZodUnion<readonly [Zod.ZodArray<Zod
|
|
8
9
|
export declare const getValue: <T extends TypePathType, N>(path: TypePath<T>, object: N) => TypePathGet<T, N>;
|
9
10
|
export declare const isWildcardSelector: (selector: TypePathSelector) => selector is WildcardSelector;
|
10
11
|
export declare const matches: <MatchingPath extends TypePathType>(targetPath: TypePath, matchingPath: TypePath<MatchingPath>) => targetPath is TypePath<MatchingPath>;
|
12
|
+
export declare const intersectAny: (targetPath: TypePath, intersectingPath: TypePath) => Result<TypePath>;
|
11
13
|
export declare const intersect: <TargetPath extends TypePathType, IntersectingPath extends TypePathType>(targetPath: TypePath<TargetPath>, intersectingPath: TypePath<IntersectingPath>) => TypePath;
|
12
14
|
//# sourceMappingURL=type-path.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"type-path.d.ts","sourceRoot":"","sources":["../../src/object/type-path.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,6BAA6B,CAAA;AAEzD,OAAO,GAAG,MAAM,KAAK,CAAA;AACrB,OAAO,EAEL,aAAa,EAEb,aAAa,EACb,oBAAoB,EACpB,WAAW,EACX,gBAAgB,EACh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AO,MAAM,YAAY,GAAI,YAAY,QAAQ,EAAE,kBAAkB,QAAQ,KAAG,MAAM,CAAC,QAAQ,CA0D9F,CAAA;AAGD,eAAO,MAAM,SAAS,GAAI,UAAU,SAAS,YAAY,EAAE,gBAAgB,SAAS,YAAY,EAC9F,YAAY,QAAQ,CAAC,UAAU,CAAC,EAChC,kBAAkB,QAAQ,CAAC,gBAAgB,CAAC,KAC3C,QAOF,CAAA"}
|
package/dist/object/type-path.js
CHANGED
@@ -4,6 +4,7 @@ import Zod from "zod";
|
|
4
4
|
import { isNil, isObject } from "@bessemer/cornerstone/object";
|
5
5
|
import { isNumber } from "@bessemer/cornerstone/math";
|
6
6
|
import { contains, containsAll, isEmpty, only } from "@bessemer/cornerstone/array";
|
7
|
+
import { failure, success } from "@bessemer/cornerstone/result";
|
7
8
|
var of = (value) => {
|
8
9
|
return value;
|
9
10
|
};
|
@@ -179,69 +180,68 @@ var matches = (targetPath, matchingPath) => {
|
|
179
180
|
}
|
180
181
|
return true;
|
181
182
|
};
|
182
|
-
var
|
183
|
-
|
183
|
+
var intersectAny = (targetPath, intersectingPath) => {
|
184
|
+
if (targetPath.length < intersectingPath.length) {
|
185
|
+
return failure(new Error(`TypePath: ${intersectingPath} can't intersect target TypePath: ${targetPath}`));
|
186
|
+
}
|
184
187
|
let index = 0;
|
185
188
|
let result = [];
|
186
189
|
for (const targetPathSelector of targetPath) {
|
187
190
|
const intersectingPathSelector = intersectingPath[index];
|
191
|
+
const makeError = () => new Error(`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`);
|
188
192
|
if (isNil(intersectingPathSelector)) {
|
189
|
-
return of(result);
|
193
|
+
return success(of(result));
|
190
194
|
} else if (isWildcardSelector(intersectingPathSelector)) {
|
191
195
|
result.push(targetPathSelector);
|
192
196
|
} else if (isWildcardSelector(targetPathSelector)) {
|
193
|
-
|
194
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
195
|
-
);
|
197
|
+
return failure(makeError());
|
196
198
|
} else if (Array.isArray(intersectingPathSelector)) {
|
197
199
|
if (Array.isArray(targetPathSelector)) {
|
198
200
|
const filteredTargetPaths = targetPathSelector.filter((it) => contains(intersectingPathSelector, it));
|
199
201
|
if (isEmpty(filteredTargetPaths)) {
|
200
|
-
|
201
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
202
|
-
);
|
202
|
+
return failure(makeError());
|
203
203
|
}
|
204
204
|
result.push(filteredTargetPaths);
|
205
205
|
} else {
|
206
206
|
if (!contains(intersectingPathSelector, Number(targetPathSelector))) {
|
207
|
-
|
208
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
209
|
-
);
|
207
|
+
return failure(makeError());
|
210
208
|
}
|
211
209
|
result.push(targetPathSelector);
|
212
210
|
}
|
213
211
|
} else {
|
214
212
|
if (Array.isArray(targetPathSelector)) {
|
215
213
|
if (targetPathSelector.length !== 1) {
|
216
|
-
|
217
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
218
|
-
);
|
214
|
+
return failure(makeError());
|
219
215
|
}
|
220
216
|
const targetPathSelectorIndex = only(targetPathSelector);
|
221
217
|
if (targetPathSelectorIndex !== Number(intersectingPathSelector)) {
|
222
|
-
|
223
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
224
|
-
);
|
218
|
+
return failure(makeError());
|
225
219
|
}
|
226
220
|
result.push(targetPathSelector);
|
227
221
|
} else {
|
228
222
|
if (targetPathSelector !== intersectingPathSelector) {
|
229
|
-
|
230
|
-
`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`
|
231
|
-
);
|
223
|
+
return failure(makeError());
|
232
224
|
}
|
233
225
|
result.push(targetPathSelector);
|
234
226
|
}
|
235
227
|
}
|
236
228
|
index++;
|
237
229
|
}
|
238
|
-
return of(result);
|
230
|
+
return success(of(result));
|
231
|
+
};
|
232
|
+
var intersect = (targetPath, intersectingPath) => {
|
233
|
+
const result = intersectAny(targetPath, intersectingPath);
|
234
|
+
if (!result.isSuccess) {
|
235
|
+
throw result.value;
|
236
|
+
}
|
237
|
+
return result.value;
|
239
238
|
};
|
240
239
|
export {
|
241
240
|
Schema,
|
242
241
|
fromString,
|
243
242
|
getValue,
|
244
243
|
intersect,
|
244
|
+
intersectAny,
|
245
245
|
isWildcardSelector,
|
246
246
|
matches,
|
247
247
|
of
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/object/type-path.ts"],"sourcesContent":["import { NominalType } from '@bessemer/cornerstone/types'\nimport { assert } from '@bessemer/cornerstone/assertion'\nimport Zod from 'zod'\nimport {\n IndexSelector,\n InferTypePath,\n NameSelector,\n ParseTypePath,\n TypePathConcreteType,\n TypePathGet,\n TypePathSelector,\n TypePathType,\n WildcardSelector,\n} from '@bessemer/cornerstone/object/type-path-type'\nimport { isNil, isObject } from '@bessemer/cornerstone/object'\nimport { isNumber } from '@bessemer/cornerstone/math'\nimport { contains, containsAll, isEmpty, only } from '@bessemer/cornerstone/array'\n\nexport type TypePath<T extends TypePathType = TypePathType> = NominalType<TypePathConcreteType, ['TypePath', T]>\n\nexport const of = <T extends TypePathConcreteType>(value: T): TypePath<InferTypePath<T>> => {\n return value as TypePath<InferTypePath<T>>\n}\n\nconst TypePathRegex =\n /^(?:[a-zA-Z_$][a-zA-Z0-9_$]*|\\*|\\d+|\\[\\s*(?:\\*|\\d+(?:\\s*,\\s*\\d+)*)\\s*])(?:\\.(?:[a-zA-Z_$][a-zA-Z0-9_$]*|\\*|\\d+)|\\[\\s*(?:\\*|\\d+(?:\\s*,\\s*\\d+)*)\\s*])*$|^$/\n\nexport const fromString = <T extends string>(path: T): TypePath<ParseTypePath<T>> => {\n assert(TypePathRegex.test(path), () => `Unable to parse TypePath from string: ${path}`)\n\n if (path === '') {\n return [] as TypePath<ParseTypePath<T>>\n }\n\n const segments: Array<string | Array<string | number>> = []\n let current = ''\n let i = 0\n\n while (i < path.length) {\n const char = path[i]\n\n if (char === '.') {\n if (current) {\n segments.push(current)\n current = ''\n }\n i++\n } else if (char === '[') {\n if (current) {\n segments.push(current)\n current = ''\n }\n\n // Find the matching closing bracket\n let bracketContent = ''\n i++ // Skip opening bracket\n let depth = 1\n\n while (i < path.length && depth > 0) {\n const bracketChar = path[i]\n if (bracketChar === '[') {\n depth++\n } else if (bracketChar === ']') {\n depth--\n }\n\n if (depth > 0) {\n bracketContent += bracketChar\n }\n i++\n }\n\n if (bracketContent === '*') {\n segments.push('*')\n } else {\n const indices = bracketContent\n .split(',')\n .map((s) => s.trim())\n .map((s) => parseInt(s, 10))\n\n segments.push(indices)\n }\n } else {\n current += char\n i++\n }\n }\n\n // Add any remaining current segment\n if (current) {\n segments.push(current)\n }\n\n return segments as TypePath<ParseTypePath<T>>\n}\n\nexport const Schema = Zod.union([Zod.array(Zod.string()), Zod.string()]).transform((it) => {\n if (Array.isArray(it)) {\n return of(it)\n } else {\n return fromString(it)\n }\n})\n\nexport const getValue = <T extends TypePathType, N>(path: TypePath<T>, object: N): TypePathGet<T, N> => {\n let current: unknown = object\n let collectorMode = false\n\n for (const selector of path) {\n if (isNil(current)) {\n return undefined as TypePathGet<T, N>\n }\n\n if (collectorMode) {\n if (!Array.isArray(current)) {\n throw new Error('Illegal State - in collectorMode but current value is not an Array')\n }\n\n const result = current.flatMap((it) => {\n const [value, isCollecting] = evaluateSelector(selector, it)\n if (isCollecting) {\n return value\n } else {\n return [value]\n }\n })\n\n current = result\n } else {\n const [result, isCollecting] = evaluateSelector(selector, current)\n current = result\n collectorMode = isCollecting\n }\n }\n\n return current as TypePathGet<T, N>\n}\n\nconst evaluateSelector = (selector: TypePathSelector, current: unknown): [unknown, boolean] => {\n if (isWildcardSelector(selector)) {\n return evaluateWildcardSelector(current)\n }\n\n if (Array.isArray(selector)) {\n return evaluateIndexSelector(selector as IndexSelector, current)\n }\n\n return evaluateNameSelector(selector, current)\n}\n\nconst evaluateNameSelector = (selector: NameSelector, current: unknown): [unknown, boolean] => {\n if (Array.isArray(current)) {\n const numberSelector = Number(selector)\n assert(isNumber(numberSelector), () => `Can't apply non-numeric selector: ${selector} to array: ${current}`)\n return evaluateIndexSelector([numberSelector], current)\n }\n\n if (!isObject(current)) {\n return [undefined, false]\n }\n\n return [current[selector], false]\n}\n\nconst evaluateWildcardSelector = (current: unknown): [unknown, boolean] => {\n if (Array.isArray(current)) {\n return [current, true]\n }\n\n if (isObject(current)) {\n throw new Error('Wildcard operations on Objects is not yet supported')\n }\n\n throw new Error(`Unable to apply wildcard operator to value: ${current}`)\n}\n\nconst evaluateIndexSelector = (selector: IndexSelector, current: unknown): [unknown, boolean] => {\n if (!Array.isArray(current)) {\n throw new Error(`Unable to apply index operator to value: ${current}`)\n }\n\n if (selector.length === 1) {\n return [current[only(selector)], false]\n }\n\n const indexSelector = selector as IndexSelector\n const result = indexSelector.map((it) => current[it])\n return [result, true]\n}\n\nexport const isWildcardSelector = (selector: TypePathSelector): selector is WildcardSelector => {\n return selector === '*'\n}\n\nexport const matches = <MatchingPath extends TypePathType>(\n targetPath: TypePath,\n matchingPath: TypePath<MatchingPath>\n): targetPath is TypePath<MatchingPath> => {\n if (targetPath.length < matchingPath.length) {\n return false\n }\n\n let index = 0\n for (const targetPathSelector of targetPath) {\n const matchingPathSelector = matchingPath[index]\n\n if (isNil(matchingPathSelector)) {\n return true\n } else if (isWildcardSelector(matchingPathSelector)) {\n // Matching path wildcards always match - they're wild\n } else if (isWildcardSelector(targetPathSelector)) {\n return false\n } else if (Array.isArray(matchingPathSelector)) {\n if (Array.isArray(targetPathSelector)) {\n if (!containsAll(matchingPathSelector, targetPathSelector)) {\n return false\n }\n } else {\n if (!contains(matchingPathSelector, Number(targetPathSelector))) {\n return false\n }\n }\n } else {\n if (Array.isArray(targetPathSelector)) {\n if (targetPathSelector.length !== 1) {\n return false\n }\n\n const targetPathSelectorIndex = only(targetPathSelector)\n if (targetPathSelectorIndex !== Number(matchingPathSelector)) {\n return false\n }\n } else {\n if (targetPathSelector !== matchingPathSelector) {\n return false\n }\n }\n }\n\n index++\n }\n\n return true\n}\n\n// JOHN this needs to do a type resolution step...\nexport const intersect = <TargetPath extends TypePathType, IntersectingPath extends TypePathType>(\n targetPath: TypePath<TargetPath>,\n intersectingPath: TypePath<IntersectingPath>\n): TypePath => {\n assert(targetPath.length >= intersectingPath.length, () => `TypePath: ${intersectingPath} can't intersect target TypePath: ${targetPath}`)\n\n let index = 0\n let result: TypePathConcreteType = []\n for (const targetPathSelector of targetPath) {\n const intersectingPathSelector = intersectingPath[index]\n\n if (isNil(intersectingPathSelector)) {\n return of(result)\n } else if (isWildcardSelector(intersectingPathSelector)) {\n result.push(targetPathSelector)\n } else if (isWildcardSelector(targetPathSelector)) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n } else if (Array.isArray(intersectingPathSelector)) {\n if (Array.isArray(targetPathSelector)) {\n const filteredTargetPaths = targetPathSelector.filter((it) => contains(intersectingPathSelector, it))\n if (isEmpty(filteredTargetPaths)) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n }\n\n result.push(filteredTargetPaths)\n } else {\n if (!contains(intersectingPathSelector, Number(targetPathSelector))) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n }\n\n result.push(targetPathSelector)\n }\n } else {\n if (Array.isArray(targetPathSelector)) {\n if (targetPathSelector.length !== 1) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n }\n\n const targetPathSelectorIndex = only(targetPathSelector)\n if (targetPathSelectorIndex !== Number(intersectingPathSelector)) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n }\n\n result.push(targetPathSelector)\n } else {\n if (targetPathSelector !== intersectingPathSelector) {\n throw new Error(\n `Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`\n )\n }\n\n result.push(targetPathSelector)\n }\n }\n\n index++\n }\n\n return of(result)\n}\n"],"mappings":";AACA,SAAS,cAAc;AACvB,OAAO,SAAS;AAYhB,SAAS,OAAO,gBAAgB;AAChC,SAAS,gBAAgB;AACzB,SAAS,UAAU,aAAa,SAAS,YAAY;AAI9C,IAAM,KAAK,CAAiC,UAAyC;AAC1F,SAAO;AACT;AAEA,IAAM,gBACJ;AAEK,IAAM,aAAa,CAAmB,SAAwC;AACnF,SAAO,cAAc,KAAK,IAAI,GAAG,MAAM,yCAAyC,IAAI,EAAE;AAEtF,MAAI,SAAS,IAAI;AACf,WAAO,CAAC;AAAA,EACV;AAEA,QAAM,WAAmD,CAAC;AAC1D,MAAI,UAAU;AACd,MAAI,IAAI;AAER,SAAO,IAAI,KAAK,QAAQ;AACtB,UAAM,OAAO,KAAK,CAAC;AAEnB,QAAI,SAAS,KAAK;AAChB,UAAI,SAAS;AACX,iBAAS,KAAK,OAAO;AACrB,kBAAU;AAAA,MACZ;AACA;AAAA,IACF,WAAW,SAAS,KAAK;AACvB,UAAI,SAAS;AACX,iBAAS,KAAK,OAAO;AACrB,kBAAU;AAAA,MACZ;AAGA,UAAI,iBAAiB;AACrB;AACA,UAAI,QAAQ;AAEZ,aAAO,IAAI,KAAK,UAAU,QAAQ,GAAG;AACnC,cAAM,cAAc,KAAK,CAAC;AAC1B,YAAI,gBAAgB,KAAK;AACvB;AAAA,QACF,WAAW,gBAAgB,KAAK;AAC9B;AAAA,QACF;AAEA,YAAI,QAAQ,GAAG;AACb,4BAAkB;AAAA,QACpB;AACA;AAAA,MACF;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F;AAEA;AAAA,EACF;AAEA,SAAO,GAAG,MAAM;AAClB;","names":[]}
|
1
|
+
{"version":3,"sources":["../../src/object/type-path.ts"],"sourcesContent":["import { NominalType } from '@bessemer/cornerstone/types'\nimport { assert } from '@bessemer/cornerstone/assertion'\nimport Zod from 'zod'\nimport {\n IndexSelector,\n InferTypePath,\n NameSelector,\n ParseTypePath,\n TypePathConcreteType,\n TypePathGet,\n TypePathSelector,\n TypePathType,\n WildcardSelector,\n} from '@bessemer/cornerstone/object/type-path-type'\nimport { isNil, isObject } from '@bessemer/cornerstone/object'\nimport { isNumber } from '@bessemer/cornerstone/math'\nimport { contains, containsAll, isEmpty, only } from '@bessemer/cornerstone/array'\nimport { failure, Result, success } from '@bessemer/cornerstone/result'\n\nexport type TypePath<T extends TypePathType = TypePathType> = NominalType<TypePathConcreteType, ['TypePath', T]>\n\nexport const of = <T extends TypePathConcreteType>(value: T): TypePath<InferTypePath<T>> => {\n return value as TypePath<InferTypePath<T>>\n}\n\nconst TypePathRegex =\n /^(?:[a-zA-Z_$][a-zA-Z0-9_$]*|\\*|\\d+|\\[\\s*(?:\\*|\\d+(?:\\s*,\\s*\\d+)*)\\s*])(?:\\.(?:[a-zA-Z_$][a-zA-Z0-9_$]*|\\*|\\d+)|\\[\\s*(?:\\*|\\d+(?:\\s*,\\s*\\d+)*)\\s*])*$|^$/\n\nexport const fromString = <T extends string>(path: T): TypePath<ParseTypePath<T>> => {\n assert(TypePathRegex.test(path), () => `Unable to parse TypePath from string: ${path}`)\n\n if (path === '') {\n return [] as TypePath<ParseTypePath<T>>\n }\n\n const segments: Array<string | Array<string | number>> = []\n let current = ''\n let i = 0\n\n while (i < path.length) {\n const char = path[i]\n\n if (char === '.') {\n if (current) {\n segments.push(current)\n current = ''\n }\n i++\n } else if (char === '[') {\n if (current) {\n segments.push(current)\n current = ''\n }\n\n // Find the matching closing bracket\n let bracketContent = ''\n i++ // Skip opening bracket\n let depth = 1\n\n while (i < path.length && depth > 0) {\n const bracketChar = path[i]\n if (bracketChar === '[') {\n depth++\n } else if (bracketChar === ']') {\n depth--\n }\n\n if (depth > 0) {\n bracketContent += bracketChar\n }\n i++\n }\n\n if (bracketContent === '*') {\n segments.push('*')\n } else {\n const indices = bracketContent\n .split(',')\n .map((s) => s.trim())\n .map((s) => parseInt(s, 10))\n\n segments.push(indices)\n }\n } else {\n current += char\n i++\n }\n }\n\n // Add any remaining current segment\n if (current) {\n segments.push(current)\n }\n\n return segments as TypePath<ParseTypePath<T>>\n}\n\nexport const Schema = Zod.union([Zod.array(Zod.string()), Zod.string()]).transform((it) => {\n if (Array.isArray(it)) {\n return of(it)\n } else {\n return fromString(it)\n }\n})\n\nexport const getValue = <T extends TypePathType, N>(path: TypePath<T>, object: N): TypePathGet<T, N> => {\n let current: unknown = object\n let collectorMode = false\n\n for (const selector of path) {\n if (isNil(current)) {\n return undefined as TypePathGet<T, N>\n }\n\n if (collectorMode) {\n if (!Array.isArray(current)) {\n throw new Error('Illegal State - in collectorMode but current value is not an Array')\n }\n\n const result = current.flatMap((it) => {\n const [value, isCollecting] = evaluateSelector(selector, it)\n if (isCollecting) {\n return value\n } else {\n return [value]\n }\n })\n\n current = result\n } else {\n const [result, isCollecting] = evaluateSelector(selector, current)\n current = result\n collectorMode = isCollecting\n }\n }\n\n return current as TypePathGet<T, N>\n}\n\nconst evaluateSelector = (selector: TypePathSelector, current: unknown): [unknown, boolean] => {\n if (isWildcardSelector(selector)) {\n return evaluateWildcardSelector(current)\n }\n\n if (Array.isArray(selector)) {\n return evaluateIndexSelector(selector as IndexSelector, current)\n }\n\n return evaluateNameSelector(selector, current)\n}\n\nconst evaluateNameSelector = (selector: NameSelector, current: unknown): [unknown, boolean] => {\n if (Array.isArray(current)) {\n const numberSelector = Number(selector)\n assert(isNumber(numberSelector), () => `Can't apply non-numeric selector: ${selector} to array: ${current}`)\n return evaluateIndexSelector([numberSelector], current)\n }\n\n if (!isObject(current)) {\n return [undefined, false]\n }\n\n return [current[selector], false]\n}\n\nconst evaluateWildcardSelector = (current: unknown): [unknown, boolean] => {\n if (Array.isArray(current)) {\n return [current, true]\n }\n\n if (isObject(current)) {\n throw new Error('Wildcard operations on Objects is not yet supported')\n }\n\n throw new Error(`Unable to apply wildcard operator to value: ${current}`)\n}\n\nconst evaluateIndexSelector = (selector: IndexSelector, current: unknown): [unknown, boolean] => {\n if (!Array.isArray(current)) {\n throw new Error(`Unable to apply index operator to value: ${current}`)\n }\n\n if (selector.length === 1) {\n return [current[only(selector)], false]\n }\n\n const indexSelector = selector as IndexSelector\n const result = indexSelector.map((it) => current[it])\n return [result, true]\n}\n\nexport const isWildcardSelector = (selector: TypePathSelector): selector is WildcardSelector => {\n return selector === '*'\n}\n\nexport const matches = <MatchingPath extends TypePathType>(\n targetPath: TypePath,\n matchingPath: TypePath<MatchingPath>\n): targetPath is TypePath<MatchingPath> => {\n if (targetPath.length < matchingPath.length) {\n return false\n }\n\n let index = 0\n for (const targetPathSelector of targetPath) {\n const matchingPathSelector = matchingPath[index]\n\n if (isNil(matchingPathSelector)) {\n return true\n } else if (isWildcardSelector(matchingPathSelector)) {\n // Matching path wildcards always match - they're wild\n } else if (isWildcardSelector(targetPathSelector)) {\n return false\n } else if (Array.isArray(matchingPathSelector)) {\n if (Array.isArray(targetPathSelector)) {\n if (!containsAll(matchingPathSelector, targetPathSelector)) {\n return false\n }\n } else {\n if (!contains(matchingPathSelector, Number(targetPathSelector))) {\n return false\n }\n }\n } else {\n if (Array.isArray(targetPathSelector)) {\n if (targetPathSelector.length !== 1) {\n return false\n }\n\n const targetPathSelectorIndex = only(targetPathSelector)\n if (targetPathSelectorIndex !== Number(matchingPathSelector)) {\n return false\n }\n } else {\n if (targetPathSelector !== matchingPathSelector) {\n return false\n }\n }\n }\n\n index++\n }\n\n return true\n}\n\nexport const intersectAny = (targetPath: TypePath, intersectingPath: TypePath): Result<TypePath> => {\n if (targetPath.length < intersectingPath.length) {\n return failure(new Error(`TypePath: ${intersectingPath} can't intersect target TypePath: ${targetPath}`))\n }\n\n let index = 0\n let result: TypePathConcreteType = []\n for (const targetPathSelector of targetPath) {\n const intersectingPathSelector = intersectingPath[index]\n const makeError = () =>\n new Error(`Path mismatch when intersecting. targetPath: ${targetPathSelector} does not match intersectingPath: ${intersectingPathSelector}`)\n\n if (isNil(intersectingPathSelector)) {\n return success(of(result))\n } else if (isWildcardSelector(intersectingPathSelector)) {\n result.push(targetPathSelector)\n } else if (isWildcardSelector(targetPathSelector)) {\n return failure(makeError())\n } else if (Array.isArray(intersectingPathSelector)) {\n if (Array.isArray(targetPathSelector)) {\n const filteredTargetPaths = targetPathSelector.filter((it) => contains(intersectingPathSelector, it))\n if (isEmpty(filteredTargetPaths)) {\n return failure(makeError())\n }\n\n result.push(filteredTargetPaths)\n } else {\n if (!contains(intersectingPathSelector, Number(targetPathSelector))) {\n return failure(makeError())\n }\n\n result.push(targetPathSelector)\n }\n } else {\n if (Array.isArray(targetPathSelector)) {\n if (targetPathSelector.length !== 1) {\n return failure(makeError())\n }\n\n const targetPathSelectorIndex = only(targetPathSelector)\n if (targetPathSelectorIndex !== Number(intersectingPathSelector)) {\n return failure(makeError())\n }\n\n result.push(targetPathSelector)\n } else {\n if (targetPathSelector !== intersectingPathSelector) {\n return failure(makeError())\n }\n\n result.push(targetPathSelector)\n }\n }\n\n index++\n }\n\n return success(of(result))\n}\n\n// JOHN this needs to do a type resolution step...\nexport const intersect = <TargetPath extends TypePathType, IntersectingPath extends TypePathType>(\n targetPath: TypePath<TargetPath>,\n intersectingPath: TypePath<IntersectingPath>\n): TypePath => {\n const result = intersectAny(targetPath, intersectingPath)\n if (!result.isSuccess) {\n throw result.value\n }\n\n return result.value\n}\n"],"mappings":";AACA,SAAS,cAAc;AACvB,OAAO,SAAS;AAYhB,SAAS,OAAO,gBAAgB;AAChC,SAAS,gBAAgB;AACzB,SAAS,UAAU,aAAa,SAAS,YAAY;AACrD,SAAS,SAAiB,eAAe;AAIlC,IAAM,KAAK,CAAiC,UAAyC;AAC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