Skip to content

Instantly share code, notes, and snippets.

@austbot
Created April 8, 2016 17:29
Show Gist options
  • Save austbot/039057ab7bb0b26eb499bb35b1799adb to your computer and use it in GitHub Desktop.
Save austbot/039057ab7bb0b26eb499bb35b1799adb to your computer and use it in GitHub Desktop.
Angular 2 NgFor over an object. Specifically a firebase list.
import {Pipe, PipeTransform} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import * as R from 'ramda';
@Pipe({
name: 'messages'
})
/**
* Take an object that is key value id message and convert it to a array of ChatMessage throwing the id in the message.
*/
export class MessagesPipe implements PipeTransform {
transform(input: any, args: any[] = []): any[] {
if (input) {
/**
* Compose a function by converting the object to a pairs array of [[key, value], ...]
* Then map over the array and take the head of each pair and throw it into the tail (value) as the id.
* Flatten the two dimensional array and return.
*/
return R.compose(R.flatten, R.map((x) => {
return R.assoc('id', R.head(x), R.last(x));
}), R.toPairs)(input);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment