@d-matrix/utils 1.19.0 → 1.20.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.
@@ -0,0 +1,2 @@
1
+ export declare function arrayMoveMutable<T>(array: T[], fromIndex: number, toIndex: number): void;
2
+ export declare function arrayMoveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[];
package/dist/array.js ADDED
@@ -0,0 +1,13 @@
1
+ export function arrayMoveMutable(array, fromIndex, toIndex) {
2
+ const startIndex = fromIndex < 0 ? array.length + fromIndex : fromIndex;
3
+ if (startIndex >= 0 && startIndex < array.length) {
4
+ const endIndex = toIndex < 0 ? array.length + toIndex : toIndex;
5
+ const [item] = array.splice(fromIndex, 1);
6
+ array.splice(endIndex, 0, item);
7
+ }
8
+ }
9
+ export function arrayMoveImmutable(array, fromIndex, toIndex) {
10
+ const newArray = [...array];
11
+ arrayMoveMutable(newArray, fromIndex, toIndex);
12
+ return newArray;
13
+ }
package/dist/index.d.ts CHANGED
@@ -11,3 +11,4 @@ export * as operator from './operator';
11
11
  export * as decimal from './decimal';
12
12
  export * as object from './object';
13
13
  export * as echarts from './echarts';
14
+ export * as array from './array';
package/dist/index.js CHANGED
@@ -11,3 +11,4 @@ export * as operator from './operator';
11
11
  export * as decimal from './decimal';
12
12
  export * as object from './object';
13
13
  export * as echarts from './echarts';
14
+ export * as array from './array';
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.19.0",
4
+ "version": "1.20.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
@@ -19,6 +19,7 @@ A dozen of utils for Front-End Development
19
19
  - [operator](#operator)
20
20
  - [decimal](#decimal)
21
21
  - [object](#object)
22
+ - [array](#array)
22
23
  - [echarts](#echarts)
23
24
 
24
25
  ### clipboard
@@ -484,6 +485,30 @@ removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g:
484
485
  // { b: 'abc', f: -1 }
485
486
  ```
486
487
 
488
+ ## array
489
+
490
+ - `arrayMoveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[]`
491
+
492
+ ```js
493
+ import { arrayMoveImmutable } from '@d-matrix/utils';
494
+
495
+ const input = ['a', 'b', 'c'];
496
+
497
+ const array1 = arrayMoveImmutable(input, 1, 2);
498
+ console.log(array1);
499
+ //=> ['a', 'c', 'b']
500
+
501
+ const array2 = arrayMoveImmutable(input, -1, 0);
502
+ console.log(array2);
503
+ //=> ['c', 'a', 'b']
504
+
505
+ const array3 = arrayMoveImmutable(input, -2, -3);
506
+ console.log(array3);
507
+ //=> ['b', 'a', 'c']
508
+ ```
509
+
510
+ - `arrayMoveMutable<T>(array: T[], fromIndex: number, toIndex: number): void`
511
+
487
512
  ## echarts
488
513
 
489
514
  - `mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption`