Merge pull request #637 from Chalarangelo/isBrowser

[FEATURE] isBrowser
This commit is contained in:
Stefan Feješ
2018-03-19 15:49:52 +01:00
committed by GitHub
2 changed files with 18 additions and 0 deletions

17
snippets/isBrowser.md Normal file
View File

@ -0,0 +1,17 @@
### isBrowser
Determines if the current runtime environment is a browser so that front-end modules can run on the server (Node)
without throwing errors.
Use `Array.includes()` on the `typeof` values of both `window` and `document` (globals usually only available in a
browser environment unless they were explicitly defined), which will return `true` if one of them is `undefined`.
`typeof` allows globals to be checked for existence without throwing a ReferenceError. If both of them are not `undefined`, then the current environment is assumed to be a browser.
```js
const isBrowser = () => ![typeof window, typeof document].includes('undefined')
```
```js
isBrowser() // true (browser)
isBrowser() // false (Node)
```

View File

@ -119,6 +119,7 @@ isAbsoluteURL:string,utility,browser,url
isAnagram:string,regexp
isArrayLike:type,array
isBoolean:type
isBrowser:utility,browser
isDivisible:math
isEmpty:type,array,object,string
isEven:math