[Fix] isFunction function for async functions (#1857)

* fix test of async function, fix TS type because Function is banned type

* add tests for isFunction function

* fix eslint

* add changelog for 2.23.1

* fix changelog

* fix changelog
This commit is contained in:
Ilya Moroz 2022-01-13 00:46:07 +03:00 committed by GitHub
parent ff91466b14
commit 40c05173a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 66 additions and 2 deletions

View file

@ -20,6 +20,7 @@
- `New``API` — The new `UiApi` section was added. It allows accessing some editor UI nodes and methods.
- `Refactoring` — Toolbox became a standalone class instead of a Module. It can be accessed only through the Toolbar module.
- `Refactoring` — CI flow optimized.
- `Fix` - Recognize async `onPaste` handlers in tools [#1803](https://github.com/codex-team/editor.js/issues/1803).
### 2.22.3

View file

@ -194,8 +194,8 @@ export function typeOf(object: any): string {
* @returns {boolean}
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function isFunction(fn: any): fn is Function {
return typeOf(fn) === 'function';
export function isFunction(fn: any): fn is (...args: any[]) => any {
return typeOf(fn) === 'function' || typeOf(fn) === 'asyncfunction';
}
/**

View file

@ -0,0 +1,63 @@
/* eslint-disable @typescript-eslint/no-empty-function */
import { isFunction } from '../../../src/components/utils';
function syncFunction(): void {}
async function asyncFunction(): Promise<void> {}
const syncArrowFunction = (): void => {};
const asyncArrowFunction = async (): Promise<void> => {};
describe('isFunction function', () => {
it('should recognise sync functions', () => {
/**
* Act
*/
const commonFunctionResult = isFunction(syncFunction);
const arrowFunctionResult = isFunction(syncArrowFunction);
/**
* Assert
*/
expect(commonFunctionResult).to.eq(true);
expect(arrowFunctionResult).to.eq(true);
});
it('should recognise async functions', () => {
/**
* Act
*/
const commonFunctionResult = isFunction(asyncFunction);
const arrowFunctionResult = isFunction(asyncArrowFunction);
/**
* Assert
*/
expect(commonFunctionResult).to.eq(true);
expect(arrowFunctionResult).to.eq(true);
});
it('should return false if it isn\'t a function', () => {
/**
* Arrange
*/
const obj = {};
const num = 123;
const str = '123';
/**
* Act
*/
const objResult = isFunction(obj);
const numResult = isFunction(num);
const strResult = isFunction(str);
/**
* Assert
*/
expect(objResult).to.eq(false);
expect(numResult).to.eq(false);
expect(strResult).to.eq(false);
});
});