Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
45 lines (39 loc) · 1.52 KB
/
Copy pathutils.ts
File metadata and controls
45 lines (39 loc) · 1.52 KB
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
import * as firebase from 'firebase/app';
import { Subscription } from 'rxjs/Subscription';
import { Scheduler } from 'rxjs/Scheduler';
import { queue } from 'rxjs/scheduler/queue';
import { PathReference, DatabaseReference, FirebaseOperation, FirebaseOperationCases } from './interfaces';
import { FirebaseApp } from 'angularfire2';
export function isString(value: any): boolean {
return typeof value === 'string';
}
export function isFirebaseDataSnapshot(value: any): boolean {
return typeof value.exportVal === 'function';
}
export function isNil(obj: any): boolean {
return obj === undefined || obj === null;
}
export function isFirebaseRef(value: any): boolean {
return typeof value.set === 'function';
}
/**
* Returns a database reference given a Firebase App and an
* absolute or relative path.
* @param app - Firebase App
* @param path - Database path, relative or absolute
*/
export function getRef(app: FirebaseApp, pathRef: PathReference): DatabaseReference {
// if a db ref was passed in, just return it
return isFirebaseRef(pathRef) ? pathRef as DatabaseReference
: app.database().ref(pathRef as string);
}
export function checkOperationCases(item: FirebaseOperation, cases: FirebaseOperationCases) : Promise<void | any> {
if (isString(item)) {
return cases.stringCase();
} else if (isFirebaseRef(item)) {
return cases.firebaseCase!();
} else if (isFirebaseDataSnapshot(item)) {
return cases.snapshotCase!();
}
throw new Error(`Expects a string, snapshot, or reference. Got: ${typeof item}`);
}