Skip to content

feat: add isNumber function - #256

Merged
afc163 merged 2 commits into
masterfrom
isNumber-update
Mar 16, 2026
Merged

feat: add isNumber function#256
afc163 merged 2 commits into
masterfrom
isNumber-update

Conversation

@li-jia-nan

@li-jia-nan li-jia-nan commented Mar 15, 2026

Copy link
Copy Markdown
Member

Summary by CodeRabbit

发行说明

  • 重构
    • 优化了内部数值检测和单位处理的实现,引入更健壮的数字校验,改进了工具函数写法,提升代码可靠性与可维护性,公共接口和外部行为保持不变。

@github-actions

github-actions Bot commented Mar 15, 2026

Copy link
Copy Markdown

🎊 PR Preview 814e198 has been successfully built and deployed to https://ant-design-cssinjs-preview-pr-256.surge.sh

🕐 Build time: 107.331s

🤖 By surge-preview

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a dedicated isNumber utility function to provide a more reliable way to determine if a value is a number, specifically addressing the behavior of NaN. By centralizing this logic, it enhances consistency and correctness across the codebase, particularly in areas dealing with numerical calculations and unit conversions, making the application more robust against unexpected input types.

Highlights

  • New Utility Function: Added a new isNumber utility function to src/util/index.ts for robust number validation, including NaN checks.
  • Improved Type Checking: Integrated the new isNumber function into src/theme/calc/CSSCalculator.ts to standardize number type checks within the add method and the local unit helper.
  • Code Refactoring: Refactored the unit helper functions in both src/theme/calc/CSSCalculator.ts and src/util/index.ts to use the new isNumber utility and converted them to arrow functions for consistency.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/theme/calc/CSSCalculator.ts
    • Imported the new isNumber utility function.
    • Converted the unit function to an arrow function and updated its internal type check to use isNumber.
    • Modified the add method to utilize isNumber for checking if num is a number.
  • src/util/index.ts
    • Introduced a new isNumber utility function that checks if a value is a number and not NaN.
    • Converted the unit function to an arrow function and updated its internal type check to use the new isNumber utility.
Activity
  • The pull request was opened by li-jia-nan.
  • No human activity (comments, reviews) has occurred yet.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@coderabbitai

coderabbitai Bot commented Mar 15, 2026

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

引入并导出新的类型守卫函数 isNumber,将 unit 从函数声明重构为箭头函数,CSSCalculator 与工具模块改用 isNumber 进行数值判断并相应调整调用位置,外部导出/公共接口未改变。

Changes

Cohort / File(s) Summary
工具函数优化
src/util/index.ts
新增并导出 isNumber(val: any): val is number;将 unit 从函数声明改为箭头函数常量;unit 使用 isNumber 判断以决定是否附加 "px"
CSSCalculator 更新
src/theme/calc/CSSCalculator.ts
导入 isNumber;将内部 unit 辅助重构为箭头函数(若在此文件定义);用 isNumber(value) 替代 typeof … === 'number' 的分支检查;更新调用处以在 isNumber 为真时直接传递数字参数。

Sequence Diagram(s)

(无)

Estimated code review effort

🎯 2 (简洁) | ⏱️ ~12 分钟

Poem

🐰 新守卫轻跳出,数值不再迷路,
箭函一挥轻又稳,px 归位如常,
变更小而明确,代码静谧如春。 🥕✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed PR标题准确概括了最主要的变更:添加isNumber函数,这是代码库中的关键新增功能。

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch isNumber-update
📝 Coding Plan
  • Generate coding plan for human review comments

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

Comment @coderabbitai help to get the list of available commands and usage tips.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request introduces a helpful isNumber utility function to differentiate between numbers and NaN. While this is a good improvement, its application has inadvertently created a critical issue in the CSSCalculator constructor where NaN values are no longer handled, leaving this.result uninitialized. Additionally, there are a couple of medium-severity type-safety concerns in the updated unit functions, which can now return a number type (NaN) where a string is expected. My review includes specific suggestions to address these points and ensure the code is robust and type-safe.

Comment thread src/theme/calc/CSSCalculator.ts
Comment thread src/theme/calc/CSSCalculator.ts
Comment thread src/util/index.ts

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/theme/calc/CSSCalculator.ts`:
- Line 1: The import "import { isNumber } from '@/util';" in CSSCalculator.ts
uses the alias "@/util" which isn't resolvable in CI; replace that alias with a
repository-resolvable relative import that points to the actual util module
(i.e., update the import for the isNumber symbol to the correct relative path
within the repo so the bundler/TS resolver can find it).
- Around line 34-35: The constructor currently only assigns this.result when
isNumber(num) is true, so NaN (typeof number but not isNumber) falls through and
leaves this.result unset; update the branch so numeric-typed inputs are handled
explicitly (e.g. add an else if (typeof num === 'number') branch) and assign
this.result = unit(num) there (referencing isNumber, unit, and this.result) to
make the constructor behavior consistent with add/sub paths.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ec7e0144-9e6b-4348-8f63-0f304c6c0bc3

📥 Commits

Reviewing files that changed from the base of the PR and between d4ae688 and b078c88.

📒 Files selected for processing (2)
  • src/theme/calc/CSSCalculator.ts
  • src/util/index.ts

Comment thread src/theme/calc/CSSCalculator.ts Outdated
Comment thread src/theme/calc/CSSCalculator.ts
@codecov

codecov Bot commented Mar 15, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.75%. Comparing base (d4ae688) to head (814e198).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #256   +/-   ##
=======================================
  Coverage   94.74%   94.75%           
=======================================
  Files          32       32           
  Lines        2873     2878    +5     
  Branches      450      452    +2     
=======================================
+ Hits         2722     2727    +5     
  Misses        151      151           

☔ 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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

♻️ Duplicate comments (1)
src/theme/calc/CSSCalculator.ts (1)

34-35: ⚠️ Potential issue | 🟠 Major

NaN 在构造路径仍会落空,导致行为与 add/sub 不一致。

isNumber(num) 会排除 NaN,因此构造函数不会给 this.result 赋值;但 add/subnumber 分支仍会接收 NaN 并拼接成字符串,前后路径不一致,可能产生空结果或异常表达式。建议补一个 numType === 'number' 的兜底分支。

🔧 建议修复
     if (num instanceof CSSCalculator) {
       this.result = `(${num.result})`;
     } else if (isNumber(num)) {
       this.result = unit(num);
+    } else if (numType === 'number') {
+      this.result = `${num}`;
     } else if (numType === 'string') {
       this.result = num as string;
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/theme/calc/CSSCalculator.ts` around lines 34 - 35, The constructor in
CSSCalculator leaves this.result unset when num is NaN because isNumber(num)
excludes NaN, causing inconsistent behavior with add/sub which accept NaN;
update the constructor of class CSSCalculator to include a fallback branch
checking numType === 'number' (or otherwise detecting numeric inputs including
NaN) and assign this.result = unit(num) there so NaN follows the same
string-path logic as add/sub; reference the constructor, isNumber, numType,
unit, add, sub and this.result when making the change so the numeric (including
NaN) path is handled consistently.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Duplicate comments:
In `@src/theme/calc/CSSCalculator.ts`:
- Around line 34-35: The constructor in CSSCalculator leaves this.result unset
when num is NaN because isNumber(num) excludes NaN, causing inconsistent
behavior with add/sub which accept NaN; update the constructor of class
CSSCalculator to include a fallback branch checking numType === 'number' (or
otherwise detecting numeric inputs including NaN) and assign this.result =
unit(num) there so NaN follows the same string-path logic as add/sub; reference
the constructor, isNumber, numType, unit, add, sub and this.result when making
the change so the numeric (including NaN) path is handled consistently.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 910ff932-e2a8-40b4-b653-081ee87bf36f

📥 Commits

Reviewing files that changed from the base of the PR and between b078c88 and 814e198.

📒 Files selected for processing (1)
  • src/theme/calc/CSSCalculator.ts

@afc163
afc163 merged commit 1160994 into master Mar 16, 2026
9 checks passed
@afc163
afc163 deleted the isNumber-update branch March 16, 2026 03:50
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