Options
All
  • Public
  • Public/Protected
  • All
Menu

@mimi-utils/types

Index

Type Aliases [Number 类]

Type Aliases [Object 类]

Type Aliases [String 类]

[Number 类] Type Aliases

IsPositiveInteger<N>: _Is<`${N}`>

是否为正整数

returns

boolean

example
type cases = [
Expect<Equal<IsPositiveInteger<1111>, true>>,
Expect<Equal<IsPositiveInteger<1111.111>, false>>,
Expect<Equal<IsPositiveInteger<'11123123.123.123.123'>, false>>,
Expect<Equal<IsPositiveInteger<'.123.???????kj'>, false>>,
Expect<Equal<IsPositiveInteger<'1231231230123012301230123'>, true>>
];

Type Parameters

  • N extends string | number

    需验证的 字符 或 整数

[Object 类] Type Aliases

GetKeyPathsByObject<Source, Options>: Options["returnType"] extends "array" ? _GetArrayPaths<Source> : _GetStringPaths<Source, unknown extends Options["returnStringTypes"] ? ["R1", "R2"] : Options["returnStringTypes"]>

获取对象的 键路径


场景:lodash-set、lodash-get 等

returns

Options 中的 returnType 字段决定是 ArrayString,默认为 Array

example
type TestObj1 = {
a: {
b: [1, { c: [1] }];
};
};

// ["a"?, "b"?, "0"?] | ["a"?, "b"?, "1"?, "c"?, "0"?]
type Test1 = GetKeyPathsByObject<TestObj1>;
const case1: Test1[] = [
['a', 'b', '0'],
['a', 'b']
];


// "a" | "a.b" | "a.b.0" | "a.b[0]" | "a.b.1" | "a.b[1]" |
// "a.b.1.c" | "a.b[1].c" | "a.b.1.c.0" | "a.b[1].c[0]"
type Test2 = GetKeyPathsByObject<TestObj1, { returnType: 'string' }>;
const case2: Test2[] = ['a', 'a.b.1.c.0', 'a.b[1].c[0]'];


// "a" | "['a']" | "a.b" | "['a']['b']" | "a.b.0" | "a.b[0]" |
// "['a']['b']['0']" | "a.b.1" | "a.b[1]" | "['a']['b']['1']" |
// "a.b.1.c" | "a.b[1].c" | "['a']['b']['1']['c']" | "a.b.1.c.0" |
//"a.b[1].c[0]" | "['a']['b']['1']['c']['0']"
type Test3 = GetKeyPathsByObject<TestObj1, { returnType: 'string'; returnStringTypes: ['R1', 'R2', 'R3'] }>;
const case3: Test3[] = ["['a']['b']['0']", 'a.b.1.c.0', "['a']['b']['1']['c']['0']", 'a.b[1].c[0]'];

Type Parameters

  • Source extends object

    Object 源

  • Options extends _Options = { returnType: "array" }

    获取的选项

[String 类] Type Aliases

GetContentBetweenTwoChar<Str, StartChar, EndChar>: [S.Length<StartChar>, S.Length<EndChar>] extends [1, 1] ? _Get<Str, StartChar, EndChar> : _Warn<S.Length<StartChar>, S.Length<EndChar>>

获取两个字符之间的内容

例如取出 {} 字符之间内容,Hello, {name},即 name


场景:取出字符中的占位符(i18n-key、接口地址等等)

example

type Str = `hello, my name is {name}, are you from {country}?`;

type Test1 = GetContentBetweenTwoChar<Str, "{", "}">;
type Test2 = GetContentBetweenTwoChar<Str, "{{", "}">;
type Test3 = GetContentBetweenTwoChar<Str, "{", "}}">;
type Test4 = GetContentBetweenTwoChar<Str, "", "">;
type Test5 = GetContentBetweenTwoChar<Str, ".", "">;

type cases = [
Expect<Equal<Test1, "name" | "country">>,
Expect<Equal<Test2, "The length of `StartChar` cannot be greater than 1">>,
Expect<Equal<Test3, "The length of `EndChar` cannot be greater than 1">>,
Expect<Equal<Test4, "`StartChar` cannot be empty">>,
Expect<Equal<Test5, "`EndChar` cannot be empty">>,
];

Type Parameters

  • Str extends string

    字符串源

  • StartChar extends string

    需要作为获取条件的第一个字符

  • EndChar extends string

    需要作为获取条件的第二个字符