Repository: IduxFE/components
Pull request #89 (2020-12-30 07:28:28)

refactor(cdk:utils): isNumber renamed to isNumberic
## PR Checklist

Please check if your PR fulfills the following requirements:

- [x] The commit message follows [our guidelines](https://github.com/IduxFE/components/blob/main/docs/contributing.zh.md#commit)
- [x] Tests for the changes have been added/updated or not needed
- [x] Docs and demo have been added/updated or not needed

## PR Type

What kind of change does this PR introduce?

- [ ] Bugfix
- [ ] Feature
- [ ] Component style update
- [ ] Code style update (formatting, local variables)
- [x] Refactoring (no functional changes, no api changes)
- [ ] Build related changes
- [ ] CI related changes
- [ ] Documentation content changes
- [ ] Application (the showcase website) / infrastructure changes
- [ ] Other... Please describe:

## What is the current behavior?
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->

## What is the new behavior?

## Other information


codecov[bot]: # [Codecov](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=h1) Report
> Merging [#89](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=desc) (364cb91) into [main](https://codecov.io/gh/IduxFE/components/commit/33551ff09736b59e92c4e1e41d151bc37226ac80?el=desc) (33551ff) will **not change** coverage.
> The diff coverage is `100.00%`.

[![Impacted file tree graph](https://codecov.io/gh/IduxFE/components/pull/89/graphs/tree.svg?width=650&height=150&src=pr&token=PGAUXP06V3)](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=tree)

```diff
@@           Coverage Diff           @@
##             main      #89   +/-   ##
=======================================
  Coverage   94.46%   94.46%           
=======================================
  Files          41       41           
  Lines         488      488           
  Branches       85       85           
=======================================
  Hits          461      461           
  Misses         10       10           
  Partials       17       17           
```


| [Impacted Files](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/cdk/utils/convert.ts](https://codecov.io/gh/IduxFE/components/pull/89/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2RrL3V0aWxzL2NvbnZlcnQudHM=) | `100.00% <100.00%> (ø)` | |
| [packages/cdk/utils/typeof.ts](https://codecov.io/gh/IduxFE/components/pull/89/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2RrL3V0aWxzL3R5cGVvZi50cw==) | `100.00% <100.00%> (ø)` | |
| [packages/components/badge/src/Badge.vue](https://codecov.io/gh/IduxFE/components/pull/89/diff?src=pr&el=tree#diff-cGFja2FnZXMvY29tcG9uZW50cy9iYWRnZS9zcmMvQmFkZ2UudnVl) | `100.00% <100.00%> (ø)` | |
| [packages/components/icon/src/Icon.vue](https://codecov.io/gh/IduxFE/components/pull/89/diff?src=pr&el=tree#diff-cGFja2FnZXMvY29tcG9uZW50cy9pY29uL3NyYy9JY29uLnZ1ZQ==) | `97.72% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=footer). Last update [33551ff...364cb91](https://codecov.io/gh/IduxFE/components/pull/89?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/docs/contributing.zh.md b/docs/contributing.zh.md
index a5498895..8f6db7cb 100644
--- a/docs/contributing.zh.md
+++ b/docs/contributing.zh.md
@@ -88,6 +88,7 @@ title: 贡献指南
   - 如果与某个 props 的功能一致时，需要跟该 props 同名
 - emits
   - 命名：`camelCase`
+- 优先使用 `template`, 只有在 `template` 不便实现的功能时，才可以使用 `tsx`, 例如：需要递归渲染的场景
 - 尽可能的使用 Composition API 去完成我们的功能，包括 Demo
 
 ### typescript
diff --git a/packages/cdk/utils/convert.ts b/packages/cdk/utils/convert.ts
index 80f2ae23..a27c0c9d 100644
--- a/packages/cdk/utils/convert.ts
+++ b/packages/cdk/utils/convert.ts
@@ -1,4 +1,4 @@
-import { isNil, isNumber } from './typeof'
+import { isNil, isNumeric } from './typeof'
 
 export function toArray<T>(value: T | T[]): T[]
 export function toArray<T>(value: T | readonly T[]): readonly T[]
@@ -9,7 +9,7 @@ export function toArray<T>(value: T | T[]): T[] {
 export function toNumber(value: unknown): number
 export function toNumber<T>(value: unknown, fallback: T): number | T
 export function toNumber(value: unknown, fallback = 0): number {
-  return isNumber(value) ? Number(value) : fallback
+  return isNumeric(value) ? Number(value) : fallback
 }
 
 export function toBoolean(value: unknown): boolean {
diff --git a/packages/cdk/utils/typeof.ts b/packages/cdk/utils/typeof.ts
index 7d964fd3..1d113f93 100644
--- a/packages/cdk/utils/typeof.ts
+++ b/packages/cdk/utils/typeof.ts
@@ -1,5 +1,6 @@
-export function isNumber(value: unknown): boolean {
-  return !isNaN(parseFloat(value as string)) && !isNaN(Number(value))
+/** The method checks whether the given value is a Numeric value or not and returns the corresponding boolean value. */
+export function isNumeric(value: unknown): boolean {
+  return !isNaN(parseFloat(value as string)) && isFinite(value as number)
 }
 
 export function isNil(value: unknown): value is null | undefined {
diff --git a/packages/components/badge/src/Badge.vue b/packages/components/badge/src/Badge.vue
index 695d6020..1f49ca61 100644
--- a/packages/components/badge/src/Badge.vue
+++ b/packages/components/badge/src/Badge.vue
@@ -9,7 +9,7 @@
 </template>
 <script lang="ts">
 import { computed, defineComponent, onUpdated, reactive } from 'vue'
-import { isNumber, PropTypes } from '@idux/cdk/utils'
+import { isNumeric, PropTypes } from '@idux/cdk/utils'
 import { useGlobalConfig } from '@idux/components/core/config'
 
 import type { ComputedRef } from 'vue'
@@ -65,8 +65,8 @@ const useCountValue = (
   return computed(() => {
     if (!slots.count && !dot.value) {
       if (!showZero.value && +props.count === 0) return false
-      if (isNumber(props.count) && isNumber(overflowCount.value)) {
-        return +props.count > +overflowCount.value ? `${overflowCount.value}+` : `${props.count}`
+      if (isNumeric(props.count) && isNumeric(overflowCount.value)) {
+        return props.count > overflowCount.value ? `${overflowCount.value}+` : `${props.count}`
       }
       return props.count
     }
diff --git a/packages/components/icon/src/Icon.vue b/packages/components/icon/src/Icon.vue
index b3e069d7..fc26baec 100644
--- a/packages/components/icon/src/Icon.vue
+++ b/packages/components/icon/src/Icon.vue
@@ -5,7 +5,7 @@
 </template>
 <script lang="ts">
 import { computed, defineComponent, onMounted, onUpdated, ref, watch } from 'vue'
-import { isNumber, PropTypes, withUndefined } from '@idux/cdk/utils'
+import { isNumeric, PropTypes, withUndefined } from '@idux/cdk/utils'
 import { useGlobalConfig } from '@idux/components/core/config'
 import { clearSVGElement, loadIconFontSvgElement, loadSVGElement } from './utils'
 
@@ -71,7 +71,7 @@ async function appendChild(props: IconProps, iconConfig: IconConfig, root: Ref<H
 }
 
 function handleRotate(svg: SVGElement, rotate?: number | string | boolean): void {
-  if (isNumber(rotate)) {
+  if (isNumeric(rotate)) {
     svg.setAttribute('style', `transform: rotate(${rotate}deg)`)
   } else {
     svg.removeAttribute('style')


Test patch:
diff --git a/packages/cdk/utils/__tests__/typeof.spec.ts b/packages/cdk/utils/__tests__/typeof.spec.ts
index ab120949..a89fc260 100644
--- a/packages/cdk/utils/__tests__/typeof.spec.ts
+++ b/packages/cdk/utils/__tests__/typeof.spec.ts
@@ -1,14 +1,27 @@
-import { isNumber, isNil, isNonNil } from '../typeof'
+import { isNumeric, isNil, isNonNil } from '../typeof'
 
 describe('typeof.ts', () => {
-  test('isNumber work', async () => {
-    expect(isNumber(1)).toEqual(true)
-    expect(isNumber('1')).toEqual(true)
-    expect(isNumber('1.1')).toEqual(true)
+  test('isNumeric work', async () => {
+    expect(isNumeric(+10)).toEqual(true)
+    expect(isNumeric('+10')).toEqual(true)
+    expect(isNumeric(-10)).toEqual(true)
+    expect(isNumeric('-10')).toEqual(true)
+    expect(isNumeric('0')).toEqual(true)
+    expect(isNumeric(0xff)).toEqual(true)
+    expect(isNumeric('0xff')).toEqual(true)
+    expect(isNumeric('8e5')).toEqual(true)
+    expect(isNumeric('3.1415')).toEqual(true)
 
-    expect(isNumber('1-1')).toEqual(false)
-    expect(isNumber('')).toEqual(false)
-    expect(isNumber(NaN)).toEqual(false)
+    expect(isNumeric('-0x42')).toEqual(false)
+    expect(isNumeric('7.2acdgs')).toEqual(false)
+    expect(isNumeric('')).toEqual(false)
+    expect(isNumeric({})).toEqual(false)
+    expect(isNumeric([])).toEqual(false)
+    expect(isNumeric(NaN)).toEqual(false)
+    expect(isNumeric(null)).toEqual(false)
+    expect(isNumeric(undefined)).toEqual(false)
+    expect(isNumeric(true)).toEqual(false)
+    expect(isNumeric(Infinity)).toEqual(false)
   })
 
   test('isNil work', async () => {