Last active
May 23, 2018 09:04
-
-
Save Alger23/5dc4ae2a312ce89e38ced0734d13e1a5 to your computer and use it in GitHub Desktop.
ng2 AutocompletePipe
This file contains hidden or 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 { 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