From 298e9396f4564765f85ef7e76bb584e052490d99 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6ren=20Meier?= Date: Sun, 4 Feb 2024 01:31:00 +0100 Subject: [PATCH] start to add types --- src/utils/colors.d.ts | 4 ++ src/utils/colors.js | 4 +- src/utils/utils.d.ts | 86 +++++++++++++++++++++++++++++++++++++++++++ src/utils/utils.js | 2 +- 4 files changed, 94 insertions(+), 2 deletions(-) create mode 100644 src/utils/colors.d.ts create mode 100644 src/utils/utils.d.ts diff --git a/src/utils/colors.d.ts b/src/utils/colors.d.ts new file mode 100644 index 0000000..38855d8 --- /dev/null +++ b/src/utils/colors.d.ts @@ -0,0 +1,4 @@ +/** + * returns expects a hex value with 6 values `rgba(0,0,0,a)` + */ +export declare function toRgba(color: string, alpha?: number): string; diff --git a/src/utils/colors.js b/src/utils/colors.js index 39fe346..9475d1e 100644 --- a/src/utils/colors.js +++ b/src/utils/colors.js @@ -1,6 +1,8 @@ import { range } from './utils.js'; -/// returns expects a hex value with 6 values `rgba(0,0,0,a)` +/** + * returns expects a hex value with 6 values `rgba(0,0,0,a)` + */ export function toRgba(val, alpha = 1) { val = val.trim(); if (!val.startsWith('#') || val.length !== 7) diff --git a/src/utils/utils.d.ts b/src/utils/utils.d.ts new file mode 100644 index 0000000..df5ae9b --- /dev/null +++ b/src/utils/utils.d.ts @@ -0,0 +1,86 @@ +/** + * Delays for a specified amount of time. + * + * @param ms - The number of milliseconds to delay for. + * @returns A promise that resolves after the delay. + */ +export declare function timeout(ms: number): Promise; + +/** + * Comparison function for sorting in descending order. + * + * @param a - The first value to compare. + * @param b - The second value to compare. + * @returns -1 if a > b, 1 if b > a, 0 otherwise. + */ +export declare function sortToLower(a: any, b: any): number; + +/** + * Comparison function for sorting in ascending order. + * + * @param a - The first value to compare. + * @param b - The second value to compare. + * @returns 1 if a > b, -1 if b > a, 0 otherwise. + */ +export declare function sortToUpper(a: any, b: any): number; + +/** + * Pads a value with leading zeros until it reaches a specified length. + * + * @param val - The value to pad. + * @param length - The desired length of the padded value. + * @returns The padded value as a string. + */ +export declare function padZero(val: any, length?: number): string; + +export declare const ALPHABET: string; +export declare const ALPHABET_LENGTH: number; + +/** + * Generates a random token of a specified length. + * + * @param {number} [length=8] - The desired length of the token. + * @returns {string} A random token. + */ +export declare function randomToken(length?: number): string; + +/** + * Creates an array of whole numbers in a specified range. + * + * @param {number} start - The start of the range. + * @param {number} end - The end of the range (exclusive). + * @param {number} [step=1] - The step size between values. + * @returns {number[]} The generated array. + */ +export declare function range( + start: number, + end: number, + step?: number, +): number[]; + +/** + * Selects a random element from an array, or returns null if the array is + * empty. + * + * @param {*[]} arr - The array to select from. + * @returns {*} A random element from the array, or null. + */ +export declare function randomEl(arr: T[]): T | null; + +/** + * Checks how closely a search string matches a value, and returns a score + * representing the match quality. + * + * @param {string} search - The search string. + * @param {string} val - The value to check for a match. + * @returns {number} 0 if no match, 1+ if there was a match (lower is better). + */ +export declare function searchScore(search: string, val: string): number; + +/** + * Calculates the sum of all numbers in an array. + * + * @param {number[]} ar - The array of numbers to sum. + * @returns {number} The sum of all numbers in the array. + */ +export declare function sum(ar: number[]): number; diff --git a/src/utils/utils.js b/src/utils/utils.js index f8b8b1e..8b5760a 100644 --- a/src/utils/utils.js +++ b/src/utils/utils.js @@ -116,7 +116,7 @@ export function randomEl(arr) { * @param {string} val - The value to check for a match. * @returns {number} 0 if no match, 1+ if there was a match (lower is better). */ -export function searchMatch(search, val) { +export function searchScore(search, val) { if (search.length === 0) return 0; search = search.normalize('NFKD').toLowerCase();