Skip to content

Commit

Permalink
bump and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
johnb8005 committed Oct 12, 2024
1 parent c286a9a commit dfdf8d7
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 49 deletions.
Binary file added bun.lockb
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nexys/validation",
"version": "2.1.7",
"version": "2.1.8",
"description": "Validation",
"keywords": [
"validation",
Expand Down
42 changes: 21 additions & 21 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { describe, test, expect } from "bun:test";
import { describe, expect, test } from "bun:test";
import * as U from "./utils";

test("check email", () => {
Expand Down Expand Up @@ -57,42 +57,42 @@ test("check JSON", () => {
expect(U.checkJSON("Invalid JSON")).toEqual(["must be a JSON"]);
});

describe('isInSet function', () => {

test('returns undefined when input is in the set', () => {
const validSet = new Set(['apple', 'banana', 'cherry']);
const result = U.isInSet(validSet)('apple');
describe("isInSet function", () => {
test("returns undefined when input is in the set", () => {
const validSet = new Set(["apple", "banana", "cherry"]);
const result = U.isInSetCheck(validSet)("apple");
expect(result).toBeUndefined();
});

test('returns an error message when input is not in the set', () => {
const validSet = new Set(['apple', 'banana', 'cherry']);
const result = U.isInSet(validSet)('orange');
test("returns an error message when input is not in the set", () => {
const validSet = new Set(["apple", "banana", "cherry"]);
const result = U.isInSetCheck(validSet)("orange");
expect(result).toEqual([`input "orange" is not in the set`]);
});

test('returns an error message when the set is empty', () => {
test("returns an error message when the set is empty", () => {
const emptySet = new Set<string>();
const result = U.isInSet(emptySet)('anything');
const result = U.isInSetCheck(emptySet)("anything");
expect(result).toEqual([`input "anything" is not in the set`]);
});

test('returns undefined when input exists in a set of numbers', () => {
test("returns undefined when input exists in a set of numbers", () => {
const numberSet = new Set([1, 2, 3]);
const result = U.isInSet(numberSet)('2');
expect(result).toBeUndefined(); // Because '2' as a string matches the number 2 when cast correctly
const result = U.isInSetCheck(numberSet)("2");
expect(result).toEqual(['input "2" is not in the set']); // Because '2' is a sring and 2 in the set is an int/number
});

test('returns an error message when input does not match any number in the set', () => {
test("returns an error message when input does not match any number in the set", () => {
const numberSet = new Set([1, 2, 3]);
const result = U.isInSet(numberSet)('4');
const result = U.isInSetCheck(numberSet)("4");
expect(result).toEqual([`input "4" is not in the set`]);
});

test('works with mixed types (e.g., string and number)', () => {
const mixedSet = new Set([1, 'apple', 3, 'banana']);
expect(U.isInSet(mixedSet)('apple')).toBeUndefined();
expect(U.isInSet(mixedSet)('1')).toEqual([`input "1" is not in the set`]);
test("works with mixed types (e.g., string and number)", () => {
const mixedSet = new Set([1, "apple", 3, "banana"]);
expect(U.isInSetCheck(mixedSet)("apple")).toBeUndefined();
expect(U.isInSetCheck(mixedSet)("1")).toEqual([
`input "1" is not in the set`,
]);
});

});
57 changes: 30 additions & 27 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import * as VT from "./type";
* @see http://www.w3.org/TR/html5/forms.html#valid-e-mail-address
*/
const isEmail = (email: string): boolean => {
const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/i;
const emailRegex =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/i;
const regexResult = email.match(emailRegex);

return !!regexResult && regexResult.includes(email);
Expand Down Expand Up @@ -86,28 +87,30 @@ export const passwordCheck = (password: string): VT.ErrorOut | undefined => {
* @param regex - The regular expression to test against.
* @return An array of error messages, or `undefined` if the string satisfies the regex.
*/
export const regexCheck = (regex: RegExp) => (s: string): VT.ErrorOut | undefined => {
const r = s.match(regex);
export const regexCheck =
(regex: RegExp) =>
(s: string): VT.ErrorOut | undefined => {
const r = s.match(regex);

if (r === null) {
return [`regex ${regex} not satisfied`];
}
if (r === null) {
return [`regex ${regex} not satisfied`];
}

return;
};
return;
};

// Function that checks if the input string exists in a provided Set
export const isInSetCheck = <A>(inputSet: Set<A>) => (input: string): VT.ErrorOut | undefined => {

// Check if the input string exists in the set (casting input as A, since Set<A> expects type A)
if (!inputSet.has(input as unknown as A)) {

// Return an error message in an array if input is not in the set
return [`input "${input}" is not in the set`];
}
export const isInSetCheck =
<A>(inputSet: Set<A>) =>
(input: string): VT.ErrorOut | undefined => {
// Check if the input string exists in the set (casting input as A, since Set<A> expects type A)
if (!inputSet.has(input as unknown as A)) {
// Return an error message in an array if input is not in the set
return [`input "${input}" is not in the set`];
}

// Return undefined if input is valid (i.e., exists in the set)
};
// Return undefined if input is valid (i.e., exists in the set)
};

/**
* Validates if a number is an integer and optionally if it can be negative.
Expand Down Expand Up @@ -162,7 +165,7 @@ export const checkISODateFormat = (
const r = s.match(/^(\d{4})-(\d{2})-(\d{2})$/);

if (r === null || r.length < 4) {
return ['date format not accepted, please pass YYYY-MM-DD'];
return ["date format not accepted, please pass YYYY-MM-DD"];
}

const [, year, month, day] = r;
Expand All @@ -176,41 +179,41 @@ export const checkISODateFormat = (
const { yearMin = 1900, yearMax = 2300 } = options;

if (iDay < 1) {
errors.push('day must be greater than zero');
errors.push("day must be greater than zero");
}

if (iMonth < 1) {
errors.push('month must be greater than zero');
errors.push("month must be greater than zero");
}

if (iMonth > 12) {
errors.push('month must be smaller than 12');
errors.push("month must be smaller than 12");
}

if (iYear < yearMin) {
errors.push('year must be greater than ' + yearMin);
errors.push("year must be greater than " + yearMin);
}

if (iYear > yearMax) {
errors.push('year must be smaller than ' + yearMax);
errors.push("year must be smaller than " + yearMax);
}

const isLeapYear = Math.abs(1988 - iYear) % 4 === 0;

if (iMonth === 2) {
if (!isLeapYear && iDay > 28) {
errors.push('day must be smaller than 28 (February)');
errors.push("day must be smaller than 28 (February)");
}

if (isLeapYear && iDay > 29) {
errors.push('day must be smaller than 29 (February and leap year)');
errors.push("day must be smaller than 29 (February and leap year)");
}
} else {
if (monthW30Days.includes(iMonth) && iDay > 30) {
errors.push(`day must be smaller than 30 (month of ${iMonth})`);
} else {
if (iDay > 31) {
errors.push('day must be smaller than 31');
errors.push("day must be smaller than 31");
}
}
}
Expand Down
54 changes: 54 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,46 @@
# yarn lockfile v1


"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-darwin-aarch64/-/bun-darwin-aarch64-1.1.30.tgz#19e3b1a38bfa148fd02d3f73022b2f71e0aa3c39"
integrity sha512-D07QioP+QXlouvIqQIS+7r2zq4lTNd6he79rhKsRQRZGFf9i3NPu87zspUpCaFEu//DZ35DYTt+5anQpAzpoxA==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-darwin-x64-baseline/-/bun-darwin-x64-baseline-1.1.30.tgz#27f11147d758b58471ea1eca473d5ae21e27bac3"
integrity sha512-1kFUCxHx7WuEbLDmqm0m2UKBd3S4Ln6qKQ4gxU4umMLFkmvDJn6PszDruFInxGKFLoTAmbXNYNVWkkG/ekt/Lg==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-darwin-x64/-/bun-darwin-x64-1.1.30.tgz#ae96fb8eb0bcad68ad8631c6eecebdd8b04f94c1"
integrity sha512-xZ4gTehS6QwN6bsJfDycCNneKoUMaFUQhQg24bJzXS4JPDxeKg1W7PS5AE+U9apz5Dx6//+D4RwVpAPG2LXt0w==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-linux-aarch64/-/bun-linux-aarch64-1.1.30.tgz#7f7385b5259aa3986c996bc1f54322446bec0fd1"
integrity sha512-SfHHLlph6fptDXyyChcUkeDbEZr2ww1p2BucV6OrvzwTOPi8pVmXA4360YT8ggR/3AHPp4GO36VaD+FU2Ocbxw==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-linux-x64-baseline/-/bun-linux-x64-baseline-1.1.30.tgz#c6e9239fc17f730ef69671d7b9118861e8462cf8"
integrity sha512-/b/VuNOaAYmsVk9MvfwKcCYARJPUg78hebxNyD5DSajAf3dqtUSnf7QYcq/3mxWH++N+gM7uRTrGksGS63+ZUw==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-linux-x64/-/bun-linux-x64-1.1.30.tgz#de83bdeee1daa43d1eb49bd236ccd77bf26babb0"
integrity sha512-1mC39jQSaECytEKAZdCZmv3ZreMsp7aoxnBwmJtVd2Z7urnw17PKi4dKkZd/R+AubsNYtXtW4jeM8SEa5sUJRw==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-windows-x64-baseline/-/bun-windows-x64-baseline-1.1.30.tgz#311fed812c2b976bc309e5f4c6e14d8305fc3c1e"
integrity sha512-ERQ4/ogzbFvHjpyHcnruc8bnryvDvUoiWi6vczfQ4M/idJc+Kg5VSEJiF5k7946rIZGamG6QWgRxtpIglD4/Zw==

"@oven/[email protected]":
version "1.1.30"
resolved "https://registry.yarnpkg.com/@oven/bun-windows-x64/-/bun-windows-x64-1.1.30.tgz#f12fb52f9c9568f70b4020473ad6510db0944cbb"
integrity sha512-mdRjNtD9NIA8CiH6N1zrIVE6oAtDko/c29H1s00UA+5O/WhXhg95G8IyInD8hN3vAEz8H2lGBgLG2EGfSFxnGg==

"@types/bun@^1.1.6":
version "1.1.6"
resolved "https://registry.yarnpkg.com/@types/bun/-/bun-1.1.6.tgz#90ea30fbbc0391484448acdf84928ab9f1e3f423"
Expand Down Expand Up @@ -38,6 +78,20 @@ [email protected]:
"@types/node" "~20.12.8"
"@types/ws" "~8.5.10"

bun@^1.1.30:
version "1.1.30"
resolved "https://registry.yarnpkg.com/bun/-/bun-1.1.30.tgz#c63152605870707d703c3fc0e49877d08e1eda27"
integrity sha512-ysRL1pq10Xba0jqVLPrKU3YIv0ohfp3cTajCPtpjCyppbn3lfiAVNpGoHfyaxS17OlPmWmR67UZRPw/EueQuug==
optionalDependencies:
"@oven/bun-darwin-aarch64" "1.1.30"
"@oven/bun-darwin-x64" "1.1.30"
"@oven/bun-darwin-x64-baseline" "1.1.30"
"@oven/bun-linux-aarch64" "1.1.30"
"@oven/bun-linux-x64" "1.1.30"
"@oven/bun-linux-x64-baseline" "1.1.30"
"@oven/bun-windows-x64" "1.1.30"
"@oven/bun-windows-x64-baseline" "1.1.30"

typescript@^5.5.4:
version "5.5.4"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-5.5.4.tgz#d9852d6c82bad2d2eda4fd74a5762a8f5909e9ba"
Expand Down

0 comments on commit dfdf8d7

Please sign in to comment.