diff --git a/src/photo/db/index.ts b/src/photo/db/index.ts index f160d076..7bfa9335 100644 --- a/src/photo/db/index.ts +++ b/src/photo/db/index.ts @@ -7,18 +7,17 @@ import { APP_DEFAULT_SORT_BY, SortBy } from '../sort'; export const GENERATE_STATIC_PARAMS_LIMIT = 1000; export const PHOTO_DEFAULT_LIMIT = 100; -const DB_PARAMETERIZE_REPLACEMENTS = [ - [',', ''], - ['/', ''], - ['+', '-'], - ['|', '-'], - [' ', '-'], -]; +const CHARACTERS_TO_REMOVE = [',', '/']; +const CHARACTERS_TO_REPLACE = ['+', '&', '|', ' ']; const parameterizeForDb = (field: string) => - DB_PARAMETERIZE_REPLACEMENTS.reduce((acc, [from, to]) => - `REPLACE(${acc}, '${from}', '${to}')` - , `LOWER(TRIM(${field}))`); + `REGEXP_REPLACE( + REGEXP_REPLACE( + LOWER(TRIM(${field})), + '[${CHARACTERS_TO_REMOVE.join('')}]', '', 'g' + ), + '[${CHARACTERS_TO_REPLACE.join('')}]', '-', 'g' + )`; export type PhotoQueryOptions = { sortBy?: SortBy diff --git a/src/utility/string.ts b/src/utility/string.ts index 99b49204..a552f7dc 100644 --- a/src/utility/string.ts +++ b/src/utility/string.ts @@ -23,9 +23,9 @@ export const parameterize = ( string .trim() // Replace spaces, underscores, slashes, pluses, pipes, dashes with dashes - .replaceAll(/[\s_–—+|]/gi, '-') + .replaceAll(/[\s_–—+&|]/gi, '-') // Remove punctuation - .replaceAll(/['"!@#$%^&*()=[\]{};:/?,<>\\/`~]/gi, '') + .replaceAll(/['"!@#$%^*()=[\]{};:/?,<>\\/`~]/gi, '') // Removes non-alphanumeric characters, if configured .replaceAll( shouldRemoveNonAlphanumeric