@spectric/ui 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (88) hide show
  1. package/.gitlab-ci.yml +28 -0
  2. package/.nvmrc +1 -0
  3. package/.storybook/analyze.sh +4 -0
  4. package/.storybook/main.ts +55 -0
  5. package/.storybook/preview.ts +42 -0
  6. package/.vscode/extensions.json +5 -0
  7. package/.vscode/settings.json +41 -0
  8. package/README.MD +50 -0
  9. package/html-include.png +0 -0
  10. package/package.json +33 -0
  11. package/src/classes/BitArray.ts +48 -0
  12. package/src/classes/DisposibleElement.ts +108 -0
  13. package/src/components/Banner.ts +102 -0
  14. package/src/components/Bitdisplay.ts +383 -0
  15. package/src/components/Button.ts +121 -0
  16. package/src/components/Header.ts +125 -0
  17. package/src/components/Page.ts +157 -0
  18. package/src/components/Panel.ts +56 -0
  19. package/src/components/ThemeProvider.ts +251 -0
  20. package/src/components/button.css.ts +160 -0
  21. package/src/components/configurations/classifications.ts +194 -0
  22. package/src/components/dialog/dialog.css.ts +50 -0
  23. package/src/components/dialog/dialog.ts +163 -0
  24. package/src/components/dialog/index.ts +1 -0
  25. package/src/components/header.css.ts +38 -0
  26. package/src/components/index.ts +10 -0
  27. package/src/components/input.css +75 -0
  28. package/src/components/input.ts +312 -0
  29. package/src/components/page.css.ts +158 -0
  30. package/src/components/panel.css.ts +44 -0
  31. package/src/components/query_bar/QueryBar.css +48 -0
  32. package/src/components/query_bar/QueryBar.ts +378 -0
  33. package/src/components/query_bar/index.ts +2 -0
  34. package/src/components/query_bar/querylanguage/kuery/ast/_generated_/kuery.js +3186 -0
  35. package/src/components/query_bar/querylanguage/kuery/ast/ast.ts +113 -0
  36. package/src/components/query_bar/querylanguage/kuery/ast/index.ts +31 -0
  37. package/src/components/query_bar/querylanguage/kuery/ast/kuery.peg +417 -0
  38. package/src/components/query_bar/querylanguage/kuery/functions/and.ts +55 -0
  39. package/src/components/query_bar/querylanguage/kuery/functions/exists.ts +62 -0
  40. package/src/components/query_bar/querylanguage/kuery/functions/index.ts +47 -0
  41. package/src/components/query_bar/querylanguage/kuery/functions/is.ts +211 -0
  42. package/src/components/query_bar/querylanguage/kuery/functions/nested.ts +63 -0
  43. package/src/components/query_bar/querylanguage/kuery/functions/not.ts +53 -0
  44. package/src/components/query_bar/querylanguage/kuery/functions/or.ts +56 -0
  45. package/src/components/query_bar/querylanguage/kuery/functions/range.ts +163 -0
  46. package/src/components/query_bar/querylanguage/kuery/functions/utils/get_fields.ts +49 -0
  47. package/src/components/query_bar/querylanguage/kuery/functions/utils/get_full_field_name_node.ts +87 -0
  48. package/src/components/query_bar/querylanguage/kuery/index.ts +38 -0
  49. package/src/components/query_bar/querylanguage/kuery/kuery_syntax_error.ts +76 -0
  50. package/src/components/query_bar/querylanguage/kuery/node_types/function.ts +75 -0
  51. package/src/components/query_bar/querylanguage/kuery/node_types/index.ts +46 -0
  52. package/src/components/query_bar/querylanguage/kuery/node_types/literal.ts +42 -0
  53. package/src/components/query_bar/querylanguage/kuery/node_types/named_arg.ts +47 -0
  54. package/src/components/query_bar/querylanguage/kuery/node_types/types.ts +108 -0
  55. package/src/components/query_bar/querylanguage/kuery/node_types/wildcard.ts +80 -0
  56. package/src/components/query_bar/querylanguage/kuery/types.ts +52 -0
  57. package/src/components/query_bar/querylanguage/outputTypes/toCQL.ts +122 -0
  58. package/src/components/query_bar/querylanguage/outputTypes/toMongo.ts +103 -0
  59. package/src/components/query_bar/querylanguage/utils.ts +35 -0
  60. package/src/components/query_bar/types.ts +59 -0
  61. package/src/components/splitview/index.ts +1 -0
  62. package/src/components/splitview/splitview.css.ts +66 -0
  63. package/src/components/splitview/splitview.ts +183 -0
  64. package/src/components/types.ts +35 -0
  65. package/src/index.ts +1 -0
  66. package/src/stories/Banner.stories.ts +46 -0
  67. package/src/stories/BitDisplay.stories.ts +68 -0
  68. package/src/stories/Button.stories.ts +138 -0
  69. package/src/stories/Header.stories.ts +55 -0
  70. package/src/stories/Page.stories.ts +108 -0
  71. package/src/stories/QueryBar.stories.ts +63 -0
  72. package/src/stories/Splitview.stories.ts +52 -0
  73. package/src/stories/fixtures/Bits.ts +15 -0
  74. package/src/stories/fixtures/ExampleContent.ts +102 -0
  75. package/src/stories/fixtures/data.ts +30 -0
  76. package/src/stories/fixtures/lorumipsum.ts +19 -0
  77. package/src/stories/input.stories.ts +77 -0
  78. package/src/stories/tsconfig.json +35 -0
  79. package/src/utils/debounce.ts +18 -0
  80. package/src/utils/spread.ts +71 -0
  81. package/src/vite-env.d.ts +1 -0
  82. package/test/__init__.py +9 -0
  83. package/test/elastic.py +9 -0
  84. package/test/interface.py +16 -0
  85. package/tsconfig.json +29 -0
  86. package/vite.config.js +34 -0
  87. package/vue-example.png +0 -0
  88. package/vue-include.png +0 -0
@@ -0,0 +1,87 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import { getFields } from './get_fields';
32
+ import { IIndexPattern, KueryNode } from '../../../../types';
33
+
34
+ export function getFullFieldNameNode(
35
+ rootNameNode: any,
36
+ indexPattern?: IIndexPattern,
37
+ nestedPath?: string
38
+ ): KueryNode {
39
+ const fullFieldNameNode = {
40
+ ...rootNameNode,
41
+ value: nestedPath ? `${nestedPath}.${rootNameNode.value}` : rootNameNode.value,
42
+ };
43
+
44
+ // Wildcards can easily include nested and non-nested fields. There isn't a good way to let
45
+ // users handle this themselves so we automatically add nested queries in this scenario and skip the
46
+ // error checking below.
47
+ if (!indexPattern || (fullFieldNameNode.type === 'wildcard' && !nestedPath)) {
48
+ return fullFieldNameNode;
49
+ }
50
+ const fields = getFields(fullFieldNameNode, indexPattern);
51
+
52
+ const errors = fields!.reduce((acc: any, field: any) => {
53
+ const nestedPathFromField =
54
+ field.subType && field.subType.nested ? field.subType.nested.path : undefined;
55
+
56
+ if (nestedPath && !nestedPathFromField) {
57
+ return [
58
+ ...acc,
59
+ `${field.name} is not a nested field but is in nested group "${nestedPath}" in the DQL expression.`,
60
+ ];
61
+ }
62
+
63
+ if (nestedPathFromField && !nestedPath) {
64
+ return [
65
+ ...acc,
66
+ `${field.name} is a nested field, but is not in a nested group in the DQL expression.`,
67
+ ];
68
+ }
69
+
70
+ if (nestedPathFromField !== nestedPath) {
71
+ return [
72
+ ...acc,
73
+ `Nested field ${field.name
74
+ } is being queried with the incorrect nested path. The correct path is ${field.subType!.nested!.path
75
+ }.`,
76
+ ];
77
+ }
78
+
79
+ return acc;
80
+ }, []);
81
+
82
+ if (errors.length > 0) {
83
+ throw new Error(errors.join('\n'));
84
+ }
85
+
86
+ return fullFieldNameNode;
87
+ }
@@ -0,0 +1,38 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ export { DQLSyntaxError } from './kuery_syntax_error';
32
+ export { nodeTypes } from './node_types';
33
+ export * from './ast';
34
+ export { fromKueryExpression as parse, toOpenSearchQuery as toDSL } from './ast';
35
+ export { toCql } from "../outputTypes/toCQL"
36
+ export { toMongo } from "../outputTypes/toMongo"
37
+
38
+ export * from './types';
@@ -0,0 +1,76 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+
32
+
33
+
34
+ const endOfInputText = 'end of input';
35
+
36
+ const grammarRuleTranslations: Record<string, string> = {
37
+ fieldName: 'field name',
38
+ value: 'value',
39
+ literal: 'literal',
40
+ whitespace: 'whitespace',
41
+ };
42
+
43
+ interface DQLSyntaxErrorData extends Error {
44
+ found: string;
45
+ expected: DQLSyntaxErrorExpected[] | null;
46
+ location: any;
47
+ }
48
+
49
+ interface DQLSyntaxErrorExpected {
50
+ description: string;
51
+ }
52
+
53
+ export class DQLSyntaxError extends Error {
54
+ shortMessage: string;
55
+
56
+ constructor(error: DQLSyntaxErrorData, expression: any) {
57
+ let message = error.message;
58
+ if (error.expected) {
59
+ const translatedExpectations = error.expected.map((expected) => {
60
+ return grammarRuleTranslations[expected.description] || expected.description;
61
+ });
62
+
63
+ const translatedExpectationText = translatedExpectations.join(', ');
64
+ let foundInput = error.found ? `"${error.found}"` : endOfInputText;
65
+ message = `Expected ${translatedExpectationText} but ${foundInput} found.`
66
+ }
67
+
68
+ const fullMessage = [message, expression, '-'.repeat(error.location.start.offset) + '^'].join(
69
+ '\n'
70
+ );
71
+
72
+ super(fullMessage);
73
+ this.name = 'DQLSyntaxError';
74
+ this.shortMessage = message;
75
+ }
76
+ }
@@ -0,0 +1,75 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+
32
+ import { functions } from '../functions';
33
+ import { IIndexPattern, KueryNode } from '../../..';
34
+ import { FunctionName, FunctionTypeBuildNode } from './types';
35
+
36
+ export function buildNode(functionName: FunctionName, ...args: any[]) {
37
+ const kueryFunction = functions[functionName];
38
+ if (kueryFunction === undefined) {
39
+ throw new Error(`Unknown function "${functionName}"`);
40
+ }
41
+
42
+ return {
43
+ type: 'function' as 'function',
44
+ function: functionName,
45
+ // This requires better typing of the different typings and their return types.
46
+ // @ts-ignore
47
+ ...kueryFunction.buildNodeParams(...args),
48
+ };
49
+ }
50
+
51
+ // Mainly only useful in the grammar where we'll already have real argument nodes in hand
52
+ export function buildNodeWithArgumentNodes(
53
+ functionName: FunctionName,
54
+ args: any[]
55
+ ): FunctionTypeBuildNode {
56
+ if (functions[functionName] === undefined) {
57
+ throw new Error(`Unknown function "${functionName}"`);
58
+ }
59
+
60
+ return {
61
+ type: 'function',
62
+ function: functionName,
63
+ arguments: args,
64
+ };
65
+ }
66
+
67
+ export function toOpenSearchQuery(
68
+ node: KueryNode,
69
+ indexPattern?: IIndexPattern,
70
+ config?: Record<string, any>,
71
+ context?: Record<string, any>
72
+ ) {
73
+ const kueryFunction = functions[node.function as FunctionName];
74
+ return kueryFunction.toOpenSearchQuery(node, indexPattern, config, context);
75
+ }
@@ -0,0 +1,46 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import * as functionType from './function';
32
+ import * as literal from './literal';
33
+ import * as namedArg from './named_arg';
34
+ import * as wildcard from './wildcard';
35
+ import { NodeTypes } from './types';
36
+
37
+ export * from "./types"
38
+
39
+ export const nodeTypes: NodeTypes = {
40
+ // This requires better typing of the different typings and their return types.
41
+ // @ts-ignore
42
+ function: functionType,
43
+ literal,
44
+ namedArg,
45
+ wildcard,
46
+ };
@@ -0,0 +1,42 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import { LiteralTypeBuildNode } from './types';
32
+
33
+ export function buildNode(value: LiteralTypeBuildNode['value']): LiteralTypeBuildNode {
34
+ return {
35
+ type: 'literal',
36
+ value,
37
+ };
38
+ }
39
+
40
+ export function toOpenSearchQuery(node: LiteralTypeBuildNode): LiteralTypeBuildNode['value'] {
41
+ return node.value;
42
+ }
@@ -0,0 +1,47 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import * as ast from '../ast';
32
+ import { nodeTypes } from '.';
33
+ import { NamedArgTypeBuildNode } from './types';
34
+ import { JsonObject } from '../../../types';
35
+
36
+ export function buildNode(name: string, value: any): NamedArgTypeBuildNode {
37
+ const argumentNode = value.type === 'literal' ? value : nodeTypes.literal.buildNode(value);
38
+ return {
39
+ type: 'namedArg',
40
+ name,
41
+ value: argumentNode,
42
+ };
43
+ }
44
+
45
+ export function toOpenSearchQuery(node: any): JsonObject {
46
+ return ast.toOpenSearchQuery(node.value);
47
+ }
@@ -0,0 +1,108 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ /**
32
+ * WARNING: these typings are incomplete
33
+ */
34
+
35
+ import { JsonValue, IIndexPattern } from '../../../types';
36
+
37
+ import { KueryNode } from '..';
38
+
39
+ export type FunctionName =
40
+ | 'is'
41
+ | 'and'
42
+ | 'or'
43
+ | 'not'
44
+ | 'range'
45
+ | 'exists'
46
+ | 'geoBoundingBox'
47
+ | 'geoPolygon'
48
+ | 'nested';
49
+
50
+ interface FunctionType {
51
+ buildNode: (functionName: FunctionName, ...args: any[]) => FunctionTypeBuildNode;
52
+ buildNodeWithArgumentNodes: (functionName: FunctionName, args: any[]) => FunctionTypeBuildNode;
53
+ toOpenSearchQuery: (
54
+ node: any,
55
+ indexPattern?: IIndexPattern,
56
+ config?: Record<string, any>,
57
+ context?: Record<string, any>
58
+ ) => JsonValue;
59
+ }
60
+
61
+ export interface FunctionTypeBuildNode {
62
+ type: 'function';
63
+ function: FunctionName;
64
+ // TODO -> Need to define a better type for DSL query
65
+ arguments: any[];
66
+ }
67
+
68
+ interface LiteralType {
69
+ buildNode: (value: null | boolean | number | string) => LiteralTypeBuildNode;
70
+ toOpenSearchQuery: (node: any) => null | boolean | number | string;
71
+ }
72
+
73
+ export interface LiteralTypeBuildNode {
74
+ type: 'literal';
75
+ value: null | boolean | number | string;
76
+ }
77
+
78
+ interface NamedArgType {
79
+ buildNode: (name: string, value: any) => NamedArgTypeBuildNode;
80
+ toOpenSearchQuery: (node: any) => JsonValue;
81
+ }
82
+
83
+ export interface NamedArgTypeBuildNode {
84
+ type: 'namedArg';
85
+ name: string;
86
+ value: any;
87
+ }
88
+
89
+ interface WildcardType {
90
+ wildcardSymbol: string;
91
+ buildNode: (value: string) => WildcardTypeBuildNode | KueryNode;
92
+ test: (node: any, string: string) => boolean;
93
+ toOpenSearchQuery: (node: any) => string;
94
+ toQueryStringQuery: (node: any) => string;
95
+ hasLeadingWildcard: (node: any) => boolean;
96
+ }
97
+
98
+ export interface WildcardTypeBuildNode {
99
+ type: 'wildcard';
100
+ value: string;
101
+ }
102
+
103
+ export interface NodeTypes {
104
+ function: FunctionType;
105
+ literal: LiteralType;
106
+ namedArg: NamedArgType;
107
+ wildcard: WildcardType;
108
+ }
@@ -0,0 +1,80 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import { fromLiteralExpression } from '../ast/ast';
32
+ import { WildcardTypeBuildNode } from './types';
33
+ import { KueryNode } from '..';
34
+
35
+ export const wildcardSymbol = '@kuery-wildcard@';
36
+
37
+ // Copied from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
38
+ function escapeRegExp(str: string) {
39
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
40
+ }
41
+
42
+ // See https://opensearch.org/docs/latest/opensearch/query-dsl/term/#regex
43
+ function escapeQueryString(str: string) {
44
+ return str.replace(/[+-=&|><!(){}[\]^"~*?:\\/]/g, '\\$&'); // $& means the whole matched string
45
+ }
46
+
47
+ export function buildNode(value: string): WildcardTypeBuildNode | KueryNode {
48
+ if (!value.includes(wildcardSymbol)) {
49
+ return fromLiteralExpression(value);
50
+ }
51
+
52
+ return {
53
+ type: 'wildcard',
54
+ value,
55
+ };
56
+ }
57
+
58
+ export function test(node: any, str: string): boolean {
59
+ const { value } = node;
60
+ const regex = value.split(wildcardSymbol).map(escapeRegExp).join('[\\s\\S]*');
61
+ const regexp = new RegExp(`^${regex}$`);
62
+ return regexp.test(str);
63
+ }
64
+
65
+ export function toOpenSearchQuery(node: any): string {
66
+ const { value } = node;
67
+ return value.split(wildcardSymbol).join('*');
68
+ }
69
+
70
+ export function toQueryStringQuery(node: any): string {
71
+ const { value } = node;
72
+ return value.split(wildcardSymbol).map(escapeQueryString).join('*');
73
+ }
74
+
75
+ export function hasLeadingWildcard(node: any): boolean {
76
+ const { value } = node;
77
+ // A lone wildcard turns into an `exists` query, so we're only concerned with
78
+ // leading wildcards followed by additional characters.
79
+ return value.startsWith(wildcardSymbol) && value.replace(wildcardSymbol, '').length > 0;
80
+ }
@@ -0,0 +1,52 @@
1
+ /*
2
+ * SPDX-License-Identifier: Apache-2.0
3
+ *
4
+ * The OpenSearch Contributors require contributions made to
5
+ * this file be licensed under the Apache-2.0 license or a
6
+ * compatible open source license.
7
+ *
8
+ * Any modifications Copyright OpenSearch Contributors. See
9
+ * GitHub history for details.
10
+ */
11
+
12
+ /*
13
+ * Licensed to Elasticsearch B.V. under one or more contributor
14
+ * license agreements. See the NOTICE file distributed with
15
+ * this work for additional information regarding copyright
16
+ * ownership. Elasticsearch B.V. licenses this file to you under
17
+ * the Apache License, Version 2.0 (the "License"); you may
18
+ * not use this file except in compliance with the License.
19
+ * You may obtain a copy of the License at
20
+ *
21
+ * http://www.apache.org/licenses/LICENSE-2.0
22
+ *
23
+ * Unless required by applicable law or agreed to in writing,
24
+ * software distributed under the License is distributed on an
25
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
26
+ * KIND, either express or implied. See the License for the
27
+ * specific language governing permissions and limitations
28
+ * under the License.
29
+ */
30
+
31
+ import { NodeTypes } from './node_types';
32
+ export * from "./node_types"
33
+ export interface KueryNode {
34
+ type: keyof NodeTypes;
35
+ [key: string]: any;
36
+ }
37
+
38
+ export type DslQuery = any;
39
+
40
+ export interface KueryParseOptions {
41
+ helpers: {
42
+ [key: string]: any;
43
+ };
44
+ startRule: string;
45
+ allowLeadingWildcards: boolean;
46
+ errorOnLuceneSyntax: boolean;
47
+ cursorSymbol?: string;
48
+ parseCursor?: boolean;
49
+ }
50
+
51
+ export { nodeTypes } from './node_types';
52
+