Skip to content

Commit

Permalink
feat(constants): add defineConstants utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
kirklin committed Jul 20, 2023
1 parent bf28364 commit 44c1d2a
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/web/constants/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./src";
export * from "./src/utils";
3 changes: 3 additions & 0 deletions packages/web/constants/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@
"module": "index.ts",
"scripts": {
"clean": "pnpm rimraf node_modules && pnpm rimraf dist"
},
"dependencies": {
"lodash.pick": "4.4.0"
}
}
75 changes: 75 additions & 0 deletions packages/web/constants/src/utils/defineConstants.ts
Original file line number Diff line number Diff line change
@@ -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<T, K extends keyof T = keyof T> = 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<T extends object, K extends keyof T>(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<T extends object, K extends ValidKeys<T>, V extends keyof T>(items: T[], key: K, values: V): Record<T[K] extends PropertyKey ? T[K] : never, T[V]>;
/**
* 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<T extends object, K extends ValidKeys<T>, V extends keyof T>(items: T[], key: K, values: V[]): Record<T[K] extends PropertyKey ? T[K] : never, Pick<T, V>>;

/**
* 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<T extends object, K extends keyof T, V extends keyof T>(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 };
}, {});
}
1 change: 1 addition & 0 deletions packages/web/constants/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./defineConstants";
1 change: 1 addition & 0 deletions packages/web/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export {
clone,
intersection,
uniqBy,
pick,
} from "lodash-es";
10 changes: 9 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 comments on commit 44c1d2a

@vercel
Copy link

@vercel vercel bot commented on 44c1d2a Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web – ./apps/admin

celeris-web-kirklin.vercel.app
celeris-web.vercel.app
celeris-web-git-master-kirklin.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 44c1d2a Jul 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

celeris-web-api – ./services/admin

celeris-web-api-git-master-kirklin.vercel.app
celeris-web-api-kirklin.vercel.app
celeris-web-api.vercel.app

Please sign in to comment.