Skip to content

Commit

Permalink
FIX-182 clean up validText()
Browse files Browse the repository at this point in the history
  • Loading branch information
janmarius committed Sep 21, 2023
1 parent a2d2ad5 commit 9d408e8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
12 changes: 6 additions & 6 deletions Src/WitsmlExplorer.Frontend/components/Modals/ModalParts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
Edit
}

export const validText = (text: string, minLength: number | undefined = undefined, maxLength: number | undefined = undefined): boolean => {
if (minLength === undefined && maxLength === undefined) return Boolean(text) && text.length > 0;
if (minLength === 0 && (text === null || text === undefined)) return true;
if (minLength > 0 && (text === null || text === undefined)) return false;
if (typeof minLength === "number" && maxLength === undefined) return text.length >= minLength;
if (minLength === undefined && typeof maxLength === "number") return text.length <= maxLength;
export const validText = (text: string, minLength: number = null, maxLength: number = null): boolean => {
if (minLength === null && maxLength === null) return text?.length > 0;
if (minLength === 0 && !text) return true;
if (minLength > 0 && !text) return false;
if (typeof minLength === "number" && maxLength === null) return text.length >= minLength;
if (minLength === null && typeof maxLength === "number") return text.length <= maxLength;
if (minLength >= maxLength) throw new Error("The value for minLength should be less than maxLength.");
return text.length >= minLength && text.length <= maxLength;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ it("Should detect any misbehavior during text validation using the validText() m
expect(validText(null, 1, 10)).toBeFalsy();
expect(validText(undefined, 1, 10)).toBeFalsy();
expect(validText("")).toBeFalsy();
expect(validText("", 0, undefined)).toBeTruthy();
expect(validText("a", 0, undefined)).toBeTruthy();
expect(validText("a", 1, undefined)).toBeTruthy();
expect(validText("", 1, undefined)).toBeFalsy();
expect(validText("abc", undefined, 3)).toBeTruthy();
expect(validText("abcd", undefined, 3)).toBeFalsy();
expect(validText("", 0, null)).toBeTruthy();
expect(validText("a", 0, null)).toBeTruthy();
expect(validText("a", 1, null)).toBeTruthy();
expect(validText("", 1, null)).toBeFalsy();
expect(validText("abc", null, 3)).toBeTruthy();
expect(validText("abcd", null, 3)).toBeFalsy();
expect(validText("ab", 1, 3)).toBeTruthy();
expect(validText("abc", 1, 3)).toBeTruthy();
expect(validText("", 1, 3)).toBeFalsy();
Expand Down

0 comments on commit 9d408e8

Please sign in to comment.