diff --git a/src/photo/db/index.ts b/src/photo/db/index.ts index f15d954c..a73a9591 100644 --- a/src/photo/db/index.ts +++ b/src/photo/db/index.ts @@ -7,13 +7,17 @@ import { Lens } from '@/lens'; export const GENERATE_STATIC_PARAMS_LIMIT = 1000; export const PHOTO_DEFAULT_LIMIT = 100; -// Trim whitespace -// Make lowercase -// Remove commas, slashes -// Replace spaces with dashes +const DB_PARAMETERIZE_REPLACEMENTS = [ + [',', ''], + ['/', ''], + ['+', '-'], + [' ', '-'], +]; + const parameterizeForDb = (field: string) => - // eslint-disable-next-line max-len - `REPLACE(REPLACE(REPLACE(LOWER(TRIM(${field})), ',', ''), '/', ''), ' ', '-')`; + DB_PARAMETERIZE_REPLACEMENTS.reduce((acc, [from, to]) => + `REPLACE(${acc}, '${from}', '${to}')` + , `LOWER(TRIM(${field}))`); export type GetPhotosOptions = { sortBy?: 'createdAt' | 'createdAtAsc' | 'takenAt' | 'priority' diff --git a/src/utility/string.ts b/src/utility/string.ts index b733b6c2..71b3b633 100644 --- a/src/utility/string.ts +++ b/src/utility/string.ts @@ -22,11 +22,11 @@ export const parameterize = ( ) => string .trim() - // Replaces spaces, underscores, slashes,and dashes with dashes - .replaceAll(/[\s_–—]/gi, '-') - // Removes punctuation - .replaceAll(/['"!@#$%^&*()_+=[\]{};:/?,<>\\/|`~]/gi, '') - // Removes all non-alphanumeric characters + // Replace spaces, underscores, slashes, pluses, dashes with dashes + .replaceAll(/[\s_–—+]/gi, '-') + // Remove punctuation + .replaceAll(/['"!@#$%^&*()=[\]{};:/?,<>\\/|`~]/gi, '') + // Removes non-alphanumeric characters, if configured .replaceAll( shouldRemoveNonAlphanumeric ? /([^a-z0-9-])/gi