diff --git a/README.md b/README.md index f84f9662b..ee4575786 100644 --- a/README.md +++ b/README.md @@ -444,6 +444,7 @@ average(1, 2, 3); * [`hexToRGB`](#hextorgb-) * [`httpGet`](#httpget) * [`httpPost`](#httppost) +* [`isBrowser`](#isbrowser) * [`mostPerformant`](#mostperformant) * [`nthArg`](#ntharg) * [`parseCookie`](#parsecookie) @@ -8257,6 +8258,32 @@ Logs: {
[⬆ Back to top](#table-of-contents) +### 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'); +``` + +
+Examples + +```js +isBrowser(); // true (browser) +isBrowser(); // false (Node) +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### mostPerformant Returns the index of the function in an array of functions which executed the fastest. diff --git a/docs/index.html b/docs/index.html index f452e44b1..6d24f7807 100644 --- a/docs/index.html +++ b/docs/index.html @@ -77,7 +77,7 @@ document.getElementById('doc-drawer-checkbox').checked = false; } }, false); - }

⚠️ WARNING: Snippets are not production ready.

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
+      }

⚠️ WARNING: Snippets are not production ready.

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

ary

Creates a function that accepts up to n arguments, ignoring any additional arguments.

Call the provided function, fn, with up to n arguments, using Array.slice(0,n) and the spread operator (...).

const ary = (fn, n) => (...args) => fn(...args.slice(0, n));
 
const firstTwoMax = ary(Math.max, 2);
 [[2, 6, 'a'], [8, 4, 6], [10]].map(x => firstTwoMax(...x)); // [6, 8, 10]
 

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
@@ -1990,6 +1990,9 @@ Logs: {
   "id": 101
 }
 */
+

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.

const isBrowser = () => ![typeof window, typeof document].includes('undefined');
+
isBrowser(); // true (browser)
+isBrowser(); // false (Node)
 

mostPerformant

Returns the index of the function in an array of functions which executed the fastest.

Use Array.map() to generate an array where each value is the total time taken to execute the function after iterations times. Use the difference in performance.now() values before and after to get the total time in milliseconds to a high degree of accuracy. Use Math.min() to find the minimum execution time, and return the index of that shortest time which corresponds to the index of the most performant function. Omit the second argument, iterations, to use a default of 10,000 iterations. The more iterations, the more reliable the result but the longer it will take.

const mostPerformant = (fns, iterations = 10000) => {
   const times = fns.map(fn => {
     const before = performance.now();
diff --git a/snippets/isBrowser.md b/snippets/isBrowser.md
index 0b41b4bba..4227234d7 100644
--- a/snippets/isBrowser.md
+++ b/snippets/isBrowser.md
@@ -8,10 +8,10 @@ browser environment unless they were explicitly defined), which will return `tru
 `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')
+const isBrowser = () => ![typeof window, typeof document].includes('undefined');
 ```
 
 ```js
-isBrowser() // true (browser)
-isBrowser() // false (Node)
+isBrowser(); // true (browser)
+isBrowser(); // false (Node)
 ```