Mars Lan b19015f45e
Advanced search autocomplete (#1443)
MP=wherehows-frontend
TO_COMMIT=067b514ddae957b14c007c62cba7124e5ee8af01
FROM_COMMIT=a7a6163cf41ee86be2dd903bfc618c71f1b92cfc
2018-10-12 10:42:18 -07:00

16 lines
525 B
TypeScript

import { CompiledRules } from 'nearley';
import nearley from 'nearley';
import { Parser } from 'wherehows-web/typings/modules/nearley';
/**
* We will preprocess the string to remove extraspaces that will slow down the parser.
* @param text
* @param grammar
*/
export const parse = (text: string, grammar: CompiledRules): Parser => {
const noExtraSpaces = text.replace(/[\s]+/gi, ' ').trim();
const parser = new nearley.Parser(nearley.Grammar.fromCompiled(grammar));
parser.feed(noExtraSpaces);
return parser;
};