Skip to content

Commit 494f74e

Browse files
committed
feat(hf): add finder
1 parent 3a45d82 commit 494f74e

33 files changed

Lines changed: 758 additions & 0 deletions

integration/finder/index.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { dataParserFinder, defaultResearchers } from "@duplojs/data-parser-tools/finder";
2+
import { DPE } from "@duplojs/utils";
3+
4+
describe("finder integration", () => {
5+
it("exposes the finder API from the package root export", () => {
6+
const target = DPE.number();
7+
const schema = DPE.object({
8+
profile: DPE.object({
9+
ids: DPE.array(target),
10+
}),
11+
});
12+
13+
const result = dataParserFinder(
14+
schema,
15+
(dataParser) => dataParser === target,
16+
{
17+
researchers: [...defaultResearchers],
18+
ignore: new Set(),
19+
},
20+
);
21+
22+
expect(result).toEqual([target]);
23+
});
24+
});

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
"import": "./dist/toDataParser/index.mjs",
5151
"require": "./dist/toDataParser/index.cjs",
5252
"types": "./dist/toDataParser/index.d.ts"
53+
},
54+
"./finder": {
55+
"import": "./dist/finder/index.mjs",
56+
"require": "./dist/finder/index.cjs",
57+
"types": "./dist/finder/index.d.ts"
5358
}
5459
},
5560
"files": [

scripts/finder/index.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
import { A, type DP } from "@duplojs/utils";
2+
import { type ResearcherParams, type createResearcher } from "./researcher";
3+
4+
export * from "./researcher";
5+
6+
export interface DataParserFinderParams {
7+
researchers: ReturnType<typeof createResearcher>[];
8+
ignore?: Set<DP.DataParser>;
9+
continueAfterMatch?: boolean;
10+
}
11+
12+
export function dataParserFinder<
13+
GenericPredicate extends DP.DataParsers,
14+
>(
15+
dataParser: DP.DataParser,
16+
predicate: (dataParser: DP.DataParsers) => dataParser is GenericPredicate,
17+
params: DataParserFinderParams,
18+
): GenericPredicate[];
19+
20+
export function dataParserFinder(
21+
dataParser: DP.DataParser,
22+
predicate: (dataParser: DP.DataParsers) => boolean,
23+
params: DataParserFinderParams,
24+
): DP.DataParsers[];
25+
26+
export function dataParserFinder(
27+
dataParser: DP.DataParser,
28+
predicate: (dataParser: DP.DataParsers) => boolean,
29+
params: DataParserFinderParams,
30+
) {
31+
const result: DP.DataParsers[] = [];
32+
const ignoreContext = params.ignore ?? new Set();
33+
34+
if (ignoreContext.has(dataParser)) {
35+
return result;
36+
}
37+
38+
ignoreContext.add(dataParser);
39+
40+
if (predicate(dataParser)) {
41+
result.push(dataParser);
42+
43+
if (params.continueAfterMatch !== true) {
44+
return result;
45+
}
46+
}
47+
48+
const researcherParams: ResearcherParams = {
49+
find(dataParser) {
50+
result.push(
51+
...dataParserFinder(
52+
dataParser,
53+
predicate,
54+
{
55+
...params,
56+
ignore: ignoreContext,
57+
},
58+
),
59+
);
60+
},
61+
};
62+
63+
A.map(
64+
params.researchers,
65+
(researcher) => void researcher(dataParser, researcherParams),
66+
);
67+
68+
return result;
69+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { type DP } from "@duplojs/utils";
2+
3+
export interface ResearcherParams {
4+
find(dataParser: DP.DataParser): void;
5+
}
6+
7+
export function createResearcher<
8+
GenericPredicate extends DP.DataParsers,
9+
>(
10+
predicate: (dataParser: DP.DataParsers) => dataParser is GenericPredicate,
11+
researcher: (dataParser: GenericPredicate, params: ResearcherParams) => void,
12+
): (dataParser: DP.DataParser, params: ResearcherParams) => void;
13+
14+
export function createResearcher(
15+
predicate: (dataParser: DP.DataParsers) => boolean,
16+
researcher: (dataParser: DP.DataParser, params: ResearcherParams) => void,
17+
): (dataParser: DP.DataParser, params: ResearcherParams) => void;
18+
19+
export function createResearcher(
20+
predicate: (dataParser: DP.DataParsers) => boolean,
21+
researcher: (dataParser: DP.DataParser, params: ResearcherParams) => void,
22+
) {
23+
return (
24+
dataParser: DP.DataParser,
25+
params: ResearcherParams,
26+
) => {
27+
if (predicate(dataParser)) {
28+
researcher(dataParser, params);
29+
}
30+
};
31+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DP } from "@duplojs/utils";
2+
import { createResearcher } from "../create";
3+
4+
export const arrayResearcher = createResearcher(
5+
DP.arrayKind.has,
6+
(dataParser, params) => {
7+
params.find(dataParser.definition.element);
8+
},
9+
);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
export * from "./array";
2+
export * from "./lazy";
3+
export * from "./nullable";
4+
export * from "./object";
5+
export * from "./optional";
6+
export * from "./pipe";
7+
export * from "./record";
8+
export * from "./recover";
9+
export * from "./templateLiteral";
10+
export * from "./transform";
11+
export * from "./tuple";
12+
export * from "./union";
13+
14+
import type { createResearcher } from "../create";
15+
import { arrayResearcher } from "./array";
16+
import { lazyResearcher } from "./lazy";
17+
import { nullableResearcher } from "./nullable";
18+
import { objectResearcher } from "./object";
19+
import { optionalResearcher } from "./optional";
20+
import { pipeResearcher } from "./pipe";
21+
import { recordResearcher } from "./record";
22+
import { recoverResearcher } from "./recover";
23+
import { templateLiteralResearcher } from "./templateLiteral";
24+
import { transformResearcher } from "./transform";
25+
import { tupleResearcher } from "./tuple";
26+
import { unionResearcher } from "./union";
27+
28+
export const defaultResearchers = [
29+
arrayResearcher,
30+
lazyResearcher,
31+
nullableResearcher,
32+
objectResearcher,
33+
optionalResearcher,
34+
pipeResearcher,
35+
recordResearcher,
36+
recoverResearcher,
37+
templateLiteralResearcher,
38+
transformResearcher,
39+
tupleResearcher,
40+
unionResearcher,
41+
] as const satisfies readonly ReturnType<typeof createResearcher>[];
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DP } from "@duplojs/utils";
2+
import { createResearcher } from "../create";
3+
4+
export const lazyResearcher = createResearcher(
5+
DP.lazyKind.has,
6+
(dataParser, params) => {
7+
params.find(dataParser.definition.getter.value);
8+
},
9+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DP } from "@duplojs/utils";
2+
import { createResearcher } from "../create";
3+
4+
export const nullableResearcher = createResearcher(
5+
DP.nullableKind.has,
6+
(dataParser, params) => {
7+
params.find(dataParser.definition.inner);
8+
},
9+
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { A, DP, O, pipe } from "@duplojs/utils";
2+
import { createResearcher } from "../create";
3+
4+
export const objectResearcher = createResearcher(
5+
DP.objectKind.has,
6+
(dataParser, params) => {
7+
pipe(
8+
dataParser.definition.shape,
9+
O.values,
10+
A.map(params.find),
11+
);
12+
},
13+
);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DP } from "@duplojs/utils";
2+
import { createResearcher } from "../create";
3+
4+
export const optionalResearcher = createResearcher(
5+
DP.optionalKind.has,
6+
(dataParser, params) => {
7+
params.find(dataParser.definition.inner);
8+
},
9+
);

0 commit comments

Comments
 (0)