@helpwave/hightide
Version:
helpwave's component and theming library
1 lines • 3.73 kB
Source Map (JSON)
{"version":3,"sources":["../../src/util/simpleSearch.ts"],"sourcesContent":["/**\n * Finds all values matching the search values by first mapping the values to a string array and then checking each entry for matches.\n * Returns the list of all matches.\n *\n * @param search The list of search strings e.g. `[name, type]`\n *\n * @param objects The list of objects to be searched in\n *\n * @param mapping The mapping of objects to the string properties they fulfil\n *\n * @return The list of objects that match all search strings\n */\nexport const MultiSubjectSearchWithMapping = <T>(search: string[], objects: T[], mapping: (value: T) => (string[] | undefined)) => {\n return objects.filter(object => {\n const mappedSearchKeywords = mapping(object)?.map(value => value.toLowerCase().trim())\n if(!mappedSearchKeywords) {\n return true\n }\n return search.every(searchValue => !!mappedSearchKeywords.find(value => !!value && value.includes(searchValue.toLowerCase().trim())))\n })\n}\n\n/**\n * Finds all values matching the search value by first mapping the values to a string array and then checking each entry for matches.\n * Returns the list of all matches.\n *\n * @param search The search string e.g `name`\n *\n * @param objects The list of objects to be searched in\n *\n * @param mapping The mapping of objects to the string properties they fulfil, if undefined it is always fulfilled\n *\n * @return The list of objects that match the search string\n */\nexport const MultiSearchWithMapping = <T>(search: string, objects: T[], mapping: (value: T) => (string[] | undefined)) => {\n return objects.filter(object => {\n const mappedSearchKeywords = mapping(object)?.map(value => value.toLowerCase().trim())\n if(!mappedSearchKeywords) {\n return true\n }\n return !!mappedSearchKeywords.find(value => value.includes(search.toLowerCase().trim()))\n })\n}\n\n/**\n * Finds all values matching the search value by first mapping the values to a string and returns the list of all matches.\n *\n * @param search The search string e.g `name`\n *\n * @param objects The list of objects to be searched in\n *\n * @param mapping The mapping of objects to a string that is compared to the search\n *\n * @return The list of objects that match the search string\n */\nexport const SimpleSearchWithMapping = <T>(search: string, objects: T[], mapping: (value: T) => string) => {\n return MultiSearchWithMapping(search, objects, value => [mapping(value)])\n}\n\n/**\n * Finds all values matching the search value and returns the list of all matches.\n *\n * @param search The search string e.g `name`\n *\n * @param objects The list of objects to be searched in\n *\n * @return The list of objects that match the search string\n */\nexport const SimpleSearch = (search: string, objects: string[]) => {\n return SimpleSearchWithMapping(search, objects, value => value)\n}\n"],"mappings":";AAYO,IAAM,gCAAgC,CAAI,QAAkB,SAAc,YAAkD;AACjI,SAAO,QAAQ,OAAO,YAAU;AAC9B,UAAM,uBAAuB,QAAQ,MAAM,GAAG,IAAI,WAAS,MAAM,YAAY,EAAE,KAAK,CAAC;AACrF,QAAG,CAAC,sBAAsB;AACxB,aAAO;AAAA,IACT;AACA,WAAO,OAAO,MAAM,iBAAe,CAAC,CAAC,qBAAqB,KAAK,WAAS,CAAC,CAAC,SAAS,MAAM,SAAS,YAAY,YAAY,EAAE,KAAK,CAAC,CAAC,CAAC;AAAA,EACtI,CAAC;AACH;AAcO,IAAM,yBAAyB,CAAI,QAAgB,SAAc,YAAkD;AACxH,SAAO,QAAQ,OAAO,YAAU;AAC9B,UAAM,uBAAuB,QAAQ,MAAM,GAAG,IAAI,WAAS,MAAM,YAAY,EAAE,KAAK,CAAC;AACrF,QAAG,CAAC,sBAAsB;AACxB,aAAO;AAAA,IACT;AACA,WAAO,CAAC,CAAC,qBAAqB,KAAK,WAAS,MAAM,SAAS,OAAO,YAAY,EAAE,KAAK,CAAC,CAAC;AAAA,EACzF,CAAC;AACH;AAaO,IAAM,0BAA0B,CAAI,QAAgB,SAAc,YAAkC;AACzG,SAAO,uBAAuB,QAAQ,SAAS,WAAS,CAAC,QAAQ,KAAK,CAAC,CAAC;AAC1E;AAWO,IAAM,eAAe,CAAC,QAAgB,YAAsB;AACjE,SAAO,wBAAwB,QAAQ,SAAS,WAAS,KAAK;AAChE;","names":[]}