refactor: flow typing (#1012)
* fix: padding * fix: Minor button alignment issues * feat: Add icon to invite people button * WIP
This commit is contained in:
@ -1,11 +1,36 @@
|
||||
// @flow
|
||||
import { sortBy } from 'lodash';
|
||||
import { deburr } from 'lodash';
|
||||
import naturalSort from 'natural-sort';
|
||||
|
||||
export default (sortableArray: *, key: string): * => {
|
||||
if (!sortableArray) return [];
|
||||
|
||||
let keys = sortableArray.map(object => object[key]);
|
||||
keys.sort(naturalSort());
|
||||
return sortBy(sortableArray, object => keys.indexOf(object[key]));
|
||||
type NaturalSortOptions = {
|
||||
caseSensitive?: boolean,
|
||||
direction?: 'asc' | 'desc',
|
||||
};
|
||||
|
||||
const sorter = naturalSort();
|
||||
|
||||
function getSortByField<T: Object>(
|
||||
item: T,
|
||||
keyOrCallback: string | (T => string)
|
||||
) {
|
||||
if (typeof keyOrCallback === 'string') {
|
||||
return deburr(item[keyOrCallback]);
|
||||
}
|
||||
|
||||
return keyOrCallback(item);
|
||||
}
|
||||
|
||||
function naturalSortBy<T>(
|
||||
items: T[],
|
||||
key: string | (T => string),
|
||||
sortOptions?: NaturalSortOptions
|
||||
): T[] {
|
||||
if (!items) return [];
|
||||
|
||||
const sort = sortOptions ? naturalSort(sortOptions) : sorter;
|
||||
return items.sort((a: any, b: any): -1 | 0 | 1 =>
|
||||
sort(getSortByField(a, key), getSortByField(b, key))
|
||||
);
|
||||
}
|
||||
|
||||
export default naturalSortBy;
|
||||
|
Reference in New Issue
Block a user