Skip to content

Instantly share code, notes, and snippets.

@Alger23
Last active May 23, 2018 09:04
Show Gist options
  • Save Alger23/5dc4ae2a312ce89e38ced0734d13e1a5 to your computer and use it in GitHub Desktop.
Save Alger23/5dc4ae2a312ce89e38ced0734d13e1a5 to your computer and use it in GitHub Desktop.
ng2 AutocompletePipe
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({ name: 'autocomplete' })
export class AutocompletePipe implements PipeTransform {
transform(value: string[], args: string[]): any {
debugger;
if (!value) return value;
let ws = value.sort();
let as = args[0].split('');
as.push("PAUSE");
let m = {}
, n = {}
, f = s => m[s] ? m[s] : m[s] = g(s[0] || '').filter(w => w.startsWith(s))
, g = c => n[c] ? n[c] : n[c] = ws.filter(w => w.startsWith(c))
, r = []
, s = ''
as.forEach(a => {
if (a === 'BACKSPACE') s = s.slice(0, -1)
else if (a === 'PAUSE') r.push(f(s))
else s += a
})
return r
}
autocomplete(ws, as) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment