Skip to content

feat: add pick method#648

Closed
li-jia-nan wants to merge 1 commit intomasterfrom
pick-update
Closed

feat: add pick method#648
li-jia-nan wants to merge 1 commit intomasterfrom
pick-update

Conversation

@li-jia-nan
Copy link
Member

@li-jia-nan li-jia-nan commented Jul 13, 2025

Summary by CodeRabbit

  • 新功能
    • 新增了 pick 工具函数,用于从对象中提取指定属性。
  • 测试
    • 增加了针对 pick 函数的测试用例,验证其功能和边界情况。

@coderabbitai
Copy link

coderabbitai bot commented Jul 13, 2025

Walkthrough

本次更新对 omit 函数的导出方式进行了调整,并优化了其内部循环结构。同时新增了 pick 工具函数,用于从对象中提取指定属性,并为其编写了单元测试,确保功能的正确性。

Changes

文件/分组 变更摘要
src/omit.ts omit 函数从默认导出声明改为命名函数,并在文件末尾默认导出;将 fields.forEach 替换为 for...of 循环。
src/pick.ts 新增泛型工具函数 pick,用于提取对象的指定属性,并默认导出。
tests/pick.test.ts 新增 pick 函数的测试用例,包括常规提取、空键数组、只读数组三种情况。

Sequence Diagram(s)

sequenceDiagram
    participant 测试用例 as Test
    participant pick模块 as pick.ts
    participant 用户对象 as Object

    Test->>pick模块: 调用 pick(obj, fields)
    pick模块->>用户对象: 读取指定 keys 的属性
    pick模块-->>Test: 返回只包含指定属性的新对象
Loading

Poem

(=^・ω・^=)

代码如风,工具添新容,
pick 与 omit,各展其功。
键值筛选,轻松如梦,
测试加持,兔子乐融融!

🥕

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

src/omit.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@3.0.0/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

src/pick.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@3.0.0/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

tests/pick.test.ts

Oops! Something went wrong! :(

ESLint: 8.57.1

ESLint couldn't find the plugin "eslint-plugin-react".

(The package "eslint-plugin-react" was not found when loaded as a Node module from the directory "".)

It's likely that the plugin isn't installed correctly. Try reinstalling by running the following:

npm install eslint-plugin-react@latest --save-dev

The plugin "eslint-plugin-react" was referenced from the config file in ".eslintrc.js » /node_modules/.pnpm/@umijs+fabric@3.0.0/node_modules/@umijs/fabric/dist/eslint.js".

If you still can't figure out the problem, please stop by https://eslint.org/chat/help to chat with the team.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch pick-update

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai auto-generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@vercel
Copy link

vercel bot commented Jul 13, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
util ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jul 13, 2025 7:06am

@codecov
Copy link

codecov bot commented Jul 13, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 90.27%. Comparing base (ea6698f) to head (12e6524).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #648      +/-   ##
==========================================
+ Coverage   90.19%   90.27%   +0.07%     
==========================================
  Files          36       37       +1     
  Lines         877      884       +7     
  Branches      285      286       +1     
==========================================
+ Hits          791      798       +7     
  Misses         84       84              
  Partials        2        2              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
tests/pick.test.ts (1)

9-12: 建议改进测试用例命名

测试用例名称 "invalidate array" 容易引起误解。建议改为更清晰的名称,如 "should return empty object when fields is null"。

-  it('invalidate array', () => {
+  it('should return empty object when fields is null', () => {
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between ea6698f and 12e6524.

📒 Files selected for processing (3)
  • src/omit.ts (1 hunks)
  • src/pick.ts (1 hunks)
  • tests/pick.test.ts (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: test / react component workflow
🔇 Additional comments (9)
src/omit.ts (3)

1-1: 重构导出方式提升一致性

将函数声明和导出分离是一个很好的改进,这与新增的 pick 函数保持了一致的代码风格。


8-10: 优化循环性能

forEach 改为 for...of 循环是一个很好的性能优化,同时与新的 pick 函数保持了一致的循环方式。


15-16: 导出语句符合预期

单独的默认导出语句清晰明了,与重构后的函数声明配合良好。

tests/pick.test.ts (2)

1-7: 基础测试用例覆盖良好

导入语句正确,基础功能测试用例验证了 pick 函数的核心功能,测试覆盖了正常使用场景。


14-17: 只读数组类型测试很有价值

这个测试用例确保了函数对 readonly 数组类型的良好支持,提升了类型安全性。

src/pick.ts (4)

1-4: 函数签名设计优秀

泛型约束和类型定义非常准确,Pick<T, K> 返回类型确保了类型安全性,函数签名与 omit 函数保持了良好的一致性。


5-5: 结果对象初始化合理

使用类型断言初始化空对象是正确的做法,确保了返回值的类型准确性。


7-11: 循环逻辑简洁高效

使用 Array.isArray 检查和 for...of 循环的组合既安全又高效,与 omit 函数的重构保持了一致的代码风格。对于 null 参数返回空对象的处理符合测试预期。


13-16: 函数完整性良好

返回逻辑正确,默认导出方式与重构后的 omit 函数保持一致,整体实现简洁而完整。

@li-jia-nan li-jia-nan closed this Jul 14, 2025
@@ -0,0 +1,16 @@
const pick = <T extends object, K extends keyof T>(
Copy link
Member

Choose a reason for hiding this comment

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

解构会更快一些,这个不用抽

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants