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 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" ,
Original file line number Diff line number Diff line change 1+ import type { IdItem } from "../../item.ts" ;
12import { 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}
Original file line number Diff line number Diff line change 11// This file is generated by gen-mod.ts
22export * from "./lexical.ts" ;
33export * 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