Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Enhancing core package, which provides common utils and types definition #13

Merged
merged 4 commits into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./src/monitor/index";
export * from "./src/indexdb/index";
export * from "./src/use-spliter/index";
export * from "./src/utils/index";
2 changes: 1 addition & 1 deletion packages/core/src/use-spliter/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isString, isFunction } from "lodash-es";
import type { Unit } from "../utils";

export type Unit = string | number;
export type TransFormFn<T extends Unit = Unit> = (v: string, index: number, arr: string[]) => T;

export default function useSpliter(spliter = ":") {
Expand Down
122 changes: 122 additions & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import { merge } from "lodash-es";

export type UnionNull<T> = T | null;
export type UnionUndefined<T> = T | undefined;
export type UnionNone<T> = UnionNull<T> | UnionUndefined<T>;
export type DeepPartial<T> = {
[K in keyof T]?: DeepPartial<T[K]>;
};
export interface ErrorTrait<T = boolean> {
error: T;
}

export type Key = keyof any;

Check warning on line 13 in packages/core/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
export type Unit<T = unknown> = T extends unknown ? string | number : string | number | T;

export interface IOption<T extends Unit = Unit> {
key: string | number;
label: string;
value: T;
[k: string]: unknown;
}
export interface TreeNode<T extends Unit = Unit> {
key: string | number;
label: string;
value: T;
icon?: string;
count?: number;
children?: TreeNode<T>[];
[k: string]: unknown;
}
export interface NodeOptions extends IOption {
children?: NodeOptions[];
}

export const genOptionMap = (options: IOption[], _fieldNames: Partial<FieldNames> = genFieldNames()) => {
const fieldNames = merge(genFieldNames(), _fieldNames);

return options.reduce(
(target, item) => {
target[item[fieldNames.key] as string] = item;
return target;
},
{} as Record<string, IOption>,
);
};

export interface HttpResponse<T = unknown> {
status: number;
errMsg: string;
errCode: number;
data: T;
}
export interface ResponseWrap<T = unknown> {
errCode: number;
errMsg: string;
data: T;
}

export interface ListResults<T = unknown> {
items: T[];
total: number;
data: T[];
count: number;
}

export interface FieldNames {
label: string;
value: string;
key: string;
icon: string;
name: string;
children: string;
}
export type PartialFieldNames = Partial<FieldNames>;
export const genFieldNames = (defaultValue?: Partial<FieldNames>): FieldNames => ({
key: "key",
label: "label",
value: "value",
icon: "icon",
name: "name",
children: "children",
...(defaultValue || {}),
});

export const PrivateID = "__pid__";
export type UnionPrivateID<T> = T & { [PrivateID]: string };
export type IterFn<T> = (value: T, index: number, arr: T[]) => UnionPrivateID<T>;
export const unionPrivateId = <T = Record<string, any>>(arr: T[], iter: IterFn<T>): UnionPrivateID<T>[] => {

Check warning on line 88 in packages/core/src/utils/index.ts

View workflow job for this annotation

GitHub Actions / lint

Unexpected any. Specify a different type
return arr.map(iter);
};

export interface RangeFnOptions {
left: number;
right: number;
contains: [boolean, boolean]; // 左开右闭,左闭右开,左开右开,左闭右闭
}
export const inRangeFn = (
val: number,
opt: RangeFnOptions = {
left: 0,
right: 100,
contains: [true, false],
},
) => {
if (Number.isNaN(val)) return false;
if (typeof val === "number") {
const { left, right, contains = [true, false] } = opt;
const leftRes = contains[0] ? val >= left : val > left;
const rightRes = contains[1] ? val <= right : val < right;

return leftRes && rightRes;
}
return false;
};

/**
* 在 template 格式化中,`v as any` 可能影响 template 代码块的高亮,使用 `(v as any)` 时,prettier 会将其变为 `v as any`,导致高亮问题。
* 当然,可以选择使用 `(v, v as any)` 方式避免自动格式化去掉括号
* @param {any} value
* @returns {any}
*/
export const asAnyType = (value: any): any => value;
Loading