From 44c1d2a8034be10c3848285e4bf04d104227978b Mon Sep 17 00:00:00 2001 From: Kirk Lin Date: Thu, 20 Jul 2023 10:42:01 +0800 Subject: [PATCH] feat(constants): add defineConstants utility function --- packages/web/constants/index.ts | 1 + packages/web/constants/package.json | 3 + .../constants/src/utils/defineConstants.ts | 75 +++++++++++++++++++ packages/web/constants/src/utils/index.ts | 1 + packages/web/utils/index.ts | 1 + pnpm-lock.yaml | 10 ++- 6 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 packages/web/constants/src/utils/defineConstants.ts create mode 100644 packages/web/constants/src/utils/index.ts diff --git a/packages/web/constants/index.ts b/packages/web/constants/index.ts index 3bd16e1..b2491a5 100644 --- a/packages/web/constants/index.ts +++ b/packages/web/constants/index.ts @@ -1 +1,2 @@ export * from "./src"; +export * from "./src/utils"; diff --git a/packages/web/constants/package.json b/packages/web/constants/package.json index 8225dbe..f6e39e5 100644 --- a/packages/web/constants/package.json +++ b/packages/web/constants/package.json @@ -9,5 +9,8 @@ "module": "index.ts", "scripts": { "clean": "pnpm rimraf node_modules && pnpm rimraf dist" + }, + "dependencies": { + "lodash.pick": "4.4.0" } } diff --git a/packages/web/constants/src/utils/defineConstants.ts b/packages/web/constants/src/utils/defineConstants.ts new file mode 100644 index 0000000..ba5ef52 --- /dev/null +++ b/packages/web/constants/src/utils/defineConstants.ts @@ -0,0 +1,75 @@ +import pick from "lodash/pick"; + +/** + * Utility function to determine the raw type of a value. + * 判断一个值的原始类型的实用函数。 + * @param val - The value to determine the raw type of. 要判断原始类型的值。 + * @returns The raw type of the value. 值的原始类型。 + */ +const toRawType = (val: unknown) => Object.prototype.toString.call(val).slice(8, -1); +/** + * Utility function to check if a value is a valid PropertyKey (String, Number, or Symbol). + * 判断一个值是否为有效的 PropertyKey(字符串、数字或符号)的实用函数。 + * @param val - The value to check. 要检查的值。 + * @returns true if the value is a PropertyKey, false otherwise. 如果值是 PropertyKey,则返回 true,否则返回 false。 + */ +const isPropertyKey = (val: unknown): val is PropertyKey => ["String", "Number", "Symbol"].includes(toRawType(val)); +/** + * Type alias to extract valid keys from an object. + * 从对象中提取有效键的类型别名。 + */ +type ValidKeys = K extends K ? T[K] extends PropertyKey ? K : never : never; +/** + * Overload 1: + * Extracts the values of a specific key from an array of objects. + * 从对象数组中提取指定键的值。 + * @param items - The array of objects. 对象数组。 + * @param key - The key to extract from each object. 从每个对象中提取的键。 + * @returns An array containing the extracted values. 包含提取值的数组。 + */ +export function defineConstants(items: T[], key: K): T[K][]; +/** + * Overload 2: + * Extracts specific values of a specific key from an array of objects and maps them to a new object. + * 从对象数组中提取指定键的特定值,并将它们映射到一个新对象中。 + * @param items - The array of objects. 对象数组。 + * @param key - The key to extract from each object. 从每个对象中提取的键。 + * @param values - The values to extract from each object. 从每个对象中提取的值。 + * @returns An object containing the extracted keys and their corresponding values. 包含提取的键和相应值的对象。 + */ +export function defineConstants, V extends keyof T>(items: T[], key: K, values: V): Record; +/** + * Overload 3: + * Extracts multiple values of a specific key from an array of objects and maps them to a new object. + * 从对象数组中提取指定键的多个值,并将它们映射到一个新对象中。 + * @param items - The array of objects. 对象数组。 + * @param key - The key to extract from each object. 从每个对象中提取的键。 + * @param values - The values to extract from each object. 从每个对象中提取的值。 + * @returns An object containing the extracted keys and their corresponding values. 包含提取的键和相应值的对象。 + */ +export function defineConstants, V extends keyof T>(items: T[], key: K, values: V[]): Record>; + +/** + * A utility function to define constants based on an array of objects. + * 根据对象数组定义常量的实用函数。 + * @param items - The array of objects. 对象数组。 + * @param key - The key to extract from each object. 从每个对象中提取的键。 + * @param values - The values to extract from each object. 从每个对象中提取的值。 + * @returns An array or object containing the extracted values. 包含提取值的数组或对象。 + */ +export function defineConstants(items: T[], key: K, values?: V | V[]) { + if (typeof values === "undefined") { + return items.map(item => item[key]); + } + + return items.reduce((map, item) => { + const _key = item[key]; + if (!isPropertyKey(_key)) { + return map; + } + + const _val = Array.isArray(values) ? pick(item, values) : item[values]; + + return { ...map, [_key]: _val }; + }, {}); +} diff --git a/packages/web/constants/src/utils/index.ts b/packages/web/constants/src/utils/index.ts new file mode 100644 index 0000000..aa19c62 --- /dev/null +++ b/packages/web/constants/src/utils/index.ts @@ -0,0 +1 @@ +export * from "./defineConstants"; diff --git a/packages/web/utils/index.ts b/packages/web/utils/index.ts index 33c2639..dbab891 100644 --- a/packages/web/utils/index.ts +++ b/packages/web/utils/index.ts @@ -5,4 +5,5 @@ export { clone, intersection, uniqBy, + pick, } from "lodash-es"; diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2e21831..eda89f9 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -301,7 +301,11 @@ importers: specifier: ^4.2.4 version: 4.2.4(vue@3.3.4) - packages/web/constants: {} + packages/web/constants: + dependencies: + lodash.pick: + specifier: 4.4.0 + version: 4.4.0 packages/web/directives: dependencies: @@ -8163,6 +8167,10 @@ packages: resolution: {integrity: sha512-GK3g5RPZWTRSeLSpgP8Xhra+pnjBC56q9FZYe1d5RN3TJ35dbkGy3YqBSMbyCrlbi+CM9Z3Jk5yTL7RCsqboyQ==} dev: true + /lodash.pick@4.4.0: + resolution: {integrity: sha512-hXt6Ul/5yWjfklSGvLQl8vM//l3FtyHZeuelpzK6mm99pNvN9yTDruNZPEJZD1oWrqo+izBmB7oUfWgcCX7s4Q==} + dev: false + /lodash.snakecase@4.1.1: resolution: {integrity: sha512-QZ1d4xoBHYUeuouhEq3lk3Uq7ldgyFXGBhg04+oRLnIz8o9T65Eh+8YdroUwn846zchkA9yDsDl5CVVaV2nqYw==} dev: true