File tree Expand file tree Collapse file tree 4 files changed +35
-12
lines changed
denops/@fall/builtin/sorter Expand file tree Collapse file tree 4 files changed +35
-12
lines changed Original file line number Diff line number Diff line change 51
51
"./builtin/sorter" : " ./denops/@fall/builtin/sorter/mod.ts" ,
52
52
"./builtin/sorter/lexical" : " ./denops/@fall/builtin/sorter/lexical.ts" ,
53
53
"./builtin/sorter/noop" : " ./denops/@fall/builtin/sorter/noop.ts" ,
54
+ "./builtin/sorter/numerical" : " ./denops/@fall/builtin/sorter/numerical.ts" ,
54
55
"./builtin/source" : " ./denops/@fall/builtin/source/mod.ts" ,
55
56
"./builtin/source/buffer" : " ./denops/@fall/builtin/source/buffer.ts" ,
56
57
"./builtin/source/file" : " ./denops/@fall/builtin/source/file.ts" ,
Original file line number Diff line number Diff line change
1
+ import type { IdItem } from "../../item.ts" ;
1
2
import { defineSorter , type Sorter } from "../../sorter.ts" ;
2
3
3
- type Options = {
4
+ type Options < T > = {
5
+ attrGetter ?: ( item : IdItem < T > ) => string ;
4
6
reverse ?: boolean ;
5
7
} ;
6
8
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 ;
9
12
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
+ } ) ;
19
18
} ) ;
20
19
}
Original file line number Diff line number Diff line change 1
1
// This file is generated by gen-mod.ts
2
2
export * from "./lexical.ts" ;
3
3
export * from "./noop.ts" ;
4
+ export * from "./numerical.ts" ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments