import { AutoCompleteService } from 'ionic2-auto-complete'; import { HttpClient} from '@angular/common/http'; import { Injectable } from '@angular/core'; import { Reference } from '../libs/Reference'; @Injectable() export class SearchAutoCompleteService implements AutoCompleteService { public words: string[] = []; constructor(private http: HttpClient) { let self = this; // getSearchRefs takes a url and uses ajax to retrieve the references and returns an array of references. this.http.get('data/index/word_to_stem_idx.json', { responseType: 'json' }).subscribe(data => { // find the right word for (let i of data as WordToStem[]) { self.words.push(i.w); } }); } getResults(keyword: string) { let qry = keyword; let prefix = ''; let idx = qry.lastIndexOf(';'); let words = []; if (idx > -1) { qry = keyword.substr(idx + 1).trim(); prefix = keyword.substr(0, idx).trim() + '; '; } if (qry.search(/[0-9]/i) === -1) { // its a word for (let item of Reference.Books) { if ( item.name !== 'Unknown' && (item.name.toLowerCase().indexOf(qry.toLowerCase()) > -1 || item.short_name.toLowerCase().indexOf(qry.toLowerCase()) > -1) ) { words.push(prefix + item.name); if (words.length > 2) { break; } } } for (let item of this.words) { if (item.toLowerCase().indexOf(qry.toLowerCase()) > -1) { words.push(prefix + item); if (words.length > 6) { return words; } } } } else if (qry.search(/(H|G)[0-9]/i) !== -1) { // its a strongs lookup if (qry.substr(0, 1).toUpperCase() === 'H') { let num = parseInt(qry.substr(1)); for (let x = num; x < num + 10 && x < 8675; x++) { words.push('H' + x) } return words; } if (qry.substr(0, 1).toUpperCase() === 'G') { let num = parseInt(qry.substr(1)); for (let x = num; x < num + 10 && x < 5625; x++) { words.push('G' + x) } return words; } } return words; } } type WordToStem = { w: string, s: string }