UNPKG

@pg-english/entity

Version:

Process text to entity tokens.

38 lines (34 loc) 1.13 kB
const token = require('@pg-english/token'); const T = token.type; const TYPE = new Map([ ['t', T.TABLE], ['c', T.COLUMN], ['r', T.ROW], ]); async function blockScan(txts, fn, ths=null) { for(var i=0, I=txts.length, z=[]; i<I;) { var ans = await fn.call(ths, txts.slice(i, I)); if(ans==null) { z.push(token(T.TEXT, txts[i++])); continue; } var typ = TYPE.get(ans.type[0].toLowerCase())||0; z.push(token(typ, ans.value, ans.hint||null)); i += ans.length; } return z; }; async function process(tkns, fn, ths=null) { var z = [], blk = [], txts = []; for(var tkn of tkns) { if((tkn.type&0xF0)===T.TEXT) { txts.push(tkn.value); continue; } if(txts.length>0) { blk.push(blockScan(txts, fn, ths)); txts = []; } blk.push([tkn]); } if(txts.length>0) blk.push(blockScan(txts, fn, ths)); var ans = await Promise.all(blk); for(var arr of ans) z.push.apply(z, arr); return z; }; async function entity(txt, fn, ths=null) { return token.toString(await process(token.parse(txt), fn, ths)); }; entity.process = process; module.exports = entity;