From 014a77224c6d9fc0bb8d05724a9db7c106e025ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20Feje=C5=A1?= Date: Mon, 25 Dec 2017 14:05:36 +0100 Subject: [PATCH] update snippets 16-31 --- snippets/cleanObj.md | 10 ++++++---- snippets/coalesce.md | 7 +++++-- snippets/coalesceFactory.md | 7 +++++-- snippets/collatz.md | 7 +++++-- snippets/collectInto.md | 9 +++++---- snippets/compact.md | 5 ++++- snippets/compose.md | 5 +++-- snippets/countOccurrences.md | 5 ++++- snippets/countVowels.md | 7 +++++-- snippets/currentURL.md | 5 ++++- snippets/curry.md | 7 +++++-- snippets/deepFlatten.md | 5 ++++- snippets/detectDeviceType.md | 9 ++++++--- snippets/difference.md | 5 ++++- snippets/differenceWith.md | 7 +++++-- 15 files changed, 70 insertions(+), 30 deletions(-) diff --git a/snippets/cleanObj.md b/snippets/cleanObj.md index 3ce8d2cdd..57ec325e0 100644 --- a/snippets/cleanObj.md +++ b/snippets/cleanObj.md @@ -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}} ``` diff --git a/snippets/coalesce.md b/snippets/coalesce.md index 1002df4d2..c112c77d4 100644 --- a/snippets/coalesce.md +++ b/snippets/coalesce.md @@ -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") // "" ``` diff --git a/snippets/coalesceFactory.md b/snippets/coalesceFactory.md index 4b9a78924..39bb98714 100644 --- a/snippets/coalesceFactory.md +++ b/snippets/coalesceFactory.md @@ -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" ``` diff --git a/snippets/collatz.md b/snippets/collatz.md index 8b8903886..fc05b2a5d 100644 --- a/snippets/collatz.md +++ b/snippets/collatz.md @@ -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 ``` diff --git a/snippets/collectInto.md b/snippets/collectInto.md index 69c036764..141a13fa2 100644 --- a/snippets/collectInto.md +++ b/snippets/collectInto.md @@ -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) -*/ -``` +``` diff --git a/snippets/compact.md b/snippets/compact.md index d09160afe..2a9528772 100644 --- a/snippets/compact.md +++ b/snippets/compact.md @@ -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 ] ``` diff --git a/snippets/compose.md b/snippets/compose.md index f6b1ce2ea..dfd40f814 100644 --- a/snippets/compose.md +++ b/snippets/compose.md @@ -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 -*/ ``` diff --git a/snippets/countOccurrences.md b/snippets/countOccurrences.md index f55de0a62..2c1463b02 100644 --- a/snippets/countOccurrences.md +++ b/snippets/countOccurrences.md @@ -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 ``` diff --git a/snippets/countVowels.md b/snippets/countVowels.md index c91f09442..47c683eb8 100644 --- a/snippets/countVowels.md +++ b/snippets/countVowels.md @@ -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 ``` diff --git a/snippets/currentURL.md b/snippets/currentURL.md index a6482a689..7e8965d92 100644 --- a/snippets/currentURL.md +++ b/snippets/currentURL.md @@ -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' ``` diff --git a/snippets/curry.md b/snippets/curry.md index c88e3418a..f1806d395 100644 --- a/snippets/curry.md +++ b/snippets/curry.md @@ -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 ``` diff --git a/snippets/deepFlatten.md b/snippets/deepFlatten.md index c6f29fb9b..cea888992 100644 --- a/snippets/deepFlatten.md +++ b/snippets/deepFlatten.md @@ -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] ``` diff --git a/snippets/detectDeviceType.md b/snippets/detectDeviceType.md index 291e148c5..b262e194d 100644 --- a/snippets/detectDeviceType.md +++ b/snippets/detectDeviceType.md @@ -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" ``` diff --git a/snippets/difference.md b/snippets/difference.md index e78a73340..dce891e60 100644 --- a/snippets/difference.md +++ b/snippets/difference.md @@ -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] ``` diff --git a/snippets/differenceWith.md b/snippets/differenceWith.md index 02f686f4b..f5ad345d6 100644 --- a/snippets/differenceWith.md +++ b/snippets/differenceWith.md @@ -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] ```