ts and assignPropValue

This commit is contained in:
mol
2024-06-11 08:58:38 +08:00
parent bca43ff93c
commit 72a1743d75
6 changed files with 49 additions and 11 deletions

22
src/js/assignPropValue.ts Normal file
View File

@ -0,0 +1,22 @@
/**
* 给列表根据每一项prop属性与给定的数据组合带有数据的列表
* @param list 带有prop属性的列表
* @param data 数据
* @param addToLabel 是否将数据加到 label 上
* @returns 返回处理后的列表
*/
export function assignPropValue<T>(
list: readonly (T & { prop: string; label: string })[],
data: Record<string, any>,
addToLabel = false
) {
return list.map((item) => {
const { prop, label, ...restField } = item;
const count = data[prop];
return {
...restField,
label: addToLabel ? `${label}(${count})` : label,
count,
};
});
}

View File

@ -7,7 +7,7 @@ import { isNumber } from "lodash-es";
* @returns 相加之和 * @returns 相加之和
* @description 使用两个数中小数点后位数较长的作为基数,抹平两个数的小数点,整数相加之后再除以基数,还原小数点。注意整体数值的位数不能超过最大整数范围 * @description 使用两个数中小数点后位数较长的作为基数,抹平两个数的小数点,整数相加之后再除以基数,还原小数点。注意整体数值的位数不能超过最大整数范围
*/ */
export const floatAdd = (num1: number, num2: number) => { export function floatAdd(num1: number, num2: number) {
if (!isNumber(num1) || !isNumber(num2)) return NaN; if (!isNumber(num1) || !isNumber(num2)) return NaN;
let baseNum1: number; let baseNum1: number;
let baseNum2: number; let baseNum2: number;
@ -24,4 +24,4 @@ export const floatAdd = (num1: number, num2: number) => {
const baseNum = Math.pow(10, Math.max(baseNum1, baseNum2)); const baseNum = Math.pow(10, Math.max(baseNum1, baseNum2));
const result = ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(2); const result = ((num1 * baseNum + num2 * baseNum) / baseNum).toFixed(2);
return +result; return +result;
}; }

View File

@ -1,9 +1,7 @@
// ValueOf 将对象类型转换成值的枚举值 // ValueOf 将对象类型转换成值的枚举值
// keyof 操作符遍历对象的key
export type ValueOf<T> = T[keyof T]; export type ValueOf<T> = T[keyof T];
export const enum1 = { export const enum1 = {
AAA: "aaa", AAA: "aaa",
BBB: "bbb", BBB: "bbb",
} as const; } as const;
// typeof 提取类型
export type TEnum1 = ValueOf<typeof enum1>; export type TEnum1 = ValueOf<typeof enum1>;

2
src/ts/WithId.ts Normal file
View File

@ -0,0 +1,2 @@
// 添加id
export type WithId<T, P = string> = T & { id: P };

16
src/ts/handle.ts Normal file
View File

@ -0,0 +1,16 @@
// keyof 操作符遍历对象的key
export type keys<T> = keyof T;
const a = { a: 1, b: 2, c: 3 };
// typeof 提取类型
export type Ta = keys<typeof a>;
// Omit 去除某一项
export type Ta2 = Omit<typeof a, "b">;
// Pick 选择某一项
export type Ta3 = Pick<typeof a, "c">;
// & 添加
export type Ta4 = Ta3 & { d: string };

View File

@ -1,8 +1,8 @@
import { isNumber } from 'lodash-es'; import { isNumber } from "lodash-es";
export const rpx2px = (rpx) => { export function rpx2px(rpx: number) {
if (!isNumber(rpx)) return 0; if (!isNumber(rpx)) return 0;
const screenWidth = uni.getSystemInfoSync().screenWidth; const screenWidth = uni.getSystemInfoSync().screenWidth;
const result = (screenWidth * rpx) / 750 const result = (screenWidth * rpx) / 750;
return result; return result;
} }