-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrate isCompatibleType to the new rule style (#9)
- Loading branch information
Showing
7 changed files
with
523 additions
and
443 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"zod-compare": minor | ||
--- | ||
|
||
refactor: redesign isCompatibleType with new rule style | ||
|
||
The function `createIsSameTypeFn` has been renamed to `createCompareFn`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import type { ZodType } from "zod"; | ||
import { type CompareContext, type CompareRule } from "./types.ts"; | ||
|
||
export const createCompareFn = (rules: CompareRule[]) => { | ||
const isSameTypeFn = ( | ||
a: ZodType, | ||
b: ZodType, | ||
context: CompareContext = {}, | ||
): boolean => { | ||
let prevIndex = -1; | ||
const runner = (index: number): boolean => { | ||
if (index === rules.length) { | ||
throw new Error("Failed to compare type! " + a + " " + b); | ||
} | ||
if (index === prevIndex) { | ||
throw new Error("next() called multiple times"); | ||
} | ||
prevIndex = index; | ||
const rule = rules[index]; | ||
|
||
if ("stacks" in context && Array.isArray(context.stacks)) { | ||
context.stacks.push({ | ||
name: rule.name, | ||
target: [a, b], | ||
}); | ||
} | ||
|
||
return rule.compare( | ||
a, | ||
b, | ||
() => runner(index + 1), | ||
(a, b) => isSameTypeFn(a, b, context), | ||
context, | ||
); | ||
}; | ||
|
||
return runner(0); | ||
}; | ||
return isSameTypeFn; | ||
}; | ||
|
||
/** | ||
* @deprecated Use {@link createCompareFn} instead. | ||
*/ | ||
export const createIsSameTypeFn = createCompareFn; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,8 @@ | ||
export { isCompatibleType } from "./is-compatible-type.ts"; | ||
export { createIsSameTypeFn, isSameType } from "./is-same-type.ts"; | ||
export { defineCompareRule, isSameTypePresetRules } from "./rules.ts"; | ||
export { createCompareFn, createIsSameTypeFn } from "./create-compare-fn.ts"; | ||
export { | ||
isCompatibleType, | ||
isCompatibleTypePresetRules, | ||
} from "./is-compatible-type.ts"; | ||
export { isSameType, isSameTypePresetRules } from "./is-same-type.ts"; | ||
export { defineCompareRule } from "./rules.ts"; | ||
export type * from "./types.ts"; |
Oops, something went wrong.