I am trying to perform a search (from an text input) in my Firestore DB. However, I would like to manipulate the Firestore items my string is being tested against (ie. trim, normalize, convert to lower case, etc.). As an example, let's say we have the following custom validator :
export class CustomValidator {
static drink(afs: AngularFirestore) {
return (control: AbstractControl) => {
const stringToSearch = control.value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim().toLocaleLowerCase();
return afs.collection('MyFirestoreCollection', ref => ref.where('drink', '==', stringToSearch)).valueChanges().pipe(
debounceTime(500), // wait 500ms delay before querying the server to avoid useless querying
take(1),
map(arr => arr.length ? { drinkAlreadyExists: false } : null ),
);
};
}
}
Let's say I type the word "Café" in the search box, because of the following line of code, the string that will be used to search against the Firestore data will become "cafe".
const stringToSearch = control.value.normalize('NFD').replace(/[\u0300-\u036f]/g, '').trim().toLocaleLowerCase();
How can I perform the same string manipulations to the Firestore data that I am searching against (in other words, to my Firestore "drink" field) so that if I type the word "cafe" in the search box and that the entry in Firestore is "Café", a match will be found and so on and so forth?