Skip to content

Commit b98ae10

Browse files
committed
👍 Add numerical sorter
1 parent bee56fb commit b98ae10

File tree

4 files changed

+35
-12
lines changed

4 files changed

+35
-12
lines changed

deno.jsonc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"./builtin/sorter": "./denops/@fall/builtin/sorter/mod.ts",
5252
"./builtin/sorter/lexical": "./denops/@fall/builtin/sorter/lexical.ts",
5353
"./builtin/sorter/noop": "./denops/@fall/builtin/sorter/noop.ts",
54+
"./builtin/sorter/numerical": "./denops/@fall/builtin/sorter/numerical.ts",
5455
"./builtin/source": "./denops/@fall/builtin/source/mod.ts",
5556
"./builtin/source/buffer": "./denops/@fall/builtin/source/buffer.ts",
5657
"./builtin/source/file": "./denops/@fall/builtin/source/file.ts",
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
1+
import type { IdItem } from "../../item.ts";
12
import { defineSorter, type Sorter } from "../../sorter.ts";
23

3-
type Options = {
4+
type Options<T> = {
5+
attrGetter?: (item: IdItem<T>) => string;
46
reverse?: boolean;
57
};
68

7-
export function lexical<T>(options: Readonly<Options> = {}): Sorter<T> {
8-
const reverse = options.reverse ?? false;
9+
export function lexical<T>(options: Readonly<Options<T>> = {}): Sorter<T> {
10+
const attrGetter = options.attrGetter ?? ((item: IdItem<T>) => item.value);
11+
const alpha = options.reverse ? -1 : 1;
912
return defineSorter<T>((_denops, { items }, _options) => {
10-
if (reverse) {
11-
items.sort((a, b) =>
12-
b.value < a.value ? -1 : (b.value > a.value ? 1 : 0)
13-
);
14-
} else {
15-
items.sort((a, b) =>
16-
a.value < b.value ? -1 : (a.value > b.value ? 1 : 0)
17-
);
18-
}
13+
items.sort((a, b) => {
14+
const va = attrGetter(a);
15+
const vb = attrGetter(b);
16+
return (va < vb ? -1 : (va > vb ? 1 : 0)) * alpha;
17+
});
1918
});
2019
}

denops/@fall/builtin/sorter/mod.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
// This file is generated by gen-mod.ts
22
export * from "./lexical.ts";
33
export * from "./noop.ts";
4+
export * from "./numerical.ts";
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import type { IdItem } from "../../item.ts";
2+
import { defineSorter, type Sorter } from "../../sorter.ts";
3+
4+
type Options<T> = {
5+
attrGetter?: (item: IdItem<T>) => unknown;
6+
reverse?: boolean;
7+
};
8+
9+
export function numerical<T>(options: Readonly<Options<T>> = {}): Sorter<T> {
10+
const attrGetter = options.attrGetter ?? ((item: IdItem<T>) => item.value);
11+
const alpha = options.reverse ? -1 : 1;
12+
return defineSorter<T>((_denops, { items }, _options) => {
13+
items.sort((a, b) => {
14+
const va = attrGetter(a);
15+
const vb = attrGetter(b);
16+
const na = typeof va === "number" ? va : Number(va);
17+
const nb = typeof vb === "number" ? vb : Number(vb);
18+
if (isNaN(na) || isNaN(nb)) return 0;
19+
return Math.sign(na - nb) * alpha;
20+
});
21+
});
22+
}

0 commit comments

Comments
 (0)