Skip to content

Commit

Permalink
feat: some refactoring, more utils + tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
baldeepsingh-pantelwar committed Jan 9, 2024
1 parent 734525a commit 991eee6
Show file tree
Hide file tree
Showing 17 changed files with 271 additions and 46 deletions.
4 changes: 2 additions & 2 deletions .vscode/settings.json
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"
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"rollup-plugin-dts": "^6.0.2",
"rollup-plugin-peer-deps-external": "^2.2.4",
"tslib": "^2.6.2",
"typescript": "^5.2.2"
"typescript": "^5.2.2",
"yup": "^1.3.2"
},
"repository": {
"type": "git",
Expand Down
33 changes: 0 additions & 33 deletions src/date/format-time.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/date/index.ts

This file was deleted.

12 changes: 12 additions & 0 deletions src/general/debounce.ts
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);
};
}
1 change: 1 addition & 0 deletions src/general/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from "./debounce";
export * from "./uuidv4";
export * from "./wait";
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from "./array";
export * from "./date";
export * from "./dom";
export * from "./file";
export * from "./form-data";
Expand All @@ -8,3 +7,4 @@ export * from "./general";
export * from "./object";
export * from "./string";
export * from "./url";
export * from "./validation";
1 change: 0 additions & 1 deletion src/string/index.ts
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";
7 changes: 1 addition & 6 deletions src/string/has-emoji.ts → src/validation/emoji.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,5 @@
* @return {boolean} - True if the text contains emoji, false otherwise.
*/
export function hasEmoji(text: string): boolean {
try {
window.btoa(text);
return false;
} catch (error) {
return true;
}
return /\p{Extended_Pictographic}/u.test(text);
}
3 changes: 3 additions & 0 deletions src/validation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./emoji";
export * from "./number";
export * from "./yup";
6 changes: 6 additions & 0 deletions src/validation/number.ts
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;
};
50 changes: 50 additions & 0 deletions src/validation/yup.ts
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;
};
45 changes: 45 additions & 0 deletions tests/general/debounce.test.ts
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import { hasEmoji } from "../../src";

describe("Has emoij", () => {
describe("Has emoji", () => {
it("can check an empty string", () => {
expect(hasEmoji("")).toBe(false);
});
Expand All @@ -13,6 +13,10 @@ describe("Has emoij", () => {
expect(hasEmoji("This is a sentence")).toBe(false);
});

it("can check a string with numbers", () => {
expect(hasEmoji("Thi5 i5 4 s3nt3nc3 wi7h numb3r5 123")).toBe(false);
});

it("can check a string with emoji", () => {
expect(hasEmoji("This is a sentence with emoji 😃")).toBe(true);
});
Expand Down
27 changes: 27 additions & 0 deletions tests/validation/number.test.ts
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);
});
});
86 changes: 86 additions & 0 deletions tests/validation/yup.test.ts
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));
});
});
Loading

0 comments on commit 991eee6

Please sign in to comment.