Browse Source

fix(nc-gui): update searchCompare function to allow nested array search

pull/10016/head
Ramesh Mane 2 days ago
parent
commit
fdfcbb17da
  1. 3
      packages/nc-gui/lib/types.ts
  2. 26
      packages/nc-gui/utils/commonUtils.ts

3
packages/nc-gui/lib/types.ts

@ -306,6 +306,8 @@ interface ProductFeedItem {
type SordDirectionType = 'asc' | 'desc' | undefined type SordDirectionType = 'asc' | 'desc' | undefined
type NestedArray<T> = T | NestedArray<T>[]
export type { export type {
User, User,
ProjectMetaInfo, ProjectMetaInfo,
@ -341,4 +343,5 @@ export type {
SordDirectionType, SordDirectionType,
ProductFeedItem, ProductFeedItem,
Attachment, Attachment,
NestedArray,
} }

26
packages/nc-gui/utils/commonUtils.ts

@ -75,11 +75,12 @@ export const isUnicodeEmoji = (emoji: string) => {
/** /**
* Performs a case-insensitive search to check if the `query` exists within the `source`. * 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`. * - Handles strings, numbers, and arrays (including nested arrays) of strings/numbers.
* - If `source` is a string or number, it checks if the `query` exists within `source` (case-insensitively). * - Treats `undefined` as an empty string.
* - 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 source - The value to search within. Can be:
* - A string or number.
* - A single-level or nested array of strings/numbers.
* @param query - The value to search for. Treated as an empty string if `undefined`. * @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`. * @returns `true` if the `query` is found within the `source` (case-insensitively), otherwise `false`.
* *
@ -87,27 +88,24 @@ export const isUnicodeEmoji = (emoji: string) => {
* ```typescript * ```typescript
* // Single string or number search * // Single string or number search
* searchCompare("Hello World", "world"); // true * searchCompare("Hello World", "world"); // true
* searchCompare("OpenAI ChatGPT", "gpt"); // true * searchCompare(12345, "234"); // true
* searchCompare("TypeScript", "JavaScript"); // false
* *
* // Array search * // Array search
* searchCompare(["apple", "banana", "cherry"], "Banana"); // true * searchCompare(["apple", "banana", "cherry"], "Banana"); // true
* searchCompare([123, 456, 789], "456"); // true * searchCompare([123, 456, 789], "456"); // true
* searchCompare([null, undefined, "test"], "TEST"); // true *
* // Nested array search
* searchCompare(["apple", ["banana", ["cherry"]]], "cherry"); // true
* searchCompare([123, [456, [789]]], "456"); // true
* *
* // Handling undefined * // Handling undefined
* searchCompare(undefined, "test"); // false * searchCompare(undefined, "test"); // false
* searchCompare("test", undefined); // true * searchCompare("test", undefined); // true
* ``` * ```
*/ */
export const searchCompare = (source?: string | number | (string | number | undefined)[], query?: string): boolean => { export const searchCompare = (source?: NestedArray<string | number | undefined>, query?: string): boolean => {
if (ncIsArray(source)) { if (ncIsArray(source)) {
return source.some((item) => { return source.some((item) => searchCompare(item, query))
return (item || '')
.toString()
.toLowerCase()
.includes((query || '').toLowerCase())
})
} }
return (source || '') return (source || '')

Loading…
Cancel
Save