@d-matrix/utils 1.20.0 → 1.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/array.d.ts +3 -2
- package/dist/array.js +9 -3
- package/package.json +1 -1
- package/readme.md +19 -6
package/dist/array.d.ts
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
export declare function
|
|
2
|
-
export declare function
|
|
1
|
+
export declare function moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void;
|
|
2
|
+
export declare function moveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[];
|
|
3
|
+
export declare function moveToStart<T>(array: T[], predicate: (item: T) => boolean): T[];
|
package/dist/array.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export function
|
|
1
|
+
export function moveMutable(array, fromIndex, toIndex) {
|
|
2
2
|
const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
|
|
3
3
|
if (startIndex >= 0 && startIndex < array.length) {
|
|
4
4
|
const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
|
|
@@ -6,8 +6,14 @@ export function arrayMoveMutable(array, fromIndex, toIndex) {
|
|
|
6
6
|
array.splice(endIndex, 0, item);
|
|
7
7
|
}
|
|
8
8
|
}
|
|
9
|
-
export function
|
|
9
|
+
export function moveImmutable(array, fromIndex, toIndex) {
|
|
10
10
|
const newArray = [...array];
|
|
11
|
-
|
|
11
|
+
moveMutable(newArray, fromIndex, toIndex);
|
|
12
|
+
return newArray;
|
|
13
|
+
}
|
|
14
|
+
export function moveToStart(array, predicate) {
|
|
15
|
+
const newArray = [...array];
|
|
16
|
+
const index = array.findIndex(predicate);
|
|
17
|
+
moveMutable(newArray, index, 0);
|
|
12
18
|
return newArray;
|
|
13
19
|
}
|
package/package.json
CHANGED
package/readme.md
CHANGED
|
@@ -487,27 +487,40 @@ removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g:
|
|
|
487
487
|
|
|
488
488
|
## array
|
|
489
489
|
|
|
490
|
-
- `
|
|
490
|
+
- `moveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[]`
|
|
491
491
|
|
|
492
492
|
```js
|
|
493
|
-
import {
|
|
493
|
+
import { array } from '@d-matrix/utils';
|
|
494
494
|
|
|
495
495
|
const input = ['a', 'b', 'c'];
|
|
496
496
|
|
|
497
|
-
const array1 =
|
|
497
|
+
const array1 = array.moveImmutable(input, 1, 2);
|
|
498
498
|
console.log(array1);
|
|
499
499
|
//=> ['a', 'c', 'b']
|
|
500
500
|
|
|
501
|
-
const array2 =
|
|
501
|
+
const array2 = array.moveImmutable(input, -1, 0);
|
|
502
502
|
console.log(array2);
|
|
503
503
|
//=> ['c', 'a', 'b']
|
|
504
504
|
|
|
505
|
-
const array3 =
|
|
505
|
+
const array3 = array.moveImmutable(input, -2, -3);
|
|
506
506
|
console.log(array3);
|
|
507
507
|
//=> ['b', 'a', 'c']
|
|
508
508
|
```
|
|
509
509
|
|
|
510
|
-
- `
|
|
510
|
+
- `moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void`
|
|
511
|
+
|
|
512
|
+
- `moveToStart<T>(array: T[], predicate: (item: T) => boolean): T[]`
|
|
513
|
+
|
|
514
|
+
移动元素到数组首位,不会修改原数组
|
|
515
|
+
|
|
516
|
+
```js
|
|
517
|
+
import { array } from '@d-matrix/utils';
|
|
518
|
+
|
|
519
|
+
const list = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }];
|
|
520
|
+
const newList = array.moveToStart(list, (item) => item.id === 4);
|
|
521
|
+
|
|
522
|
+
// [{ id: 4 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 5 }]
|
|
523
|
+
```
|
|
511
524
|
|
|
512
525
|
## echarts
|
|
513
526
|
|