-
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.
feat: some refactoring, more utils + tests added
- Loading branch information
1 parent
734525a
commit 991eee6
Showing
17 changed files
with
271 additions
and
46 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"editor.codeActionsOnSave": { | ||
"source.fixAll": true, | ||
"source.organizeImports": true | ||
"source.fixAll": "explicit", | ||
"source.organizeImports": "explicit" | ||
} | ||
} |
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,12 @@ | ||
export function debounce<T extends Array<unknown>>( | ||
cb: (...args: T) => void, | ||
delay = 1000 | ||
) { | ||
let timeout: NodeJS.Timeout; | ||
return (...args: T) => { | ||
clearTimeout(timeout); | ||
timeout = setTimeout(() => { | ||
cb(...args); | ||
}, delay); | ||
}; | ||
} |
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,2 +1,3 @@ | ||
export * from "./debounce"; | ||
export * from "./uuidv4"; | ||
export * from "./wait"; |
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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
export * from "./capitalize"; | ||
export * from "./convert-listcases"; | ||
export * from "./has-emoji"; | ||
export * from "./param-case"; | ||
export * from "./snake-case"; | ||
export * from "./title-case"; |
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,3 @@ | ||
export * from "./emoji"; | ||
export * from "./number"; | ||
export * from "./yup"; |
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,6 @@ | ||
export const checkNumber = (value: string) => { | ||
if (Number.isNaN(+value)) { | ||
return false; | ||
} | ||
return true; | ||
}; |
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,50 @@ | ||
import type { TestFunction } from "yup"; | ||
|
||
export const panValidate: TestFunction<string | undefined> = (value) => { | ||
if (!value) return false; | ||
if (value.length < 10 || value.length > 10) { | ||
return false; | ||
} | ||
if (value.charAt(3) !== "P") { | ||
return false; | ||
} | ||
if ( | ||
Number.isNaN(+value.charAt(5)) || | ||
Number.isNaN(+value.charAt(6)) || | ||
Number.isNaN(+value.charAt(7)) || | ||
Number.isNaN(+value.charAt(8)) | ||
) { | ||
return false; | ||
} | ||
if ( | ||
!Number.isNaN(+value.charAt(0)) || | ||
!Number.isNaN(+value.charAt(1)) || | ||
!Number.isNaN(+value.charAt(2)) || | ||
!Number.isNaN(+value.charAt(9)) | ||
) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export const mobileValidate: TestFunction<string | undefined> = (value) => { | ||
if (!value) return false; | ||
if (value.length < 10 || value.length > 10) { | ||
return false; | ||
} | ||
if (Number.isNaN(+value)) { | ||
return false; | ||
} | ||
return true; | ||
}; | ||
|
||
export const otpValidate: TestFunction<string | undefined> = (value) => { | ||
if (!value) return false; | ||
if (value.length < 6 || value.length > 6) { | ||
return false; | ||
} | ||
if (Number.isNaN(+value)) { | ||
return false; | ||
} | ||
return true; | ||
}; |
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 { debounce, wait } from "../../src"; | ||
|
||
describe("Debounce", () => { | ||
it("debounces a function correctly", async () => { | ||
let called = 0; | ||
const fn = () => { | ||
called++; | ||
}; | ||
const debouncedFn = debounce(fn, 1000); | ||
debouncedFn(); | ||
return wait(100) | ||
.then(() => { | ||
debouncedFn(); | ||
return wait(200); | ||
}) | ||
.then(() => { | ||
debouncedFn(); | ||
return wait(1000); | ||
}) | ||
.then(() => { | ||
expect(called).toBe(1); | ||
}); | ||
}); | ||
|
||
it("debounces a function once correctly", async () => { | ||
let called = 0; | ||
const fn = () => { | ||
called++; | ||
}; | ||
const debouncedFn = debounce(fn, 1000); | ||
debouncedFn(); | ||
return wait(100) | ||
.then(() => { | ||
debouncedFn(); | ||
return wait(200); | ||
}) | ||
.then(() => { | ||
debouncedFn(); | ||
return wait(1000); | ||
}) | ||
.then(() => { | ||
expect(called).toBe(1); | ||
}); | ||
}); | ||
}); |
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,27 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import { checkNumber } from "../../src"; | ||
|
||
describe("Check Number", () => { | ||
it("can check an empty string", () => { | ||
expect(checkNumber("")).toBe(true); | ||
}); | ||
|
||
it("can check a string with alphabets", () => { | ||
expect(checkNumber("asdf")).toBe(false); | ||
}); | ||
|
||
it("can check a string with digits", () => { | ||
expect(checkNumber("123")).toBe(true); | ||
}); | ||
|
||
it("can check a string with alphabets and digits", () => { | ||
expect(checkNumber("asdf123")).toBe(false); | ||
}); | ||
|
||
it("can check a string with digits and alphabets", () => { | ||
expect(checkNumber("123asdf")).toBe(false); | ||
}); | ||
}); |
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,86 @@ | ||
/** | ||
* @jest-environment jsdom | ||
*/ | ||
|
||
import type { Reference, TestContext } from "yup"; | ||
import { ValidationError } from "yup"; | ||
|
||
import { mobileValidate, otpValidate, panValidate } from "../../src"; | ||
|
||
const context: TestContext = { | ||
createError() { | ||
return new ValidationError(""); | ||
}, | ||
options: {}, | ||
originalValue: "", | ||
parent: {}, | ||
path: "", | ||
resolve<T>(value: T | Reference<T>) { | ||
return value as T; | ||
}, | ||
schema: {}, | ||
}; | ||
|
||
const panTest = (value: string) => { | ||
return panValidate.bind(context)(value, context); | ||
}; | ||
|
||
const mobileTest = (value: string) => { | ||
return mobileValidate.bind(context)(value, context); | ||
}; | ||
|
||
const otpTest = (value: string) => { | ||
return otpValidate.bind(context)(value, context); | ||
}; | ||
|
||
describe("Yup Tests", () => { | ||
it("can validate PAN", () => { | ||
const valid = ["SFIPS7992Q", "AWSPD6389A", "OIYPN2398Q"]; | ||
valid.forEach((pan) => expect(panTest(pan)).toBe(true)); | ||
const invalid = [ | ||
"SFIPS7992", | ||
"AWSPDD389A", | ||
"AWSPD63898", | ||
"OIYPN2398SS", | ||
"", | ||
"TESTING123", | ||
"I P2323 ", | ||
]; | ||
invalid.forEach((pan) => expect(panTest(pan)).toBe(false)); | ||
}); | ||
|
||
it("can validate mobile", () => { | ||
const valid = ["9876547234", "1234567890", "9876543210"]; | ||
valid.forEach((num) => expect(mobileTest(num)).toBe(true)); | ||
const invalid = [ | ||
"", | ||
"123456789", | ||
"987654321", | ||
"ASSKJFKSDS", | ||
"1234567890 ", | ||
"9876543210 ", | ||
"eeeeeeeeeee", | ||
"NaN", | ||
"1e10", | ||
]; | ||
invalid.forEach((num) => expect(mobileTest(num)).toBe(false)); | ||
}); | ||
|
||
it("can validate OTP", () => { | ||
const valid = ["123456", "987654"]; | ||
valid.forEach((otp) => expect(otpTest(otp)).toBe(true)); | ||
const invalid = [ | ||
"", | ||
"1234567", | ||
"9876543", | ||
"12345O", | ||
"ASSKJFKSDS", | ||
"1234567 ", | ||
"9876543 ", | ||
"eeeeeeeeeee", | ||
"NaN", | ||
"1e10", | ||
]; | ||
invalid.forEach((otp) => expect(otpTest(otp)).toBe(false)); | ||
}); | ||
}); |
Oops, something went wrong.