mirror of
https://github.com/outline/outline.git
synced 2026-11-23 06:11:04 +03:00
* chore: replace explicit any with concrete types in shared Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> * chore: address review feedback - naturalSort: guard non-string field values instead of asserting string - ProsemirrorHelper: type stored mark attrs as Partial<CommentMark> - env: revert to Record<string, any>; safer typing requires fixing many consumers Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.7 <noreply@anthropic.com>
49 lines
1.2 KiB
TypeScript
49 lines
1.2 KiB
TypeScript
import emojiRegex from "emoji-regex";
|
|
import deburr from "lodash/deburr";
|
|
import naturalSort from "natural-sort";
|
|
|
|
type NaturalSortOptions = {
|
|
caseSensitive?: boolean;
|
|
direction?: "asc" | "desc";
|
|
};
|
|
|
|
const sorter = naturalSort();
|
|
const regex = emojiRegex();
|
|
|
|
const stripEmojis = (value: string) => value.replace(regex, "");
|
|
|
|
const cleanValue = (value: string) => stripEmojis(deburr(value));
|
|
|
|
function getSortByField<T extends object>(
|
|
item: T,
|
|
keyOrCallback: string | ((item: T) => string)
|
|
) {
|
|
const field =
|
|
typeof keyOrCallback === "string"
|
|
? (item as Record<string, unknown>)[keyOrCallback]
|
|
: keyOrCallback(item);
|
|
return cleanValue(typeof field === "string" ? field : "");
|
|
}
|
|
|
|
function naturalSortBy<T extends object>(
|
|
items: T[],
|
|
key: string | ((item: T) => string),
|
|
sortOptions?: NaturalSortOptions
|
|
): T[] {
|
|
if (!items) {
|
|
return [];
|
|
}
|
|
const sort = sortOptions
|
|
? naturalSort({
|
|
caseSensitive: sortOptions.caseSensitive,
|
|
direction: sortOptions.direction === "desc" ? "desc" : undefined,
|
|
})
|
|
: sorter;
|
|
|
|
return items.sort((a: T, b: T) =>
|
|
sort(getSortByField(a, key), getSortByField(b, key))
|
|
);
|
|
}
|
|
|
|
export default naturalSortBy;
|