Skip to content

Commit 820afb7

Browse files
committed
feat(FirebaseListFactory): add $key and $value field to unwrapped snapshots
Closes angular#78
1 parent fdc3b4f commit 820afb7

File tree

3 files changed

+48
-4
lines changed

3 files changed

+48
-4
lines changed

src/utils/firebase_list_factory.spec.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ import {
44
onChildAdded,
55
onChildChanged,
66
onChildRemoved,
7-
onChildUpdated
7+
onChildUpdated,
8+
unwrapMapFn
89
} from './firebase_list_factory';
910
import {FirebaseListObservable} from './firebase_list_observable';
1011
import {
@@ -183,6 +184,37 @@ describe('FirebaseListFactory', () => {
183184
onChildRemoved([val1, val2, val3], val2)
184185
).toEqual([val1, val3]);
185186
});
187+
});
188+
189+
190+
describe('unwrapMapFn', () => {
191+
var val = {unwrapped: true};
192+
var snapshot = {
193+
key: () => 'key',
194+
val: () => val
195+
};
196+
197+
it('should return an object value with a $key property', () => {
198+
expect(unwrapMapFn(snapshot as FirebaseDataSnapshot)).toEqual({
199+
$key: 'key',
200+
unwrapped: true
201+
});
202+
});
186203

204+
205+
it('should return an object value with a $value property if value is scalar', () => {
206+
expect(unwrapMapFn(Object.assign(snapshot, {val: () => 5}) as FirebaseDataSnapshot)).toEqual({
207+
$key: 'key',
208+
$value: 5
209+
});
210+
expect(unwrapMapFn(Object.assign(snapshot, {val: () => false}) as FirebaseDataSnapshot)).toEqual({
211+
$key: 'key',
212+
$value: false
213+
});
214+
expect(unwrapMapFn(Object.assign(snapshot, {val: () => 'lol'}) as FirebaseDataSnapshot)).toEqual({
215+
$key: 'key',
216+
$value: 'lol'
217+
});
218+
});
187219
});
188220
});

src/utils/firebase_list_factory.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {FirebaseListObservable} from './firebase_list_observable';
1+
import {FirebaseListObservable, AFUnwrappedDataSnapshot} from './firebase_list_observable';
22
import {Observer} from 'rxjs/Observer';
33
import * as Firebase from 'firebase';
44

@@ -49,8 +49,15 @@ export interface FirebaseListFactoryOpts {
4949
preserveSnapshot?: boolean;
5050
}
5151

52-
function unwrapMapFn (snapshot:FirebaseDataSnapshot): any {
53-
return snapshot.val();
52+
export function unwrapMapFn (snapshot:FirebaseDataSnapshot): AFUnwrappedDataSnapshot {
53+
var unwrapped = snapshot.val();
54+
if ((/string|number|boolean/).test(typeof unwrapped)) {
55+
unwrapped = {
56+
$value: unwrapped
57+
};
58+
}
59+
unwrapped.$key = snapshot.key();
60+
return unwrapped;
5461
}
5562

5663
export function onChildAdded(arr:any[], child:any, prevKey:string): any[] {

src/utils/firebase_list_observable.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ export class FirebaseListObservable<T> extends Observable<T> {
2323
this._ref.push(val);
2424
}
2525
}
26+
27+
export interface AFUnwrappedDataSnapshot {
28+
$key: string;
29+
$value?: string | number | boolean;
30+
}

0 commit comments

Comments
 (0)