lodash remove duplicated

This commit is contained in:
sabareesh
2017-12-14 16:55:06 +05:30
18 changed files with 183 additions and 74 deletions

View File

@ -5,7 +5,7 @@ Pass `location.search` as the argument to apply to the current `url`.
```js
const getUrlParameters = url =>
url.match(/([^?=&]+)(=([^&]*))?/g).reduce(
url.match(/([^?=&]+)(=([^&]*))/g).reduce(
(a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
);
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}

View File

@ -0,0 +1,8 @@
### Array concatenation
Use `Array.concat()` to concatenate and array with any additional arrays and/or values, specified in `args`.
```js
const ArrayConcat = (arr, ...args) => arr.concat(...args);
// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]]
```

View File

@ -1,4 +1,4 @@
### Array difference (complement)
### Array difference
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.

View File

@ -1,4 +1,4 @@
### Array intersection (Common values between two arrays)
### Array intersection
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.

View File

@ -1,11 +1,11 @@
### Chunk array
Use `Array.apply()` to create a new array, that fits the number of chunks that will be produced.
Use `Array.map()` to map each element of the new array to a chunk the length of `size`.
Use `Array.from()` to create a new array, that fits the number of chunks that will be produced.
Use `Array.slice()` to map each element of the new array to a chunk the length of `size`.
If the original array can't be split evenly, the final chunk will contain the remaining elements.
```js
const chunk = (arr, size) =>
Array.apply(null, {length: Math.ceil(arr.length / size)}).map((v, i) => arr.slice(i * size, i * size + size));
Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size));
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5]
```

View File

@ -1,8 +0,0 @@
### Concat
Creates a new array concatenating array with any additional arrays and/or values `args` using `Array.concat()`.
```js
const ArrayConcat = (arr, ...args) => arr.concat(...args);
// ArrayConcat([1], [1, 2, 3, [4]]) -> [1, 2, 3, [4]]
```

14
snippets/group-by.md Normal file
View File

@ -0,0 +1,14 @@
### Group by
Use `Array.map()` to map the values of an array to a function or property name.
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
```js
const groupBy = (arr, func) =>
(typeof func === 'function' ? arr.map(func) : arr.map(val => val[func]))
.reduce((acc, val, i) => {
acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc;
}, {});
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
```

View File

@ -1,6 +1,6 @@
### Head of list
Return `arr[0]`.
Use `arr[0]` to return the first element of the passed array.
```js
const head = arr => arr[0];

View File

@ -1,6 +1,6 @@
### Initial of list
Return `arr.slice(0,-1)`.
Use `arr.slice(0,-1)`to return all but the last element of the array.
```js
const initial = arr => arr.slice(0, -1);

View File

@ -1,6 +1,6 @@
### Last of list
Return `arr.slice(-1)[0]`.
Use `arr.slice(-1)[0]` to get the last element of the given array.
```js
const last = arr => arr.slice(-1)[0];

View File

@ -1,8 +0,0 @@
### Randomize order of array
Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
```js
const randomizeOrder = arr => arr.sort((a, b) => Math.random() >= 0.5 ? -1 : 1);
// randomizeOrder([1,2,3]) -> [1,3,2]
```

View File

@ -1,12 +0,0 @@
### Shuffle array values
Create an array of random values by using `Array.map()` and `Math.random()`.
Use `Array.sort()` to sort the elements of the original array based on the random values.
```js
const shuffle = arr => {
let r = arr.map(Math.random);
return arr.sort((a, b) => r[a] - r[b]);
};
// shuffle([1,2,3]) -> [2, 1, 3]
```

View File

@ -0,0 +1,8 @@
### Shuffle array
Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator.
```js
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
// shuffle([1,2,3]) -> [2,3,1]
```

14
snippets/sleep.md Normal file
View File

@ -0,0 +1,14 @@
### Sleep
Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`.
```js
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
/*
async function sleepyWork() {
console.log('I\'m going to sleep for 1 second.');
await sleep(1000);
console.log('I woke up after 1 second.');
}
*/
```

View File

@ -0,0 +1,17 @@
### Standard deviation
Use `Array.reduce()` to calculate the mean, variance and the sum of the variance of the values, the variance of the values, then
determine the standard deviation.
You can omit the second argument to get the sample standard deviation or set it to `true` to get the population standard deviation.
```js
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) / (arr.length - (usePopulation ? 0 : 1))
);
}
// standardDeviation([10,2,38,23,38,23,21]) -> 13.284434142114991 (sample)
// standardDeviation([10,2,38,23,38,23,21], true) -> 12.29899614287479 (population)
```

10
snippets/take.md Normal file
View File

@ -0,0 +1,10 @@
### Take
Use `Array.slice()` to create a slice of the array with `n` elements taken from the beginning.
```js
const take = (arr, n = 1) => arr.slice(0, n);
// take([1, 2, 3], 5) -> [1, 2, 3]
// take([1, 2, 3], 0) -> []
```

View File

@ -0,0 +1,10 @@
### Validate email
Use a regular experssion to check if the email is valid.
Returns `true` if email is valid, `false` if not.
```js
const validateEmail = str =>
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
// validateEmail(mymail@gmail.com) -> true
```