Skip to content

Instantly share code, notes, and snippets.

@nclarx
Last active August 13, 2017 05:19
Show Gist options
  • Save nclarx/ca0a3c766146e0d38c54b7a623fb99a1 to your computer and use it in GitHub Desktop.
Save nclarx/ca0a3c766146e0d38c54b7a623fb99a1 to your computer and use it in GitHub Desktop.
Filter/Sort Pipe for History Week 2017 App
import {Pipe, PipeTransform} from '@angular/core';
import {IEvent} from './event';
import * as moment from 'moment/moment';
export const EventFilter: (value: IEvent[], region: string, dayOfWeek: string, showFeaturedEvents: boolean) => IEvent[] = function (value: IEvent[], region: string, dayOfWeek: string, showFeaturedEvents: boolean) {
return value.filter(event => {
let today = moment();
let eventDate = moment(event.date, 'YYYY-MM-DD');
if (eventDate.isAfter(today)) {
if (region !== null) {
if (showFeaturedEvents === true) {
return event.featuredEvent === 'TRUE'
} else {
return event.region === region;
}
} else {
if(showFeaturedEvents === true){
return event.featuredEvent === 'TRUE'
} else {
return true;
}
}
}
});
};
const EventSort: (value: IEvent[], eventSort: keyof IEvent, sortDirection: boolean) => IEvent[] =
function (value: IEvent[], eventSort: keyof IEvent, sortDirection: boolean) {
return value.sort((a, b) => {
if (a[eventSort] < b[eventSort]) {
return sortDirection ? -1 : 1;
}
if (a[eventSort] > b[eventSort]) {
return sortDirection ? 1 : -1;
}
return 0;
});
};
@Pipe({
name: 'EventsPipe'
})
export class EventsPipe implements PipeTransform {
transform(value: any,
regionSelected: string,
dayOfWeekSelected: string,
sortField: keyof IEvent,
sortDirection: boolean,
showFeaturedEvents: boolean): any {
if (!value) {
return value;
}
// console.log(regionSelected, dayOfWeekSelected, sortField, sortDirection, value);
value = EventFilter(value, regionSelected, dayOfWeekSelected, showFeaturedEvents);
value = EventSort(value, sortField, sortDirection);
return value;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment