Skip to content

Commit

Permalink
feat(utils): 新增 ii 立即调用函数并返回其返回值
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jan 8, 2021
1 parent 69e3edc commit 362ccd3
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/utils/ii.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ii } from './ii'

describe('ii', () => {
test('表现正常', async () => {
expect(ii(() => 1)).toBe(1)
expect(await ii(() => Promise.resolve(1))).toBe(1)
})
})
17 changes: 17 additions & 0 deletions src/utils/ii.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { AnyFunction } from '../types'

/**
* 立即调用函数并返回其返回值。
*
* 注:`ii = immediately invoke`
*
* @param fn 要调用的函数
* @returns 返回被调用函数的返回值
* @example
* ```typescript
* ii(() => 1) // => 1
* ```
*/
export function ii<F extends AnyFunction>(fn: F): ReturnType<F> {
return fn()
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './formatBytes'
export * from './formatNumber'
export * from './getEnvironment'
export * from './getWechatPublicAccountQrcodeUrl'
export * from './ii'
export * from './inAndroid'
export * from './inBrowser'
export * from './inDeno'
Expand Down

0 comments on commit 362ccd3

Please sign in to comment.