Repository navigation
Expand file tree
/
Copy pathutils.ts
More file actions
121 lines (104 loc) · 3.5 KB
/
Copy pathutils.ts
File metadata and controls
121 lines (104 loc) · 3.5 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import { Subscription, Scheduler, queueScheduler as queue } from 'rxjs';
import { DataSnapshot, AFUnwrappedDataSnapshot, PathReference, DatabaseReference } from './interfaces';
import { FirebaseDatabase } from '@angular/fire';
const REGEX_ABSOLUTE_URL = /^[a-z]+:\/\/.*/;
export function isNil(obj: any): boolean {
return obj === undefined || obj === null;
}
export function hasKey(obj: Object, key: string): boolean {
return obj && obj[key] !== undefined;
}
export function isString(value: any): boolean {
return typeof value === 'string';
}
export function isFirebaseRef(value: any): boolean {
return typeof value.set === 'function';
}
export function isFirebaseDataSnapshot(value: any): boolean {
return typeof value.exportVal === 'function';
}
export function isAFUnwrappedSnapshot(value: any): boolean {
return typeof value.$key === 'string';
}
export function isFirebaseQuery(value: any): boolean {
return typeof value.orderByChild === 'function';
}
export function isEmptyObject(obj: Object): boolean {
if (isNil(obj)) { return false; }
return Object.keys(obj).length === 0 && JSON.stringify(obj) === JSON.stringify({});
}
export interface CheckUrlRef {
isUrl: () => any;
isRef: () => any;
isQuery?: () => any;
}
/**
* Unwraps the data returned in the DataSnapshot. Exposes the DataSnapshot key and exists methods through the $key and $exists properties respectively. If the value is primitive, it is unwrapped using a $value property. The $ properies mean they cannot be saved in the Database as those characters are invalid.
* @param {DataSnapshot} snapshot - The snapshot to unwrap
* @return AFUnwrappedDataSnapshot
* @example
* unwrapMapFn(snapshot) => { name: 'David', $key: 'david', $exists: Function }
*/
export function unwrapMapFn (snapshot:DataSnapshot): AFUnwrappedDataSnapshot {
var unwrapped = !isNil(snapshot.val()) ? snapshot.val() : { $value: null };
if ((/string|number|boolean/).test(typeof unwrapped)) {
unwrapped = {
$value: unwrapped
};
}
Object.defineProperty(unwrapped, '$key', {
value: snapshot.ref.key,
enumerable: false
});
Object.defineProperty(unwrapped, '$exists', {
value: () => {
return snapshot.exists();
},
enumerable: false
});
return unwrapped;
}
export function stripTrailingSlash(value: string): string {
// Is the last char a /
if (value.substring(value.length - 1, value.length) === '/') {
return value.substring(0, value.length - 1);
} else {
return value;
}
}
function getAbsUrl(root: string, url:string) {
if (!(/^[a-z]+:\/\/.*/.test(url))) {
// Provided url is relative.
// Strip any leading slash
url = root + '/' + stripLeadingSlash(url);
}
return url;
}
export function stripLeadingSlash(value: string): string {
// Is the last char a /
if (value.substring(0, 1) === '/') {
return value.substring(1, value.length);
} else {
return value;
}
}
export function isAbsoluteUrl(url: string) {
return REGEX_ABSOLUTE_URL.test(url);
}
/**
* 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(database: FirebaseDatabase, pathRef: PathReference): DatabaseReference {
// if a db ref was passed in, just return it
if(isFirebaseRef(pathRef)) {
return pathRef as DatabaseReference;
}
const path = pathRef as string;
if(isAbsoluteUrl(<string>pathRef)) {
return database.refFromURL(path);
}
return database.ref(path);
}