From 2d6766f096d2d5a23dced21438dbfb4b1abff45d Mon Sep 17 00:00:00 2001 From: Tsu-ba-me Date: Thu, 4 Aug 2022 22:00:00 -0400 Subject: [PATCH] fix(striker-ui): add testLength() and make test defaults optional --- striker-ui/lib/test_input/testInput.ts | 2 +- striker-ui/lib/test_input/testLength.ts | 23 +++++++++++++++++++++++ striker-ui/types/TestInputFunction.ts | 4 ++-- 3 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 striker-ui/lib/test_input/testLength.ts diff --git a/striker-ui/lib/test_input/testInput.ts b/striker-ui/lib/test_input/testInput.ts index 9f73ea1b..c70bc5e0 100644 --- a/striker-ui/lib/test_input/testInput.ts +++ b/striker-ui/lib/test_input/testInput.ts @@ -55,7 +55,7 @@ const testInput: TestInputFunction = ({ min: dMin = 0, onSuccess: dOnSuccess, value: dValue = null, - }, + } = {}, onFinishBatch, optionalTests, tests: requiredTests, diff --git a/striker-ui/lib/test_input/testLength.ts b/striker-ui/lib/test_input/testLength.ts new file mode 100644 index 00000000..cb0d3a42 --- /dev/null +++ b/striker-ui/lib/test_input/testLength.ts @@ -0,0 +1,23 @@ +import { MinimalInputTestArgs } from '../../types/TestInputFunction'; + +const testLength: ( + args: Pick & + Partial>, +) => boolean = ({ max, min, value }) => { + const { length } = String(value); + + let isGEMin = true; + let isLEMax = true; + + if (min) { + isGEMin = length >= min; + } + + if (max) { + isLEMax = length <= max; + } + + return isGEMin && isLEMax; +}; + +export default testLength; diff --git a/striker-ui/types/TestInputFunction.ts b/striker-ui/types/TestInputFunction.ts index fe59aa89..36f4a983 100644 --- a/striker-ui/types/TestInputFunction.ts +++ b/striker-ui/types/TestInputFunction.ts @@ -18,8 +18,8 @@ export type InputTest = { export type InputTestBatches = { [id: string]: { - defaults: InputTestArgs & { - onSuccess: () => void; + defaults?: InputTestArgs & { + onSuccess?: () => void; }; onFinishBatch?: () => void; optionalTests?: Array;