🔧 Resolve oxlint warnings; defer stylistic rules to oxfmt
This commit is contained in:
1 parent
b3a4e14f45
commit
99fd85ca7e
5 files changed
+99
-67
No files matched your search
+68
-57
@@ -14,69 +14,80 @@ const literalMatcher = <
|
||||
): Matcher<L> => ({
|
||||
matches: (candidate): candidate is L => candidate === value,
|
||||
});
|
||||
|
||||
const typeMatcher = <T>(
|
||||
type:
|
||||
| "string"
|
||||
| "number"
|
||||
| "boolean"
|
||||
| "bigint"
|
||||
| "symbol"
|
||||
| "undefined"
|
||||
| "object"
|
||||
| "function",
|
||||
): Matcher<T> => {
|
||||
const matches = (value: unknown): value is T => {
|
||||
if (type === "undefined") return value === undefined;
|
||||
if (type === "object")
|
||||
return (
|
||||
(typeof value === "object" && value !== null) ||
|
||||
typeof value === "function"
|
||||
);
|
||||
return typeof value === type;
|
||||
};
|
||||
return { matches };
|
||||
};
|
||||
|
||||
const whenMatcher = <T>(
|
||||
predicate: (value: unknown) => value is T,
|
||||
): Matcher<T> => ({
|
||||
matches: predicate,
|
||||
});
|
||||
|
||||
const whenMatcherAny = <T>(
|
||||
predicate: (value: unknown) => boolean,
|
||||
): Matcher<T> => ({
|
||||
matches: (value: unknown): value is T => predicate(value),
|
||||
});
|
||||
|
||||
const structuralMatcher = <S extends object, T extends S>(
|
||||
shape: S,
|
||||
refine?: (value: S) => value is T,
|
||||
): Matcher<T> => ({
|
||||
matches: (value: unknown): value is T => {
|
||||
if (typeof value !== "object" || value === null) return false;
|
||||
const candidate = value as S;
|
||||
for (const key of Object.keys(shape) as Array<keyof S>) {
|
||||
if (!(key in candidate)) return false;
|
||||
const expected = shape[key];
|
||||
const actual = candidate[key];
|
||||
if (
|
||||
typeof expected === "object" &&
|
||||
expected !== null &&
|
||||
"matches" in expected
|
||||
) {
|
||||
const nested = expected as unknown as Matcher<unknown>;
|
||||
if (!nested.matches(actual)) return false;
|
||||
type:
|
||||
| "string"
|
||||
| "number"
|
||||
| "boolean"
|
||||
| "bigint"
|
||||
| "symbol"
|
||||
| "undefined"
|
||||
| "object"
|
||||
| "function",
|
||||
): Matcher<T> => {
|
||||
const matches = (value: unknown): value is T => {
|
||||
if (type === "undefined") {
|
||||
return value === undefined;
|
||||
}
|
||||
if (type === "object") {
|
||||
return (
|
||||
(typeof value === "object" && value !== null) ||
|
||||
typeof value === "function"
|
||||
);
|
||||
}
|
||||
return typeof value === type;
|
||||
};
|
||||
return { matches };
|
||||
},
|
||||
whenMatcher = <T>(
|
||||
predicate: (value: unknown) => value is T,
|
||||
): Matcher<T> => ({
|
||||
matches: predicate,
|
||||
}),
|
||||
whenMatcherAny = <T>(
|
||||
predicate: (value: unknown) => boolean,
|
||||
): Matcher<T> => ({
|
||||
matches: (value: unknown): value is T => predicate(value),
|
||||
}),
|
||||
isNestedMatcher = (expected: unknown): expected is Matcher<unknown> =>
|
||||
typeof expected === "object" &&
|
||||
expected !== null &&
|
||||
"matches" in expected,
|
||||
keysMatch = <S extends object>(shape: S, candidate: object): boolean => {
|
||||
for (const key of Object.keys(shape) as (keyof S)[]) {
|
||||
if (!(key in candidate)) {
|
||||
return false;
|
||||
}
|
||||
const expected = shape[key],
|
||||
actual = candidate[key as keyof object];
|
||||
if (isNestedMatcher(expected)) {
|
||||
if (!expected.matches(actual)) {
|
||||
return false;
|
||||
}
|
||||
} else if (actual !== expected) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return refine
|
||||
? refine(candidate)
|
||||
: (true as T extends S ? true : never);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
structuralMatcher = <S extends object, T extends S>(
|
||||
shape: S,
|
||||
refine?: (value: S) => value is T,
|
||||
): Matcher<T> => ({
|
||||
matches: (value: unknown): value is T => {
|
||||
if (typeof value !== "object" || value === null) {
|
||||
return false;
|
||||
}
|
||||
const candidate = value as S;
|
||||
if (!keysMatch(shape, candidate)) {
|
||||
return false;
|
||||
}
|
||||
if (refine && !refine(candidate)) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
export const P = {
|
||||
literal: literalMatcher,
|
||||
|
||||
Reference in new issue
Block a user