diff --git a/packages/cli/src/getGitCommitHash.ts b/packages/cli/src/getGitCommitHash.ts index 3353d60..53c7d09 100644 --- a/packages/cli/src/getGitCommitHash.ts +++ b/packages/cli/src/getGitCommitHash.ts @@ -4,7 +4,7 @@ import { execSync } from 'node:child_process' * return the current commit hash * @returns The git commit hash */ -export function getGitCommitHash() { +export function getGitCommitHash(): string | undefined { try { const hash = execSync('git rev-parse --short HEAD').toString().replace('\n', '').trim() if (hash === 'undefined') diff --git a/packages/cli/src/getHostProjectPkgVersion.ts b/packages/cli/src/getHostProjectPkgVersion.ts index c094469..c3488a5 100644 --- a/packages/cli/src/getHostProjectPkgVersion.ts +++ b/packages/cli/src/getHostProjectPkgVersion.ts @@ -2,6 +2,6 @@ * It returns the version of the host project's package.json file * @returns version */ -export function getHostProjectPkgVersion() { +export function getHostProjectPkgVersion(): string { return process.env.npm_package_version as string } diff --git a/packages/cli/src/isDirector.ts b/packages/cli/src/isDirector.ts index c2cf9f9..20af3e6 100644 --- a/packages/cli/src/isDirector.ts +++ b/packages/cli/src/isDirector.ts @@ -1,6 +1,6 @@ import fs from 'node:fs/promises' -export async function isDirector(path: string) { +export async function isDirector(path: string): Promise { try { const stat = await fs.stat(path) return stat.isDirectory() diff --git a/packages/cli/src/isFile.ts b/packages/cli/src/isFile.ts index 403c1b8..a0d981f 100644 --- a/packages/cli/src/isFile.ts +++ b/packages/cli/src/isFile.ts @@ -5,7 +5,7 @@ import fs from 'node:fs/promises' * @param {string} path - The path to the file to check. * @returns A boolean value. */ -export async function isFile(path: string) { +export async function isFile(path: string): Promise { try { const stat = await fs.stat(path) return stat.isFile() diff --git a/packages/cli/src/pathExists.ts b/packages/cli/src/pathExists.ts index 5e19ce9..4f05297 100644 --- a/packages/cli/src/pathExists.ts +++ b/packages/cli/src/pathExists.ts @@ -5,7 +5,7 @@ import { access } from 'node:fs/promises' * @param {string} path - The path to check. * @returns A promise that resolves to a boolean. */ -export async function pathExists(path: string) { +export async function pathExists(path: string): Promise { try { await access(path) return true diff --git a/packages/core/src/callLimit.ts b/packages/core/src/callLimit.ts index 5f155e6..2d38ae1 100644 --- a/packages/core/src/callLimit.ts +++ b/packages/core/src/callLimit.ts @@ -18,7 +18,7 @@ import { isFunction, isNumber } from '@utopia-utils/share' * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/callLimit.ts */ -export function callLimit any>(fn: T, limit = 1) { +export function callLimit any>(fn: T, limit = 1): (this: ThisParameterType, ...args: Parameters) => ReturnType { if (!isFunction(fn)) throw new TypeError('fn expected a function') diff --git a/packages/core/src/capitalize.ts b/packages/core/src/capitalize.ts index 3aa7ed5..598081d 100644 --- a/packages/core/src/capitalize.ts +++ b/packages/core/src/capitalize.ts @@ -4,6 +4,6 @@ * @returns {string} - The capitalized string. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/capitalize.ts */ -export function capitalize(str: T) { +export function capitalize(str: T): Capitalize { return str.charAt(0).toUpperCase() + str.slice(1) as Capitalize } diff --git a/packages/core/src/createEnumFromOptions.ts b/packages/core/src/createEnumFromOptions.ts index 2d1cee0..2602efe 100644 --- a/packages/core/src/createEnumFromOptions.ts +++ b/packages/core/src/createEnumFromOptions.ts @@ -31,7 +31,7 @@ * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/createEnumFromOptions.ts */ -export function createEnumFromOptions(options: T) { +export function createEnumFromOptions(options: T): MapTuple { const res: Record = {} try { options.forEach((v) => { diff --git a/packages/core/src/csv.ts b/packages/core/src/csv.ts index 73bd988..09d3f15 100644 --- a/packages/core/src/csv.ts +++ b/packages/core/src/csv.ts @@ -1,6 +1,6 @@ import { isArray, isPlainObject } from '@utopia-utils/share' -function escapeDoubleQuotes(val: string) { +function escapeDoubleQuotes(val: string): string { return val.replace(/"/g, '""') } @@ -8,7 +8,7 @@ function escapeDoubleQuotes(val: string) { * check separator is valid * @param {string} separator */ -function checkSeparator(separator: string) { +function checkSeparator(separator: string): void { if (!separator) throw new Error('The separator cannot be empty') @@ -68,7 +68,7 @@ interface ArrayToCSVOptions { * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/csv.ts */ -export function arrayToCSV(arr: T[], options: ArrayToCSVOptions = {}) { +export function arrayToCSV(arr: T[], options: ArrayToCSVOptions = {}): string { const { headers, separator = ',', getRow, withPrefix } = options checkSeparator(separator) diff --git a/packages/core/src/deepClone.ts b/packages/core/src/deepClone.ts index 3d6aa63..2e1998c 100644 --- a/packages/core/src/deepClone.ts +++ b/packages/core/src/deepClone.ts @@ -1,4 +1,4 @@ -function clone(src: T, seen = new Map()) { +function clone(src: T, seen = new Map()): T { // Immutable things - null, undefined, functions, symbols, etc. if (!src || typeof src !== 'object') return src diff --git a/packages/core/src/defineDictionary.ts b/packages/core/src/defineDictionary.ts index 5c85b84..0bef117 100644 --- a/packages/core/src/defineDictionary.ts +++ b/packages/core/src/defineDictionary.ts @@ -107,7 +107,16 @@ type ToValueKey = T extends readonly [infer A, ...infer B] * @example * ```ts // at src/constant.ts - const { get_MUSIC_TYPE_KEYS, get_MUSIC_TYPE_KV, get_MUSIC_TYPE_MAP, get_MUSIC_TYPE_MAP_BY_KEY, get_MUSIC_TYPE_MAP_BY_VALUE, get_MUSIC_TYPE_OPTIONS, get_MUSIC_TYPE_VALUES, get_MUSIC_TYPE_VK } = defineDictionary([ + const { + get_MUSIC_TYPE_KEYS, + get_MUSIC_TYPE_KV, + get_MUSIC_TYPE_MAP, + get_MUSIC_TYPE_MAP_BY_KEY, + get_MUSIC_TYPE_MAP_BY_VALUE, + get_MUSIC_TYPE_OPTIONS, + get_MUSIC_TYPE_VALUES, + get_MUSIC_TYPE_VK + } = defineDictionary([ { key: 'POP', value: 1, diff --git a/packages/core/src/encryptPhone.ts b/packages/core/src/encryptPhone.ts index f0201ee..4704f90 100644 --- a/packages/core/src/encryptPhone.ts +++ b/packages/core/src/encryptPhone.ts @@ -4,6 +4,6 @@ * @returns The phone number with the middle 4 digits replaced with asterisks. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/encryptPhone.ts */ -export function encryptPhone(phone: `${number}`) { +export function encryptPhone(phone: `${number}`): string { return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2') } diff --git a/packages/core/src/escapeStringRegexp.ts b/packages/core/src/escapeStringRegexp.ts index 8ad6e66..9b43f04 100644 --- a/packages/core/src/escapeStringRegexp.ts +++ b/packages/core/src/escapeStringRegexp.ts @@ -5,5 +5,5 @@ * @param [string] - The string to escape. Defaults to an empty string. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/escapeStringRegexp.ts */ -export const escapeStringRegexp = (string = '') => +export const escapeStringRegexp = (string = ''): string => string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d') diff --git a/packages/core/src/formatNumberThousand.ts b/packages/core/src/formatNumberThousand.ts index 13be27b..99db3cb 100644 --- a/packages/core/src/formatNumberThousand.ts +++ b/packages/core/src/formatNumberThousand.ts @@ -19,7 +19,7 @@ interface Options { * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/formatNumberThousand.ts */ -export function formatNumberThousand(num: number, options: Options = {}) { +export function formatNumberThousand(num: number, options: Options = {}): string { if (Number.isNaN(num)) return '' diff --git a/packages/core/src/getFileName.ts b/packages/core/src/getFileName.ts index ac339bf..347cca9 100644 --- a/packages/core/src/getFileName.ts +++ b/packages/core/src/getFileName.ts @@ -6,6 +6,6 @@ * @returns file name * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/getFileName.ts */ -export function getFileName(path: string) { +export function getFileName(path: string): string { return path.replace(/^.*(\\|\/)/, '') } diff --git a/packages/core/src/getQueryParams.ts b/packages/core/src/getQueryParams.ts index 8872f92..df8d4f1 100644 --- a/packages/core/src/getQueryParams.ts +++ b/packages/core/src/getQueryParams.ts @@ -19,7 +19,7 @@ import type { LocationQuery } from './parseQuery' * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/getQueryParams.ts */ -export function getQueryParams(location: string) { +export function getQueryParams(location: string): Partial { let query: LocationQuery = {} const searchPos = location.indexOf('?') diff --git a/packages/core/src/objectKeys.ts b/packages/core/src/objectKeys.ts index 5f23b71..4d98e25 100644 --- a/packages/core/src/objectKeys.ts +++ b/packages/core/src/objectKeys.ts @@ -1,10 +1,11 @@ +type ObjectKeysReturn = (keyof T & (string | number | boolean | null | undefined))[] /** * strict type Object.keys() * @param {T} obj - T * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/objectKeys.ts */ -export function objectKeys(obj: T) { - return Object.keys(obj) as (keyof T & (string | number | boolean | null | undefined))[] +export function objectKeys(obj: T): ObjectKeysReturn { + return Object.keys(obj) as ObjectKeysReturn } // const symbol = Symbol('symbol') diff --git a/packages/core/src/onWindowFocus.ts b/packages/core/src/onWindowFocus.ts index 53e6a72..a2aa000 100644 --- a/packages/core/src/onWindowFocus.ts +++ b/packages/core/src/onWindowFocus.ts @@ -15,7 +15,7 @@ import { debounce } from './vendor' * ``` * */ -export function onWindowFocus(callback: (...args: any[]) => any) { +export function onWindowFocus(callback: (...args: any[]) => any): () => void { const listener = debounce(100, callback) window.addEventListener('focus', listener, false) diff --git a/packages/core/src/once.ts b/packages/core/src/once.ts index 14af1b0..bda7536 100644 --- a/packages/core/src/once.ts +++ b/packages/core/src/once.ts @@ -15,6 +15,6 @@ import { callLimit } from './callLimit' * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/once.ts */ -export function once any>(fn: T) { +export function once any>(fn: T): (this: ThisParameterType, ...args: Parameters) => ReturnType { return callLimit(fn, 1) } diff --git a/packages/core/src/onlyResolvesLast.ts b/packages/core/src/onlyResolvesLast.ts index fb73092..59104ea 100644 --- a/packages/core/src/onlyResolvesLast.ts +++ b/packages/core/src/onlyResolvesLast.ts @@ -12,7 +12,7 @@ * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/onlyResolvesLast.ts */ -export function onlyResolvesLast Promise>(fn: T) { +export function onlyResolvesLast Promise>(fn: T): (this: ThisParameterType, ...args: Parameters) => ReturnType { let time = 0 function wrappedFn(this: ThisParameterType, ...args: Parameters): ReturnType { const currentTime = time + 1 diff --git a/packages/core/src/parseQuery.ts b/packages/core/src/parseQuery.ts index c33c565..a19e4f7 100644 --- a/packages/core/src/parseQuery.ts +++ b/packages/core/src/parseQuery.ts @@ -45,7 +45,7 @@ export function decode(text: string | number): string { expect(params.sex).toBe(null) ``` */ -export function parseQuery(search: string) { +export function parseQuery(search: string): T { const query: LocationQuery = {} // avoid creating an object with an empty key and empty value // because of split('&') diff --git a/packages/core/src/randomInt.ts b/packages/core/src/randomInt.ts index 2139842..a86ed4b 100644 --- a/packages/core/src/randomInt.ts +++ b/packages/core/src/randomInt.ts @@ -5,6 +5,6 @@ * @returns A random integer between min and max. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/randomInt.ts */ -export function randomInt(max: number, min = 0) { +export function randomInt(max: number, min = 0): number { return Math.floor(Math.random() * (max - min + 1) + min) } diff --git a/packages/core/src/randomString.ts b/packages/core/src/randomString.ts index 9adcbae..96d9a11 100644 --- a/packages/core/src/randomString.ts +++ b/packages/core/src/randomString.ts @@ -6,7 +6,7 @@ * @returns A random string of the length specified. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/core/src/randomString.ts */ -export const randomString = (length: number, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789') => { +export const randomString = (length: number, chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'): string => { const maxPos = chars.length let str = '' for (let i = 0; i < length; i++) diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts deleted file mode 100644 index e69de29..0000000 diff --git a/packages/dom/src/canUseDom.ts b/packages/dom/src/canUseDom.ts index c6b214c..a02de61 100644 --- a/packages/dom/src/canUseDom.ts +++ b/packages/dom/src/canUseDom.ts @@ -3,6 +3,6 @@ * @returns a boolean value indicating whether the DOM (Document Object Model) can be used. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/canUseDom.ts */ -export function canUseDom() { +export function canUseDom(): boolean { return !!(typeof window !== 'undefined' && window.document && window.document.createElement) } diff --git a/packages/dom/src/domContains.ts b/packages/dom/src/domContains.ts index 2c057d8..af20074 100644 --- a/packages/dom/src/domContains.ts +++ b/packages/dom/src/domContains.ts @@ -9,7 +9,7 @@ * otherwise. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/contains.ts */ -export function domContains(root: Node | null | undefined, n: Node | null) { +export function domContains(root: Node | null | undefined, n: Node | null): boolean { if (!root) return false diff --git a/packages/dom/src/dynamicCSS.ts b/packages/dom/src/dynamicCSS.ts index 0699b90..6a175a9 100644 --- a/packages/dom/src/dynamicCSS.ts +++ b/packages/dom/src/dynamicCSS.ts @@ -23,7 +23,7 @@ interface Options { mark?: string } -function getMark({ mark }: Options = {}) { +function getMark({ mark }: Options = {}): string { if (mark) return mark.startsWith('data-') ? mark : `data-${mark}` @@ -48,13 +48,13 @@ function getOrder(prepend?: Prepend): AppendType { /** * Find style which inject by rc-util */ -function findStyles(container: ContainerType) { +function findStyles(container: ContainerType): HTMLStyleElement[] { return Array.from( (containerCache.get(container) || container).children, ).filter(node => node.tagName === 'STYLE') as HTMLStyleElement[] } -export function injectCSS(css: string, option: Options = {}) { +export function injectCSS(css: string, option: Options = {}): HTMLStyleElement | null { if (!canUseDom()) return null @@ -111,7 +111,7 @@ export function injectCSS(css: string, option: Options = {}) { return styleNode } -function findExistNode(key: string, option: Options = {}) { +function findExistNode(key: string, option: Options = {}): HTMLStyleElement | undefined { const container = getContainer(option) return findStyles(container).find( @@ -119,7 +119,7 @@ function findExistNode(key: string, option: Options = {}) { ) } -export function removeCSS(key: string, option: Options = {}) { +export function removeCSS(key: string, option: Options = {}): void { const existNode = findExistNode(key, option) if (existNode) { const container = getContainer(option) @@ -130,7 +130,7 @@ export function removeCSS(key: string, option: Options = {}) { /** * qiankun will inject `appendChild` to insert into other */ -function syncRealContainer(container: ContainerType, option: Options) { +function syncRealContainer(container: ContainerType, option: Options): void { const cachedRealContainer = containerCache.get(container) // Find real container when not cached or cached container removed @@ -149,7 +149,7 @@ function syncRealContainer(container: ContainerType, option: Options) { /** * manually clear container cache to avoid global cache in unit testes */ -export function clearContainerCache() { +export function clearContainerCache(): void { containerCache.clear() } @@ -166,7 +166,7 @@ export function clearContainerCache() { * updateCSS('body { color: red }', 'my-style') * ``` * */ -export function updateCSS(css: string, key: string, option: Options = {}) { +export function updateCSS(css: string, key: string, option: Options = {}): HTMLStyleElement | undefined { const container = getContainer(option) // Sync real parent diff --git a/packages/dom/src/isAndroid.ts b/packages/dom/src/isAndroid.ts index b2c5aec..739d0ec 100644 --- a/packages/dom/src/isAndroid.ts +++ b/packages/dom/src/isAndroid.ts @@ -3,7 +3,7 @@ * @returns A boolean value. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isAndroid.ts */ -export function isAndroid() { +export function isAndroid(): boolean { if (typeof navigator === 'undefined') return false return /android/i.test(navigator.userAgent) diff --git a/packages/dom/src/isIOS.ts b/packages/dom/src/isIOS.ts index eaf3bc1..4f3c9b5 100644 --- a/packages/dom/src/isIOS.ts +++ b/packages/dom/src/isIOS.ts @@ -3,7 +3,7 @@ * @returns A boolean value. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isIOS.ts */ -export function isIOS() { +export function isIOS(): boolean { if (typeof navigator === 'undefined') return false return /iPad|iPhone|iPod/.test(navigator.userAgent) diff --git a/packages/dom/src/isMobile.ts b/packages/dom/src/isMobile.ts index 67f43ff..b396c20 100644 --- a/packages/dom/src/isMobile.ts +++ b/packages/dom/src/isMobile.ts @@ -3,7 +3,7 @@ * @returns A boolean value. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isMobile.ts */ -export function isMobile() { +export function isMobile(): boolean { if (typeof navigator === 'undefined' || typeof window === 'undefined') return false diff --git a/packages/dom/src/isWeixin.ts b/packages/dom/src/isWeixin.ts index 7677799..cc9a75b 100644 --- a/packages/dom/src/isWeixin.ts +++ b/packages/dom/src/isWeixin.ts @@ -3,7 +3,7 @@ * @returns A boolean value. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/isWeixin.ts */ -export function isWeixin() { +export function isWeixin(): boolean { const ua = navigator.userAgent.toLowerCase() return /MicroMessenger/i.test(ua) } diff --git a/packages/dom/src/loadCSS.ts b/packages/dom/src/loadCSS.ts index 11726dd..eba59a9 100644 --- a/packages/dom/src/loadCSS.ts +++ b/packages/dom/src/loadCSS.ts @@ -1,3 +1,6 @@ +/** + * Options for {@link loadCSS} function + */ interface LoadCSSOptions { /** * Media query for styles to apply @@ -11,6 +14,14 @@ interface LoadCSSOptions { attrs?: Record } +/** + * return type of {@link loadCSS} function + */ +interface LoadCSSReturn { + unload: () => void + linkTag: HTMLLinkElement +} + /** * It loads a CSS file into the page * @param {string} path - the path to the CSS file @@ -20,7 +31,7 @@ interface LoadCSSOptions { * - linkTag: the link tag that was created * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/loadCSS.ts */ -export function loadCSS(path: string, options?: LoadCSSOptions) { +export function loadCSS(path: string, options?: LoadCSSOptions): LoadCSSReturn { const { attrs = {}, media, @@ -48,7 +59,7 @@ export function loadCSS(path: string, options?: LoadCSSOptions) { } /** remove the script tag */ -function unload(path: string) { +function unload(path: string): void { const linkEl = document.querySelector(`link[href="${path}"]`) if (linkEl) linkEl.remove() diff --git a/packages/dom/src/loadScript.ts b/packages/dom/src/loadScript.ts index fc9dd55..4c62bc4 100644 --- a/packages/dom/src/loadScript.ts +++ b/packages/dom/src/loadScript.ts @@ -1,3 +1,6 @@ +/** + * options type for {@link loadScript} function + */ interface LoadScriptOptions { /** * Add `async` attribute to the script tag @@ -34,6 +37,14 @@ interface LoadScriptOptions { onStatusChange?: (status: 'loading' | 'loaded' | 'error') => void } +/** + * return type of {@link loadScript} function + */ +interface LoadScriptReturn { + unload: () => void + scriptTag: HTMLScriptElement +} + /** * It loads a script tag into the DOM * @param {string} src - The URL of the script to load. @@ -43,7 +54,7 @@ interface LoadScriptOptions { * - scriptTag: the script tag that was created * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/loadScript.ts */ -export function loadScript(src: string, options?: LoadScriptOptions) { +export function loadScript(src: string, options?: LoadScriptOptions): LoadScriptReturn { const { async = true, defer, @@ -100,7 +111,7 @@ export function loadScript(src: string, options?: LoadScriptOptions) { } /** remove the script tag */ -function unload(src: string) { +function unload(src: string): void { const script = document.querySelector(`script[src="${src}"]`) if (script) script.remove() diff --git a/packages/dom/src/panzoom/utils.ts b/packages/dom/src/panzoom/utils.ts index dfd90b3..c090368 100644 --- a/packages/dom/src/panzoom/utils.ts +++ b/packages/dom/src/panzoom/utils.ts @@ -1,12 +1,12 @@ import type { Transform } from './type' -export function getScaleMultiplier(delta: number, zoomFactor: number) { +export function getScaleMultiplier(delta: number, zoomFactor: number): number { const sign = Math.sign(delta) const deltaAdjustedSpeed = Math.min(0.25, Math.abs(zoomFactor * delta / 128)) return 1 - sign * deltaAdjustedSpeed } -export function applyTransform(dom: HTMLElement, transform: Transform, cb?: (val: Transform) => void) { +export function applyTransform(dom: HTMLElement, transform: Transform, cb?: (val: Transform) => void): void { dom.style.transformOrigin = '0 0 0' dom.style.transform = `matrix(${transform.scale}, 0, 0, ${transform.scale}, ${transform.x}, ${transform.y})` cb?.(transform) diff --git a/packages/dom/src/setCssVar.ts b/packages/dom/src/setCssVar.ts index 5e1e3d3..6e9fd0d 100644 --- a/packages/dom/src/setCssVar.ts +++ b/packages/dom/src/setCssVar.ts @@ -10,7 +10,7 @@ import { isPlainObject } from '@utopia-utils/share' * refers to the root element of the current document. * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/dom/src/setCssVar.ts */ -export function setCssVar(variables: Record, root = window?.document?.documentElement) { +export function setCssVar(variables: Record, root = window?.document?.documentElement): void { if (variables && isPlainObject(variables) && root) { Object.keys(variables).forEach((key) => { root.style.setProperty(key, variables[key]) diff --git a/packages/dom/src/style.ts b/packages/dom/src/style.ts index 7942667..35246ab 100644 --- a/packages/dom/src/style.ts +++ b/packages/dom/src/style.ts @@ -13,13 +13,13 @@ export const hasClass = (el: Element, cls: string): boolean => { return el.classList.contains(cls) } -export const addClass = (el: Element, cls: string) => { +export const addClass = (el: Element, cls: string): void => { if (!el || !cls.trim()) return el.classList.add(...classNameToArray(cls)) } -export const removeClass = (el: Element, cls: string) => { +export const removeClass = (el: Element, cls: string): void => { if (!el || !cls.trim()) return el.classList.remove(...classNameToArray(cls)) diff --git a/packages/dom/src/waitForSelector.ts b/packages/dom/src/waitForSelector.ts index beeaa97..ebbbeb7 100644 --- a/packages/dom/src/waitForSelector.ts +++ b/packages/dom/src/waitForSelector.ts @@ -1,3 +1,6 @@ +/** + * Options for {@link waitForSelector} + */ interface WaitForOptions { /** Maximum time to wait in milliseconds, default is 30000 (30s) */ timeoutMillisecond?: number diff --git a/packages/dom/src/webp.ts b/packages/dom/src/webp.ts index 20f30f6..9a56cd6 100644 --- a/packages/dom/src/webp.ts +++ b/packages/dom/src/webp.ts @@ -4,7 +4,7 @@ * @returns A promise that resolves to a boolean indicating whether the feature is supported. * @link Official way by Google: https://developers.google.com/speed/webp/faq?hl=zh-cn#how_can_i_detect_browser_support_for_webp */ -export async function checkWebpFeature(feature: 'lossy' | 'lossless' | 'alpha' | 'animation' = 'lossy') { +export async function checkWebpFeature(feature: 'lossy' | 'lossless' | 'alpha' | 'animation' = 'lossy'): Promise { return new Promise((resolve) => { const kTestImages = { lossy: 'UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA', @@ -28,6 +28,6 @@ export async function checkWebpFeature(feature: 'lossy' | 'lossless' | 'alpha' | * Checks the browser's support for WebP image format. * @returns A promise that resolves to a boolean indicating whether WebP is supported. */ -export async function checkWebpSupport() { +export async function checkWebpSupport(): Promise { return checkWebpFeature('lossy') } diff --git a/packages/share/src/is.ts b/packages/share/src/is.ts index d5bbc76..3fcdd01 100644 --- a/packages/share/src/is.ts +++ b/packages/share/src/is.ts @@ -31,7 +31,7 @@ export const isObject = (val: unknown): val is Record => export const isWindow = (val: unknown): boolean => typeof window !== 'undefined' && toTypeString(val) === 'Window' -export const isIntegerKey = (key: unknown) => +export const isIntegerKey = (key: unknown): boolean => isString(key) && key !== 'NaN' && key[0] !== '-' diff --git a/packages/share/src/isNumberLike.ts b/packages/share/src/isNumberLike.ts index 0e97182..b61020f 100644 --- a/packages/share/src/isNumberLike.ts +++ b/packages/share/src/isNumberLike.ts @@ -9,7 +9,7 @@ * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isNumberLike.ts */ -export function isNumberLike(val: unknown) { +export function isNumberLike(val: unknown): boolean { return (typeof val === 'number' && !Number.isNaN(val)) || (typeof val === 'string' && val.trim() !== '' && !Number.isNaN(Number(val))) } diff --git a/packages/share/src/isValidUrl.ts b/packages/share/src/isValidUrl.ts index 8860101..2794047 100644 --- a/packages/share/src/isValidUrl.ts +++ b/packages/share/src/isValidUrl.ts @@ -12,7 +12,7 @@ * ``` * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/share/src/isValidUrl.ts */ -export function isValidUrl(urlString: string) { +export function isValidUrl(urlString: string): boolean { try { return Boolean(new URL(urlString)) } diff --git a/packages/share/src/measurePerformance.ts b/packages/share/src/measurePerformance.ts index 8188e7f..3cbe7dd 100644 --- a/packages/share/src/measurePerformance.ts +++ b/packages/share/src/measurePerformance.ts @@ -1,5 +1,5 @@ /* eslint-disable no-console */ -export function measurePerformance any>(this: ThisParameterType, fn: T, ...args: Parameters) { +export function measurePerformance any>(this: ThisParameterType, fn: T, ...args: Parameters): number { const start = performance.now() fn.call(this, ...args) const end = performance.now() diff --git a/packages/tree/src/breadthFirstTraverse.ts b/packages/tree/src/breadthFirstTraverse.ts index a195014..ca189a0 100644 --- a/packages/tree/src/breadthFirstTraverse.ts +++ b/packages/tree/src/breadthFirstTraverse.ts @@ -34,7 +34,7 @@ interface Options { // output 'a', 'c', 'b' * ``` */ -export function breadthFirstTraverse(tree: TreeNode[] | TreeNode, action: (node: TreeNode) => unknown, options?: Options) { +export function breadthFirstTraverse(tree: TreeNode[] | TreeNode, action: (node: TreeNode) => unknown, options?: Options): void { if (!isFunction(action)) throw new Error('traverse action should be a function') diff --git a/packages/tree/src/buildTreeFromList.ts b/packages/tree/src/buildTreeFromList.ts index b926f64..a03b792 100644 --- a/packages/tree/src/buildTreeFromList.ts +++ b/packages/tree/src/buildTreeFromList.ts @@ -43,7 +43,7 @@ interface Options { }) * ``` */ -export function buildTreeFromList(list: any[], options?: Options) { +export function buildTreeFromList(list: any[], options?: Options): TreeNode[] { const { listFieldNames, treeFieldNames } = options || {} const { id: listIdField, parentId: listParentIdField } = { ...DEFAULT_FIELD_NAMES, ...listFieldNames } const { id: treeIdField, parentId: treeParentIdField, children: treeChildrenField } = { ...DEFAULT_FIELD_NAMES, ...treeFieldNames } diff --git a/packages/tree/src/deepFirstTraverse.ts b/packages/tree/src/deepFirstTraverse.ts index e93c45f..55bf215 100644 --- a/packages/tree/src/deepFirstTraverse.ts +++ b/packages/tree/src/deepFirstTraverse.ts @@ -40,7 +40,7 @@ interface TraverseAction { * // output 'a', 'b', 'c' * ``` */ -export function deepFirstTraverse(tree: TreeNode | TreeNode[], action: TraverseAction, options?: Options) { +export function deepFirstTraverse(tree: TreeNode | TreeNode[], action: TraverseAction, options?: Options): void { if (!isFunction(action)) throw new Error('traverse action should be a function') diff --git a/packages/tree/src/flattenTree.ts b/packages/tree/src/flattenTree.ts index 0408ae3..9fb3ef3 100644 --- a/packages/tree/src/flattenTree.ts +++ b/packages/tree/src/flattenTree.ts @@ -46,7 +46,7 @@ interface Options { // ] * ``` */ -export function flattenTree(tree: TreeNode | TreeNode[], options?: Options) { +export function flattenTree(tree: TreeNode | TreeNode[], options?: Options): TreeNode[] { const { fieldNames, onEachTraverse } = options || {} const { children } = { ...DEFAULT_FIELD_NAMES, ...fieldNames } diff --git a/packages/tree/src/treeFilterNode.ts b/packages/tree/src/treeFilterNode.ts index 06ba55b..20d0c17 100644 --- a/packages/tree/src/treeFilterNode.ts +++ b/packages/tree/src/treeFilterNode.ts @@ -17,7 +17,7 @@ interface Options { * @returns A filtered tree * @linkcode https://github.com/GreatAuk/utopia-utils/blob/main/packages/tree/src/treeFilterNode.ts */ -export function treeFilterNode(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options) { +export function treeFilterNode(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options): TreeNode[] { const { fieldNames, onEachTraverse } = options || {} const { children } = { ...DEFAULT_FIELD_NAMES, ...fieldNames } const _tree = Array.isArray(tree) ? [...tree] : [tree] diff --git a/packages/tree/src/treeFindNode.ts b/packages/tree/src/treeFindNode.ts index 2147515..3019bcc 100644 --- a/packages/tree/src/treeFindNode.ts +++ b/packages/tree/src/treeFindNode.ts @@ -31,7 +31,7 @@ interface Options { const res = treeFindNode(tree, node => node.name === 'b') // res is [{ name: 'b' }] * ``` */ -export function treeFindNode(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options) { +export function treeFindNode(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options): TreeNode[] { const { isFindAll, fieldNames, onEachTraverse } = options || {} if (!isFunction(predicate)) throw new Error('predicate should be a function') diff --git a/packages/tree/src/treeFindPath.ts b/packages/tree/src/treeFindPath.ts index 6f0d6c9..ecd3566 100644 --- a/packages/tree/src/treeFindPath.ts +++ b/packages/tree/src/treeFindPath.ts @@ -31,7 +31,7 @@ interface Options { console.log(path?.map(v => v.name)) // ['a', 'b'] * ``` */ -export function treeFindPath(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options) { +export function treeFindPath(tree: TreeNode[] | TreeNode, predicate: (node: TreeNode) => boolean, options?: Options): TreeNode[] | null { const { fieldNames } = options || {} const { children } = { ...DEFAULT_FIELD_NAMES, ...fieldNames }