Skip to content

Instantly share code, notes, and snippets.

@knshiro
Created October 1, 2020 10:12
Show Gist options
  • Save knshiro/175bd5c2051de3d746babf9a92ba092d to your computer and use it in GitHub Desktop.
Save knshiro/175bd5c2051de3d746babf9a92ba092d to your computer and use it in GitHub Desktop.
Firebase prefixed case sensitive query
import type firebase from "firebase";
function findUpperBound(s: string): string {
if (!s.length) {
return s;
}
const c = String.fromCharCode(s.charCodeAt(s.length - 1) + 1);
return s.substring(0, s.length - 1) + c;
}
export function queryByPrefix(
query: firebase.firestore.Query<firebase.firestore.DocumentData>,
field: string,
prefix: string
): firebase.firestore.Query<firebase.firestore.DocumentData> {
return query
.where(field, ">=", prefix)
.where(field, "<", findUpperBound(prefix))
.orderBy(field, "asc");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment