Skip to content

Commit 40c0517

Browse files
authored
[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
1 parent ff91466 commit 40c0517

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/CHANGELOG.md

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

2425
### 2.22.3
2526

src/components/utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,8 @@ export function typeOf(object: any): string {
194194
* @returns {boolean}
195195
*/
196196
// eslint-disable-next-line @typescript-eslint/no-explicit-any
197-
export function isFunction(fn: any): fn is Function {
198-
return typeOf(fn) === 'function';
197+
export function isFunction(fn: any): fn is (...args: any[]) => any {
198+
return typeOf(fn) === 'function' || typeOf(fn) === 'asyncfunction';
199199
}
200200

201201
/**

test/cypress/tests/utils.spec.ts

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/* eslint-disable @typescript-eslint/no-empty-function */
2+
import { isFunction } from '../../../src/components/utils';
3+
4+
function syncFunction(): void {}
5+
6+
async function asyncFunction(): Promise<void> {}
7+
8+
const syncArrowFunction = (): void => {};
9+
10+
const asyncArrowFunction = async (): Promise<void> => {};
11+
12+
describe('isFunction function', () => {
13+
it('should recognise sync functions', () => {
14+
/**
15+
* Act
16+
*/
17+
const commonFunctionResult = isFunction(syncFunction);
18+
const arrowFunctionResult = isFunction(syncArrowFunction);
19+
20+
/**
21+
* Assert
22+
*/
23+
expect(commonFunctionResult).to.eq(true);
24+
expect(arrowFunctionResult).to.eq(true);
25+
});
26+
27+
it('should recognise async functions', () => {
28+
/**
29+
* Act
30+
*/
31+
const commonFunctionResult = isFunction(asyncFunction);
32+
const arrowFunctionResult = isFunction(asyncArrowFunction);
33+
34+
/**
35+
* Assert
36+
*/
37+
expect(commonFunctionResult).to.eq(true);
38+
expect(arrowFunctionResult).to.eq(true);
39+
});
40+
41+
it('should return false if it isn\'t a function', () => {
42+
/**
43+
* Arrange
44+
*/
45+
const obj = {};
46+
const num = 123;
47+
const str = '123';
48+
49+
/**
50+
* Act
51+
*/
52+
const objResult = isFunction(obj);
53+
const numResult = isFunction(num);
54+
const strResult = isFunction(str);
55+
56+
/**
57+
* Assert
58+
*/
59+
expect(objResult).to.eq(false);
60+
expect(numResult).to.eq(false);
61+
expect(strResult).to.eq(false);
62+
});
63+
});

0 commit comments

Comments
 (0)