dsl-builder-test
Version:
OpenSearch Query Builder - Extract from OpenSearch Dashboards
90 lines (89 loc) • 3.41 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 { defaults, isEqual, omit, map } from 'lodash';
/**
* Include disabled, negate and store when comparing filters
*/
export const COMPARE_ALL_OPTIONS = {
index: true,
disabled: true,
negate: true,
state: true,
alias: true,
};
const mapFilter = (filter, comparators, excludedAttributes) => {
var _a, _b;
const cleaned = omit(filter, excludedAttributes);
if (comparators.index)
cleaned.index = (_a = filter.meta) === null || _a === void 0 ? void 0 : _a.index;
if (comparators.negate)
cleaned.negate = filter.meta && Boolean(filter.meta.negate);
if (comparators.disabled)
cleaned.disabled = filter.meta && Boolean(filter.meta.disabled);
if (comparators.alias)
cleaned.alias = (_b = filter.meta) === null || _b === void 0 ? void 0 : _b.alias;
return cleaned;
};
const mapFilterArray = (filters, comparators, excludedAttributes) => {
return map(filters, (filter) => mapFilter(filter, comparators, excludedAttributes));
};
/**
* Compare two filters or filter arrays to see if they match.
* For filter arrays, the assumption is they are sorted.
*
* @param {Filter | Filter[]} first The first filter or filter array to compare
* @param {Filter | Filter[]} second The second filter or filter array to compare
* @param {FilterCompareOptions} comparatorOptions Parameters to use for comparison
*
* @returns {bool} Filters are the same
*/
export const compareFilters = (first, second, comparatorOptions = {}) => {
if (!first || !second)
return false;
let comparators = {};
const excludedAttributes = ['$$hashKey', 'meta'];
comparators = defaults(comparatorOptions || {}, {
index: false,
state: false,
negate: false,
disabled: false,
alias: false,
});
if (!comparators.state)
excludedAttributes.push('$state');
if (Array.isArray(first) && Array.isArray(second)) {
return isEqual(mapFilterArray(first, comparators, excludedAttributes), mapFilterArray(second, comparators, excludedAttributes));
}
else if (!Array.isArray(first) && !Array.isArray(second)) {
return isEqual(mapFilter(first, comparators, excludedAttributes), mapFilter(second, comparators, excludedAttributes));
}
else {
return false;
}
};