1
1
import {
2
+ getYangHuiTriangleOne ,
3
+ arrayRemoveItemsBy ,
4
+ getYangHuiTriangle ,
5
+ binaryFindIndex ,
6
+ arrayRemoveItem ,
7
+ findIndexRight ,
8
+ insertToArray ,
9
+ forEachAround ,
10
+ forEachRight ,
2
11
createArray ,
3
- forEach ,
4
12
forEachObj ,
5
- isEqual ,
6
- forEachRight ,
7
- findIndex ,
8
- findIndexRight ,
9
13
binaryFind ,
10
- binaryFindIndex ,
11
- insertToArray ,
12
- arrayRemoveItem ,
13
- arrayRemoveItemsBy ,
14
- unique ,
15
- chunk ,
16
- inRange ,
17
- inRanges ,
18
- groupBy ,
19
14
someInList ,
15
+ findIndex ,
20
16
castArray ,
17
+ joinArray ,
18
+ inRanges ,
19
+ forEach ,
20
+ isEqual ,
21
+ inRange ,
22
+ groupBy ,
23
+ unique ,
24
+ chunk ,
21
25
sum ,
22
26
avg ,
23
- getYangHuiTriangle ,
24
- getYangHuiTriangleOne ,
25
- forEachAround ,
26
- joinArray ,
27
27
} from '../../src' ;
28
28
29
29
describe ( 'array' , function ( ) {
@@ -38,21 +38,21 @@ describe('array', function () {
38
38
expect ( createArray ( { start : 3 , len : 1 , end : 5 } ) ) . toEqual ( [ 3 ] ) ;
39
39
expect (
40
40
createArray ( {
41
- start : 3 ,
42
- len : 5 ,
43
- end : 5 ,
44
41
fill ( item , index ) {
45
42
return item + '' + index ;
46
43
} ,
44
+ start : 3 ,
45
+ len : 5 ,
46
+ end : 5 ,
47
47
} ) ,
48
48
) . toEqual ( [ '30' , '41' ] ) ;
49
- expect ( createArray ( { start : 3 , len : 5 , end : 6 , fill : 0 } ) ) . toEqual ( [ 0 , 0 , 0 ] ) ;
49
+ expect ( createArray ( { start : 3 , fill : 0 , len : 5 , end : 6 } ) ) . toEqual ( [ 0 , 0 , 0 ] ) ;
50
50
51
51
// 测试fill参数
52
52
const fn = jest . fn ( ( a ) => a ) ;
53
53
54
54
// end为len
55
- createArray ( { len : 5 , fill : fn } ) ;
55
+ createArray ( { fill : fn , len : 5 } ) ;
56
56
expect ( fn . mock . calls ) . toEqual ( [
57
57
[ 0 , 0 , 5 ] ,
58
58
[ 1 , 1 , 5 ] ,
@@ -64,7 +64,7 @@ describe('array', function () {
64
64
fn . mock . calls . length = 0 ;
65
65
66
66
// end为start+len
67
- createArray ( { start : 3 , len : 5 , fill : fn } ) ;
67
+ createArray ( { start : 3 , fill : fn , len : 5 } ) ;
68
68
expect ( fn . mock . calls ) . toEqual ( [
69
69
[ 3 , 0 , 8 ] ,
70
70
[ 4 , 1 , 8 ] ,
@@ -76,7 +76,7 @@ describe('array', function () {
76
76
fn . mock . calls . length = 0 ;
77
77
78
78
// end为end
79
- createArray ( { start : 2 , end : 3 , fill : fn } ) ;
79
+ createArray ( { start : 2 , fill : fn , end : 3 } ) ;
80
80
expect ( fn . mock . calls ) . toEqual ( [ [ 2 , 0 , 3 ] ] ) ;
81
81
} ) ;
82
82
test ( 'forEach' , ( ) => {
@@ -86,7 +86,7 @@ describe('array', function () {
86
86
expect ( isDone ) . toBe ( true ) ;
87
87
88
88
// ArrayLike
89
- isDone = forEach ( { 0 : 1 , 1 : 2 , length : 2 } , ( _v , k ) => ( arr1 [ k ] = k + k ) ) ;
89
+ isDone = forEach ( { length : 2 , 0 : 1 , 1 : 2 } , ( _v , k ) => ( arr1 [ k ] = k + k ) ) ;
90
90
expect ( isEqual ( arr1 , [ 0 , 2 , 2 ] ) ) . toBe ( true ) ;
91
91
expect ( isDone ) . toBe ( true ) ;
92
92
@@ -209,12 +209,12 @@ describe('array', function () {
209
209
const list : { id : number } [ ] = Array . from ( { length : 100 } ) . map ( ( _ , i ) => ( { id : i * 2 } ) ) ;
210
210
211
211
function find ( target : number ) : {
212
- times : number ;
213
212
index : ReturnType < typeof binaryFindIndex > ;
213
+ times : number ;
214
214
} {
215
215
// 查找次数
216
216
let times = 0 ;
217
- const index = binaryFindIndex ( list , function ( { item , index, start, end } ) {
217
+ const index = binaryFindIndex ( list , function ( { index, start, item , end } ) {
218
218
times ++ ;
219
219
// console.log(index);
220
220
// 判断index是否正确
@@ -281,9 +281,9 @@ describe('array', function () {
281
281
test ( 'binaryFind' , ( ) => {
282
282
const list : { id : number } [ ] = [ ...Array ( 100 ) . keys ( ) ] . map ( ( i ) => ( { id : i * 2 } ) ) ;
283
283
284
- function find ( target : number ) : { times : number ; value : ReturnType < typeof binaryFind > } {
284
+ function find ( target : number ) : { value : ReturnType < typeof binaryFind > ; times : number } {
285
285
let times = 0 ;
286
- const value = binaryFind ( list , ( { item , index } ) => {
286
+ const value = binaryFind ( list , ( { index , item } ) => {
287
287
times ++ ;
288
288
// console.log(index);
289
289
// 判断index是否正确
@@ -315,23 +315,23 @@ describe('array', function () {
315
315
// 正常查找
316
316
let findTimes = 0 ;
317
317
const arr = [
318
- { id : 1 , text : '1' } ,
319
- { id : 2 , text : '2' } ,
320
- { id : 3 , text : '3' } ,
321
- { id : 4 , text : '4' } ,
322
- { id : 5 , text : '5' } ,
323
- { id : 6 , text : '6' } ,
318
+ { text : '1' , id : 1 } ,
319
+ { text : '2' , id : 2 } ,
320
+ { text : '3' , id : 3 } ,
321
+ { text : '4' , id : 4 } ,
322
+ { text : '5' , id : 5 } ,
323
+ { text : '6' , id : 6 } ,
324
324
] ;
325
325
326
- expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 3 - o . item . id ) ) ) . toEqual ( { id : 3 , text : '3' } ) ;
326
+ expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 3 - o . item . id ) ) ) . toEqual ( { text : '3' , id : 3 } ) ;
327
327
expect ( findTimes ) . toBe ( 3 ) ;
328
328
329
329
findTimes = 0 ;
330
- expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 2 - o . item . id ) ) ) . toEqual ( { id : 2 , text : '2' } ) ;
330
+ expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 2 - o . item . id ) ) ) . toEqual ( { text : '2' , id : 2 } ) ;
331
331
expect ( findTimes ) . toBe ( 2 ) ;
332
332
333
333
findTimes = 0 ;
334
- expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 6 - o . item . id ) ) ) . toEqual ( { id : 6 , text : '6' } ) ;
334
+ expect ( binaryFind ( arr , ( o ) => ( findTimes ++ , 6 - o . item . id ) ) ) . toEqual ( { text : '6' , id : 6 } ) ;
335
335
expect ( findTimes ) . toBe ( 2 ) ;
336
336
337
337
findTimes = 0 ;
@@ -382,7 +382,7 @@ describe('array', function () {
382
382
383
383
const a2 : number [ ] = [ ] ;
384
384
expect (
385
- insertToArray ( 3 , ( { item , insert } ) => ( a2 . push ( item ) , item < insert ) , arr7 , {
385
+ insertToArray ( 3 , ( { insert , item } ) => ( a2 . push ( item ) , item < insert ) , arr7 , {
386
386
reverse : true ,
387
387
after : true ,
388
388
} ) ,
@@ -450,7 +450,7 @@ describe('array', function () {
450
450
// 恢复
451
451
arr = [ 1 , 2 , 3 , 2 ] ;
452
452
// 反向查找
453
- expect ( insertToArray ( 5 , ( o ) => o . item === 2 , arr , { after : false , reverse : true } ) ) . toBe ( 3 ) ;
453
+ expect ( insertToArray ( 5 , ( o ) => o . item === 2 , arr , { reverse : true , after : false } ) ) . toBe ( 3 ) ;
454
454
expect ( arr ) . toEqual ( [ 1 , 2 , 3 , 5 , 2 ] ) ;
455
455
456
456
// 插入多个个 动态位置
@@ -504,7 +504,7 @@ describe('array', function () {
504
504
expect ( chunk ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , 1 ) ) . toEqual ( [ [ 0 ] , [ 1 ] , [ 2 ] , [ 3 ] , [ 4 ] , [ 5 ] , [ 6 ] ] ) ;
505
505
expect ( chunk ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , 0 ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
506
506
// 不支持-1
507
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
507
+
508
508
// @ts -expect-error
509
509
expect ( chunk ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , - 1 ) ) . toEqual ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] ) ;
510
510
expect ( chunk ( [ 0 , 1 , 2 , 3 , 4 , 5 , 6 ] , 3 ) ) . toEqual ( [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 ] ] ) ;
@@ -567,25 +567,25 @@ describe('array', function () {
567
567
expect (
568
568
groupBy (
569
569
[
570
- { type : 1 , value : 111 } ,
571
- { type : 2 , value : 222 } ,
572
- { type : 1 , value : 222 } ,
573
- { type : 2 , value : 33344 } ,
574
- { type : 1 , value : 333 } ,
575
- { type : 1 , value : 444 } ,
570
+ { value : 111 , type : 1 } ,
571
+ { value : 222 , type : 2 } ,
572
+ { value : 222 , type : 1 } ,
573
+ { value : 33344 , type : 2 } ,
574
+ { value : 333 , type : 1 } ,
575
+ { value : 444 , type : 1 } ,
576
576
] ,
577
577
'type' ,
578
578
) ,
579
579
) . toEqual ( {
580
580
1 : [
581
- { type : 1 , value : 111 } ,
582
- { type : 1 , value : 222 } ,
583
- { type : 1 , value : 333 } ,
584
- { type : 1 , value : 444 } ,
581
+ { value : 111 , type : 1 } ,
582
+ { value : 222 , type : 1 } ,
583
+ { value : 333 , type : 1 } ,
584
+ { value : 444 , type : 1 } ,
585
585
] ,
586
586
2 : [
587
- { type : 2 , value : 222 } ,
588
- { type : 2 , value : 33344 } ,
587
+ { value : 222 , type : 2 } ,
588
+ { value : 33344 , type : 2 } ,
589
589
] ,
590
590
} ) ;
591
591
expect ( groupBy ( [ ] , '' ) ) . toEqual ( { } ) ;
@@ -625,7 +625,7 @@ describe('array', function () {
625
625
{ name : 'b' , score : 90 } ,
626
626
{ name : 'c' , score : 70 } ,
627
627
{ name : 'd' , score : 10 } ,
628
- { name : 'e' , score : 100 } ,
628
+ { score : 100 , name : 'e' } ,
629
629
] ,
630
630
( item ) => {
631
631
const score = item . score ;
@@ -637,13 +637,13 @@ describe('array', function () {
637
637
) . toEqual ( {
638
638
A : [
639
639
{ name : 'b' , score : 90 } ,
640
- { name : 'e' , score : 100 } ,
640
+ { score : 100 , name : 'e' } ,
641
641
] ,
642
- B : [ { name : 'c' , score : 70 } ] ,
643
642
C : [
644
643
{ name : 'a' , score : 50 } ,
645
644
{ name : 'd' , score : 10 } ,
646
645
] ,
646
+ B : [ { name : 'c' , score : 70 } ] ,
647
647
} ) ;
648
648
649
649
const b = groupBy ( [ 50 , 90 , 70 , 10 , 100 ] , ( score ) => {
@@ -655,7 +655,7 @@ describe('array', function () {
655
655
656
656
const c = groupBy (
657
657
[ 50 , 90 , 70 , 10 , 100 ] ,
658
- ( score ) : 'A' | 'B' | void => {
658
+ ( score ) : void | 'A' | 'B' => {
659
659
if ( score >= 90 ) return 'A' ;
660
660
if ( score >= 60 ) return 'B' ;
661
661
} ,
@@ -674,8 +674,8 @@ describe('array', function () {
674
674
} ) ,
675
675
) . toEqual ( {
676
676
A : [ 90 , 100 ] ,
677
- B : [ 70 ] ,
678
677
C : [ 50 , 10 ] ,
678
+ B : [ 70 ] ,
679
679
} ) ;
680
680
expect (
681
681
groupBy (
@@ -688,8 +688,8 @@ describe('array', function () {
688
688
) ,
689
689
) . toEqual ( {
690
690
A : [ 90 , 100 ] ,
691
- B : [ 70 ] ,
692
691
C : [ 50 , 10 ] ,
692
+ B : [ 70 ] ,
693
693
} ) ;
694
694
695
695
const list = [
@@ -839,7 +839,7 @@ describe('array', function () {
839
839
res . push ( v ) ;
840
840
if ( i [ 0 ] === 0 && i [ 1 ] === 0 ) return false ;
841
841
} ,
842
- { startIndexes : [ 1 , 4 ] , startDirect : 'bottom' } ,
842
+ { startDirect : 'bottom' , startIndexes : [ 1 , 4 ] } ,
843
843
) ;
844
844
expect ( res ) . toEqual ( [ 10 , 15 , 20 , 25 , 24 , 23 , 22 , 21 , 16 , 11 , 6 , 1 ] ) ;
845
845
0 commit comments