MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / ArrayBase

Function ArrayBase

framework/framework/validator.ts:14–27  ·  view source on GitHub ↗
(check: CheckFunction, number: IsNumber, doSplit: boolean = number)

Source from the content-addressed store, hash-verified

12const MaybeArray = <T>(inner: Schema<T>) => Schema.union([Schema.array(inner), inner]);
13type CheckFunction = <IsNumber extends boolean>(v: IsNumber extends true ? number : string) => boolean;
14const ArrayBase = <IsNumber extends boolean>(check: CheckFunction, number: IsNumber, doSplit: boolean = number) => Schema.transform(
15 MaybeArray(Schema.union([Number, String])),
16 (v) => {
17 const input = (doSplit && typeof v === 'string') ? v.split(',') : v;
18 const res = number
19 ? (typeof input === 'string' ? [+input] : typeof input === 'number' ? [input] : input.map(Number))
20 : typeof input === 'string' ? [input] : typeof input === 'number' ? [input.toString()] : input.map(String);
21 const locate = number
22 ? res.find((i) => !Number.isFinite(+i) || !check(+i as any))
23 : res.find((i) => !check(i as any));
24 if (locate !== undefined) throw new Error(`Invalid input: ${locate}`);
25 return res;
26 },
27) as Schema<any, IsNumber extends true ? number[] : string[]>;
28
29export interface Types {
30 // String outputs

Callers 1

validator.tsFile · 0.85

Calls 2

MaybeArrayFunction · 0.85
findMethod · 0.80

Tested by

no test coverage detected