/
util.ts
64 lines (54 loc) · 1.57 KB
/
util.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { FirebaseError } from "../error";
interface IndexName {
projectId: string;
collectionGroupId: string;
indexId: string;
}
interface FieldName {
projectId: string;
collectionGroupId: string;
fieldPath: string;
}
// projects/$PROJECT_ID/databases/(default)/collectionGroups/$COLLECTION_GROUP_ID/indexes/$INDEX_ID
const INDEX_NAME_REGEX =
/projects\/([^\/]+?)\/databases\/\(default\)\/collectionGroups\/([^\/]+?)\/indexes\/([^\/]*)/;
// projects/$PROJECT_ID/databases/(default)/collectionGroups/$COLLECTION_GROUP_ID/fields/$FIELD_ID
const FIELD_NAME_REGEX =
/projects\/([^\/]+?)\/databases\/\(default\)\/collectionGroups\/([^\/]+?)\/fields\/([^\/]*)/;
/**
* Parse an Index name into useful pieces.
*/
export function parseIndexName(name?: string): IndexName {
if (!name) {
throw new FirebaseError(`Cannot parse undefined index name.`);
}
const m = name.match(INDEX_NAME_REGEX);
if (!m || m.length < 4) {
throw new FirebaseError(`Error parsing index name: ${name}`);
}
return {
projectId: m[1],
collectionGroupId: m[2],
indexId: m[3],
};
}
/**
* Parse an Field name into useful pieces.
*/
export function parseFieldName(name: string): FieldName {
const m = name.match(FIELD_NAME_REGEX);
if (!m || m.length < 4) {
throw new FirebaseError(`Error parsing field name: ${name}`);
}
return {
projectId: m[1],
collectionGroupId: m[2],
fieldPath: m[3],
};
}
/**
* Performs XOR operator between two boolean values
*/
export function booleanXOR(a: boolean, b: boolean): boolean {
return !!(Number(a) - Number(b));
}