Skip to content

Commit

Permalink
TESTS
Browse files Browse the repository at this point in the history
  • Loading branch information
AmmarHalees committed Dec 23, 2023
1 parent 9c708e9 commit aae1439
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 57 deletions.
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

`onsubmit` provides a simple and headless interface for validating form fields, with emphasis on security.


### Features

- Light-weight.
Expand Down Expand Up @@ -148,6 +149,8 @@ const errors = validateForm(data, rulesObject);

### Utils and patterns

#### _utils

`onsubmit` provides a set of utility functions and patterns to help you build your forms.

| Function Name | Type Signature | Description |
Expand All @@ -160,6 +163,20 @@ const errors = validateForm(data, rulesObject);
| `isFile` | `(element: HTMLInputElement) => element is HTMLInputElement` | Checks if an HTML input element is of type `file`. |


#### _patterns


1. `email`
2. `uri`
3. `alphanumeric`
4. `cuid`
5. `kebabCase`
6. `arabic`



<br>


## FAQ

Expand Down Expand Up @@ -205,5 +222,7 @@ interface RulesObject {
- `minTime` rule.rule
- `maxTime` rule
- `file` rule: { minSize, maxSize, type, name }
- `cardNumber` pattern.
- `cardCompany` utility.
- Benchmarking
- Allow for multiple patterns
Empty file removed playground/index.ts
Empty file.
26 changes: 14 additions & 12 deletions src/regex/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ const email = /^(?!.*\.\.)[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[A-Za-z]{2,}$/; // n
const uri =
/^(https?:\/\/)?((www\.)?([a-z\d](?:[a-z\d-]*[a-z\d])?\.)+[a-z]{2,6}|(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))(:\d+)?(\/[-a-z\d%_.~+]*)*(\?[;&a-z\d%_.~+=-]*)?(\#[-a-z\d_]*)?$/i;

const cuid = /^c[^\s-]{8,}$/i;
const ulid = /^[0-9A-HJKMNP-TV-Z]{26}$/;
const cuid = /^c[\da-zA-Z]{8,}$/;

const alphanumeric = /^[-\w\s]+$/;
const alphanumeric = /^[-\w\s]+$/;

const kebabCase = /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/;
const kebabCase = /^([a-z][a-z0-9]*)(-[a-z0-9]+)*$/;

const _regex = {
email,
uri,
alphanumeric,
cuid,
ulid,
kebabCase,
};
const arabic =
/^[\u0600-\u06FF\u0750-\u077F\u08A0-\u08FF\uFB50-\uFDFF\uFE70-\uFEFF\u0660-\u0669،؛؟ـ\s\-.:!"]+$/u;

const _regex = {
email,
uri,
alphanumeric,
cuid,
kebabCase,
arabic,
};

export default _regex;
101 changes: 56 additions & 45 deletions src/tests/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,53 +83,22 @@ describe("Regex Tests", () => {
});
});

// Test CUID Regex
// describe("cuid", () => {
// // Valid CUIDs
// it("validates correct CUIDs", () => {
// const validCUIDs = [
// "ckvztazbu000001l5wth4lzdl",
// "c0123456789abcdef01234567",
// ];

// validCUIDs.forEach((cuid) => {
// expect(_regex.cuid.test(cuid)).toBe(true);
// });
// });

// // Invalid CUIDs
// it("rejects incorrect CUIDs", () => {
// const invalidCUIDs = [
// "0123456789abcdef01234567",
// "ckvztazbu0001l5wth4lzdl",
// "c!@#$$%^&*()",
// "ckvztazbu000001l5wth4lzdl-too-long",
// "ckvztazbu000001l5wth4",
// ];

// invalidCUIDs.forEach((cuid) => {
// expect(_regex.cuid.test(cuid)).toBe(false);
// });
// });
// });

// // Test ULID Regex
// describe("ulid", () => {
// it("validates correct ULIDs", () => {
// expect(_regex.ulid.test("01B4EEAFD4F12R3D7TYMGJ21LZ")).toBe(true); // Corrected ULID
// // Add more valid ULID cases
// });

// it("rejects incorrect ULIDs", () => {
// expect(_regex.ulid.test("01B4E")).toBe(false);
// expect(_regex.ulid.test("01B4EEAFD4G12R3D7TYMGJ21LZO")).toBe(false);
// // Add more invalid ULID cases
// });
// });
// Invalid CUIDs
it("rejects incorrect CUIDs", () => {
const invalidCUIDs = [
"0123456789abcdef01234567",
"c!@#$$%^&*()",
"ckvztazbu000001l5wth4lzdl-too-long",
// Other invalid CUIDs
];

invalidCUIDs.forEach((cuid) => {
expect(_regex.cuid.test(cuid)).toBe(false);
});
});

// Test Alphanumeric Regex
describe("alphanumeric", () => {
// Valid alphanumeric strings
it("validates correct alphanumeric strings", () => {
const validStrings = [
"Hello123",
Expand All @@ -143,7 +112,6 @@ describe("Regex Tests", () => {
});
});

// Invalid alphanumeric strings
it("rejects incorrect alphanumeric strings", () => {
const invalidStrings = [
"Hello@123",
Expand Down Expand Up @@ -187,4 +155,47 @@ describe("Regex Tests", () => {
});
});
});

describe("Strict Arabic Text and Numbers Regex", () => {
// Test cases for matching Arabic text with Arabic-Indic numbers
it("validates strings with Arabic characters and numbers", () => {
const validStrings = [
"مرحبا١٢٣٤", // Arabic text with Arabic-Indic numbers
"١٢٣٤", // Only Arabic-Indic numbers
"العربية٧٨٩", // Arabic text with Arabic-Indic numbers
"١٢٣٤",
"مرحبا، كيف حالك؟",
"مرحبا١٢٣٤، كيف حالك؟", // Combination of text, numbers, and punctuation
"٥٦٧٨٩ - أرقام عربية", // Arabic-Indic numerals with text
"هذه جملة عربية بالكامل.", // Sentence with full stop
"فاصلة؛ تستخدم للتوقف", // Sentence with Arabic semicolon
"هل هذا صحيح؟", // Question mark
"جملة بالعربية - مع شرطة", // Sentence with a dash
"هل تعلم؟ اللغة العربية ممتعة للغاية.",
'في العربية، يُستخدم "الفاصلة"، بشكل مختلف.',
"١٢٣٤ - هذه أرقامٌ عربية؛ وهي مختلفة عن الأرقام الإنجليزية.",
'أحب البرمجة: هي مجال "رائع" للتعلم!',
"تُستخدم النقطة العربية . بشكل مماثل للإنجليزية.",
];

validStrings.forEach((str) => {
expect(_regex.arabic.test(str)).toBe(true);
});
});

// Test cases for rejecting strings with non-Arabic characters or non-Arabic numbers
it("rejects strings without Arabic characters or numbers", () => {
const invalidStrings = [
"Hello - مرحبا", // Mixed English and Arabic
"مرحبا 1234", // Arabic text with Latin numbers
"こんにちは, مرحبا", // Mixed Japanese and Arabic
"مرحبا! English text", // Mixed Arabic and English with non-Arabic punctuation
"أرقام بالإنجليزية: 12345",
];

invalidStrings.forEach((str) => {
expect(_regex.arabic.test(str)).toBe(false);
});
});
});
});

0 comments on commit aae1439

Please sign in to comment.