update snippets 16-31

This commit is contained in:
Stefan Feješ
2017-12-25 14:05:36 +01:00
committed by Agamemnon Zorbas
parent b2cffa6858
commit 014a77224c
15 changed files with 70 additions and 30 deletions

View File

@ -16,8 +16,10 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => {
 });
return obj;
};
/*
const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
cleanObj(testObj, ["a"],"children") // { a: 1, children : { a: 1}}
*/
```
```js
const testObj = {a: 1, b: 2, children: {a: 1, b: 2}}
cleanObj(testObj, ["a"],"children")
console.log(testObj) // { a: 1, children : { a: 1}}
```

View File

@ -5,6 +5,9 @@ Returns the first non-null/undefined argument.
Use `Array.find()` to return the first non `null`/`undefined` argument.
```js
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_));
// coalesce(null,undefined,"",NaN, "Waldo") -> ""
const coalesce = (...args) => args.find(_ => ![undefined, null].includes(_))
```
```js
coalesce(null,undefined,"",NaN, "Waldo") // ""
```

View File

@ -6,6 +6,9 @@ Use `Array.find()` to return the first argument that returns `true` from the pro
```js
const coalesceFactory = valid => (...args) => args.find(valid);
// const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
// customCoalesce(undefined, null, NaN, "", "Waldo") //-> "Waldo"
```
```js
const customCoalesce = coalesceFactory(_ => ![null, undefined, "", NaN].includes(_))
customCoalesce(undefined, null, NaN, "", "Waldo") // "Waldo"
```

View File

@ -6,6 +6,9 @@ If `n` is even, return `n/2`. Otherwise, return `3n+1`.
```js
const collatz = n => (n % 2 == 0) ? (n / 2) : (3 * n + 1);
// collatz(8) --> 4
// collatz(5) --> 16
```
```js
collatz(8) -> 4
collatz(5) -> 16
```

View File

@ -5,12 +5,13 @@ Changes a function that accepts an array into a variadic function.
Given a function, return a closure that collects all inputs into an array-accepting function.
```js
const collectInto = fn => (...args) => fn(args);
/*
const collectInto = fn => ( ...args ) => fn( args );
```
```js
const Pall = collectInto( Promise.all.bind(Promise) )
let p1 = Promise.resolve(1)
let p2 = Promise.resolve(2)
let p3 = new Promise((resolve) => setTimeout(resolve,2000,3))
Pall(p1, p2, p3).then(console.log)
*/
```
```

View File

@ -6,5 +6,8 @@ Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `u
```js
const compact = arr => arr.filter(Boolean);
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
```
```js
compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
```

View File

@ -7,10 +7,11 @@ The last (rightmost) function can accept one or more arguments; the remaining fu
```js
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
/*
```
```js
const add5 = x => x + 5
const multiply = (x, y) => x * y
const multiplyAndAdd5 = compose(add5, multiply)
multiplyAndAdd5(5, 2) -> 15
*/
```

View File

@ -6,5 +6,8 @@ Use `Array.reduce()` to increment a counter each time you encounter the specific
```js
const countOccurrences = (arr, value) => arr.reduce((a, v) => v === value ? a + 1 : a + 0, 0);
// countOccurrences([1,1,2,1,2,3], 1) -> 3
```
```js
countOccurrences([1,1,2,1,2,3], 1) -> 3
```

View File

@ -6,6 +6,9 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a `s
```js
const countVowels = str => (str.match(/[aeiou]/ig) || []).length;
// countVowels('foobar') -> 3
// countVowels('gym') -> 0
```
```js
countVowels('foobar') -> 3
countVowels('gym') -> 0
```

View File

@ -6,5 +6,8 @@ Use `window.location.href` to get current URL.
```js
const currentURL = () => window.location.href;
// currentUrl() -> 'https://google.com'
```
```js
currentUrl() -> 'https://google.com'
```

View File

@ -12,6 +12,9 @@ const curry = (fn, arity = fn.length, ...args) =>
arity <= args.length
? fn(...args)
: curry.bind(null, fn, arity, ...args);
// curry(Math.pow)(2)(10) -> 1024
// curry(Math.min, 3)(10)(50)(2) -> 2
```
```js
curry(Math.pow)(2)(10) -> 1024
curry(Math.min, 3)(10)(50)(2) -> 2
```

View File

@ -8,5 +8,8 @@ Recursively flatten each element that is an array.
```js
const deepFlatten = arr => [].concat(...arr.map(v => Array.isArray(v) ? deepFlatten(v) : v));
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
```
```js
deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
```

View File

@ -5,7 +5,10 @@ Detects wether the website is being opened in a mobile device or a desktop/lapto
Use a regular expression to test the `navigator.userAgent` property to figure out if the device is a mobile device or a desktop/laptop.
```js
const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? 'Mobile' : 'Desktop';
// detectDeviceType() -> "Mobile"
// detectDeviceType() -> "Desktop"
const detectDeviceType = () => /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ? "Mobile" : "Desktop";
```
```js
detectDeviceType() // "Mobile"
detectDeviceType() // "Desktop"
```

View File

@ -6,5 +6,8 @@ Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values no
```js
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); };
// difference([1,2,3], [1,2,4]) -> [3]
```
```js
difference([1,2,3], [1,2,4]) -> [3]
```

View File

@ -5,6 +5,9 @@ Filters out all values from an array for which the comparator function does not
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]
const differenceWith = (arr, val, comp) => arr.filter(a => !val.find(b => comp(a, b)))
```
```js
differenceWith([1, 1.2, 1.5, 3], [1.9, 3], (a,b) => Math.round(a) == Math.round(b)) // [1, 1.2]
```