@d-matrix/utils 1.21.1 → 1.22.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/index.d.ts CHANGED
@@ -12,3 +12,4 @@ export * as decimal from './decimal';
12
12
  export * as object from './object';
13
13
  export * as echarts from './echarts';
14
14
  export * as array from './array';
15
+ export * as number from './number';
package/dist/index.js CHANGED
@@ -12,3 +12,4 @@ export * as decimal from './decimal';
12
12
  export * as object from './object';
13
13
  export * as echarts from './echarts';
14
14
  export * as array from './array';
15
+ export * as number from './number';
@@ -0,0 +1,2 @@
1
+ /** 返回[min, max]之间的随机整数 */
2
+ export declare function randomInt(min: number, max: number): number;
package/dist/number.js ADDED
@@ -0,0 +1,4 @@
1
+ /** 返回[min, max]之间的随机整数 */
2
+ export function randomInt(min, max) {
3
+ return Math.floor(Math.random() * (max - min + 1) + min);
4
+ }
package/dist/object.d.ts CHANGED
@@ -4,3 +4,10 @@ export declare const ZeroValues: ({} | null | undefined)[];
4
4
  * 默认的零值是:undefined、null、空字符串, NaN, [], {}
5
5
  */
6
6
  export declare const removeZeroValueKeys: <T extends Record<string, any>>(obj: T, zeroValues?: ({} | null | undefined)[]) => T;
7
+ /**
8
+ * 返回tuple,而不是string[]
9
+ * const obj = { a: 1, b: '2' };
10
+ * Object.keys(obj) => string[]
11
+ * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
12
+ */
13
+ export declare const typedKeys: <T extends object>(obj: T) => (keyof T)[];
package/dist/object.js CHANGED
@@ -19,3 +19,10 @@ export const removeZeroValueKeys = (obj, zeroValues = ZeroValues) => {
19
19
  }
20
20
  return r;
21
21
  };
22
+ /**
23
+ * 返回tuple,而不是string[]
24
+ * const obj = { a: 1, b: '2' };
25
+ * Object.keys(obj) => string[]
26
+ * typedKeys({ a: 1, b: '2' }) => ('a' | 'b')[]
27
+ */
28
+ export const typedKeys = Object.keys;
@@ -1,3 +1,9 @@
1
1
  import type { ForwardRefExoticComponent, NamedExoticComponent, ComponentPropsWithoutRef, ComponentPropsWithRef, RefAttributes, ElementType } from 'react';
2
2
  export declare type ComponentRef<T extends ElementType> = T extends NamedExoticComponent<ComponentPropsWithoutRef<T> & RefAttributes<infer Method>> ? Method : ComponentPropsWithRef<T> extends RefAttributes<infer Method> ? Method : never;
3
3
  export declare type InferRef<T> = T extends ForwardRefExoticComponent<infer Ref> ? Ref extends React.RefAttributes<infer RefElement> ? RefElement : never : never;
4
+ declare type ReactRefComponent<Props extends {
5
+ ref?: React.Ref<any> | string;
6
+ }> = (props: Props) => React.ReactNode;
7
+ declare type ExtractRefAttributesRef<T> = T extends React.RefAttributes<infer P> ? P : never;
8
+ export declare type GetRef<T extends ReactRefComponent<any> | React.Component<any>> = T extends React.Component<any> ? T : T extends React.ComponentType<infer P> ? ExtractRefAttributesRef<P> : never;
9
+ export {};
package/dist/types.d.ts CHANGED
@@ -15,3 +15,7 @@ export declare type NonFunctionPropertyNames<T> = {
15
15
  }[keyof T];
16
16
  /** 获取对象中key的值,返回由这些值组成的union type */
17
17
  export declare type ValueOf<T> = T[keyof T];
18
+ /** 指定属性变为必选 */
19
+ export declare type WithRequired<T, K extends keyof T> = T & {
20
+ [P in K]-?: T[P];
21
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@d-matrix/utils",
3
3
  "sideEffects": false,
4
- "version": "1.21.1",
4
+ "version": "1.22.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
@@ -20,6 +20,7 @@ A dozen of utils for Front-End Development
20
20
  - [decimal](#decimal)
21
21
  - [object](#object)
22
22
  - [array](#array)
23
+ - [number](#number)
23
24
  - [echarts](#echarts)
24
25
 
25
26
  ### clipboard
@@ -339,6 +340,18 @@ const map = {
339
340
  type T0 = ValueOf<typeof map>; // '0m' | '1m' | '2m' | '3m' | '4m' | '5m' | '6m'
340
341
  ```
341
342
 
343
+ - `WithRequired<T, K extends keyof T>`
344
+
345
+ 指定属性变为必选
346
+
347
+ ```ts
348
+ type Input = {
349
+ a: number;
350
+ b?: string;
351
+ };
352
+ type Output = WithRequired<Input, 'b'> // { a: number; b: string }
353
+ ```
354
+
342
355
  ### algorithm
343
356
 
344
357
  - `moveMulti<T extends unknown>(arr: T[], indexes: number[], start: number): T[]`
@@ -485,6 +498,16 @@ removeZeroValueKeys({ a: '', b: 'abc', c: undefined, d: null, e: NaN, f: -1, g:
485
498
  // { b: 'abc', f: -1 }
486
499
  ```
487
500
 
501
+ - `typedKeys(obj: T): Array<keyof T>`
502
+
503
+ 返回`tuple`,而不是`string[]`
504
+
505
+ ```ts
506
+ const obj = { a: 1, b: '2' };
507
+ Object.keys(obj) // string[]
508
+ object.typedKeys({ a: 1, b: '2' }) // ('a' | 'b')[]
509
+ ```
510
+
488
511
  ## array
489
512
 
490
513
  - `moveImmutable<T>(array: T[], fromIndex: number, toIndex: number): T[]`
@@ -522,6 +545,12 @@ const newList = array.moveToStart(list, (item) => item.id === 4);
522
545
  // [{ id: 4 }, { id: 1 }, { id: 2 }, { id: 3 }, { id: 5 }]
523
546
  ```
524
547
 
548
+ ## number
549
+
550
+ - `randomInt(min: number, max: number): number`
551
+
552
+ 返回`min`, `max`之间的随机整数
553
+
525
554
  ## echarts
526
555
 
527
556
  - `mergeOption(defaults: EChartsOption, overrides: EChartsOption, option?: deepmerge.Options): EChartsOption`