mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
3.7 KiB
117 lines
3.7 KiB
export const modalSizes = { |
|
xs: { |
|
width: 'min(calc(100vw - 32px), 448px)', |
|
height: 'min(90vh, 448px)', |
|
}, |
|
sm: { |
|
width: 'min(calc(100vw - 32px), 640px)', |
|
height: 'min(90vh, 424px)', |
|
}, |
|
md: { |
|
width: 'min(80vw, 900px)', |
|
height: 'min(90vh, 540px)', |
|
}, |
|
lg: { |
|
width: 'min(80vw, 1280px)', |
|
height: 'min(90vh, 864px)', |
|
}, |
|
} |
|
|
|
/** |
|
* Creates a promise that resolves after a specified delay. |
|
* |
|
* @param ms - The delay in milliseconds. |
|
* @returns A promise that resolves after the specified delay. |
|
* |
|
* @example |
|
* ```ts |
|
* // Wait for 2 seconds |
|
* await delay(2000); |
|
* console.log('2 seconds have passed'); |
|
* ``` |
|
*/ |
|
export const ncDelay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)) |
|
|
|
/** |
|
* Generates an array of a given length with content generated by the provided callback function. |
|
* |
|
* @param length - The length of the array to be created. |
|
* @param contentCallback - Optional function to generate content for each index. Defaults to using the index as content. |
|
* @returns The generated array with content. |
|
* |
|
* @example |
|
* // Generate an array of length 5 with default content |
|
* const array = ncArrayFrom(5); |
|
* console.log(array); // ['Item 1', 'Item 2', 'Item 3', 'Item 4', 'Item 5'] |
|
* |
|
* @example |
|
* // Generate an array of length 3 with custom content |
|
* const customArray = ncArrayFrom(3, (i) => `Custom Content ${i}`); |
|
* console.log(customArray); // ['Custom Content 0', 'Custom Content 1', 'Custom Content 2'] |
|
*/ |
|
export const ncArrayFrom = <T>( |
|
length: number, |
|
contentCallback: (i: number) => T = (i) => `Item ${i + 1}` as unknown as T, |
|
): T[] => { |
|
return Array.from({ length }, (_, i) => contentCallback(i)) |
|
} |
|
|
|
/** |
|
* Checks if a string contains Unicode emojis. |
|
* |
|
* @param emoji - The string to check. |
|
* @returns A boolean indicating if the string contains Unicode emojis. |
|
* |
|
* @example |
|
* ```ts |
|
* const hasEmoji = isUnicodeEmoji('Hello World 😊'); |
|
* console.log(hasEmoji); // Output: true |
|
* ``` |
|
*/ |
|
export const isUnicodeEmoji = (emoji: string) => { |
|
return !!emoji?.match(/(\p{Emoji}|\p{Extended_Pictographic})/gu) |
|
} |
|
|
|
/** |
|
* Performs a case-insensitive search to check if the `query` exists within the `source`. |
|
* |
|
* - If `source` is an array, the function checks if any element (converted to a string) contains the `query`. |
|
* - If `source` is a string or number, it checks if the `query` exists within `source` (case-insensitively). |
|
* - If `source` or `query` is `undefined`, they are treated as empty strings. |
|
* |
|
* @param source - The value to search within. Can be a string, number, or an array of strings/numbers. |
|
* @param query - The value to search for. Treated as an empty string if `undefined`. |
|
* @returns `true` if the `query` is found within the `source` (case-insensitively), otherwise `false`. |
|
* |
|
* @example |
|
* ```typescript |
|
* // Single string or number search |
|
* searchCompare("Hello World", "world"); // true |
|
* searchCompare("OpenAI ChatGPT", "gpt"); // true |
|
* searchCompare("TypeScript", "JavaScript"); // false |
|
* |
|
* // Array search |
|
* searchCompare(["apple", "banana", "cherry"], "Banana"); // true |
|
* searchCompare([123, 456, 789], "456"); // true |
|
* searchCompare([null, undefined, "test"], "TEST"); // true |
|
* |
|
* // Handling undefined |
|
* searchCompare(undefined, "test"); // false |
|
* searchCompare("test", undefined); // true |
|
* ``` |
|
*/ |
|
export const searchCompare = (source?: string | number | (string | number | undefined)[], query?: string): boolean => { |
|
if (ncIsArray(source)) { |
|
return source.some((item) => { |
|
return (item || '') |
|
.toString() |
|
.toLowerCase() |
|
.includes((query || '').toLowerCase()) |
|
}) |
|
} |
|
|
|
return (source || '') |
|
.toString() |
|
.toLowerCase() |
|
.includes((query || '').toLowerCase()) |
|
}
|
|
|