This repository has been archived on 2022-08-14. You can view files and clone it, but cannot push or open issues or pull requests.
Files
outline/shared/utils/naturalSort.js
Tom Moor d024d31f66 refactor: flow typing (#1012)
* fix: padding

* fix: Minor button alignment issues

* feat: Add icon to invite people button

* WIP
2019-08-08 23:09:09 -07:00

37 lines
768 B
JavaScript

// @flow
import { deburr } from 'lodash';
import naturalSort from 'natural-sort';
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;