Archive, multitagging, cleanup
Cleaned up the current snippets for consistency and minor problems, added multiple tags to most of them, archived a few.
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Converts the values of RGB components to a color code.
|
Converts the values of RGB components to a color code.
|
||||||
|
|
||||||
Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `toString(16)`, then `padStart(6,'0')` to get a 6-digit hexadecimal value.
|
Convert given RGB parameters to hexadecimal string using bitwise left-shift operator (`<<`) and `toString(16)`, then `String.padStart(6,'0')` to get a 6-digit hexadecimal value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
||||||
|
|||||||
@ -1,17 +1,14 @@
|
|||||||
### average
|
### average
|
||||||
|
|
||||||
Returns the average of an of two or more numbers/arrays.
|
Returns the average of an of two or more numbers.
|
||||||
|
|
||||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const average = (...arr) => {
|
const average = (...nums) => [...nums].reduce((acc, val) => acc + val, 0) / nums.length;
|
||||||
const nums = [].concat(...arr);
|
|
||||||
return nums.reduce((acc, val) => acc + val, 0) / nums.length;
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
average([1, 2, 3]); // 2
|
average(...[1, 2, 3]); // 2
|
||||||
average(1, 2, 3); // 2
|
average(1, 2, 3); // 2
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### byteSize
|
### byteSize
|
||||||
|
|
||||||
Returns the length of string.
|
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) and find its `size`.
|
||||||
|
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Capitalizes the first letter of a string.
|
Capitalizes the first letter of a string.
|
||||||
|
|
||||||
Use destructuring and `toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.join('')` to make it a string again.
|
Use array destructuring and `String.toUpperCase()` to capitalize first letter, `...rest` to get array of characters after first letter and then `Array.join('')` to make it a string again.
|
||||||
Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
|
Omit the `lowerRest` parameter to keep the rest of the string intact, or set it to `true` to convert to lowercase.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Capitalizes the first letter of every word in a string.
|
Capitalizes the first letter of every word in a string.
|
||||||
|
|
||||||
Use `replace()` to match the first character of each word and `toUpperCase()` to capitalize it.
|
Use `String.replace()` to match the first character of each word and `String.toUpperCase()` to capitalize it.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
|
const capitalizeEveryWord = str => str.replace(/\b[a-z]/g, char => char.toUpperCase());
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Removes any properties except the ones specified from a JSON object.
|
Removes any properties except the ones specified from a JSON object.
|
||||||
|
|
||||||
Use `Object.keys()` method to loop over given JSON object and deleting keys that are not `include`d in given array.
|
Use `Object.keys()` method to loop over given JSON object and deleting keys that are not included in given array.
|
||||||
Also if you give it a special key (`childIndicator`) it will search deeply inside it to apply function to inner objects too.
|
If you pass a special key,`childIndicator`, it will search deeply apply the function to inner objects, too.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
const cleanObj = (obj, keysToKeep = [], childIndicator) => {
|
||||||
|
|||||||
@ -5,7 +5,7 @@ Counts the occurrences of a value in an array.
|
|||||||
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
|
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const countOccurrences = (arr, value) => arr.reduce((a, v) => (v === value ? a + 1 : a + 0), 0);
|
const countOccurrences = (arr, val) => arr.reduce((a, v) => (v === val ? a + 1 : a + 0), 0);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -4,8 +4,10 @@ Computes the new ratings between two or more opponents using the [Elo rating sys
|
|||||||
of pre-ratings and returns an array containing post-ratings.
|
of pre-ratings and returns an array containing post-ratings.
|
||||||
The array should be ordered from best performer to worst performer (winner -> loser).
|
The array should be ordered from best performer to worst performer (winner -> loser).
|
||||||
|
|
||||||
Use the exponent `**` operator and math operators to compute the expected score (chance of winning)
|
Use the exponent `**` operator and math operators to compute the expected score (chance of winning).
|
||||||
of each opponent and compute the new rating for each. Loop through the ratings, using each permutation to compute the post-Elo rating for each player in a pairwise fashion. Omit the second argument to use the default K-factor of 32, or supply a custom K-factor value. For details on the third argument, see the last example.
|
of each opponent and compute the new rating for each.
|
||||||
|
Loop through the ratings, using each permutation to compute the post-Elo rating for each player in a pairwise fashion.
|
||||||
|
Omit the second argument to use the default `kFactor` of 32.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const elo = ([...ratings], kFactor = 32, selfRating) => {
|
const elo = ([...ratings], kFactor = 32, selfRating) => {
|
||||||
@ -35,7 +37,7 @@ elo([1200, 1200], 64); // [1232, 1168]
|
|||||||
// 4 player FFA, all same rank
|
// 4 player FFA, all same rank
|
||||||
elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154]
|
elo([1200, 1200, 1200, 1200]).map(Math.round); // [1246, 1215, 1185, 1154]
|
||||||
/*
|
/*
|
||||||
For teams, each rating can adjusted based on own team's average rating vs.
|
For teams, each rating can adjusted based on own team's average rating vs.
|
||||||
average rating of opposing team, with the score being added to their
|
average rating of opposing team, with the score being added to their
|
||||||
own individual rating by supplying it as the third argument.
|
own individual rating by supplying it as the third argument.
|
||||||
*/
|
*/
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Escapes a string for use in HTML.
|
Escapes a string for use in HTML.
|
||||||
|
|
||||||
Use `String.replace()` with a regex that matches the characters that need to be escaped, using a callback function to replace each character instance with its associated escaped character using a dictionary (object).
|
Use `String.replace()` with a regexp that matches the characters that need to be escaped, using a callback function to replace each character instance with its associated escaped character using a dictionary (object).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const escapeHTML = str =>
|
const escapeHTML = str =>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Escapes a string to use in a regular expression.
|
Escapes a string to use in a regular expression.
|
||||||
|
|
||||||
Use `replace()` to escape special characters.
|
Use `String.replace()` to escape special characters.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||||
|
|||||||
@ -2,8 +2,9 @@
|
|||||||
|
|
||||||
Extends a 3-digit color code to a 6-digit color code.
|
Extends a 3-digit color code to a 6-digit color code.
|
||||||
|
|
||||||
Use `Array.map()`, `split()` and `Array.join()` to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
|
Use `Array.map()`, `String.split()` and `Array.join()` to join the mapped array for converting a 3-digit RGB notated hexadecimal color-code to the 6-digit form.
|
||||||
`String.slice()` is used to remove `#` from string start since it's added once.
|
`Array.slice()` is used to remove `#` from string start since it's added once.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const extendHex = shortHex =>
|
const extendHex = shortHex =>
|
||||||
'#' +
|
'#' +
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Flattens an array.
|
Flattens an array.
|
||||||
|
|
||||||
Use a new array and concatenate it with the spread input array causing a shallow denesting of any contained arrays.
|
Use a new array, `Array.concat()` and the spread operator (`...`) to cause a shallow denesting of any contained arrays.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const flatten = arr => [].concat(...arr);
|
const flatten = arr => [].concat(...arr);
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### flip
|
### flip
|
||||||
|
|
||||||
Flip takes a function as an argument, then makes the first argument the last
|
Flip takes a function as an argument, then makes the first argument the last.
|
||||||
|
|
||||||
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
|
Return a closure that takes variadic inputs, and splices the last argument to make it the first argument before applying the rest.
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Converts a string from camelcase.
|
Converts a string from camelcase.
|
||||||
|
|
||||||
Use `replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
Use `String.replace()` to remove underscores, hyphens, and spaces and convert words to camelcase.
|
||||||
Omit the second argument to use a default separator of `_`.
|
Omit the second argument to use a default `separator` of `_`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const fromCamelCase = (str, separator = '_') =>
|
const fromCamelCase = (str, separator = '_') =>
|
||||||
|
|||||||
@ -9,10 +9,11 @@ Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
|||||||
```js
|
```js
|
||||||
const gcd = (...arr) => {
|
const gcd = (...arr) => {
|
||||||
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
const _gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||||
return [].concat(...arr).reduce((a, b) => _gcd(a, b));
|
return [...arr].reduce((a, b) => _gcd(a, b));
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
gcd(8, 36); // 4
|
gcd(8, 36); // 4
|
||||||
|
gcd(...[12,8,32]); // 4
|
||||||
```
|
```
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Returns the native type of a value.
|
Returns the native type of a value.
|
||||||
|
|
||||||
Returns lowercased constructor name of value, "undefined" or "null" if value is undefined or null
|
Returns lowercased constructor name of value, `"undefined"` or `"null"` if value is `undefined` or `null`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const getType = v =>
|
const getType = v =>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Returns an object containing the parameters of the current URL.
|
Returns an object containing the parameters of the current URL.
|
||||||
|
|
||||||
Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object.
|
Use `String.match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object.
|
||||||
Pass `location.search` as the argument to apply to the current `url`.
|
Pass `location.search` as the argument to apply to the current `url`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -3,10 +3,10 @@
|
|||||||
Initializes and fills an array with the specified values.
|
Initializes and fills an array with the specified values.
|
||||||
|
|
||||||
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
Use `Array(n)` to create an array of the desired length, `fill(v)` to fill it with the desired values.
|
||||||
You can omit `value` to use a default value of `0`.
|
You can omit `val` to use a default value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const initializeArrayWithValues = (n, value = 0) => Array(n).fill(value);
|
const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -5,10 +5,9 @@ Checks if the given argument is an array.
|
|||||||
Use `Array.isArray()` to check if a value is classified as an array.
|
Use `Array.isArray()` to check if a value is classified as an array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const isArray = val => !!val && Array.isArray(val);
|
const isArray = val => Array.isArray(val);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
isArray(null); // false
|
|
||||||
isArray([1]); // true
|
isArray([1]); // true
|
||||||
```
|
```
|
||||||
|
|||||||
@ -9,7 +9,6 @@ const isFunction = val => typeof val === 'function';
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
isFunction(null); // false
|
|
||||||
isFunction('x'); // false
|
isFunction('x'); // false
|
||||||
isFunction(x => x); // true
|
isFunction(x => x); // true
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### lcm
|
### lcm
|
||||||
|
|
||||||
Returns the least common multiple of two or more numbers/arrays.
|
Returns the least common multiple of two or more numbers.
|
||||||
|
|
||||||
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
|
Use the greatest common divisor (GCD) formula and `Math.abs()` to determine the least common multiple.
|
||||||
The GCD formula uses recursion.
|
The GCD formula uses recursion.
|
||||||
@ -9,11 +9,11 @@ The GCD formula uses recursion.
|
|||||||
const lcm = (...arr) => {
|
const lcm = (...arr) => {
|
||||||
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
const gcd = (x, y) => (!y ? x : gcd(y, x % y));
|
||||||
const _lcm = (x, y) => x * y / gcd(x, y);
|
const _lcm = (x, y) => x * y / gcd(x, y);
|
||||||
return [].concat(...arr).reduce((a, b) => _lcm(a, b));
|
return [...arr].reduce((a, b) => _lcm(a, b));
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
lcm(12, 7); // 84
|
lcm(12, 7); // 84
|
||||||
lcm([1, 3, 4], 5); // 60
|
lcm(...[1, 3, 4, 5]); // 60
|
||||||
```
|
```
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Replaces all but the last `num` of characters with the specified mask character.
|
Replaces all but the last `num` of characters with the specified mask character.
|
||||||
|
|
||||||
Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regex to replace every character with the mask character.
|
Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regexp to replace every character with the mask character.
|
||||||
Concatenate the masked characters with the remaining unmasked portion of the string.
|
Concatenate the masked characters with the remaining unmasked portion of the string.
|
||||||
Omit the second argument, `num`, to keep a default of `4` characters unmasked. If `num` is negative, the unmasked characters will be at the start of the string.
|
Omit the second argument, `num`, to keep a default of `4` characters unmasked. If `num` is negative, the unmasked characters will be at the start of the string.
|
||||||
Omit the third argument, `mask`, to use a default character of `'*'` for the mask.
|
Omit the third argument, `mask`, to use a default character of `'*'` for the mask.
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Negates a predicate function.
|
Negates a predicate function.
|
||||||
|
|
||||||
Take a predicate function and apply `not` to it with its arguments.
|
Take a predicate function and apply the not operator (`!`) to it with its arguments.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const negate = func => (...args) => !func(...args);
|
const negate = func => (...args) => !func(...args);
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Returns a sorted array of objects ordered by properties and orders.
|
Returns a sorted array of objects ordered by properties and orders.
|
||||||
|
|
||||||
Uses a custom implementation of sort, that reduces the props array argument with a default value of 0, it uses destructuring to swap the properties position depending on the order passed.
|
Uses `Array.sort()`, `Array.reduce()` on the `props` array with a default value of `0`, use array destructuring to swap the properties position depending on the order passed.
|
||||||
If no orders array is passed it sort by 'asc' by default.
|
If no `orders` array is passed it sort by `'asc'` by default.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const orderBy = (arr, props, orders) =>
|
const orderBy = (arr, props, orders) =>
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Returns `true` if the given string is a palindrome, `false` otherwise.
|
Returns `true` if the given string is a palindrome, `false` otherwise.
|
||||||
|
|
||||||
Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric characters from it.
|
Convert string `String.toLowerCase()` and use `String.replace()` to remove non-alphanumeric characters from it.
|
||||||
Then, `split('')` into individual characters, `reverse()`, `join('')` and compare to the original, unreversed string, after converting it `tolowerCase()`.
|
Then, `String.split('')` into individual characters, `Array.reverse()`, `String.join('')` and compare to the original, unreversed string, after converting it `String.tolowerCase()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const palindrome = str => {
|
const palindrome = str => {
|
||||||
|
|||||||
@ -1,16 +0,0 @@
|
|||||||
### repeatString
|
|
||||||
|
|
||||||
Repeats a string n times using `String.repeat()`
|
|
||||||
|
|
||||||
If no string is provided the default is `""` and the default number of times is 2.
|
|
||||||
|
|
||||||
```js
|
|
||||||
const repeatString = (str = '', num = 2) => {
|
|
||||||
return num >= 0 ? str.repeat(num) : str;
|
|
||||||
};
|
|
||||||
```
|
|
||||||
|
|
||||||
```js
|
|
||||||
repeatString('abc', 3); // 'abcabcabc'
|
|
||||||
repeatString('abc'); // 'abcabc'
|
|
||||||
```
|
|
||||||
@ -2,13 +2,12 @@
|
|||||||
|
|
||||||
Reverses a string.
|
Reverses a string.
|
||||||
|
|
||||||
Use `split('')` and `Array.reverse()` to reverse the order of the characters in the string.
|
Use the spread operator (`...`) and `Array.reverse()` to reverse the order of the characters in the string.
|
||||||
Combine characters to get a string using `join('')`.
|
Combine characters to get a string using `String.join('')`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const reverseString = str =>
|
const reverseString = str =>
|
||||||
str
|
[..str]
|
||||||
.split('')
|
|
||||||
.reverse()
|
.reverse()
|
||||||
.join('');
|
.join('');
|
||||||
```
|
```
|
||||||
|
|||||||
@ -1,8 +1,8 @@
|
|||||||
### sbdm
|
### sbdm
|
||||||
|
|
||||||
This algorithm is a simple hash-algorithm that hashes it input string `s` into a whole number.
|
Hashes the input string into a whole number.
|
||||||
|
|
||||||
Use `split('')` and `Array.reduce()` to create a hash of the input string, utilizing bit shifting.
|
Use `String.split('')` and `Array.reduce()` to create a hash of the input string, utilizing bit shifting.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sdbm = str => {
|
const sdbm = str => {
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
### select
|
### select
|
||||||
|
|
||||||
Retrieve a property that indicated by the selector from an object.
|
Retrieve a property indicated by the selector from an object.
|
||||||
|
|
||||||
If the property does not exists returns `undefined`.
|
If the property does not exists returns `undefined`.
|
||||||
|
|
||||||
|
|||||||
@ -2,10 +2,10 @@
|
|||||||
|
|
||||||
Sets the value of a CSS rule for the specified element.
|
Sets the value of a CSS rule for the specified element.
|
||||||
|
|
||||||
Use `element.style` to set the value of the CSS rule for the specified element to `value`.
|
Use `element.style` to set the value of the CSS rule for the specified element to `val`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const setStyle = (el, ruleName, value) => (el.style[ruleName] = value);
|
const setStyle = (el, ruleName, val) => (el.style[ruleName] = val);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Randomizes the order of the values of an array, returning a new array.
|
Randomizes the order of the values of an array, returning a new array.
|
||||||
|
|
||||||
Uses the Fisher-Yates algorithm to reorder the elements of the array, based on the [Lodash implementation](https://github.com/lodash/lodash/blob/b2ea6b1cd251796dcb5f9700c4911a7b6223920b/shuffle.js), but as a pure function.
|
Uses the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle) to reorder the elements of the array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const shuffle = ([...arr]) => {
|
const shuffle = ([...arr]) => {
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Returns an array of elements that appear in both arrays.
|
Returns an array of elements that appear in both arrays.
|
||||||
|
|
||||||
Use `filter()` to remove values that are not part of `values`, determined using `includes()`.
|
Use `Array.filter()` to remove values that are not part of `values`, determined using `Array.includes()`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const similarity = (arr, values) => arr.filter(v => values.includes(v));
|
const similarity = (arr, values) => arr.filter(v => values.includes(v));
|
||||||
|
|||||||
@ -2,20 +2,20 @@
|
|||||||
|
|
||||||
Get size of arrays, objects or strings.
|
Get size of arrays, objects or strings.
|
||||||
|
|
||||||
Get type of `value` (`array`, `object` or `string`).
|
Get type of `val` (`array`, `object` or `string`).
|
||||||
Use `length` property for arrays.
|
Use `length` property for arrays.
|
||||||
Use `length` or `size` value if available or number of keys for objects.
|
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 `value` for strings.
|
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 `split('')` and return its length.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const size = value =>
|
const size = val =>
|
||||||
Array.isArray(value)
|
Array.isArray(val)
|
||||||
? value.length
|
? val.length
|
||||||
: value && typeof value === 'object'
|
: val && typeof val === 'object'
|
||||||
? value.size || value.length || Object.keys(value).length
|
? val.size || val.length || Object.keys(val).length
|
||||||
: typeof value === 'string' ? new Blob([value]).size : 0;
|
: typeof val === 'string' ? new Blob([val]).size : 0;
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -2,12 +2,11 @@
|
|||||||
|
|
||||||
Alphabetically sorts the characters in a string.
|
Alphabetically sorts the characters in a string.
|
||||||
|
|
||||||
Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
Use the spread operator (`...`), `Array.sort()` and `String.localeCompare()` to sort the characters in `str`, recombine using `String.join('')`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sortCharactersInString = str =>
|
const sortCharactersInString = str =>
|
||||||
str
|
[...str]
|
||||||
.split('')
|
|
||||||
.sort((a, b) => a.localeCompare(b))
|
.sort((a, b) => a.localeCompare(b))
|
||||||
.join('');
|
.join('');
|
||||||
```
|
```
|
||||||
|
|||||||
@ -5,9 +5,9 @@ Returns the sum of two or more numbers/arrays.
|
|||||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const sum = (...arr) => [].concat(...arr).reduce((acc, val) => acc + val, 0);
|
const sum = (...arr) => [...arr].reduce((acc, val) => acc + val, 0);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
sum([1, 2, 3, 4]); // 10
|
sum(...[1, 2, 3, 4]); // 10
|
||||||
```
|
```
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Returns all elements in an array except for the first one.
|
Returns all elements in an array except for the first one.
|
||||||
|
|
||||||
Return `arr.slice(1)` if the array's `length` is more than `1`, otherwise, return the whole array.
|
Return `Array.slice(1)` if the array's `length` is more than `1`, otherwise, return the whole array.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
|
const tail = arr => (arr.length > 1 ? arr.slice(1) : arr);
|
||||||
|
|||||||
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
Converts a string to camelcase.
|
Converts a string to camelcase.
|
||||||
|
|
||||||
Break the string into words and combine them capitalizing the first letter of each word.
|
Break the string into words and combine them capitalizing the first letter of each word, using a regexp.
|
||||||
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toCamelCase = str => {
|
const toCamelCase = str => {
|
||||||
|
|||||||
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
Converts a string to kebab case.
|
Converts a string to kebab case.
|
||||||
|
|
||||||
Break the string into words and combine them using `-` as a separator.
|
Break the string into words and combine them adding `-` as a separator, using a regexp.
|
||||||
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toKebabCase = str =>
|
const toKebabCase = str =>
|
||||||
|
|||||||
@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
Converts a string to snake case.
|
Converts a string to snake case.
|
||||||
|
|
||||||
Break the string into words and combine them using `_` as a separator.
|
Break the string into words and combine them adding `_` as a separator, using a regexp.
|
||||||
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const toSnakeCase = str =>
|
const toSnakeCase = str =>
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
### tomorrow
|
### tomorrow
|
||||||
|
|
||||||
Results in a string representation of tomorrow's date.
|
Results in a string representation of tomorrow's date.
|
||||||
Use `new Date()` to get today's date, adding `86400000` of seconds to it(24 hours), using `toISOString` to convert Date object to string.
|
Use `new Date()` to get today's date, adding `86400000` of seconds to it(24 hours), using `Date.toISOString()` to convert Date object to string.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const tomorrow = () => new Date(new Date().getTime() + 86400000).toISOString().split('T')[0];
|
const tomorrow = () => new Date(new Date().getTime() + 86400000).toISOString().split('T')[0];
|
||||||
|
|||||||
@ -3,7 +3,7 @@
|
|||||||
Truncates a string up to a specified length.
|
Truncates a string up to a specified length.
|
||||||
|
|
||||||
Determine if the string's `length` is greater than `num`.
|
Determine if the string's `length` is greater than `num`.
|
||||||
Return the string truncated to the desired length, with `...` appended to the end or the original string.
|
Return the string truncated to the desired length, with `'...'` appended to the end or the original string.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const truncateString = (str, num) =>
|
const truncateString = (str, num) =>
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
Returns `true` if the given value is a number, `false` otherwise.
|
Returns `true` if the given value is a number, `false` otherwise.
|
||||||
|
|
||||||
Use `!isNaN` in combination with `parseFloat()` to check if the argument is a number.
|
Use `!isNaN()` in combination with `parseFloat()` to check if the argument is a number.
|
||||||
Use `isFinite()` to check if the number is finite.
|
Use `isFinite()` to check if the number is finite.
|
||||||
Use `Number()` to check if the coercion holds.
|
Use `Number()` to check if the coercion holds.
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,8 @@
|
|||||||
|
|
||||||
Converts a given string into an array of words.
|
Converts a given string into an array of words.
|
||||||
|
|
||||||
Use `String.split()` with a supplied pattern (defaults to non-alpha as a regex) to convert to an array of strings. Use `Array.filter()` to remove any empty strings.
|
Use `String.split()` with a supplied pattern (defaults to non-alpha as a regexp) to convert to an array of strings. Use `Array.filter()` to remove any empty strings.
|
||||||
Omit the second argument to use the default regex.
|
Omit the second argument to use the default regexp.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const words = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
|
const words = (str, pattern = /[^a-zA-Z-]+/) => str.split(pattern).filter(Boolean);
|
||||||
|
|||||||
216
tag_database
216
tag_database
@ -1,186 +1,176 @@
|
|||||||
anagrams:string
|
anagrams:string,recursion
|
||||||
arrayToHtmlList:browser
|
arrayToHtmlList:browser,array
|
||||||
average:math
|
average:math,array
|
||||||
bottomVisible:browser
|
bottomVisible:browser
|
||||||
byteSize:string
|
byteSize:string
|
||||||
call:adapter
|
call:adapter,function
|
||||||
capitalize:string
|
capitalize:string,array
|
||||||
capitalizeEveryWord:string
|
capitalizeEveryWord:string,regexp
|
||||||
chainAsync:function
|
chainAsync:function
|
||||||
chunk:array
|
chunk:array
|
||||||
clampNumber:math
|
clampNumber:math
|
||||||
cleanObj:object
|
cleanObj:object,json
|
||||||
cloneRegExp:utility
|
cloneRegExp:utility,regexp
|
||||||
coalesce:utility
|
coalesce:utility
|
||||||
coalesceFactory:utility
|
coalesceFactory:utility
|
||||||
collatz:math
|
collectInto:adapter,function,array
|
||||||
collectInto:adapter
|
|
||||||
compact:array
|
compact:array
|
||||||
compose:function
|
compose:function
|
||||||
copyToClipboard:browser
|
copyToClipboard:browser,string,advanced
|
||||||
countOccurrences:array
|
countOccurrences:array
|
||||||
countVowels:string
|
countVowels:string
|
||||||
currentURL:browser
|
currentURL:browser,url
|
||||||
curry:function
|
curry:function,recursion
|
||||||
deepFlatten:array
|
deepFlatten:array,recursion
|
||||||
defer:function
|
defer:function
|
||||||
detectDeviceType:browser
|
detectDeviceType:browser
|
||||||
difference:array
|
difference:array,math
|
||||||
differenceWith:array
|
differenceWith:array,function
|
||||||
digitize:math
|
digitize:math,array
|
||||||
distance:math
|
distance:math
|
||||||
distinctValuesOfArray:array
|
distinctValuesOfArray:array
|
||||||
dropElements:array
|
dropElements:array,function
|
||||||
dropRight:array
|
dropRight:array
|
||||||
elementIsVisibleInViewport:browser
|
elementIsVisibleInViewport:browser
|
||||||
elo:math
|
elo:math,array,advanced
|
||||||
escapeHTML:string
|
escapeHTML:string,browser,regexp
|
||||||
escapeRegExp:string
|
escapeRegExp:string,regexp
|
||||||
everyNth:array
|
everyNth:array
|
||||||
extendHex:utility
|
extendHex:utility,string
|
||||||
factorial:math
|
factorial:math,recursion
|
||||||
factors:math
|
fibonacci:math,array
|
||||||
fibonacci:math
|
|
||||||
fibonacciCountUntilNum:math
|
|
||||||
fibonacciUntilNum:math
|
|
||||||
filterNonUnique:array
|
filterNonUnique:array
|
||||||
flatten:array
|
flatten:array
|
||||||
flattenDepth:array
|
flattenDepth:array,recursion
|
||||||
flip:adapter
|
flip:adapter,function
|
||||||
formatDuration:date
|
formatDuration:date,math,string,utility
|
||||||
fromCamelCase:string
|
fromCamelCase:string
|
||||||
functionName:function
|
functionName:function,utility
|
||||||
gcd:math
|
gcd:math,recursion
|
||||||
geometricProgression:math
|
geometricProgression:math
|
||||||
getDaysDiffBetweenDates:date
|
getDaysDiffBetweenDates:date
|
||||||
getScrollPosition:browser
|
getScrollPosition:browser
|
||||||
getStyle:browser
|
getStyle:browser,css
|
||||||
getType:utility
|
getType:type
|
||||||
getURLParameters:utility
|
getURLParameters:utility,browser,string,url
|
||||||
groupBy:array
|
groupBy:array
|
||||||
hammingDistance:math
|
hammingDistance:math
|
||||||
hasClass:browser
|
hasClass:browser,css
|
||||||
hasFlags:node
|
hasFlags:node
|
||||||
head:array
|
head:array
|
||||||
hexToRGB:utility
|
hexToRGB:utility,string,math,advanced
|
||||||
hide:browser
|
hide:browser,css
|
||||||
howManyTimes:math
|
httpsRedirect:browser,url
|
||||||
httpsRedirect:browser
|
|
||||||
initial:array
|
initial:array
|
||||||
initialize2DArray:array
|
initialize2DArray:array
|
||||||
initializeArrayWithRange:array
|
initializeArrayWithRange:array,math
|
||||||
initializeArrayWithValues:array
|
initializeArrayWithValues:array,amth
|
||||||
inRange:math
|
inRange:math
|
||||||
intersection:array
|
intersection:array,math
|
||||||
invertKeyValues:object
|
invertKeyValues:object
|
||||||
isAbsoluteURL:string
|
isAbsoluteURL:string,utility,browser,url
|
||||||
isArmstrongNumber:math
|
isArray:type,array
|
||||||
isArray:utility
|
isArrayLike:type,array
|
||||||
isArrayLike:utility
|
isBoolean:type
|
||||||
isBoolean:utility
|
|
||||||
isDivisible:math
|
isDivisible:math
|
||||||
isEven:math
|
isEven:math
|
||||||
isFunction:utility
|
isFunction:type,function
|
||||||
isNull:utility
|
isNull:type
|
||||||
isNumber:utility
|
isNumber:type,math
|
||||||
isPrime:math
|
isPrime:math
|
||||||
isPrimitive:utility
|
isPrimitive:type,function,array,string
|
||||||
isPromiseLike:utility
|
isPromiseLike:type,function,promise
|
||||||
isSorted:array
|
isSorted:array
|
||||||
isString:utility
|
isString:type,string
|
||||||
isSymbol:utility
|
isSymbol:type
|
||||||
isTravisCI:node
|
isTravisCI:node
|
||||||
isValidJSON:utility
|
isValidJSON:type,json
|
||||||
join:array
|
join:array
|
||||||
JSONToDate:date
|
JSONToFile:node,json
|
||||||
JSONToFile:node
|
|
||||||
last:array
|
last:array
|
||||||
lcm:math
|
lcm:math,recursion
|
||||||
lowercaseKeys:object
|
lowercaseKeys:object
|
||||||
luhnCheck:math
|
luhnCheck:math,utility
|
||||||
mapObject:array
|
mapObject:array,object
|
||||||
mask:string
|
mask:string,utility,regexp
|
||||||
maxN:array
|
maxN:array,math
|
||||||
median:math
|
median:math,array
|
||||||
memoize:function
|
memoize:function
|
||||||
minN:array
|
minN:array,amth
|
||||||
negate:logic
|
negate:logic,function
|
||||||
nthElement:array
|
nthElement:array
|
||||||
objectFromPairs:object
|
objectFromPairs:object,array
|
||||||
objectToPairs:object
|
objectToPairs:object,array
|
||||||
once:function
|
once:function
|
||||||
onUserInputChange:browser
|
onUserInputChange:browser,advanced
|
||||||
orderBy:object
|
orderBy:object,array
|
||||||
palindrome:string
|
palindrome:string
|
||||||
percentile:math
|
percentile:math
|
||||||
pick:array
|
pick:array
|
||||||
pipeFunctions:adapter
|
pipeFunctions:adapter,function
|
||||||
pluralize:string
|
pluralize:string
|
||||||
powerset:math
|
powerset:math
|
||||||
prettyBytes:utility
|
prettyBytes:utility,string,math
|
||||||
primes:math
|
primes:math,array
|
||||||
promisify:adapter
|
promisify:adapter,function,promise
|
||||||
pull:array
|
pull:array
|
||||||
pullAtIndex:array
|
pullAtIndex:array
|
||||||
pullAtValue:array
|
pullAtValue:array
|
||||||
quickSort:array
|
randomHexColorCode:utility,random
|
||||||
randomHexColorCode:utility
|
randomIntegerInRange:math,utility,random
|
||||||
randomIntegerInRange:math
|
randomNumberInRange:math,utility,random
|
||||||
randomNumberInRange:math
|
readFileLines:node,array,string
|
||||||
readFileLines:node
|
redirect:browser,url
|
||||||
redirect:browser
|
|
||||||
reducedFilter:array
|
reducedFilter:array
|
||||||
remove:array
|
remove:array
|
||||||
repeatString:string
|
repeatString:string
|
||||||
reverseString:string
|
reverseString:string,array
|
||||||
RGBToHex:utility
|
RGBToHex:utility
|
||||||
round:math
|
round:math
|
||||||
runAsync:browser
|
runAsync:browser,function,advanced,promise,url
|
||||||
runPromisesInSeries:function
|
runPromisesInSeries:function,promise
|
||||||
sample:array
|
sample:array,random
|
||||||
sampleSize:array
|
sampleSize:array,random
|
||||||
scrollToTop:browser
|
scrollToTop:browser
|
||||||
sdbm:utility
|
sdbm:math,utility
|
||||||
select:object
|
select:object
|
||||||
setStyle:browser
|
setStyle:browser
|
||||||
shallowClone:object
|
shallowClone:object
|
||||||
show:browser
|
show:browser,css
|
||||||
shuffle:array
|
shuffle:array,random
|
||||||
similarity:array
|
similarity:array,math
|
||||||
size:object
|
size:object,array,string
|
||||||
sleep:function
|
sleep:function,promise
|
||||||
solveRPN:math
|
|
||||||
sortCharactersInString:string
|
sortCharactersInString:string
|
||||||
sortedIndex:array
|
sortedIndex:array,math
|
||||||
speechSynthesis:browser
|
|
||||||
splitLines:string
|
splitLines:string
|
||||||
spreadOver:adapter
|
spreadOver:adapter
|
||||||
standardDeviation:math
|
standardDeviation:math,array
|
||||||
sum:math
|
sum:math,array
|
||||||
sumPower:math
|
sumPower:math
|
||||||
symmetricDifference:array
|
symmetricDifference:array,math
|
||||||
tail:array
|
tail:array
|
||||||
take:array
|
take:array
|
||||||
takeRight:array
|
takeRight:array
|
||||||
timeTaken:utility
|
timeTaken:utility
|
||||||
toCamelCase:string
|
toCamelCase:string,regexp
|
||||||
toDecimalMark:utility
|
toDecimalMark:utility,math
|
||||||
toEnglishDate:date
|
toEnglishDate:date,string
|
||||||
toggleClass:browser
|
toggleClass:browser
|
||||||
toKebabCase:string
|
toKebabCase:string,regexp
|
||||||
tomorrow:date
|
tomorrow:date
|
||||||
toOrdinalSuffix:utility
|
toOrdinalSuffix:utility,math
|
||||||
toSnakeCase:string
|
toSnakeCase:string,regexp
|
||||||
truncateString:string
|
truncateString:string
|
||||||
truthCheckCollection:object
|
truthCheckCollection:object,logic,array
|
||||||
unescapeHTML:string
|
unescapeHTML:string,browser
|
||||||
union:array
|
union:array,math
|
||||||
untildify:node
|
untildify:node,string
|
||||||
UUIDGeneratorBrowser:browser
|
UUIDGeneratorBrowser:browser,utility,random
|
||||||
UUIDGeneratorNode:node
|
UUIDGeneratorNode:node,utility,random
|
||||||
validateNumber:utility
|
validateNumber:utility,math
|
||||||
without:array
|
without:array
|
||||||
words:string
|
words:string,regexp
|
||||||
yesNo:utility
|
yesNo:utility,regexp
|
||||||
zip:array
|
zip:array
|
||||||
zipObject:array
|
zipObject:array,object
|
||||||
|
|||||||
Reference in New Issue
Block a user