import { fromLiteralExpression } from '../ast/ast' ; import { WildcardTypeBuildNode } from './types' ; import { KueryNode } from '..' ; export const wildcardSymbol = '@kuery-wildcard@' ; function escapeRegExp ( str ) { return str . replace ( /[.*+?^${}()|[\]\\]/g , '\\$&' ) ; } function escapeQueryString ( str ) { return str . replace ( /[+-=&|><!(){}[\]^"~*?:\\/]/g , '\\$&' ) ; } export function buildNode ( value ) { if ( ! value . includes ( wildcardSymbol ) ) { return fromLiteralExpression ( value ) ; } return { type : 'wildcard' , value , } ; } export function test ( node , str ) { const { value } = node ; const regex = value . split ( wildcardSymbol ) . map ( escapeRegExp ) . join ( '[\\s\\S]*' ) ; const regexp = new RegExp ( `^${ regex }$` ) ; return regexp . test ( str ) ; } export function toOpenSearchQuery ( node ) { const { value } = node ; return value . split ( wildcardSymbol ) . join ( '*' ) ; } export function toQueryStringQuery ( node ) { const { value } = node ; return value . split ( wildcardSymbol ) . map ( escapeQueryString ) . join ( '*' ) ; } export function hasLeadingWildcard ( node ) { const { value } = node ; return value . startsWith ( wildcardSymbol ) && value . replace ( wildcardSymbol , '' ) . length > 0 ; }