@heliosgraphics/utils
Advanced tools
Comparing version 5.3.1 to 5.3.3
@@ -6,8 +6,5 @@ import { it, describe, expect } from "vitest" | ||
describe("getClasses", () => { | ||
it("returns valid with string", () => | ||
expect(getClasses("burn")).toEqual("burn")) | ||
it("returns valid with string", () => expect(getClasses("burn")).toEqual("burn")) | ||
it("returns conditional object", () => | ||
expect(getClasses("burn", { "burn--alternative": true })).toEqual( | ||
"burn burn--alternative", | ||
)) | ||
expect(getClasses("burn", { "burn--alternative": true })).toEqual("burn burn--alternative")) | ||
it("returns valid with invalid", () => | ||
@@ -14,0 +11,0 @@ expect( |
@@ -14,8 +14,4 @@ import { describe, it, expect, vi } from "vitest" | ||
const spyCreateElement = vi | ||
.spyOn(document, "createElement") | ||
.mockImplementation(() => createElementMock) | ||
const spyAppendChild = vi | ||
.spyOn(document.body, "appendChild") | ||
.mockImplementation((node) => node) | ||
const spyCreateElement = vi.spyOn(document, "createElement").mockImplementation(() => createElementMock) | ||
const spyAppendChild = vi.spyOn(document.body, "appendChild").mockImplementation((node) => node) | ||
@@ -29,4 +25,3 @@ document.execCommand = vi.fn() | ||
const inputElement: HTMLInputElement = | ||
spyCreateElement.mock.results[0].value | ||
const inputElement: HTMLInputElement = spyCreateElement.mock.results[0].value | ||
expect(inputElement.value).toBe(TEXT_STRING) | ||
@@ -33,0 +28,0 @@ |
@@ -1,2 +0,2 @@ | ||
// copies the given value to the clipboard. | ||
// copies the given value to the clipboard | ||
export const copyValue = (text: string): void => { | ||
@@ -3,0 +3,0 @@ const input: HTMLInputElement = document.createElement("input") |
@@ -6,20 +6,13 @@ import { it, describe, expect } from "vitest" | ||
describe("hexToRgb", () => { | ||
it("converts hex to rgb", () => | ||
expect(hexToRgb("#0c2c78")).toEqual([12, 44, 120])) | ||
it("returns default for 0", () => | ||
expect(hexToRgb(<any>0)).toEqual(DEFAULT_PROFILE_RGB)) | ||
it("returns default for undefined", () => | ||
expect(hexToRgb(<any>undefined)).toEqual(DEFAULT_PROFILE_RGB)) | ||
it("converts hex to rgb", () => expect(hexToRgb("#0c2c78")).toEqual([12, 44, 120])) | ||
it("returns default for 0", () => expect(hexToRgb(<any>0)).toEqual(DEFAULT_PROFILE_RGB)) | ||
it("returns default for undefined", () => expect(hexToRgb(<any>undefined)).toEqual(DEFAULT_PROFILE_RGB)) | ||
}) | ||
describe("rgbToHex", () => { | ||
it("converts rgb to hex", () => | ||
expect(rgbToHex(12, 44, 120)).toEqual("#0c2c78")) | ||
it("converts string to hex", () => | ||
expect(rgbToHex(<any>"12", <any>"44", <any>"120")).toEqual("#0c2c78")) | ||
it("converts null to hex", () => | ||
expect(rgbToHex(<any>null, 44, 120)).toEqual("#002c78")) | ||
it("returns undefined to hex", () => | ||
expect(rgbToHex(12, <any>undefined, 120)).toEqual("#0cff78")) | ||
it("converts rgb to hex", () => expect(rgbToHex(12, 44, 120)).toEqual("#0c2c78")) | ||
it("converts string to hex", () => expect(rgbToHex(<any>"12", <any>"44", <any>"120")).toEqual("#0c2c78")) | ||
it("converts null to hex", () => expect(rgbToHex(<any>null, 44, 120)).toEqual("#002c78")) | ||
it("returns undefined to hex", () => expect(rgbToHex(12, <any>undefined, 120)).toEqual("#0cff78")) | ||
}) | ||
}) |
@@ -1,6 +0,6 @@ | ||
import type { TypeRGB } from "@heliosgraphics/library/types/colors" | ||
type TypeRGB = [number, number, number] | ||
export const DEFAULT_PROFILE_RGB: TypeRGB = [199, 201, 209] as const | ||
// converts a hex value to a TypeRGB. | ||
// converts a hex value to a TypeRGB | ||
export const hexToRgb = (hex?: string | null): TypeRGB => { | ||
@@ -21,8 +21,4 @@ const isValid: boolean = !!hex && typeof hex === "string" | ||
// converts an rgb value to a hex string (#0cd0cd). | ||
export const rgbToHex = ( | ||
r: number | string = 255, | ||
g: number | string = 255, | ||
b: number | string = 255, | ||
): string => { | ||
// converts an rgb value to a hex string (#0cd0cd) | ||
export const rgbToHex = (r: number | string = 255, g: number | string = 255, b: number | string = 255): string => { | ||
const _toHex = (c: unknown): string => { | ||
@@ -29,0 +25,0 @@ const value = Number(c) |
export type CallbackFunction = (...args: Array<unknown>) => void | ||
// debounces the function with wait time passed. | ||
export const debounce = ( | ||
callback: CallbackFunction, | ||
wait: number, | ||
): CallbackFunction => { | ||
// debounces the function with wait time passed | ||
export const debounce = (callback: CallbackFunction, wait: number): CallbackFunction => { | ||
let timeoutId: any | ||
@@ -9,0 +6,0 @@ |
{ | ||
"name": "@heliosgraphics/utils", | ||
"version": "5.3.1", | ||
"private": false, | ||
"version": "5.3.3", | ||
"type": "module", | ||
"author": "03b8 <03b8@helios.graphics>", | ||
"description": "Helios Utils", | ||
"license": "MIT", | ||
"private": false, | ||
"description": "Universal javascript helpers", | ||
"main": "index.ts", | ||
"engines": { | ||
@@ -13,3 +15,2 @@ "npm": ">=10.2.4", | ||
"dependencies": { | ||
"@heliosgraphics/library": "latest", | ||
"uuid": "latest", | ||
@@ -16,0 +17,0 @@ "xss": "latest" |
@@ -6,16 +6,11 @@ import { it, describe, expect } from "vitest" | ||
describe("getSlug", () => { | ||
it("returns valid from string with dashes", () => | ||
expect(getSlug("--B—uRn--")).toEqual("-burn-")) | ||
it("returns valid from special string", () => | ||
expect(getSlug("#$%^B#uR#n-")).toEqual("burn-")) | ||
it("returns valid from string with dashes", () => expect(getSlug("--B—uRn--")).toEqual("-burn-")) | ||
it("returns valid from special string", () => expect(getSlug("#$%^B#uR#n-")).toEqual("burn-")) | ||
it("returns valid from parens string", () => | ||
expect(getSlug("Gaussian Blur [1](2){3}")).toEqual("gaussian-blur-123")) | ||
it("replaces àáäâèéëêìíïîòóöôùúüûñç", () => | ||
expect(getSlug("àáäâèéëêìíïîòóöôùúüûñç")).toEqual( | ||
"aaaaeeeeiiiioooouuuunc", | ||
)) | ||
it("fails silently from undefined", () => | ||
expect(getSlug(undefined)).toEqual("")) | ||
expect(getSlug("àáäâèéëêìíïîòóöôùúüûñç")).toEqual("aaaaeeeeiiiioooouuuunc")) | ||
it("fails silently from undefined", () => expect(getSlug(undefined)).toEqual("")) | ||
it("fails silently from null", () => expect(getSlug(<any>null)).toEqual("")) | ||
}) | ||
}) |
@@ -1,2 +0,2 @@ | ||
// gets a valid slug from the given string. | ||
// gets a valid slug from the given string | ||
export const getSlug = (text?: string): string => { | ||
@@ -3,0 +3,0 @@ const isValid: boolean = Boolean(text && typeof text == "string") |
@@ -7,30 +7,19 @@ import { it, describe, expect } from "vitest" | ||
it("removes an onclick tag", () => | ||
expect( | ||
sanitizeText("a <b onClick=\"alert('hee hee')\">basic</b> tag"), | ||
).toEqual("a <b>basic</b> tag")) | ||
it("returns empty for undefined", () => | ||
expect(sanitizeText(<any>undefined)).toEqual("")) | ||
expect(sanitizeText("a <b onClick=\"alert('hee hee')\">basic</b> tag")).toEqual("a <b>basic</b> tag")) | ||
it("returns empty for undefined", () => expect(sanitizeText(<any>undefined)).toEqual("")) | ||
}) | ||
describe("removeMarkdown", () => { | ||
it("removes a bold format", () => | ||
expect(removeMarkdown("a **bold** text")).toEqual("a bold text")) | ||
it("removes a bold format", () => expect(removeMarkdown("a **bold** text")).toEqual("a bold text")) | ||
it("removes a link but keeps the name", () => | ||
expect(removeMarkdown("a [link](https://x.com) hello")).toEqual( | ||
"a link hello", | ||
)) | ||
expect(removeMarkdown("a [link](https://x.com) hello")).toEqual("a link hello")) | ||
it("removes a bold link", () => | ||
expect(removeMarkdown("a [bold **link**](https://x.com) hello")).toEqual( | ||
"a bold link hello", | ||
)) | ||
expect(removeMarkdown("a [bold **link**](https://x.com) hello")).toEqual("a bold link hello")) | ||
}) | ||
describe("middleEllipsis", () => { | ||
it("works with short word", () => | ||
expect(middleEllipsis("ellipsis", 5)).toEqual("e...s")) | ||
it("works with longer word", () => | ||
expect(middleEllipsis("lorem ipsum dolor", 12)).toEqual("lore...olor")) | ||
it("fails silently for undefined", () => | ||
expect(middleEllipsis(<any>null, 12)).toEqual("")) | ||
it("works with short word", () => expect(middleEllipsis("ellipsis", 5)).toEqual("e...s")) | ||
it("works with longer word", () => expect(middleEllipsis("lorem ipsum dolor", 12)).toEqual("lore...olor")) | ||
it("fails silently for undefined", () => expect(middleEllipsis(<any>null, 12)).toEqual("")) | ||
}) | ||
}) |
import xss from "xss" | ||
// sanitizes a given input. you're trusting the `xss` package here. | ||
// sanitizes a given input, you're trusting the `xss` package here | ||
export const sanitizeText = (input: string = ""): string => { | ||
@@ -10,3 +10,3 @@ const clean: string = xss(input) | ||
// removes markdown formatting. | ||
// removes markdown formatting | ||
export const removeMarkdown = (markdownText: string): string => { | ||
@@ -49,25 +49,13 @@ const patternsToRemove: Array<{ pattern: RegExp; replacement: string }> = [ | ||
// adds a middle ellipsis, eg.: (ellipsis, 6) gets "ell...sis". | ||
export const middleEllipsis = ( | ||
text: string = "", | ||
length: number = 64, | ||
): string => { | ||
// adds a middle ellipsis, eg.: (ellipsis, 6) gets "ell...sis" | ||
export const middleEllipsis = (text: string = "", length: number = 64): string => { | ||
const diff: number = Math.floor((length - 3) / 2) | ||
const isValid: boolean = Boolean( | ||
!!text && typeof text === "string" && text.length > length, | ||
) | ||
const isValid: boolean = Boolean(!!text && typeof text === "string" && text.length > length) | ||
if (!isValid) return "" | ||
return ( | ||
text.substring(0, diff) + | ||
"..." + | ||
text.substring(text.length - diff, text.length) | ||
) | ||
return text.substring(0, diff) + "..." + text.substring(text.length - diff, text.length) | ||
} | ||
export const removeNewlines = ( | ||
text?: string | null, | ||
limit?: number, | ||
): string => { | ||
export const removeNewlines = (text?: string | null, limit?: number): string => { | ||
if (!text) return "" | ||
@@ -74,0 +62,0 @@ |
export type CallbackFunction = (...args: Array<unknown>) => void | ||
// throttles the passed function with wait. | ||
// throttles the passed function with wait | ||
export const throttle = (callbackFunction: CallbackFunction, wait: number) => { | ||
@@ -5,0 +5,0 @@ let lastCall = 0 |
@@ -9,4 +9,3 @@ import { it, describe, expect } from "vitest" | ||
it("returns test uuid", () => expect(getUUID(MOCK_UUID)).toEqual(TEST_UUID)) | ||
it("returns any string", () => | ||
expect(getUUID("any-string")).toEqual("any-string")) | ||
it("returns any string", () => expect(getUUID("any-string")).toEqual("any-string")) | ||
}) | ||
@@ -16,4 +15,3 @@ | ||
it("validates test uuid", () => expect(isUUID(TEST_UUID)).toEqual(true)) | ||
it("validates a random uuid", () => | ||
expect(isUUID("101bfe56-8c16-4f94-9b45-759ea5e67cea")).toEqual(true)) | ||
it("validates a random uuid", () => expect(isUUID("101bfe56-8c16-4f94-9b45-759ea5e67cea")).toEqual(true)) | ||
it("catches empty string", () => expect(isUUID("")).toEqual(false)) | ||
@@ -20,0 +18,0 @@ it("catches undefined", () => expect(isUUID(<any>undefined)).toEqual(false)) |
@@ -9,3 +9,3 @@ import { v4 as uuidv4 } from "uuid" | ||
// this is necessary for snapshot tests, but should be dynamic. | ||
// this is necessary for snapshot tests, but should be dynamic | ||
if (IS_TEST) return TEST_UUID | ||
@@ -17,4 +17,3 @@ | ||
export const isUUID = (uuid?: unknown): boolean => { | ||
const uuidRegex = | ||
/^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/ | ||
const uuidRegex = /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/ | ||
const isValid = typeof uuid === "string" | ||
@@ -21,0 +20,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { validateHttpUrl, validateEmail } from "./validations" | ||
import { validateUrl, validateEmail } from "./validations" | ||
import { it, describe, expect } from "vitest" | ||
@@ -7,34 +7,21 @@ | ||
it("validates email", () => expect(validateEmail("x@x.com")).toEqual(true)) | ||
it("validates email with +", () => | ||
expect(validateEmail("x+x@x.com")).toEqual(true)) | ||
it("validates email with long tld", () => | ||
expect(validateEmail("0@helios.graphics")).toEqual(true)) | ||
it("catches long invalid string", () => | ||
expect(validateEmail("@space.city")).toEqual(false)) | ||
it("validates email with +", () => expect(validateEmail("x+x@x.com")).toEqual(true)) | ||
it("validates email with long tld", () => expect(validateEmail("0@helios.graphics")).toEqual(true)) | ||
it("catches long invalid string", () => expect(validateEmail("@space.city")).toEqual(false)) | ||
it("catches empty string", () => expect(validateEmail("")).toEqual(false)) | ||
it("catches undefined", () => | ||
expect(validateEmail(<any>undefined)).toEqual(false)) | ||
it("catches multi @", () => | ||
expect(validateEmail("x@@x.com")).toEqual(false)) | ||
it("catches a weird one", () => | ||
expect(validateEmail("x@@@x.com@/@x.com")).toEqual(false)) | ||
it("catches undefined", () => expect(validateEmail(<any>undefined)).toEqual(false)) | ||
it("catches multi @", () => expect(validateEmail("x@@x.com")).toEqual(false)) | ||
it("catches a weird one", () => expect(validateEmail("x@@@x.com@/@x.com")).toEqual(false)) | ||
}) | ||
describe("validateHttpUrl", () => { | ||
it("validates url", () => | ||
expect(validateHttpUrl("https://x.com")).toEqual(true)) | ||
it("validates url with long tld", () => | ||
expect(validateHttpUrl("https://lorem-ipsum.graphics")).toEqual(true)) | ||
it("validates url with double subdomain", () => | ||
expect(validateHttpUrl("https://0.x.x.com")).toEqual(true)) | ||
it("catches ftp", () => | ||
expect(validateHttpUrl("ftp://x.com")).toEqual(false)) | ||
it("catches string containing url", () => | ||
expect(validateHttpUrl("lorem ipsum https://x.com")).toEqual(false)) | ||
it("catches unsafe http", () => | ||
expect(validateHttpUrl("http://x.com")).toEqual(false)) | ||
it("catches empty string", () => expect(validateHttpUrl("")).toEqual(false)) | ||
it("catches undefined", () => | ||
expect(validateHttpUrl(<any>undefined)).toEqual(false)) | ||
it("validates url", () => expect(validateUrl("https://x.com")).toEqual(true)) | ||
it("validates url with long tld", () => expect(validateUrl("https://lorem-ipsum.graphics")).toEqual(true)) | ||
it("validates url with double subdomain", () => expect(validateUrl("https://0.x.x.com")).toEqual(true)) | ||
it("catches ftp", () => expect(validateUrl("ftp://x.com")).toEqual(false)) | ||
it("catches string containing url", () => expect(validateUrl("lorem ipsum https://x.com")).toEqual(false)) | ||
it("catches unsafe http", () => expect(validateUrl("http://x.com")).toEqual(false)) | ||
it("catches empty string", () => expect(validateUrl("")).toEqual(false)) | ||
it("catches undefined", () => expect(validateUrl(<any>undefined)).toEqual(false)) | ||
}) | ||
}) |
@@ -1,3 +0,3 @@ | ||
// validates a URL. | ||
export const validateHttpUrl = (text?: string | null): boolean => { | ||
// validates a URL | ||
export const validateUrl = (text?: string | null): boolean => { | ||
let url: URL | ||
@@ -11,3 +11,3 @@ | ||
// "http" is unsafe. | ||
// "http" is unsafe | ||
const isValid: boolean = Boolean(url.protocol === "https:") | ||
@@ -18,3 +18,3 @@ | ||
// validates an email. | ||
// validates an email | ||
export const validateEmail = (email: string = ""): boolean => { | ||
@@ -21,0 +21,0 @@ const re: RegExp = /^[a-zA-Z0-9._+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/ |
16761
2
21
373
- Removed@heliosgraphics/library@latest
- Removed@heliosgraphics/library@4.0.0-alpha-5(transitive)