mirror of
https://github.com/YunoHost/yunohost-admin.git
synced 2024-09-03 20:06:15 +02:00
ts: add types to common helpers
This commit is contained in:
parent
83e09d6ae9
commit
016acc7b50
1 changed files with 71 additions and 34 deletions
|
@ -1,13 +1,19 @@
|
||||||
|
import type { UnwrapRef } from 'vue'
|
||||||
|
|
||||||
|
import type { Obj } from '@/types/commons'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Allow to set a timeout on a `Promise` expected response.
|
* Allow to set a timeout on a `Promise` expected response.
|
||||||
* The returned Promise will be rejected if the original Promise is not resolved or
|
* The returned Promise will be rejected if the original Promise is not resolved or
|
||||||
* rejected before the delay.
|
* rejected before the delay.
|
||||||
*
|
*
|
||||||
* @param {Promise} promise - A promise (like a fetch call).
|
* @param promise - A promise (like a fetch call).
|
||||||
* @param {Number} delay - delay after which the promise is rejected
|
* @param delay - delay after which the promise is rejected
|
||||||
* @return {Promise}
|
|
||||||
*/
|
*/
|
||||||
export function timeout(promise, delay) {
|
export function timeout<T extends unknown>(
|
||||||
|
promise: Promise<T>,
|
||||||
|
delay: number,
|
||||||
|
): Promise<T> {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
// FIXME reject(new Error('api_not_responding')) for post-install
|
// FIXME reject(new Error('api_not_responding')) for post-install
|
||||||
setTimeout(() => reject, delay)
|
setTimeout(() => reject, delay)
|
||||||
|
@ -18,10 +24,9 @@ export function timeout(promise, delay) {
|
||||||
/**
|
/**
|
||||||
* Check if passed value is an object literal.
|
* Check if passed value is an object literal.
|
||||||
*
|
*
|
||||||
* @param {*} value - Anything.
|
* @param value - Anything.
|
||||||
* @return {Boolean}
|
|
||||||
*/
|
*/
|
||||||
export function isObjectLiteral(value) {
|
export function isObjectLiteral(value: any): value is object {
|
||||||
return (
|
return (
|
||||||
value !== null &&
|
value !== null &&
|
||||||
value !== undefined &&
|
value !== undefined &&
|
||||||
|
@ -41,23 +46,52 @@ export function objectGet<
|
||||||
* Check if value is "empty" (`null`, `undefined`, `''`, `[]`, '{}').
|
* Check if value is "empty" (`null`, `undefined`, `''`, `[]`, '{}').
|
||||||
* Note: `0` is not considered "empty" in that helper.
|
* Note: `0` is not considered "empty" in that helper.
|
||||||
*
|
*
|
||||||
* @param {*} value - Anything.
|
* @param value - Anything.
|
||||||
* @return {Boolean}
|
|
||||||
*/
|
*/
|
||||||
export function isEmptyValue(value) {
|
export function isEmptyValue(
|
||||||
if (typeof value === 'number') return false
|
value: any,
|
||||||
return !value || value.length === 0 || Object.keys(value).length === 0
|
): value is null | undefined | '' | [] | {} {
|
||||||
|
if (typeof value === 'number' || typeof value === 'boolean') return false
|
||||||
|
return (
|
||||||
|
!value ||
|
||||||
|
(Array.isArray(value) && value.length === 0) ||
|
||||||
|
Object.keys(value).length === 0
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Flatten<T extends object> = object extends T
|
||||||
|
? object
|
||||||
|
: {
|
||||||
|
[K in keyof T]-?: (
|
||||||
|
x: NonNullable<T[K]> extends infer V
|
||||||
|
? V extends object
|
||||||
|
? V extends readonly any[]
|
||||||
|
? Pick<T, K>
|
||||||
|
: Flatten<V> extends infer FV
|
||||||
|
? {
|
||||||
|
[P in keyof FV as `${Extract<P, string | number>}`]: FV[P]
|
||||||
|
}
|
||||||
|
: never
|
||||||
|
: Pick<T, K>
|
||||||
|
: never,
|
||||||
|
) => void
|
||||||
|
} extends Record<keyof T, (y: infer O) => void>
|
||||||
|
? O extends infer U
|
||||||
|
? { [K in keyof O]: O[K] }
|
||||||
|
: never
|
||||||
|
: never
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an flattened object literal, with all keys at first level and removing nested ones.
|
* Returns an flattened object literal, with all keys at first level and removing nested ones.
|
||||||
*
|
*
|
||||||
* @param {Object} obj - An object literal to flatten.
|
* @param obj - An object literal to flatten.
|
||||||
* @param {Object} [flattened={}] - An object literal to add passed obj keys/values.
|
* @param flattened - An object literal to add passed obj keys/values.
|
||||||
* @return {Object}
|
|
||||||
*/
|
*/
|
||||||
export function flattenObjectLiteral(obj, flattened = {}) {
|
export function flattenObjectLiteral<T extends object>(
|
||||||
function flatten(objLit) {
|
obj: T,
|
||||||
|
flattened: Partial<Flatten<T>> = {},
|
||||||
|
) {
|
||||||
|
function flatten(objLit: Partial<Flatten<T>>) {
|
||||||
for (const key in objLit) {
|
for (const key in objLit) {
|
||||||
const value = objLit[key]
|
const value = objLit[key]
|
||||||
if (isObjectLiteral(value)) {
|
if (isObjectLiteral(value)) {
|
||||||
|
@ -68,18 +102,24 @@ export function flattenObjectLiteral(obj, flattened = {}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
flatten(obj)
|
flatten(obj)
|
||||||
return flattened
|
return flattened as Flatten<T>
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an new Object filtered with passed filter function.
|
* Returns an new Object filtered with passed filter function.
|
||||||
* Each entry `[key, value]` will be forwarded to the `filter` function.
|
* Each entry `[key, value]` will be forwarded to the `filter` function.
|
||||||
*
|
*
|
||||||
* @param {Object} obj - object to filter.
|
* @param obj - object to filter.
|
||||||
* @param {Function} filter - the filter function to call for each entry.
|
* @param filter - the filter function to call for each entry.
|
||||||
* @return {Object}
|
|
||||||
*/
|
*/
|
||||||
export function filterObject(obj, filter) {
|
export function filterObject<T extends Obj>(
|
||||||
|
obj: T,
|
||||||
|
filter: (
|
||||||
|
entries: [string, any],
|
||||||
|
index: number,
|
||||||
|
array: [string, any][],
|
||||||
|
) => boolean,
|
||||||
|
) {
|
||||||
return Object.fromEntries(
|
return Object.fromEntries(
|
||||||
Object.entries(obj).filter((...args) => filter(...args)),
|
Object.entries(obj).filter((...args) => filter(...args)),
|
||||||
)
|
)
|
||||||
|
@ -87,22 +127,20 @@ export function filterObject(obj, filter) {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an new array containing items that are in first array but not in the other.
|
* Returns an new array containing items that are in first array but not in the other.
|
||||||
*
|
|
||||||
* @param {Array} [arr1=[]]
|
|
||||||
* @param {Array} [arr2=[]]
|
|
||||||
* @return {Array}
|
|
||||||
*/
|
*/
|
||||||
export function arrayDiff(arr1 = [], arr2 = []) {
|
export function arrayDiff<T extends string>(
|
||||||
|
arr1: T[] = [],
|
||||||
|
arr2: T[] = [],
|
||||||
|
): T[] {
|
||||||
return arr1.filter((item) => !arr2.includes(item))
|
return arr1.filter((item) => !arr2.includes(item))
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns a new string with escaped HTML (`&<>"'` replaced by entities).
|
* Returns a new string with escaped HTML (`&<>"'` replaced by entities).
|
||||||
*
|
*
|
||||||
* @param {String} unsafe
|
* @param unsafe - string to escape
|
||||||
* @return {String}
|
|
||||||
*/
|
*/
|
||||||
export function escapeHtml(unsafe) {
|
export function escapeHtml(unsafe: string) {
|
||||||
return unsafe
|
return unsafe
|
||||||
.replace(/&/g, '&')
|
.replace(/&/g, '&')
|
||||||
.replace(/</g, '<')
|
.replace(/</g, '<')
|
||||||
|
@ -114,11 +152,10 @@ export function escapeHtml(unsafe) {
|
||||||
/**
|
/**
|
||||||
* Returns a random integer between `min` and `max`.
|
* Returns a random integer between `min` and `max`.
|
||||||
*
|
*
|
||||||
* @param {Number} min
|
* @param min - min possible value
|
||||||
* @param {Number} max
|
* @param max - max possible value
|
||||||
* @return {Number}
|
|
||||||
*/
|
*/
|
||||||
export function randint(min, max) {
|
export function randint(min: number, max: number) {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min
|
return Math.floor(Math.random() * (max - min + 1)) + min
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue