@netgrif/components-core
Version:
Netgrif Application engine frontend core Angular library
99 lines (98 loc) • 5.18 kB
TypeScript
import { EscapeResult } from '../escape-result';
import { Query } from '../query/query';
import { WrapResult } from '../wrap-result';
import { Operators } from './operators';
/**
* Represents the low level abstraction of query generation that is responsible for the creation of queries themselves.
*
* Operators are ment to be stateless and held as singleton instances, as they can be shared without any issues.
* This library uses the {@link OperatorService} to store the singleton instances, but you can use your own solution,
* or instantiate them multiple times if you prefer.
*
* @typeparam T type of arguments this Operator can generate queries from
*/
export declare abstract class Operator<T> {
/**
* Represents the placeholder "block" in operator display names.
*/
static readonly INPUT_PLACEHOLDER = "";
/**
* Reserved characters for Elasticsearch queries. These characters can be escaped with a `\` character.
*/
private static readonly ESCAPABLE_CHARACTERS;
/**
* Reserved characters for Elasticsearch queries. These characters cannot be escaped and must be removed from queries.
*/
private static readonly UNESCAPABLE_CHARACTERS;
/**
* Determines the arity of the operator, that is the number of arguments/operands it takes.
*/
private readonly _numberOfOperands;
/**
* The operator symbol that is used to generate the query.
*/
private readonly _operatorSymbols;
protected constructor(numberOfOperands: number, operatorSymbols?: string);
/**
* Escapes all escapable Elasticsearch symbols. Removes all unescapable Elasticsearch symbols.
*
* For a list of symbols see Elasticsearch's Query string query
* [doc]{@link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_reserved_characters}.
* @param input user input that should have special characters escaped
* @returns user input with the escapable characters escaped and the unescapable characters removed
*/
static escapeInput(input: string): EscapeResult;
/**
* Creates a Query string query string literal with the provided arguments.
* @param elasticKeyword Elasticsearch index keyword for the field you want to query
* @param arg The value that you want to query the property for
* @param operator The operator you want to use to query the indexed field. Consult the Elasticsearch's
* [documentation]{@link https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html}
* for more information.
* @returns combines the input strings by this pattern: `([elasticKeyword]:[operator][arg])`
*/
static query(elasticKeyword: string, arg: string, operator: string): string;
/**
* Applies the provided function to all keywords and combines the resulting queries with an `OR` operator.
* @param elasticKeywords keywords that the function is call on
* @param queryConstructor function that generates a `Query` object for each keyword
*/
static forEachKeyword(elasticKeywords: Array<string>, queryConstructor: (keyword: string) => Query): Query;
/**
* If the value contains a space character, or if `force` is set to `true`.
* @param input user input that should be wrapped with double quotes
* @param forceWrap if set to `true` the value will be wrapped regardless of it's content
*/
static wrapInputWithQuotes(input: string, forceWrap?: boolean): WrapResult;
/**
* @returns the arity of the operator.
*/
get numberOfOperands(): number;
/**
* Simple implementation of query generation. Will not be suitable for all Operator derivatives.
*
* Escapes the first argument from the `args` array, calls the [query()]{@link Operator#query} function for each `keyword` and combines
* the results with an `OR` operator.
* @returns query that wos constructed with the given arguments and keywords. Returns an empty query if no arguments are provided.
*/
createQuery(elasticKeywords: Array<string>, args: Array<T>, escapeArgs?: boolean): Query;
/**
* The name template is used when generating search GUI, and so the arity of the operator should match the number of
* {@link INPUT_PLACEHOLDER} constant occurrences in the returned array.
*
* @returns an array of translation paths that represent the operator name, as it should be displayed to the user.
* The {@link INPUT_PLACEHOLDER} constant (or any falsy value) can be used to place visual input placeholder blocks in the
* operator name where user input is expected.
*/
abstract getOperatorNameTemplate(): Array<string>;
/**
* @returns the operator class in a serializable form
*/
abstract serialize(): Operators | string;
/**
* Checks whether the provided array contains at leas as many arguments, as is the operators number of operands.
* Throws an error if not enough arguments is provided.
* @param args an array of potential operands
*/
protected checkArgumentsCount(args: Array<any>): void;
}