Merge branch 'master' into add-pullAtIndex
This commit is contained in:
@ -6,6 +6,6 @@ Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of t
|
||||
|
||||
```js
|
||||
const bottomVisible = () =>
|
||||
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
|
||||
document.documentElement.clientHeight + window.scrollY >= (document.documentElement.scrollHeight || document.documentElement.clientHeight);
|
||||
// bottomVisible() -> true
|
||||
```
|
||||
|
||||
11
snippets/countVowels.md
Normal file
11
snippets/countVowels.md
Normal file
@ -0,0 +1,11 @@
|
||||
### countVowels
|
||||
|
||||
Retuns `number` of vowels in provided string.
|
||||
|
||||
Use a regular expression to count number of vowels `(A, E, I, O, U)` in a `string`.
|
||||
|
||||
```js
|
||||
const countVowels = str => (str.match(/[aeiou]/ig) || []).length;
|
||||
// countVowels('foobar') -> 3
|
||||
// countVowels('gym') -> 0
|
||||
```
|
||||
10
snippets/differenceWith.md
Normal file
10
snippets/differenceWith.md
Normal file
@ -0,0 +1,10 @@
|
||||
### differenceWith
|
||||
|
||||
Filters out all values from an array for which the comparator function does not return `true`.
|
||||
|
||||
Use `Array.filter()` and `Array.find()` to find the appropriate values.
|
||||
|
||||
```js
|
||||
const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b)))
|
||||
// differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) -> [1, 1.2]
|
||||
```
|
||||
@ -2,12 +2,12 @@
|
||||
|
||||
Removes elements in an array until the passed function returns `true`. Returns the remaining elements in the array.
|
||||
|
||||
Loop through the array, using `Array.shift()` to drop the first element of the array until the returned value from the function is `true`.
|
||||
Loop through the array, using `Array.slice()` to drop the first element of the array until the returned value from the function is `true`.
|
||||
Returns the remaining elements.
|
||||
|
||||
```js
|
||||
const dropElements = (arr, func) => {
|
||||
while (arr.length > 0 && !func(arr[0])) arr.shift();
|
||||
while (arr.length > 0 && !func(arr[0])) arr = arr.slice(1);
|
||||
return arr;
|
||||
};
|
||||
// dropElements([1, 2, 3, 4], n => n >= 3) -> [3,4]
|
||||
|
||||
12
snippets/dropRight.md
Normal file
12
snippets/dropRight.md
Normal file
@ -0,0 +1,12 @@
|
||||
### dropRight
|
||||
|
||||
Returns a new array with `n` elements removed from the right
|
||||
|
||||
Check if `n` is shorter than the given array and use `Array.slice()` to slice it accordingly or return an empty array.
|
||||
|
||||
```js
|
||||
const dropRight = (arr, n = 1) => n < arr.length ? arr.slice(0, arr.length - n) : []
|
||||
//dropRight([1,2,3]) -> [1,2]
|
||||
//dropRight([1,2,3], 2) -> [1]
|
||||
//dropRight([1,2,3], 42) -> []
|
||||
```
|
||||
@ -1,12 +1,13 @@
|
||||
### initializeArrayWithRange
|
||||
|
||||
Initializes an array containing the numbers in the specified range.
|
||||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive.
|
||||
|
||||
Use `Array(end-start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
||||
Use `Array((end + 1) - start)` to create an array of the desired length, `Array.map()` to fill with the desired values in a range.
|
||||
You can omit `start` to use a default value of `0`.
|
||||
|
||||
```js
|
||||
const initializeArrayWithRange = (end, start = 0) =>
|
||||
Array.from({ length: end - start }).map((v, i) => i + start);
|
||||
// initializeArrayWithRange(5) -> [0,1,2,3,4]
|
||||
const initializeArrayWithRange = (end, start = 0) =>
|
||||
Array.from({ length: (end + 1) - start }).map((v, i) => i + start);
|
||||
// initializeArrayWithRange(5) -> [0,1,2,3,4,5]
|
||||
// initializeArrayWithRange(7, 3) -> [3,4,5,6,7]
|
||||
```
|
||||
|
||||
@ -7,7 +7,7 @@ Return the number at the midpoint if `length` is odd, otherwise the average of t
|
||||
|
||||
```js
|
||||
const median = arr => {
|
||||
const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b);
|
||||
const mid = Math.floor(arr.length / 2), nums = [...arr].sort((a, b) => a - b);
|
||||
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
|
||||
};
|
||||
// median([5,6,50,1,-5]) -> 5
|
||||
|
||||
@ -5,6 +5,8 @@ Mutates the original array to filter out the values specified.
|
||||
Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
|
||||
Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
|
||||
|
||||
_(For a snippet that does not mutate the original array see [`without`](#without))_
|
||||
|
||||
```js
|
||||
const pull = (arr, ...args) => {
|
||||
let pulled = arr.filter((v, i) => !args.includes(v));
|
||||
|
||||
16
snippets/pullAll.md
Normal file
16
snippets/pullAll.md
Normal file
@ -0,0 +1,16 @@
|
||||
### pullAll
|
||||
|
||||
Mutates the original array to filter out the values specified (accepts an array of values).
|
||||
|
||||
Use `Array.filter()` and `Array.includes()` to pull out the values that are not needed.
|
||||
Use `Array.length = 0` to mutate the passed in array by resetting it's length to zero and `Array.push()` to re-populate it with only the pulled values.
|
||||
|
||||
```js
|
||||
const pullAll = (arr, pullArr) => {
|
||||
let pulled = arr.filter((v, i) => !pullArr.includes(v));
|
||||
arr.length = 0; pulled.forEach(v => arr.push(v));
|
||||
}
|
||||
// let myArray = ['a', 'b', 'c', 'a', 'b', 'c'];
|
||||
// pullAll(myArray, ['a', 'c']);
|
||||
// console.log(myArray) -> [ 'b', 'b' ]
|
||||
```
|
||||
@ -2,10 +2,10 @@
|
||||
|
||||
Reverses a string.
|
||||
|
||||
Use array destructuring and `Array.reverse()` to reverse the order of the characters in the string.
|
||||
Use `split('')` and `Array.reverse()` to reverse the order of the characters in the string.
|
||||
Combine characters to get a string using `join('')`.
|
||||
|
||||
```js
|
||||
const reverseString = str => [...str].reverse().join('');
|
||||
const reverseString = str => str.split('').reverse().join('');
|
||||
// reverseString('foobar') -> 'raboof'
|
||||
```
|
||||
|
||||
13
snippets/select.md
Normal file
13
snippets/select.md
Normal file
@ -0,0 +1,13 @@
|
||||
### select
|
||||
|
||||
Retrieve a property that indicated by the selector from object.
|
||||
|
||||
If property not exists returns `undefined`.
|
||||
|
||||
```js
|
||||
const select = (from, selector) =>
|
||||
selector.split('.').reduce((prev, cur) => prev && prev[cur], from);
|
||||
|
||||
// const obj = {selector: {to: {val: 'val to select'}}};
|
||||
// select(obj, 'selector.to.val'); -> 'val to select'
|
||||
```
|
||||
@ -4,6 +4,8 @@ Filters out the elements of an array, that have one of the specified values.
|
||||
|
||||
Use `Array.filter()` to create an array excluding(using `!Array.includes()`) all given values.
|
||||
|
||||
_(For a snippet that mutates the original array see [`pull`](#pull))_
|
||||
|
||||
```js
|
||||
const without = (arr, ...args) => arr.filter(v => !args.includes(v));
|
||||
// without([2, 1, 2, 3], 1, 2) -> [3]
|
||||
|
||||
Reference in New Issue
Block a user