dsl-builder-test
Version:
OpenSearch Query Builder - Extract from OpenSearch Dashboards
101 lines (100 loc) • 4.23 kB
JavaScript
"use strict";
/*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.fieldList = void 0;
/*
* 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.
*/
const lodash_1 = require("lodash");
const index_pattern_field_1 = require("./index_pattern_field");
const utils_1 = require("../../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
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 ? (0, utils_1.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 index_pattern_field_1.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 = (0, lodash_1.findIndex)(this, { name: field.name });
this.splice(fieldIndex, 1);
};
this.update = (field) => {
const newField = new index_pattern_field_1.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();
};
exports.fieldList = fieldList;