support for case insensitive search

main
Inga 🏳‍🌈 12 months ago
parent 4b9d59ba89
commit e078010afe
  1. 12
      src/app.module.spec.ts
  2. 13
      src/services/omdbEnrichedDataService.spec.ts
  3. 19
      src/services/omdbEnrichedDataService.ts
  4. 24
      src/utils/stringHelpers.spec.ts
  5. 8
      src/utils/stringHelpers.ts

@ -90,4 +90,16 @@ describe('AppController (e2e)', () => {
expect(response.body).toMatchObject([]); expect(response.body).toMatchObject([]);
}); });
}); });
it('/api/movies?writers=gEoRgE lUcAs (GET)', () => {
return request(app.getHttpServer())
.get('/api/movies?writers=gEoRgE lUcAs')
.expect(200)
.expect((response) => {
expect(response.body).toMatchObject([
{ title: 'Indiana Jones and the Last Crusade' },
{ title: 'Star Wars: Episode IV - A New Hope' },
]);
});
});
}); });

@ -304,4 +304,17 @@ describe('createOmdbEnrichedDataService', () => {
expect(result).toMatchObject([]); expect(result).toMatchObject([]);
}); });
it('returns correct search results for writer = gEoRgE lUcAs and actor = hArRiSoN fOrD', async () => {
const service = await servicePromise;
const result = service.getSearchResults({
actors: 'hArRiSoN fOrD',
writers: 'gEoRgE lUcAs',
});
expect(result).toMatchObject([
{ title: 'Indiana Jones and the Last Crusade' },
{ title: 'Star Wars: Episode IV - A New Hope' },
]);
});
}); });

@ -5,6 +5,7 @@ import type {
OmdbProvider, OmdbProvider,
SearchFilters, SearchFilters,
} from '../types'; } from '../types';
import { areStringsEqual } from '../utils/stringHelpers';
export const createOmdbEnrichedDataService = async ( export const createOmdbEnrichedDataService = async (
internalProvider: InternalProvider, internalProvider: InternalProvider,
@ -48,11 +49,27 @@ export const createOmdbEnrichedDataService = async (
const metadataValue = const metadataValue =
metadata[fieldName as keyof SearchFilters]; metadata[fieldName as keyof SearchFilters];
if (Array.isArray(metadataValue)) { if (Array.isArray(metadataValue)) {
return metadataValue.some((singularValue) => {
if (typeof singularValue === 'string') {
return areStringsEqual(
singularValue,
// filterValue here is filters[fieldName], and is not undefined (because it comes from nonEmptyFilters), // filterValue here is filters[fieldName], and is not undefined (because it comes from nonEmptyFilters),
// so it's guaranteed to have a matching type because of SearchFilters / GenericSearchFilters definition, // so it's guaranteed to have a matching type because of SearchFilters / GenericSearchFilters definition,
// but this logic is too complicated for TS to prove it automatically. // but this logic is too complicated for TS to prove it automatically.
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument // eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
return metadataValue.includes(filterValue as any); filterValue as any,
);
} else {
return singularValue === filterValue;
}
});
} else if (typeof metadataValue === 'string') {
return areStringsEqual(
metadataValue,
// see comment above
// eslint-disable-next-line @typescript-eslint/no-explicit-any, @typescript-eslint/no-unsafe-argument
filterValue as any,
);
} else { } else {
return metadataValue === filterValue; return metadataValue === filterValue;
} }

@ -0,0 +1,24 @@
import { describe, it, expect } from '@jest/globals';
import { areStringsEqual } from './stringHelpers';
describe('areStringsEqual', () => {
it('returns true for identical strings', () => {
expect(areStringsEqual('abc', 'abc')).toBe(true);
});
it('returns false for different strings', () => {
expect(areStringsEqual('abc', 'def')).toBe(false);
});
it('returns true for strings with different casing', () => {
expect(areStringsEqual('abc', 'ABC')).toBe(true);
});
it('returns true for strings with different variants', () => {
expect(areStringsEqual('Nürnberg', 'Nurnberg')).toBe(true);
});
it('returns true for strings with different casing (turkish I)', () => {
expect(areStringsEqual('İSMİNİZ.GIF', 'isminiz.gif')).toBe(true);
});
});

@ -0,0 +1,8 @@
export const areStringsEqual = (a: string, b: string) => {
return (
a.localeCompare(b, undefined, {
usage: 'search',
sensitivity: 'base',
}) === 0
);
};
Loading…
Cancel
Save