Skip to content

Commit

Permalink
feat: 新增 ii
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed May 22, 2019
1 parent 6d519dd commit 855e508
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/ii.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { AnyFunction } from './isFunction'

/**
* 立即调用函数并返回其返回值。
*
* 注:`ii = immediately invoke`
*
* @param fn 要调用的函数
* @returns 返回被调用函数的返回值
*/
export function ii<F extends AnyFunction>(fn: F): ReturnType<F> {
return fn()
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export * from './getDaysInMonth'
export * from './getType'
export * from './groupBy'
export * from './has'
export * from './ii'
export * from './inBrowser'
export * from './includes'
export * from './inNode'
Expand Down
17 changes: 17 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1783,3 +1783,20 @@ describe('request', () => {
server.close()
})
})

describe('ii', () => {
test('ok', () => {
typedExpectEqual(
vtils.ii(() => 1),
1,
)
typedExpectEqual(
vtils.ii(() => [2]),
[2],
)
typedExpectEqual(
vtils.ii(() => ({ hello: 'world' })),
{ hello: 'world' },
)
})
})

0 comments on commit 855e508

Please sign in to comment.