dsl-builder
Version:
OpenSearch Query Builder - Extract from OpenSearch Dashboards
97 lines (96 loc) • 4.04 kB
JavaScript
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { findIndex } from 'lodash';
import { IndexPatternField } from './index_pattern_field';
import { shortenDottedString } from '../../utils';
// extending the array class and using a constructor doesn't work well
// when calling filter and similar so wrapping in a callback.
// to be removed in the future
export const fieldList = (specs = [], shortDotsEnable = false) => {
class FldList extends Array {
constructor() {
super();
this.byName = new Map();
this.groups = new Map();
this.setByName = (field) => this.byName.set(field.name, field);
this.setByGroup = (field) => {
if (typeof this.groups.get(field.type) === 'undefined') {
this.groups.set(field.type, new Map());
}
this.groups.get(field.type).set(field.name, field);
};
this.removeByGroup = (field) => this.groups.get(field.type).delete(field.name);
this.calcDisplayName = (name) => shortDotsEnable ? shortenDottedString(name) : name;
this.getAll = () => [...this.byName.values()];
this.getByName = (name) => this.byName.get(name);
this.getByType = (type) => [
...(this.groups.get(type) || new Map()).values(),
];
this.add = (field) => {
const newField = new IndexPatternField(field, this.calcDisplayName(field.name));
this.push(newField);
this.setByName(newField);
this.setByGroup(newField);
};
this.remove = (field) => {
this.removeByGroup(field);
this.byName.delete(field.name);
const fieldIndex = findIndex(this, { name: field.name });
this.splice(fieldIndex, 1);
};
this.update = (field) => {
const newField = new IndexPatternField(field, this.calcDisplayName(field.name));
const index = this.findIndex(f => f.name === newField.name);
this.splice(index, 1, newField);
this.setByName(newField);
this.removeByGroup(newField);
this.setByGroup(newField);
};
this.removeAll = () => {
this.length = 0;
this.byName.clear();
this.groups.clear();
};
this.replaceAll = (spcs = []) => {
this.removeAll();
spcs.forEach(this.add);
};
specs.map(field => this.add(field));
}
toSpec({ getFormatterForField, } = {}) {
return {
...this.reduce((collector, field) => {
collector[field.name] = field.toSpec({ getFormatterForField });
return collector;
}, {}),
};
}
}
return new FldList();
};