@d-matrix/utils 1.20.1 → 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 CHANGED
@@ -1,2 +1,3 @@
1
1
  export declare function moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void;
2
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
@@ -11,3 +11,9 @@ export function moveImmutable(array, fromIndex, toIndex) {
11
11
  moveMutable(newArray, fromIndex, toIndex);
12
12
  return newArray;
13
13
  }
14
+ export function moveToStart(array, predicate) {
15
+ const newArray = [...array];
16
+ const index = array.findIndex(predicate);
17
+ moveMutable(newArray, index, 0);
18
+ return newArray;
19
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.20.1",
4
+ "version": "1.21.0",
5
5
  "description": "A dozen of utils for Front-End Development",
6
6
  "main": "dist/index.js",
7
7
  "scripts": {
package/readme.md CHANGED
@@ -509,6 +509,19 @@ console.log(array3);
509
509
 
510
510
  - `moveMutable<T>(array: T[], fromIndex: number, toIndex: number): void`
511
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
+ ```
524
+
512
525
  ## echarts
513
526
 
514
527
  - `mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption`