Skip to content

Commit

Permalink
feat: add inNode
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Nov 10, 2018
1 parent fb69462 commit d85ee1e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/inNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import isFunction from './isFunction'

let isInNode: boolean | undefined

/**
* 检查是否在 Node 环境中。
*
* @param [callback] 在 Node 环境中执行的回调
* @returns 是(true)或否(false)
*/
export default function inNode(callback?: () => void): boolean {
if (isInNode === undefined) {
isInNode = typeof process !== 'undefined' &&
process.versions != null &&
process.versions.node != null
}
if (isInNode && isFunction(callback)) {
callback()
}
return isInNode
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export { default as get } from './get'
export { default as getType } from './getType'
export { default as has } from './has'
export { default as inBrowser } from './inBrowser'
export { default as inNode } from './inNode'
export { default as inWechatMiniProgram } from './inWechatMiniProgram'
export { default as inWechatWebview } from './inWechatWebview'
export { default as isArray } from './isArray'
Expand Down
9 changes: 9 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1303,3 +1303,12 @@ describe('sample', () => {
})
})
})

describe('inNode', () => {
test('ok', () => {
expect(vtils.inNode()).toBeTruthy()
const cb = sinon.fake()
vtils.inNode(cb)
expect(cb.calledOnce).toBeTruthy()
})
})

0 comments on commit d85ee1e

Please sign in to comment.