Update formatting

This commit is contained in:
Isabelle Viktoria Maciohsek
2022-01-29 20:36:07 +02:00
parent a2f959334b
commit c56b5a11da
16 changed files with 23 additions and 22 deletions

View File

@ -7,8 +7,8 @@ lastUpdated: 2020-10-18T20:24:28+03:00
Attempts to invoke a function with the provided arguments, returning either the result or the caught error object.
- Use a `try... catch` block to return either the result of the function or an appropriate error.
- If the caught object is not an `Error`, use it to create a new `Error`.
- Use a `try...catch` block to return either the result of the function or an appropriate error.
- If the caught object is not an `Error`, use it to create a new `Error`.
```js
const attempt = (fn, ...args) => {

View File

@ -7,7 +7,7 @@ lastUpdated: 2020-12-29T12:22:44+02:00
Sorts an array of numbers, using the bucket sort algorithm.
- Use `Math.min(),` `Math.max()` and the spread operator (`...`) to find the minimum and maximum values of the given array.
- Use `Math.min()`, `Math.max()` and the spread operator (`...`) to find the minimum and maximum values of the given array.
- Use `Array.from()` and `Math.floor()` to create the appropriate number of `buckets` (empty arrays).
- Use `Array.prototype.forEach()` to populate each bucket with the appropriate elements from the array.
- Use `Array.prototype.reduce()`, the spread operator (`...`) and `Array.prototype.sort()` to sort each bucket and append it to the result.

View File

@ -12,7 +12,7 @@ Only works as a result of user action (i.e. inside a `click` event listener).
- Use `Selection.getRangeAt()`to store the selected range (if any).
- Use `Document.execCommand('copy')` to copy to the clipboard.
- Remove the `<textarea>` element from the HTML document.
- Finally, use `Selection().addRange()` to recover the original selected range (if any).
- Finally, use `Selection.addRange()` to recover the original selected range (if any).
- **Note:** You can use the asynchronous [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API) in most current browsers. You can find out more about it in the [copyToClipboardAsync snippet](/js/s/copy-to-clipboard-async).
```js

View File

@ -5,13 +5,13 @@ firstSeen: 2020-10-10T21:09:04+03:00
lastUpdated: 2020-10-19T18:51:03+03:00
---
Creates a debounced function that returns a promise, but delays invoking the provided function until at least `ms` milliseconds have elapsed since the last time it was invoked.
Creates a debounced function that returns a promise, but delays invoking the provided function until at least `ms` milliseconds have elapsed since the last time it was invoked.
All promises returned during this time will return the same data.
- Each time the debounced function is invoked, clear the current pending timeout with `clearTimeout()` and use `setTimeout()` to create a new timeout that delays invoking the function until at least `ms` milliseconds has elapsed.
- Use `Function.prototype.apply()` to apply the `this` context to the function and provide the necessary arguments.
- Create a new `Promise` and add its `resolve` and `reject` callbacks to the `pending` promises stack.
- When `setTimeout` is called, copy the current stack (as it can change between the provided function call and its resolution), clear it and call the provided function.
- When `setTimeout()` is called, copy the current stack (as it can change between the provided function call and its resolution), clear it and call the provided function.
- When the provided function resolves/rejects, resolve/reject all promises in the stack (copied when the function was called) with the returned data.
- Omit the second argument, `ms`, to set the timeout at a default of `0` ms.

View File

@ -7,7 +7,7 @@ lastUpdated: 2020-10-22T20:23:47+03:00
Detects whether the page is being viewed on a mobile device or a desktop.
- Use a regular expression to test the `navigator.userAgent` property to figure out if the device is a mobile device or a desktop.
- Use a regular expression to test the `Navigator.userAgent` property to figure out if the device is a mobile device or a desktop.
```js
const detectDeviceType = () =>

View File

@ -7,11 +7,11 @@ lastUpdated: 2020-10-06T18:47:16+03:00
Detects the preferred language of the current user.
- Use `NavigationLanguage.language` or the first `NavigationLanguage.languages` if available, otherwise return `defaultLang`.
- Use `Navigator.language` or the first value of `Navigator.languages` if available, otherwise return `defaultLang`.
- Omit the second argument, `defaultLang`, to use `'en-US'` as the default language code.
```js
const detectLanguage = (defaultLang = 'en-US') =>
const detectLanguage = (defaultLang = 'en-US') =>
navigator.language ||
(Array.isArray(navigator.languages) && navigator.languages[0]) ||
defaultLang;

View File

@ -7,7 +7,7 @@ lastUpdated: 2021-05-09T13:31:36+03:00
Finds the first `n` elements for which the provided function returns a truthy value.
- Use a `for..in` loop to execute the provided `matcher` for each element of `arr`.
- Use a `for...in` loop to execute the provided `matcher` for each element of `arr`.
- Use `Array.prototype.push()` to append elements to the results array and return them if its `length` is equal to `n`.
```js

View File

@ -7,7 +7,7 @@ lastUpdated: 2020-10-22T20:23:09+03:00
Checks if there are duplicate values in a flat array.
- Use `Set()` to get the unique values in the array.
- Use `Set` to get the unique values in the array.
- Use `Set.prototype.size` and `Array.prototype.length` to check if the count of the unique values is the same as elements in the original array.
```js

View File

@ -6,7 +6,7 @@ firstSeen: 2018-04-11T16:39:49+03:00
lastUpdated: 2021-01-04T13:04:15+02:00
---
Measures the number of times a function is executed per second (`hz`/`hertz`).
Measures the number of times a function is executed per second (hz/hertz).
- Use `performance.now()` to get the difference in milliseconds before and after the iteration loop to calculate the time elapsed executing the function `iterations` times.
- Return the number of cycles per second by converting milliseconds to seconds and dividing it by the time elapsed.

View File

@ -7,7 +7,8 @@ lastUpdated: 2020-10-20T23:02:01+03:00
Initializes an array containing the numbers in the specified range (in reverse) where `start` and `end` are inclusive with their common difference `step`.
- Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.prototype.map()` to fill with the desired values in a range.
- Use `Array.from()` to create an array of the desired length, `(end - start + 1)/step`.
- Use `Array.prototype.map()` to fill the array with the desired values in the given range.
- Omit the second argument, `start`, to use a default value of `0`.
- Omit the last argument, `step`, to use a default value of `1`.

View File

@ -8,8 +8,8 @@ lastUpdated: 2020-10-22T20:24:04+03:00
Returns an array of lines from the specified file.
- Use `fs.readFileSync()` to create a `Buffer` from a file.
- Convert buffer to string using `buf.toString(encoding)` function.
- Use `String.prototype.split(\n)` to create an array of lines from the contents of the file.
- Convert buffer to string using `Buffer.toString(encoding)` function.
- Use `String.prototype.split('\n')` to create an array of lines from the contents of the file.
```js
const fs = require('fs');

View File

@ -9,7 +9,7 @@ Replaces the last occurence of a pattern in a string.
- Use `typeof` to determine if `pattern` is a string or a regular expression.
- If the `pattern` is a string, use it as the `match`.
- Otherwise, use the `RegeExp` constructor to create a new regular expression using the `RegExp.source` of the `pattern` and adding the `'g'` flag to it. Use `String.prototype.match()` and `Array.prototype.slice()` to get the last match, if any.
- Otherwise, use the `RegeExp` constructor to create a new regular expression using the `RegExp.prototype.source` of the `pattern` and adding the `'g'` flag to it. Use `String.prototype.match()` and `Array.prototype.slice()` to get the last match, if any.
- Use `String.prototype.lastIndexOf()` to find the last occurence of the match in the string.
- If a match is found, use `String.prototype.slice()` and a template literal to replace the matching substring with the given `replacement`.
- If no match is found, return the original string.

View File

@ -11,7 +11,7 @@ Gets the size of an array, object or string.
- Use `Array.prototype.length` property for arrays.
- Use `length` or `size` value if available or number of keys for objects.
- Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `val` for strings.
- Split strings into array of characters with `split('')` and return its length.
- Split strings into array of characters with `String.prototype.split('')` and return its length.
```js

View File

@ -7,7 +7,7 @@ lastUpdated: 2020-10-22T20:24:30+03:00
Measures the time it takes for a function to execute.
- Use `Console.time()` and `Console.timeEnd()` to measure the difference between the start and end times to determine how long the callback took to execute.
- Use `console.time()` and `console.timeEnd()` to measure the difference between the start and end times to determine how long the callback took to execute.
```js
const timeTaken = callback => {

View File

@ -8,7 +8,7 @@ lastUpdated: 2021-10-13T19:29:39+02:00
Converts a date to extended ISO format (ISO 8601), including timezone offset.
- Use `Date.prototype.getTimezoneOffset()` to get the timezone offset and reverse it. Store its sign in `diff`.
- Define a helper function, `pad`, that normalizes any passed number to an integer using `Math.floor()` and `Math.abs()` and pads it to `2` digits, using `String.prototype.padStart()`.
- Define a helper function, `pad()`, that normalizes any passed number to an integer using `Math.floor()` and `Math.abs()` and pads it to `2` digits, using `String.prototype.padStart()`.
- Use `pad()` and the built-in methods in the `Date` prototype to build the ISO 8601 string with timezone offset.
```js

View File

@ -6,10 +6,10 @@ firstSeen: 2017-12-30T18:35:54+02:00
lastUpdated: 2021-01-04T13:04:15+02:00
---
Returns `true` if the string is `y`/`yes` or `false` if the string is `n`/`no`.
Returns `true` if the string is `'y'`/`'yes'` or `false` if the string is `'n'`/`'no'`.
- Use `RegExp.prototype.test()` to check if the string evaluates to `y/yes` or `n/no`.
- Omit the second argument, `def` to set the default answer as `no`.
- Use `RegExp.prototype.test()` to check if the string evaluates to `'y'`/`'yes'` or `'n'`/`'no'`.
- Omit the second argument, `def` to set the default answer as `'no'`.
```js
const yesNo = (val, def = false) =>