Skip to content

Commit

Permalink
format files, fix lint errors and add function for inputting field va…
Browse files Browse the repository at this point in the history
…lues
  • Loading branch information
piotrkulpinski committed Apr 26, 2024
1 parent 90a356c commit 560d164
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 42 deletions.
50 changes: 50 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
{
"$schema": "https://biomejs.dev/schemas/1.7.1/schema.json",
"organizeImports": {
"enabled": true
},
"formatter": {
"enabled": true,
"lineWidth": 100,
"indentStyle": "space",
"indentWidth": 2
},
"javascript": {
"formatter": {
"quoteStyle": "double",
"semicolons": "asNeeded",
"arrowParentheses": "asNeeded",
"bracketSpacing": true,
"trailingComma": "all"
}
},
"json": {
"parser": {
"allowComments": false,
"allowTrailingCommas": true
}
},
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"suspicious": {
"noExplicitAny": "off",
"noArrayIndexKey": "off",
"noConfusingLabels": "off",
"noShadowRestrictedNames": "off"
},
"security": {
"noDangerouslySetInnerHtml": "off"
},
"a11y": {
"useAnchorContent": "off"
}
}
}
}
Binary file modified bun.lockb
Binary file not shown.
9 changes: 2 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@curiousleaf/utils",
"description": "A lightweight set of utilities",
"version": "1.0.25",
"version": "1.0.26",
"license": "MIT",
"type": "module",
"author": {
Expand All @@ -25,6 +25,7 @@
},
"devDependencies": {
"@babel/runtime": "^7.23.8",
"@biomejs/biome": "^1.7.1",
"bun-types": "latest",
"prettier": "^3.1.1",
"typescript": "^5.3.3"
Expand All @@ -33,11 +34,5 @@
"@sindresorhus/slugify": "^2.2.1",
"@uiw/color-convert": "^2.0.9",
"scule": "^1.3.0"
},
"prettier": {
"semi": false,
"singleQuote": false,
"trailingComma": "es5",
"printWidth": 100
}
}
2 changes: 1 addition & 1 deletion src/colors/colors.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ColorResult, HsvaColor, color, hexToRgba } from "@uiw/color-convert"
import { type ColorResult, type HsvaColor, color, hexToRgba } from "@uiw/color-convert"

/**
* Check if a given color in hexadecimal format is a light color.
Expand Down
8 changes: 4 additions & 4 deletions src/format/format.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
formatCurrency,
formatIntervalAmount,
formatMimeType,
formatToDecimals
formatToDecimals,
} from "./format"

describe("formatCurrency", () => {
Expand Down Expand Up @@ -41,9 +41,9 @@ describe("formatToDecimals", () => {
})

it("trims trailing double zeros", () => {
expect(formatToDecimals(1234.00, 2)).toEqual("1234")
expect(formatToDecimals(1234.00, 0)).toEqual("1234")
expect(formatToDecimals(1234.10, 2)).toEqual("1234.10")
expect(formatToDecimals(1234.0, 2)).toEqual("1234")
expect(formatToDecimals(1234.0, 0)).toEqual("1234")
expect(formatToDecimals(1234.1, 2)).toEqual("1234.10")
})
})

Expand Down
2 changes: 1 addition & 1 deletion src/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export const formatIntervalAmount = (amount: number, interval: "month" | "year"
* @returns The formatted number as a string.
*/
export const formatToDecimals = (number: number, precision = 0): string => {
return number.toFixed(precision < 0 ? 0 : precision).replace(/\.0+$/, '')
return number.toFixed(precision < 0 ? 0 : precision).replace(/\.0+$/, "")
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ describe("getExcerpt", () => {
it("gets an excerpt from a string", () => {
expect(getExcerpt("<p>Hello, <strong>world!</strong></p>", 10)).toEqual("Hello, wor...")
expect(getExcerpt("Lorem ipsum dolor sit amet, consectetur adipiscing elit.", 20)).toEqual(
"Lorem ipsum dolor si..."
"Lorem ipsum dolor si...",
)
expect(getExcerpt("", 10)).toEqual(null)
})
Expand Down
28 changes: 25 additions & 3 deletions src/helpers/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const range = (start: number, end: number) => {
* @param delay - The amount of time to delay the execution of the function, in milliseconds.
*/
export const sleep = async (delay: number) => {
new Promise((resolve) => setTimeout(resolve, delay))
new Promise(resolve => setTimeout(resolve, delay))
}

/**
Expand Down Expand Up @@ -123,7 +123,7 @@ export const getInitials = (value?: string | null, limit = 0) => {
}

const values = val.split(" ").filter(isTruthy)
const initials = values.map((name) => name.charAt(0).toUpperCase()).join("")
const initials = values.map(name => name.charAt(0).toUpperCase()).join("")

if (limit > 0) {
return initials.slice(0, limit)
Expand All @@ -142,7 +142,7 @@ export const toBase64 = (file: File): Promise<string> => {
const reader = new FileReader()
reader.readAsDataURL(file)
reader.onload = () => resolve(reader.result as string)
reader.onerror = (error) => reject(error)
reader.onerror = error => reject(error)
})
}

Expand All @@ -161,3 +161,25 @@ export const splitArrayChunks = <T>(array: T[], chunkSize: number) => {

return chunks
}

/**
* Set the value of an HTMLInputElement using its native value setter.
*
* @param input - The HTMLInputElement to set the value of.
* @param value - The value to set on the input element.
*/
export const setInputValue = (
input: HTMLInputElement | null | undefined,
value: unknown,
triggerChange = false,
) => {
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
HTMLInputElement.prototype,
'value',
)?.set

nativeInputValueSetter?.call(input, value)

// Trigger a change event if the value was changed
triggerChange && input?.dispatchEvent(new Event('input', { bubbles: true }))
}
10 changes: 0 additions & 10 deletions src/numbers/numbers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,3 @@ describe("parseNumericValue", () => {
expect(parseNumericValue("not a number")).toBe("not a number")
})
})

describe("getRandomNumber", () => {
it("returns a random number within the specified range", () => {
const min = 1
const max = 10
const randomNumber = getRandomNumber(min, max)
expect(randomNumber).toBeGreaterThanOrEqual(min)
expect(randomNumber).toBeLessThanOrEqual(max)
})
})
13 changes: 1 addition & 12 deletions src/numbers/numbers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,6 @@
* Utility functions for working with numbers.
*/

/**
* Generates a random number between the specified minimum and maximum values (inclusive).
*
* @param min The minimum value for the random number.
* @param max The maximum value for the random number.
* @returns A random number between the specified minimum and maximum values.
*/
export const getRandomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min
}

/**
* Keep a number within a specified range.
* @param value - The number to keep within the range.
Expand Down Expand Up @@ -40,7 +29,7 @@ export const keepNumberInRange = (value: number, min?: number, max?: number) =>
* @returns The parsed numeric value, or the original string if it cannot be parsed.
*/
export const parseNumericValue = (string: string) => {
const number = parseFloat(string)
const number = Number.parseFloat(string)

return Number.isNaN(number) ? string : number
}
2 changes: 1 addition & 1 deletion src/params/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export const getSearchParams = (request: Request) => {
* @returns The current page number as a number.
*/
export const getCurrentPage = (page?: string | null) => {
return Math.max(page && !Number.isNaN(Number(page)) ? parseInt(page || "1", 10) : 1, 1)
return Math.max(page && !Number.isNaN(Number(page)) ? Number.parseInt(page || "1", 10) : 1, 1)
}

/**
Expand Down
10 changes: 10 additions & 0 deletions src/random/random.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ describe("getRandomString", () => {
})
})

describe("getRandomNumber", () => {
it("returns a random number within the specified range", () => {
const min = 1
const max = 10
const randomNumber = getRandomNumber(min, max)

Check failure on line 40 in src/random/random.test.ts

View workflow job for this annotation

GitHub Actions / publish

ReferenceError: Can't find variable: getRandomNumber

at /home/runner/work/utils/utils/src/random/random.test.ts:40:26
expect(randomNumber).toBeGreaterThanOrEqual(min)
expect(randomNumber).toBeLessThanOrEqual(max)
})
})

describe("getRandomProperty", () => {
it("returns a value from the object", () => {
const obj = { a: 1, b: 2, c: 3 }
Expand Down
15 changes: 13 additions & 2 deletions src/random/random.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ export const getRandomString = (): string => {
return Math.random().toString(36).substring(2, 15)
}

/**
* Generates a random number between the specified minimum and maximum values (inclusive).
*
* @param min The minimum value for the random number.
* @param max The maximum value for the random number.
* @returns A random number between the specified minimum and maximum values.
*/
export const getRandomNumber = (min: number, max: number) => {
return Math.floor(Math.random() * (max - min + 1)) + min
}

/**
* Returns a random property value from an object.
* @param obj - The object to get a random property value from.
* @returns A random property value from the object.
*/
export const getRandomProperty = <T>(obj: Record<string, T>): T => {
const keys = Object.keys(obj)
const randomKey = keys[Math.floor(keys.length * Math.random())]!
const randomKey = keys[Math.floor(keys.length * Math.random())]

return obj[randomKey]!
return obj[randomKey]
}

0 comments on commit 560d164

Please sign in to comment.