Last active
September 23, 2019 16:05
-
-
Save jsettlem/4a2fc7f96a0b3e37c5482023de15c998 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {Component} from '@angular/core'; | |
import {filter} from 'rxjs/operators'; | |
interface ItemType { | |
[key: string]: string | Array<string>; | |
} | |
type filterType = ({ filterValue: string | string[] | null; filterTarget: string }); | |
@Component({ | |
selector: 'app-root', | |
templateUrl: './app.component.html', | |
styleUrls: ['./app.component.css'] | |
}) | |
export class AppComponent { | |
title = 'testing-testing'; | |
constructor() { | |
const items: ItemType[] = [ | |
{ | |
question: 'campells soup', | |
categories: 'bonnie, kyled, others', | |
other: 'my name is matthew' | |
}, | |
{ | |
question: 'bad soup', | |
categories: 'jessie, james, me', | |
other: 'my name is jaco' | |
}, | |
]; | |
const filters0: filterType[] = [ | |
{filterValue: null, filterTarget: 'question'}, | |
{filterValue: null, filterTarget: 'categories'}, | |
{filterValue: null, filterTarget: 'other'} | |
]; | |
const filters1: filterType[] = [ | |
{filterValue: 'camp', filterTarget: 'question'}, | |
{filterValue: ['second', 'filter'], filterTarget: 'categories'}, | |
{filterValue: null, filterTarget: 'other'}, | |
]; | |
const filters2: filterType[] = [ | |
{filterValue: 'bobbo', filterTarget: 'question'}, | |
{filterValue: ['jessie', 'buster'], filterTarget: 'categories'}, | |
{filterValue: 'nothing', filterTarget: 'other'}, | |
]; | |
const filters3: filterType[] = [ | |
{filterValue: 'bobbo', filterTarget: 'question'}, | |
{filterValue: null, filterTarget: 'categories'}, | |
{filterValue: ['matthew', 'jaco'], filterTarget: 'other'}, | |
]; | |
console.log(this.applyFilters(items, filters0)); | |
console.log(this.applyFilters(items, filters1)); | |
console.log(this.applyFilters(items, filters2)); | |
console.log(this.applyFilters(items, filters3)); | |
const bobbo = new Set([1, 2, 2, 3, 4]); | |
for (const i of bobbo.values()) { | |
console.log(i); | |
} | |
} | |
private applyFilters(items: ItemType[], filters: filterType[]): ItemType[] { | |
if (filters.every(f => f.filterValue === null)) { | |
return items; | |
} | |
return items.filter(item => | |
filters.some(({filterTarget, filterValue}) => { | |
if (filterValue === null) { | |
return false; | |
} else if (filterValue instanceof Array) { | |
return filterValue.some(filterString => match(item, filterTarget, filterString)); | |
} else { | |
return match(item, filterTarget, filterValue); | |
} | |
}) | |
); | |
function match(item: ItemType, filterTarget: string, filterValue: string): boolean { | |
const itemElement = item[filterTarget]; | |
if (itemElement instanceof Array) { | |
return itemElement.some(element => element.includes(filterValue)); | |
} else { | |
return itemElement.includes(filterValue); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment