-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ui/public/utils] Copy rarely used items to where they are consumed (#…
…53819) * [ui/public/utils] Copy rarely used items to where they are consumed Closes: #52841 * sort_prefix_first 👉x-pack/legacy/plugins/kuery_autocomplete * numeric 👉src/legacy/core_plugins/kibana/public/management * diff_object + tests 👉ui/state_management * function + tests 👉ui/state_management (function.js was removed!) * key_map 👉ui/directives * leastCommonMultiple 👉ui/vis * string_utils 👉ui/saved_objects * collection * parse_interval * it -> test * fix CI * fix PR comments Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
- Loading branch information
1 parent
007ea5f
commit 71ff2de
Showing
47 changed files
with
756 additions
and
769 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 { keysToSnakeCaseShallow } from './case_conversion'; | ||
|
||
describe('keysToSnakeCaseShallow', () => { | ||
test("should convert all of an object's keys to snake case", () => { | ||
const data = { | ||
camelCase: 'camel_case', | ||
'kebab-case': 'kebab_case', | ||
snake_case: 'snake_case', | ||
}; | ||
|
||
const result = keysToSnakeCaseShallow(data); | ||
|
||
expect(result.camel_case).toBe('camel_case'); | ||
expect(result.kebab_case).toBe('kebab_case'); | ||
expect(result.snake_case).toBe('snake_case'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/legacy/ui/public/indexed_array/helpers/organize_by.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 { groupBy } from 'lodash'; | ||
import { organizeBy } from './organize_by'; | ||
|
||
describe('organizeBy', () => { | ||
test('it works', () => { | ||
const col = [ | ||
{ | ||
name: 'one', | ||
roles: ['user', 'admin', 'owner'], | ||
}, | ||
{ | ||
name: 'two', | ||
roles: ['user'], | ||
}, | ||
{ | ||
name: 'three', | ||
roles: ['user'], | ||
}, | ||
{ | ||
name: 'four', | ||
roles: ['user', 'admin'], | ||
}, | ||
]; | ||
|
||
const resp = organizeBy(col, 'roles'); | ||
expect(resp).toHaveProperty('user'); | ||
expect(resp.user.length).toBe(4); | ||
|
||
expect(resp).toHaveProperty('admin'); | ||
expect(resp.admin.length).toBe(2); | ||
|
||
expect(resp).toHaveProperty('owner'); | ||
expect(resp.owner.length).toBe(1); | ||
}); | ||
|
||
test('behaves just like groupBy in normal scenarios', () => { | ||
const col = [{ name: 'one' }, { name: 'two' }, { name: 'three' }, { name: 'four' }]; | ||
|
||
const orgs = organizeBy(col, 'name'); | ||
const groups = groupBy(col, 'name'); | ||
|
||
expect(orgs).toEqual(groups); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* 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 { each, isFunction } from 'lodash'; | ||
|
||
/** | ||
* Like _.groupBy, but allows specifying multiple groups for a | ||
* single object. | ||
* | ||
* organizeBy([{ a: [1, 2, 3] }, { b: true, a: [1, 4] }], 'a') | ||
* // Object {1: Array[2], 2: Array[1], 3: Array[1], 4: Array[1]} | ||
* | ||
* _.groupBy([{ a: [1, 2, 3] }, { b: true, a: [1, 4] }], 'a') | ||
* // Object {'1,2,3': Array[1], '1,4': Array[1]} | ||
* | ||
* @param {array} collection - the list of values to organize | ||
* @param {Function} callback - either a property name, or a callback. | ||
* @return {object} | ||
*/ | ||
export function organizeBy(collection: object[], callback: ((obj: object) => string) | string) { | ||
const buckets: { [key: string]: object[] } = {}; | ||
|
||
function add(key: string, obj: object) { | ||
if (!buckets[key]) { | ||
buckets[key] = []; | ||
} | ||
buckets[key].push(obj); | ||
} | ||
|
||
each(collection, (obj: Record<string, any>) => { | ||
const keys = isFunction(callback) ? callback(obj) : obj[callback]; | ||
|
||
if (!Array.isArray(keys)) { | ||
add(keys, obj); | ||
return; | ||
} | ||
|
||
let length = keys.length; | ||
while (length-- > 0) { | ||
add(keys[length], obj); | ||
} | ||
}); | ||
|
||
return buckets; | ||
} |
Oops, something went wrong.