Repository: IduxFE/components
Pull request #100 (2021-01-02 07:58:59)

feat(cdk:utils): add type check util function
add unified type check until functions

feat #77

## 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
- [x] Feature
- [ ] Component style update
- [ ] Code style update (formatting, local variables)
- [ ] 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. -->
when component developer develop an component that need check a variable‘ s type，he/her need to write a type check funcion
## What is the new behavior?
the developer just use the type check function that in ckd/utils/typeof



codecov[bot]: # [Codecov](https://codecov.io/gh/IduxFE/components/pull/100?src=pr&el=h1) Report
> Merging [#100](https://codecov.io/gh/IduxFE/components/pull/100?src=pr&el=desc) (38e20c8) into [main](https://codecov.io/gh/IduxFE/components/commit/1877c65d37695f87e2a87c871f9f1684126904d4?el=desc) (1877c65) will **increase** coverage by `0.30%`.
> The diff coverage is `100.00%`.

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

```diff
@@            Coverage Diff             @@
##             main     #100      +/-   ##
==========================================
+ Coverage   94.46%   94.76%   +0.30%     
==========================================
  Files          41       41              
  Lines         488      516      +28     
  Branches       85       87       +2     
==========================================
+ Hits          461      489      +28     
  Misses         10       10              
  Partials       17       17              
```


| [Impacted Files](https://codecov.io/gh/IduxFE/components/pull/100?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [packages/cdk/utils/typeof.ts](https://codecov.io/gh/IduxFE/components/pull/100/diff?src=pr&el=tree#diff-cGFja2FnZXMvY2RrL3V0aWxzL3R5cGVvZi50cw==) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/IduxFE/components/pull/100?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/100?src=pr&el=footer). Last update [1877c65...38e20c8](https://codecov.io/gh/IduxFE/components/pull/100?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).

clfeng: haved resolved all converstation
danranVm: 感谢你的 PR, LGTM.

Patch:
diff --git a/packages/cdk/utils/typeof.ts b/packages/cdk/utils/typeof.ts
index 1d113f93..de4c8fea 100644
--- a/packages/cdk/utils/typeof.ts
+++ b/packages/cdk/utils/typeof.ts
@@ -1,4 +1,7 @@
+/* eslint-disable @typescript-eslint/no-explicit-any */
 /** The method checks whether the given value is a Numeric value or not and returns the corresponding boolean value. */
+const _toString = Object.prototype.toString
+
 export function isNumeric(value: unknown): boolean {
   return !isNaN(parseFloat(value as string)) && isFinite(value as number)
 }
@@ -10,3 +13,57 @@ export function isNil(value: unknown): value is null | undefined {
 export function isNonNil<T>(value: T): value is NonNullable<T> {
   return typeof value !== 'undefined' && value !== null
 }
+
+export function isNumber(val: unknown): val is number {
+  return typeof val === 'number'
+}
+
+export function isBoolean(val: unknown): val is boolean {
+  return typeof val === 'boolean'
+}
+
+export function isString(val: unknown): val is string {
+  return typeof val === 'string'
+}
+
+export function isUndefined(val: unknown): val is undefined {
+  return typeof val === 'undefined'
+}
+
+export function isNull(val: unknown): val is null {
+  return val === null
+}
+
+export function isSymbol(val: unknown): val is symbol {
+  return typeof val === 'symbol'
+}
+
+export function isPlainObject(val: unknown): val is Record<any, any> {
+  return _toString.call(val) === '[object Object]'
+}
+
+export function isObject(val: unknown): val is Record<any, any> {
+  return val !== null && typeof val === 'object'
+}
+
+export function isFunction(val: unknown): val is typeof Function {
+  return _toString.call(val) === '[object Function]'
+}
+
+export const isArray = Array.isArray
+
+export function isMap(val: unknown): val is Array<unknown> {
+  return _toString.call(val) === '[object Map]'
+}
+
+export function isSet(val: unknown): val is Set<unknown> {
+  return _toString.call(val) === '[object Set]'
+}
+
+export function isDate(val: unknown): val is Date {
+  return val instanceof Date
+}
+
+export function isPromise<T = any>(val: unknown): val is Promise<T> {
+  return isObject(val) && isFunction(val.then) && isFunction(val.catch)
+}


Test patch:
diff --git a/packages/cdk/utils/__tests__/typeof.spec.ts b/packages/cdk/utils/__tests__/typeof.spec.ts
index a89fc260..e6326e4f 100644
--- a/packages/cdk/utils/__tests__/typeof.spec.ts
+++ b/packages/cdk/utils/__tests__/typeof.spec.ts
@@ -1,4 +1,22 @@
-import { isNumeric, isNil, isNonNil } from '../typeof'
+import {
+  isNumber,
+  isBoolean,
+  isString,
+  isUndefined,
+  isNull,
+  isSymbol,
+  isPlainObject,
+  isObject,
+  isFunction,
+  isArray,
+  isMap,
+  isSet,
+  isDate,
+  isPromise,
+  isNil,
+  isNonNil,
+  isNumeric,
+} from '../typeof'
 
 describe('typeof.ts', () => {
   test('isNumeric work', async () => {
@@ -45,4 +63,95 @@ describe('typeof.ts', () => {
     expect(isNonNil('')).toEqual(true)
     expect(isNonNil({})).toEqual(true)
   })
+
+  test('isNumber function', () => {
+    expect(isNumber(1)).toEqual(true)
+    expect(isNumber('1')).toEqual(false)
+    expect(isNumber(false)).toEqual(false)
+  })
+
+  test('isBoolean function', () => {
+    expect(isBoolean(true)).toEqual(true)
+    expect(isBoolean(1)).toEqual(false)
+    expect(isBoolean('true')).toEqual(false)
+  })
+
+  test('isString function', () => {
+    expect(isString('clf')).toEqual(true)
+    expect(isString(1)).toEqual(false)
+    expect(isString(false)).toEqual(false)
+  })
+
+  test('isUndefined function', () => {
+    expect(isUndefined(undefined)).toEqual(true)
+    expect(isUndefined('undefined')).toEqual(false)
+    expect(isUndefined(1)).toEqual(false)
+  })
+
+  test('isNull function', () => {
+    expect(isNull(null)).toEqual(true)
+    expect(isNull('null')).toEqual(false)
+    expect(isNull(1)).toEqual(false)
+  })
+
+  test('isSymbol function', () => {
+    expect(isSymbol(Symbol('1'))).toEqual(true)
+    expect(isSymbol(1)).toEqual(false)
+    expect(isSymbol(false)).toEqual(false)
+  })
+
+  test('isPlainObject function', () => {
+    expect(isPlainObject({})).toEqual(true)
+    expect(isPlainObject(function () {})).toEqual(false)
+    expect(isPlainObject(null)).toEqual(false)
+    expect(isPlainObject(1)).toEqual(false)
+  })
+
+  test('isObject function', () => {
+    expect(isObject({})).toEqual(true)
+    expect(isObject(new Date())).toEqual(true)
+    expect(isObject(new Set())).toEqual(true)
+    expect(isObject(new Map())).toEqual(true)
+    expect(isObject(null)).toEqual(false)
+    expect(isObject(1)).toEqual(false)
+  })
+
+  test('isFunction function', () => {
+    expect(isFunction(function () {})).toEqual(true)
+    expect(isFunction(() => {})).toEqual(true)
+    expect(isFunction(11)).toEqual(false)
+  })
+
+  test('isArray function', () => {
+    expect(isArray([])).toEqual(true)
+    expect(isArray({})).toEqual(false)
+    expect(isArray(new Map())).toEqual(false)
+  })
+
+  test('isMap function', () => {
+    expect(isMap(new Map())).toEqual(true)
+    expect(isMap({})).toEqual(false)
+    expect(isMap([])).toEqual(false)
+  })
+
+  test('isSet function', () => {
+    expect(isSet(new Set())).toEqual(true)
+    expect(isSet({})).toEqual(false)
+    expect(isSet([])).toEqual(false)
+  })
+
+  test('isDate function', () => {
+    expect(isDate(new Date())).toEqual(true)
+    expect(isDate({})).toEqual(false)
+    expect(isDate('2020-01-01')).toEqual(false)
+  })
+
+  test('isPromise function', () => {
+    const promise = new Promise(resolve => {
+      resolve(1)
+    })
+    expect(isPromise(promise)).toEqual(true)
+    expect(isPromise({})).toEqual(false)
+    expect(isPromise(function () {})).toEqual(false)
+  })
 })