-
-
Save sebmck/26ade0d73de7daee729a0848d5d2c41f to your computer and use it in GitHub Desktop.
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
diff --git a/library/src/schemas/number/number.ts b/library/src/schemas/number/number.ts | |
index fa314d4b..c39b5549 100644 | |
--- a/library/src/schemas/number/number.ts | |
+++ b/library/src/schemas/number/number.ts | |
@@ -20,6 +20,8 @@ export interface NumberSchema<TOutput = number> | |
pipe: Pipe<number> | undefined; | |
} | |
+let memoized: NumberSchema | undefined; | |
+ | |
/** | |
* Creates a number schema. | |
* | |
@@ -46,11 +48,16 @@ export function number( | |
arg1?: ErrorMessage | Pipe<number>, | |
arg2?: Pipe<number> | |
): NumberSchema { | |
+ const memoizable = arg1 === undefined && arg2 === undefined; | |
+ if (memoizable && memoized) { | |
+ return memoized; | |
+ } | |
+ | |
// Get message and pipe argument | |
const [message, pipe] = defaultArgs(arg1, arg2); | |
// Create and return number schema | |
- return { | |
+ const schema: NumberSchema = { | |
type: 'number', | |
expects: 'number', | |
async: false, | |
@@ -66,4 +73,8 @@ export function number( | |
return schemaIssue(this, number, input, config); | |
}, | |
}; | |
+ if (memoizable) { | |
+ memoized = schema; | |
+ } | |
+ return schema; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment