Update snippet descriptions & tags

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-10-18 23:04:45 +03:00
parent 216c3ac77a
commit 2b6f2b1740
22 changed files with 47 additions and 38 deletions

View File

@ -3,9 +3,10 @@ title: bifurcate
tags: array,intermediate
---
Splits values into two groups. If an element in `filter` is truthy, the corresponding element in the collection belongs to the first group; otherwise, it belongs to the second group.
Splits values into two groups, based on the result of the given `filter` array.
- Use `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on `filter`.
- If `filter` has a truthy value for any element, add it to the first group, otherwise add it to the second group.
```js
const bifurcate = (arr, filter) =>
@ -13,5 +14,6 @@ const bifurcate = (arr, filter) =>
```
```js
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]); // [ ['beep', 'boop', 'bar'], ['foo'] ]
bifurcate(['beep', 'boop', 'foo', 'bar'], [true, true, false, true]);
// [ ['beep', 'boop', 'bar'], ['foo'] ]
```

View File

@ -3,9 +3,10 @@ title: bifurcateBy
tags: array,intermediate
---
Splits values into two groups according to a predicate function, which specifies which group an element in the input collection belongs to. If the predicate function returns a truthy value, the collection element belongs to the first group; otherwise, it belongs to the second group.
Splits values into two groups, based on the result of the given filtering function.
- Use `Array.prototype.reduce()` and `Array.prototype.push()` to add elements to groups, based on the value returned by `fn` for each element.
- If `fn` returns a truthy value for any element, add it to the first group, otherwise add it to the second group.
```js
const bifurcateBy = (arr, fn) =>

View File

@ -1,6 +1,6 @@
---
title: binary
tags: function,beginner
tags: function,intermediate
---
Creates a function that accepts up to two arguments, ignoring any additional arguments.

View File

@ -1,15 +1,16 @@
---
title: bind
tags: function,object,intermediate
tags: function,object,advanced
---
Creates a function that invokes `fn` with a given context, optionally adding any additional supplied parameters to the beginning of the arguments.
Creates a function that invokes `fn` with a given context, optionally prepending any additional supplied parameters to the arguments.
- Return a `function` that uses `Function.prototype.apply()` to apply the given `context` to `fn`.
- Use `Array.prototype.concat()` to prepend any additional supplied parameters to the arguments.
- Use the spread operator (`...`) to prepend any additional supplied parameters to the arguments.
```js
const bind = (fn, context, ...boundArgs) => (...args) => fn.apply(context, [...boundArgs, ...args]);
const bind = (fn, context, ...boundArgs) => (...args) =>
fn.apply(context, [...boundArgs, ...args]);
```
```js

View File

@ -5,7 +5,8 @@ tags: object,function,intermediate
Binds methods of an object to the object itself, overwriting the existing method.
- Use `Array.prototype.forEach()` to return a `function` that uses `Function.prototype.apply()` to apply the given context (`obj`) to `fn` for each function specified.
- Use `Array.prototype.forEach()` to iterate over the given `fns`.
- Return a function for each one, using `Function.prototype.apply()` to apply the given context (`obj`) to `fn`.
```js
const bindAll = (obj, ...fns) =>

View File

@ -1,9 +1,9 @@
---
title: bindKey
tags: function,object,intermediate
tags: function,object,advanced
---
Creates a function that invokes the method at a given key of an object, optionally adding any additional supplied parameters to the beginning of the arguments.
Creates a function that invokes the method at a given key of an object, optionally prepending any additional supplied parameters to the arguments.
- Return a `function` that uses `Function.prototype.apply()` to bind `context[fn]` to `context`.
- Use the spread operator (`...`) to prepend any additional supplied parameters to the arguments.

View File

@ -1,9 +1,9 @@
---
title: binomialCoefficient
tags: math,intermediate
tags: math,beginner
---
Evaluates the binomial coefficient of two integers `n` and `k`.
Calculates the number of ways to choose `k` items from `n` items without repetition and without order.
- Use `Number.isNaN()` to check if any of the two values is `NaN`.
- Check if `k` is less than `0`, greater than or equal to `n`, equal to `1` or `n - 1` and return the appropriate result.

View File

@ -3,7 +3,7 @@ title: both
tags: function,logic,beginner
---
Returns `true` if both functions return `true` for a given set of arguments, `false` otherwise.
Checks if both of the given functions return `true` for a given set of arguments.
- Use the logical and (`&&`) operator on the result of calling the two functions with the supplied `args`.

View File

@ -1,9 +1,9 @@
---
title: bottomVisible
tags: browser,intermediate
tags: browser,beginner
---
Returns `true` if the bottom of the page is visible, `false` otherwise.
Checks if the bottom of the page is visible.
- Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible.

View File

@ -5,7 +5,8 @@ tags: string,beginner
Returns the length of a string in bytes.
- Convert a given string to a [`Blob` Object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) and find its `size`.
- Convert a given string to a [`Blob` Object](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
- Use `Blob.size` to get the length of the string in bytes.
```js
const byteSize = str => new Blob([str]).size;

View File

@ -1,11 +1,11 @@
---
title: call
tags: function,intermediate
tags: function,advanced
---
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Given a key and a set of arguments, call them when given a context.
- Use a closure to call a stored key with stored arguments.
- Use a closure to call `key` with `args` for the given `context`.
```js
const call = (key, ...args) => context => context[key](...args);

View File

@ -5,7 +5,8 @@ tags: string,intermediate
Capitalizes the first letter of a string.
- Use array destructuring and `String.prototype.toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.prototype.join('')` to make it a string again.
- Use array destructuring and `String.prototype.toUpperCase()` to capitalize the first letter of the string.
- Use `Array.prototype.join('')` to combine the capitalized `first` with the `...rest` of the characters.
- Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
```js

View File

@ -5,7 +5,7 @@ tags: math,beginner
Converts Celsius to Fahrenheit.
- Follows the conversion formula `F = 1.8C + 32`.
- Follows the conversion formula `F = 1.8 * C + 32`.
```js
const celsiusToFahrenheit = degrees => 1.8 * degrees + 32;

View File

@ -3,9 +3,9 @@ title: checkProp
tags: function,object,beginner
---
Given a `predicate` function and a `prop` string, this curried function will then take an `object` to inspect by calling the property and passing it to the predicate.
Creates a function that will invoke a predicate function for the specified property on a given object.
- Summon `prop` on `obj`, pass it to a provided `predicate` function and return a masked boolean.
- Return a curried function, that will invoke `predicate` for the specified `prop` on `obj` and return a boolean.
```js
const checkProp = (predicate, prop) => obj => !!predicate(obj[prop]);

View File

@ -1,17 +1,17 @@
---
title: coalesceFactory
tags: type,intermediate
tags: function,type,intermediate
---
Returns a customized coalesce function that returns the first argument that returns `true` from the provided argument validation function.
Customizes a coalesce function that returns the first argument which is true based on the given validator.
- Use `Array.prototype.find()` to return the first argument that returns `true` from the provided argument validation function.
- Use `Array.prototype.find()` to return the first argument that returns `true` from the provided argument validation function, `valid`.
```js
const coalesceFactory = valid => (...args) => args.find(valid);
```
```js
const customCoalesce = coalesceFactory(_ => ![null, undefined, '', NaN].includes(_));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // "Waldo"
const customCoalesce = coalesceFactory(v => ![null, undefined, '', NaN].includes(v));
customCoalesce(undefined, null, NaN, '', 'Waldo'); // 'Waldo'
```

View File

@ -3,7 +3,7 @@ title: colorize
tags: node,string,intermediate
---
Add special characters to text to print in color in the console (combined with `console.log()`).
Adds special characters to text to print in color in the console (combined with `console.log()`).
- Use template literals and special characters to add the appropriate color code to the string output.
- For background colors, add a special character that resets the background color at the end of the string.

View File

@ -3,7 +3,7 @@ title: compactWhitespace
tags: string,regexp,beginner
---
Returns a string with whitespaces compacted.
Compacts whitespaces in a string.
- Use `String.prototype.replace()` with a regular expression to replace all occurrences of 2 or more whitespace characters with a single space.

View File

@ -3,7 +3,7 @@ title: containsWhitespace
tags: string,regexp,beginner
---
Returns `true` if the given string contains any whitespace characters, `false` otherwise.
Checks if the given string contains any whitespace characters.
- Use `RegExp.prototype.test()` with an appropriate regular expression to check if the given string contains any whitespace characters.

View File

@ -9,7 +9,8 @@ Accepts a converging function and a list of branching functions and returns a fu
- Use the spread operator (`...`) to call `coverger` with the results of all other functions.
```js
const converge = (converger, fns) => (...args) => converger(...fns.map(fn => fn.apply(null, args)));
const converge = (converger, fns) => (...args) =>
converger(...fns.map(fn => fn.apply(null, args)));
```
```js

View File

@ -1,9 +1,9 @@
---
title: copyToClipboard
tags: browser,string,advanced
tags: browser,string,event,advanced
---
Copy a string to the clipboard.
Copies a string to the clipboard.
Only works as a result of user action (i.e. inside a `click` event listener).
- Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.

View File

@ -5,10 +5,11 @@ tags: array,intermediate
Counts the occurrences of a value in an array.
- Use `Array.prototype.reduce()` to increment a counter each time you encounter the specific value inside the array.
- Use `Array.prototype.reduce()` to increment a counter each time the specific value is encountered inside the array.
```js
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
const countOccurrences = (arr, val) =>
arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
```
```js

View File

@ -3,7 +3,7 @@ title: countWeekDaysBetween
tags: date,intermediate
---
Returns the weekday count between two dates.
Counts the weekdays between two dates.
- Use `Array.from()` to construct an array with `length` equal to the number of days between `startDate` and `endDate`.
- Use `Array.prototype.reduce()` to iterate over the array, checking if each date is a weekday and incrementing `count`.