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

feat: add utils #2050

Merged
merged 4 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
15 changes: 15 additions & 0 deletions js/utils/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isFunction, isObject } from 'lodash-es';

const { hasOwnProperty } = Object.prototype;

export const hasOwn = <T extends object>(val: T, key: string | symbol | number): key is keyof T => hasOwnProperty.call(val, key);
export const getPropertyValFromObj = <T extends object>(
val: T,
key: string | symbol | number,
): T[keyof T] | undefined => (hasOwn(val, key) ? val[key] : undefined);

const objectToString: typeof Object.prototype.toString = Object.prototype.toString;
const toTypeString = (value: unknown): string => objectToString.call(value);
export const isPlainObject = <T extends object>(val: unknown): val is T => toTypeString(val) === '[object Object]';
// eslint-disable-next-line max-len
export const isPromise = <T = any>(val: unknown): val is Promise<T> => (isObject(val) || isFunction(val)) && isFunction((val as any).then) && isFunction((val as any).catch);
12 changes: 12 additions & 0 deletions js/utils/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export function omit(obj: Record<string, any>, fields: string[]) {
return shallowCopy;
}

export function getValidAttrs<T extends Record<string, any>>(obj: T): Partial<T> {
const newObj: Partial<T> = {};

Object.keys(obj).forEach((key) => {
if (!isUndefined(obj[key]) || isNull(obj[key])) {
newObj[key as keyof T] = obj[key];
}
});

return newObj;
}

export function removeEmptyAttrs<T extends Record<string, any>>(obj: T): Partial<T> {
const newObj: Partial<T> = {};

Expand Down
7 changes: 5 additions & 2 deletions js/utils/set-style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ import { Styles } from '../common';
* @param style Styles
*/
function setStyle(el: HTMLElement, styles: Styles): void {
Object.entries(styles).forEach(([key, value]) => {
el.style.setProperty(key, String(value));
const keys = Object.keys(styles);
keys.forEach((key) => {
// @ts-ignore
// eslint-disable-next-line no-param-reassign
el.style[key] = styles[key];
});
}

Expand Down
9 changes: 9 additions & 0 deletions js/utils/stringTemplate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* 用正则实现模板字符串功能
* @param str 模板字符串
* @param vars 取值的对象
* @returns 替换后的字符串
*/
function template<T extends Record<string, string>>(str: string, vars: T): string {
return str.replace(/\${(.*?)}/g, (_, prop: string) => vars[prop.trim()] || '');
}
14 changes: 14 additions & 0 deletions js/utils/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* 类型函数
* 将字符类型转换成 CamelCase 类型
* @param S 字符串
* @param B 字符串的分隔符,默认为 '_'
* @example CamelCase<'a_b_c'> -> aBC
* @example CamelCase<'a-b-c', '-'> -> aBC
* @author zhangpaopao0609
*/
export type CamelCase<S extends string, B extends string = '_'> = S extends Lowercase<S>
? S extends `${infer F}${B}${infer RF}${infer R}`
? `${F}${Uppercase<RF>}${CamelCase<R, B>}`
: S
: CamelCase<Lowercase<S>, B>;