Options
All
  • Public
  • Public/Protected
  • All
Menu

External module @tomato-js/shared

Index

Type aliases

$Keys

$Keys<T>: keyof T

取出类型的keys

新增于v0.0.15

脚本举例

 import { $Keys } from "@tomato-js/shared"
 type Props = { name: string; age: number; visible: boolean };
 type newProps = $Keys<Props>;
 //'name'|'age'|'visible'

Type parameters

  • T: object

$Values

$Values<T>: T[keyof T]

取出类型的values

新增于v0.0.15

脚本举例

 import { $Values } from "@tomato-js/shared"
 type Props = { name: string; age: number; visible: boolean };
 type newProps = $Values<Props>;
 //string|number|boolean

Type parameters

  • T: object

Diff

Diff<T, U>: Pick<T, Exclude<keyof T, keyof U>>

两个类型取Diff

新增于v0.0.15

脚本举例

 import { Diff } from "@tomato-js/shared"
 type Props = { name: string; age: number; visible: boolean };
 type DefaultProps = { age: number };
 type newProps = Diff<Props, DefaultProps>;
 //{ name: string; visible: boolean };

Type parameters

  • T: object

  • U: object

Eachable

Eachable<T>: T[] | ObjectType<T>

Type parameters

  • T

Falsy

Falsy: false | "" | 0 | null | undefined

FunctionKeys

FunctionKeys<T>: {}[keyof T]

获取类型中value为函数的key

新增于v0.0.15

脚本举例

 import { FunctionKeys } from "@tomato-js/shared"
 type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;};
 type Keys = FunctionKeys<MixedProps>;
 //"setName | someFn"

Type parameters

  • T: object

FunctionType

FunctionType<T, U>: (...args: T[]) => U

Type parameters

  • T

  • U

Type declaration

    • (...args: T[]): U
    • Parameters

      • Rest ...args: T[]

      Returns U

HTMLElementKey

HTMLElementKey: keyof HTMLElementTagNameMap

HTMLSelector

HTMLSelector: string | HTMLElement

Intersection

Intersection<T, U>: Pick<T, Extract<keyof T, keyof U> & Extract<keyof U, keyof T>>

两个类型取交集

新增于v0.0.15

脚本举例

 import { Intersection } from "@tomato-js/shared"
 type Props = { name: string; age: number; visible: boolean };
 type DefaultProps = { age: number };
 type newProps = Intersection<Props, DefaultProps>;
 //{ age: number };

Type parameters

  • T: object

  • U: object

Methods

Methods: "get" | "post" | "put" | "delete" | "head" | "options" | "patch"

NonFunctionKeys

NonFunctionKeys<T>: {}[keyof T]

获取类型中value不为函数的key

新增于v0.0.15

脚本举例

 import { NonFunctionKeys } from "@tomato-js/shared"
 type MixedProps = {name: string; setName: (name: string) => void; someKeys?: string; someFn?: (...args: any) => any;};
 type Keys = NonFunctionKeys<MixedProps>;
 //"name"

Type parameters

  • T: object

NonUndefined

NonUndefined<A>: A extends undefined ? never : A

Type parameters

  • A

Optional

Optional<T, K>: Partial<Pick<T, K>>

构建可选的新类型

新增于v0.0.15

脚本举例

 import { Optional } from "@tomato-js/shared"
 type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; };
 type OptionalType = Optional<Props>;
 //{ req?: number; reqUndef?: number | undefined; opt?: string; optUndef?: number | undefined; };
 type OptionalType2 = Optional<Props,'req'|'reqUndef'>;
 //{ req?: number; reqUndef?: number | undefined;};

Type parameters

  • T: object

  • K: keyof T

OptionalKeys

OptionalKeys<T>: {}[keyof T]

获取可选项的key

新增于v0.0.15

脚本举例

 import { OptionalKeys } from "@tomato-js/shared"
 type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; };
 type Keys = OptionalKeys<Props>;
 //"opt" | "optUndef"

Type parameters

  • T

RequiredKeys

RequiredKeys<T>: {}[keyof T]

获取必填项的key

新增于v0.0.15

脚本举例

 import { RequiredKeys } from "@tomato-js/shared"
 type Props = { req: number; reqUndef: number | undefined; opt?: string; optUndef?: number | undefined; };
 type Keys = RequiredKeys<Props>;
 //"req" | "reqUndef"

Type parameters

  • T

Variables

Const asReg

asReg: RegExp = /(.+)\sas\s(.+)/gi

Const blankReg

blankReg: RegExp = /[\s\r\n]+/gi

Const chineseReg

chineseReg: RegExp = /^(?:[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\uD86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/

中文汉字

Const englishReg

englishReg: RegExp = /^[a-zA-Z]+$/

英文字母

Const integerReg

integerReg: RegExp = /^(-|\+)?\d+$/

整数

Const negativeNumReg

negativeNumReg: RegExp = /^-[1-9]d*$/

负整数

Const positiveNumReg

positiveNumReg: RegExp = /^[1-9]d*$/

正整数

Functions

forEach

  • forEach(elements: Eachable<any>, func: (k: any, v: any) => any): void
  • 遍历对象或数组

    脚本举例

      import { forEach } from '@tomato-js/shared'
      const obj = {
        a: 1,
        b: 2
      };
      const obj2 = {};
      const arr = [1,2,3,4,5];
      forEach(obj, (k, v) => (obj2[v] = k));
      forEach(obj, (k, v) => console.log(v));

    Parameters

    • elements: Eachable<any>

      需要遍历的值

    • func: (k: any, v: any) => any

      执行函数

        • (k: any, v: any): any
        • Parameters

          • k: any
          • v: any

          Returns any

    Returns void

isArray

  • isArray(value: unknown): boolean

isDef

  • isDef(val: unknown): val is NonNullable<any>
  • 是否已定义

    新增于v0.0.18

    脚本举例

      import { isDef } from '@tomato-js/shared'
      const node = isDef(undefined);//false

    Parameters

    • val: unknown

      需要判断的值

    Returns val is NonNullable<any>

    是否为已定义的值

isEmpty

  • isEmpty(collection: any, containNil?: boolean): boolean
  • 是否为空对象或空数组

    新增于v0.0.22

    脚本举例

      import { isEmpty } from '@tomato-js/shared'
      const node = isEmptyArray([]);//true
      const node = isEmptyArray([1]);//false

    Parameters

    • collection: any

      需要判断的集合

    • Default value containNil: boolean = true

      是否需要算上null或undefined,默认为true

    Returns boolean

    是否为没有属性的空对象如{},数组如[],undefined,null

isEmptyArray

  • isEmptyArray(arr: any[]): boolean
  • 是否为空数组

    新增于v0.0.22

    脚本举例

      import { isEmptyArray } from '@tomato-js/shared';
      const node = isEmptyArray([]);//true
      const node = isEmptyArray([1]);//false

    Parameters

    • arr: any[]

      需要判断的数组

    Returns boolean

    是否为没有属性的数组比如[]

isEmptyObject

  • isEmptyObject(object: any): boolean
  • 是否为空对象

    脚本举例

      import { isEmptyObject } from '@tomato-js/shared'
      const node = isEmptyObject({});//true

    Parameters

    • object: any

      需要判断的对象

    Returns boolean

    是否为没有属性的空对象如{}

isFalsy

  • isFalsy(val: unknown): val is Falsy
  • 是否为falsy类型

    脚本举例

      import { isFalsy } from '@tomato-js/shared'
      isFalsy(1);//false
      isFalsy('');//true

    Parameters

    • val: unknown

      需要判断的值

    Returns val is Falsy

    是否为数字或字符串数字类型

isFunction

  • isFunction(value: unknown): value is Function

isNil

  • isNil(val: unknown): val is undefined | null
  • 是否为 null 或 undefined 类型

    脚本举例

      import { isNil } from '@tomato-js/shared'
      const node = isNil(undefined);//true

    Parameters

    • val: unknown

      需要判断的值

    Returns val is undefined | null

    是否为undefined或null

isNull

  • isNull(val: unknown): val is null
  • 是否是Null

    脚本举例

      import { isNull } from '@tomato-js/shared'
      const node = isNull(null);//true

    Parameters

    • val: unknown

      需要判断的值

    Returns val is null

    是否为null

isNumber

  • isNumber(str: unknown): str is number

isNumberLike

  • isNumberLike(val: unknown): boolean
  • 是否为 数字或字符串数字类型

    脚本举例

      import { isNumberLike } from '@tomato-js/shared'
      isNumberLike(‘3’);//true
      isNumberLike(3);//true

    Parameters

    • val: unknown

      需要判断的值

    Returns boolean

    是否为数字或字符串数字类型

isObject

  • isObject(value: unknown): boolean

isString

  • isString(str: unknown): str is string

isType

  • isType<T>(value: unknown, type: string): value is T

isUndefined

  • isUndefined(val: unknown): val is undefined
  • 是否是undefined

    脚本举例

      import { isUndefined } from '@tomato-js/shared'
      const node = isUndefined(undefined);//true

    Parameters

    • val: unknown

      需要判断的值

    Returns val is undefined

    是否为undefined

map

  • map(elements: Eachable<any>, func: (k: any, v: any) => any): undefined | ObjectType<any>
  • 遍历对象或数组,返回新对象或数组

    脚本举例

      import { map } from '@tomato-js/shared'
      const obj = {
        a: 1,
        b: 2
      };
      const obj2 = {};
      const arr = [1,2,3,4,5];
      map(obj, (k, v) => (obj2[v] = k));
      map(arr, (k, v) => v=v+1);//[2,3,4,5,6]

    Parameters

    • elements: Eachable<any>

      需要遍历的值

    • func: (k: any, v: any) => any

      执行函数

        • (k: any, v: any): any
        • Parameters

          • k: any
          • v: any

          Returns any

    Returns undefined | ObjectType<any>

Const noop

  • noop(...args: any[]): any

not

  • not(a: any): boolean
  • 取反

    脚本举例

      import { not } from '@tomato-js/shared'
      not(true)//false;
      not(false)//true;
      not(0)//true;
      not(1)//false;

    Parameters

    • a: any

      需要取反的值

    Returns boolean

    取反后的值

Generated using TypeDoc