import { Token, TokenGroup } from "../types/Token"; type VocabGroup = { [section: string]: { [type: string]: string[] } } export const unimportantWords = [ 'what', 'when', 'where', 'how', 'is', 'a', 'the', 'it' ] const vocabulary: {[group: string]: VocabGroup} = { [TokenGroup.CSS]: { initiator: { style: [ 'style', 'visual', 'visually' ] }, font: { bold: [ 'bold' ], fontSize: [ 'size' ] }, subProperty: { top: [ 'top' ], bottom: [ 'bottom' ], left: [ 'left' ], right: [ 'right' ] }, member: { background: [ 'background' ], color: ['color', 'colored' ], padding: [ 'padding', 'padded', 'pad' ], margin: [ 'margin', 'spacing', 'spaced' ], border: [ 'border' ], float: [ 'float', 'floating', 'floated' ] } }, [TokenGroup.HTML]: { initiator: { div: [ 'box', 'div' ], p: [ 'text', 'paragraph', 'p' ], button: [ 'button' ], input: [ 'input', 'text box' ], label: [ 'label' ], list: [ 'list', 'collection' ] }, member: { }, event: { click: [ 'click', 'clicks', 'clicked', 'clicking' ], hover: [ 'hover', 'hovers', 'hovered' ] } }, [TokenGroup.Script]: { initiator: { if: [ 'if' ], does: [ 'does', 'performs' ] }, method: { alert: [ 'alert', 'alerts' ], prompt: [ 'ask', 'asks', 'prompt', 'prompts' ], store: [ 'store', 'stores', 'save', 'saves' ], ajax: [ 'request', 'requests', 'get', 'gets', 'post', 'posts' ], return: [ 'return', 'gives back', 'provides' ], }, value: { false: [ 'false' ], true: [ 'true' ] } }, [TokenGroup.Generic]: { reference: { calling: [ 'calling', 'calls', 'invoke', 'invokes' ], with: [ 'with', 'using', 'uses' ], from: [ 'from' ], to: [ 'to' ], alias: [ 'as', 'named' ] }, compare: { equal: [ 'equal', 'is', 'equals' ], lessThan: [ 'less than', 'fewer than', '<' ], greaterThan: [ 'greater than', 'more than', '>' ] }, operation: { contains: [ 'contains', 'has' ], startsWith: [ 'starts with', 'begins with' ], endsWith: [ 'ends with' ], empty: [ 'empty' ] }, logic: { or: [ 'or' ], and: [ 'and' ], not: [ 'not', 'isn\'t', 'isnt' ], }, hint: { length: [ 'characters', 'items', 'elements' ] } } } export function findRoot( query: string ): Token | null { for( const kind of Object.keys( vocabulary ) ) { const result = findInGroup( kind, vocabulary[kind], query ); if(!result) continue; return result; } return null; } export function findGroupType( tokens: Token[] | Token[][] ): string | null { const startLine: Token | Token[] = tokens[0]; const startTokens: Token[] = isArray( startLine ) ? startLine as Token[] : [startLine as Token]; const initiators: Token[] = startTokens .map( (token: Token) => findRoot( token.key ) ) .filter( (token: Token | null) => !!token && token.group !== TokenGroup.Custom ) as Token[]; if( initiators.length === 0 ) return null; const initiator = initiators.find( (x: Token) => x.section === 'initiator' ); return initiator?.group || null; } function findInGroup(groupName: string, group: VocabGroup, element: string): Token | null { for( const section of Object.keys( group ) ) { for( const key of Object.keys( group[section] ) ) { if( group[section][key].some( (x: string) => x === element ) ) { return { group: groupName as any, section, key, original: element }; } } } return null; } function isArray( val: any ): boolean { return typeof(val['length']) !== 'undefined'; }