@heliosgraphics/utils 6.0.0-alpha.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/classnames.spec.ts +17 -0
- package/classnames.ts +21 -0
- package/clipboard.spec.ts +40 -0
- package/clipboard.ts +10 -0
- package/colors.spec.ts +40 -0
- package/colors.ts +33 -0
- package/debounce.spec.ts +101 -0
- package/debounce.ts +15 -0
- package/equals.spec.ts +168 -0
- package/equals.ts +120 -0
- package/index.ts +20 -0
- package/package.json +14 -0
- package/sanitize.spec.ts +558 -0
- package/sanitize.ts +219 -0
- package/sleep.spec.ts +59 -0
- package/sleep.ts +1 -0
- package/slug.spec.ts +15 -0
- package/slug.ts +14 -0
- package/strings.spec.ts +77 -0
- package/strings.ts +46 -0
- package/throttle.spec.ts +142 -0
- package/throttle.ts +17 -0
- package/tsconfig.json +40 -0
- package/uuid.spec.ts +32 -0
- package/uuid.ts +14 -0
- package/validations.spec.ts +26 -0
- package/validations.ts +19 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { validateUrl, validateEmail } from "./validations"
|
|
2
|
+
import { it, describe, expect } from "vitest"
|
|
3
|
+
|
|
4
|
+
describe("validations", () => {
|
|
5
|
+
describe("validateEmail", () => {
|
|
6
|
+
it("validates email", () => expect(validateEmail("x@x.com")).toEqual(true))
|
|
7
|
+
it("validates email with +", () => expect(validateEmail("x+x@x.com")).toEqual(true))
|
|
8
|
+
it("validates email with long tld", () => expect(validateEmail("0@helios.graphics")).toEqual(true))
|
|
9
|
+
it("catches long invalid string", () => expect(validateEmail("@space.city")).toEqual(false))
|
|
10
|
+
it("catches empty string", () => expect(validateEmail("")).toEqual(false))
|
|
11
|
+
it("catches undefined", () => expect(validateEmail(undefined)).toEqual(false))
|
|
12
|
+
it("catches multi @", () => expect(validateEmail("x@@x.com")).toEqual(false))
|
|
13
|
+
it("catches a weird one", () => expect(validateEmail("x@@@x.com@/@x.com")).toEqual(false))
|
|
14
|
+
})
|
|
15
|
+
|
|
16
|
+
describe("validateHttpUrl", () => {
|
|
17
|
+
it("validates url", () => expect(validateUrl("https://x.com")).toEqual(true))
|
|
18
|
+
it("validates url with long tld", () => expect(validateUrl("https://lorem-ipsum.graphics")).toEqual(true))
|
|
19
|
+
it("validates url with double subdomain", () => expect(validateUrl("https://0.x.x.com")).toEqual(true))
|
|
20
|
+
it("catches ftp", () => expect(validateUrl("ftp://x.com")).toEqual(false))
|
|
21
|
+
it("catches string containing url", () => expect(validateUrl("lorem ipsum https://x.com")).toEqual(false))
|
|
22
|
+
it("catches unsafe http", () => expect(validateUrl("http://x.com")).toEqual(false))
|
|
23
|
+
it("catches empty string", () => expect(validateUrl("")).toEqual(false))
|
|
24
|
+
it("catches undefined", () => expect(validateUrl(undefined)).toEqual(false))
|
|
25
|
+
})
|
|
26
|
+
})
|
package/validations.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export const validateUrl = (text?: string | null): boolean => {
|
|
2
|
+
let url: URL
|
|
3
|
+
|
|
4
|
+
try {
|
|
5
|
+
url = new URL(text as string)
|
|
6
|
+
} catch (_) {
|
|
7
|
+
return false
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const isValid: boolean = Boolean(url.protocol === "https:")
|
|
11
|
+
|
|
12
|
+
return isValid
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export const validateEmail = (email: string = ""): boolean => {
|
|
16
|
+
const re: RegExp = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
|
|
17
|
+
|
|
18
|
+
return re.test(email)
|
|
19
|
+
}
|