diff --git a/src/legacy/ui/public/agg_types/filter/index.ts b/src/legacy/ui/public/agg_types/filter/index.ts index 9582119b28b36..3fc577e7e9a23 100644 --- a/src/legacy/ui/public/agg_types/filter/index.ts +++ b/src/legacy/ui/public/agg_types/filter/index.ts @@ -18,3 +18,4 @@ */ export { aggTypeFilters } from './agg_type_filters'; +export { propFilter } from './prop_filter'; diff --git a/src/legacy/ui/public/filters/__tests__/prop_filter.js b/src/legacy/ui/public/agg_types/filter/prop_filter.test.ts similarity index 70% rename from src/legacy/ui/public/filters/__tests__/prop_filter.js rename to src/legacy/ui/public/agg_types/filter/prop_filter.test.ts index ec60121960ecb..0d32c9dc769da 100644 --- a/src/legacy/ui/public/filters/__tests__/prop_filter.js +++ b/src/legacy/ui/public/agg_types/filter/prop_filter.test.ts @@ -18,16 +18,16 @@ */ import expect from '@kbn/expect'; -import { propFilter } from '../_prop_filter'; +import { propFilter } from './prop_filter'; -describe('prop filter', function () { - let nameFilter; +describe('prop filter', () => { + let nameFilter: Function; - beforeEach(function () { + beforeEach(() => { nameFilter = propFilter('name'); }); - function getObjects(...names) { + function getObjects(...names: string[]) { const count = new Map(); const objects = []; @@ -36,56 +36,56 @@ describe('prop filter', function () { count.set(name, 1); } objects.push({ - name: name, - title: `${name} ${count.get(name)}` + name, + title: `${name} ${count.get(name)}`, }); count.set(name, count.get(name) + 1); } return objects; } - it('returns list when no filters are provided', function () { + it('returns list when no filters are provided', () => { const objects = getObjects('table', 'table', 'pie'); expect(nameFilter(objects)).to.eql(objects); }); - it('returns list when empty list of filters is provided', function () { + it('returns list when empty list of filters is provided', () => { const objects = getObjects('table', 'table', 'pie'); expect(nameFilter(objects, [])).to.eql(objects); }); - it('should keep only the tables', function () { + it('should keep only the tables', () => { const objects = getObjects('table', 'table', 'pie'); expect(nameFilter(objects, 'table')).to.eql(getObjects('table', 'table')); }); - it('should support comma-separated values', function () { + it('should support comma-separated values', () => { const objects = getObjects('table', 'line', 'pie'); expect(nameFilter(objects, 'table,line')).to.eql(getObjects('table', 'line')); }); - it('should support an array of values', function () { + it('should support an array of values', () => { const objects = getObjects('table', 'line', 'pie'); - expect(nameFilter(objects, [ 'table', 'line' ])).to.eql(getObjects('table', 'line')); + expect(nameFilter(objects, ['table', 'line'])).to.eql(getObjects('table', 'line')); }); - it('should return all objects', function () { + it('should return all objects', () => { const objects = getObjects('table', 'line', 'pie'); expect(nameFilter(objects, '*')).to.eql(objects); }); - it('should allow negation', function () { + it('should allow negation', () => { const objects = getObjects('table', 'line', 'pie'); - expect(nameFilter(objects, [ '!line' ])).to.eql(getObjects('table', 'pie')); + expect(nameFilter(objects, ['!line'])).to.eql(getObjects('table', 'pie')); }); - it('should support a function for specifying what should be kept', function () { + it('should support a function for specifying what should be kept', () => { const objects = getObjects('table', 'line', 'pie'); - const line = (value) => value === 'line'; + const line = (value: string) => value === 'line'; expect(nameFilter(objects, line)).to.eql(getObjects('line')); }); - it('gracefully handles a filter function with zero arity', function () { + it('gracefully handles a filter function with zero arity', () => { const objects = getObjects('table', 'line', 'pie'); const rejectEverything = () => false; expect(nameFilter(objects, rejectEverything)).to.eql([]); diff --git a/src/legacy/ui/public/filters/_prop_filter.js b/src/legacy/ui/public/agg_types/filter/prop_filter.ts similarity index 70% rename from src/legacy/ui/public/filters/_prop_filter.js rename to src/legacy/ui/public/agg_types/filter/prop_filter.ts index ff0f48028da30..45f350ea6adc8 100644 --- a/src/legacy/ui/public/filters/_prop_filter.js +++ b/src/legacy/ui/public/agg_types/filter/prop_filter.ts @@ -19,14 +19,16 @@ import { isFunction } from 'lodash'; +type FilterFunc

= (item: T[P]) => boolean; + /** * Filters out a list by a given filter. This is currently used to implement: * - fieldType filters a list of fields by their type property * - aggFilter filters a list of aggs by their name property * - * @returns {function} - the filter function which can be registered with angular + * @returns the filter function which can be registered with angular */ -export function propFilter(prop) { +function propFilter

(prop: P) { /** * List filtering function which accepts an array or list of values that a property * must contain @@ -37,9 +39,12 @@ export function propFilter(prop) { * - Can be also an array, a single value as a string, or a comma-separated list of items * @return {array} - the filtered list */ - return function (list, filters = []) { + return function filterByName( + list: T[], + filters: string[] | string | FilterFunc = [] + ): T[] { if (isFunction(filters)) { - return list.filter((item) => filters(item[prop])); + return list.filter(item => (filters as FilterFunc)(item[prop])); } if (!Array.isArray(filters)) { @@ -54,21 +59,26 @@ export function propFilter(prop) { return list; } - const options = filters.reduce(function (options, filter) { - let type = 'include'; - let value = filter; + const options = filters.reduce( + (acc, filter) => { + let type = 'include'; + let value = filter; - if (filter.charAt(0) === '!') { - type = 'exclude'; - value = filter.substr(1); - } + if (filter.charAt(0) === '!') { + type = 'exclude'; + value = filter.substr(1); + } - if (!options[type]) options[type] = []; - options[type].push(value); - return options; - }, {}); + if (!acc[type]) { + acc[type] = []; + } + acc[type].push(value); + return acc; + }, + {} as { [type: string]: string[] } + ); - return list.filter(function (item) { + return list.filter(item => { const value = item[prop]; const excluded = options.exclude && options.exclude.includes(value); @@ -85,3 +95,5 @@ export function propFilter(prop) { }); }; } + +export { propFilter }; diff --git a/src/legacy/ui/public/agg_types/param_types/field.js b/src/legacy/ui/public/agg_types/param_types/field.js index 5c883c2c588b5..af6cd71b3fdfb 100644 --- a/src/legacy/ui/public/agg_types/param_types/field.js +++ b/src/legacy/ui/public/agg_types/param_types/field.js @@ -26,7 +26,7 @@ import '../filters/sort_prefix_first'; import { IndexedArray } from '../../indexed_array'; import { toastNotifications } from '../../notify'; import { createLegacyClass } from '../../utils/legacy_class'; -import { propFilter } from '../../filters/_prop_filter'; +import { propFilter } from '../filter'; import { i18n } from '@kbn/i18n'; const filterByType = propFilter('type'); diff --git a/src/legacy/ui/public/filters/_prop_filter.d.ts b/src/legacy/ui/public/filters/_prop_filter.d.ts deleted file mode 100644 index 284619b50fb60..0000000000000 --- a/src/legacy/ui/public/filters/_prop_filter.d.ts +++ /dev/null @@ -1,24 +0,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. - */ - -type FilterFunc = (item: I) => boolean; - -export const propFilter: ( - prop: string -) => (list: T[], filters: string[] | string | FilterFunc) => T[]; diff --git a/src/legacy/ui/public/vis/editors/default/vis_type_agg_filter.ts b/src/legacy/ui/public/vis/editors/default/vis_type_agg_filter.ts index 02949c847badf..5842d7579f60a 100644 --- a/src/legacy/ui/public/vis/editors/default/vis_type_agg_filter.ts +++ b/src/legacy/ui/public/vis/editors/default/vis_type_agg_filter.ts @@ -17,12 +17,10 @@ * under the License. */ import { AggType } from '../../../agg_types'; -import { aggTypeFilters } from '../../../agg_types/filter'; +import { aggTypeFilters, propFilter } from '../../../agg_types/filter'; import { IndexPattern } from '../../../index_patterns'; import { AggConfig } from '../../../vis'; -import { propFilter } from '../../../filters/_prop_filter'; - const filterByName = propFilter('name'); /**