diff --git a/snippets/flattenDepth.md b/snippets/flattenDepth.md index c07b837ff..6034b9135 100644 --- a/snippets/flattenDepth.md +++ b/snippets/flattenDepth.md @@ -11,5 +11,8 @@ Omit the second element, `depth` to flatten only to a depth of `1` (single flatt const flattenDepth = (arr, depth = 1) => depth != 1 ? arr.reduce((a, v) => a.concat(Array.isArray(v) ? flattenDepth(v, depth - 1) : v), []) : arr.reduce((a, v) => a.concat(v), []); -// flattenDepth([1,[2],[[[3],4],5]], 2) -> [1,2,[3],4,5] +``` + +```js +flatten([1,[2],3,4]) -> [1,2,3,4] ``` diff --git a/snippets/flip.md b/snippets/flip.md index 34a7d1f3b..a6ac82d15 100644 --- a/snippets/flip.md +++ b/snippets/flip.md @@ -6,7 +6,9 @@ Return a closure that takes variadic inputs, and splices the last argument to ma ```js const flip = fn => (...args) => fn(args.pop(), ...args) -/* +``` + +```js let a = {name: 'John Smith'} let b = {} const mergeFrom = flip(Object.assign) @@ -14,5 +16,4 @@ let mergePerson = mergeFrom.bind(null, a) mergePerson(b) // == b b = {} Object.assign(b, a) // == b -*/ ``` diff --git a/snippets/fromCamelCase.md b/snippets/fromCamelCase.md index 3d480dc85..14c21bc4c 100644 --- a/snippets/fromCamelCase.md +++ b/snippets/fromCamelCase.md @@ -9,7 +9,10 @@ Omit the second argument to use a default separator of `_`. const fromCamelCase = (str, separator = '_') => str.replace(/([a-z\d])([A-Z])/g, '$1' + separator + '$2') .replace(/([A-Z]+)([A-Z][a-z\d]+)/g, '$1' + separator + '$2').toLowerCase(); -// fromCamelCase('someDatabaseFieldName', ' ') -> 'some database field name' -// fromCamelCase('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized' -// fromCamelCase('someJavascriptProperty', '_') -> 'some_javascript_property' +``` + +```js +fromCamelCase('someDatabaseFieldName', ' ') -> 'some database field name' +fromCamelCase('someLabelThatNeedsToBeCamelized', '-') -> 'some-label-that-needs-to-be-camelized' +fromCamelCase('someJavascriptProperty', '_') -> 'some_javascript_property' ``` diff --git a/snippets/functionName.md b/snippets/functionName.md index 54603d39c..1ad25772a 100644 --- a/snippets/functionName.md +++ b/snippets/functionName.md @@ -6,5 +6,8 @@ Use `console.debug()` and the `name` property of the passed method to log the me ```js const functionName = fn => (console.debug(fn.name), fn); -// functionName(Math.max) -> max (logged in debug channel of console) +``` + +```js +functionName(Math.max) -> max (logged in debug channel of console) ``` diff --git a/snippets/gcd.md b/snippets/gcd.md index 34fb21833..8affada12 100644 --- a/snippets/gcd.md +++ b/snippets/gcd.md @@ -8,5 +8,8 @@ Otherwise, return the GCD of `y` and the remainder of the division `x/y`. ```js const gcd = (x, y) => !y ? x : gcd(y, x % y); -// gcd (8, 36) -> 4 +``` + +```js +gcd (8, 36) -> 4 ``` diff --git a/snippets/getDaysDiffBetweenDates.md b/snippets/getDaysDiffBetweenDates.md index 3b63440c1..6c73366ac 100644 --- a/snippets/getDaysDiffBetweenDates.md +++ b/snippets/getDaysDiffBetweenDates.md @@ -6,5 +6,8 @@ Calculate the difference (in days) between two `Date` objects. ```js const getDaysDiffBetweenDates = (dateInitial, dateFinal) => (dateFinal - dateInitial) / (1000 * 3600 * 24); -// getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9 +``` + +```js +getDaysDiffBetweenDates(new Date("2017-12-13"), new Date("2017-12-22")) -> 9 ``` diff --git a/snippets/getScrollPosition.md b/snippets/getScrollPosition.md index b51ec3241..2708210a1 100644 --- a/snippets/getScrollPosition.md +++ b/snippets/getScrollPosition.md @@ -9,5 +9,8 @@ You can omit `el` to use a default value of `window`. const getScrollPosition = (el = window) => ({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft, y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop}); -// getScrollPosition() -> {x: 0, y: 200} +``` + +```js +getScrollPosition() -> {x: 0, y: 200} ``` diff --git a/snippets/getType.md b/snippets/getType.md index 53f660420..f0b9be8d5 100644 --- a/snippets/getType.md +++ b/snippets/getType.md @@ -7,5 +7,8 @@ Returns lowercased constructor name of value, "undefined" or "null" if value is ```js const getType = v => v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase(); -// getType(new Set([1,2,3])) -> "set" +``` + +```js +getType(new Set([1,2,3])) -> "set" ``` diff --git a/snippets/getURLParameters.md b/snippets/getURLParameters.md index 77dddd532..5f4690bfc 100644 --- a/snippets/getURLParameters.md +++ b/snippets/getURLParameters.md @@ -10,5 +10,8 @@ const getURLParameters = url => 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'} +``` + +```js +getURLParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'} ``` diff --git a/snippets/groupBy.md b/snippets/groupBy.md index d83795fdc..4b301e2b8 100644 --- a/snippets/groupBy.md +++ b/snippets/groupBy.md @@ -9,6 +9,9 @@ Use `Array.reduce()` to create an object, where the keys are produced from the m const groupBy = (arr, func) => arr.map(typeof func === 'function' ? func : val => val[func]) .reduce((acc, val, i) => { acc[val] = (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']} +``` + +```js +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']} ``` diff --git a/snippets/hammingDistance.md b/snippets/hammingDistance.md index 97c08d2aa..e0f22dba7 100644 --- a/snippets/hammingDistance.md +++ b/snippets/hammingDistance.md @@ -8,5 +8,8 @@ Count and return the number of `1`s in the string, using `match(/1/g)`. ```js const hammingDistance = (num1, num2) => ((num1 ^ num2).toString(2).match(/1/g) || '').length; -// hammingDistance(2,3) -> 1 +``` + +```js +hammingDistance(2,3) -> 1 ``` diff --git a/snippets/head.md b/snippets/head.md index ece926a50..30b8951a6 100644 --- a/snippets/head.md +++ b/snippets/head.md @@ -6,5 +6,8 @@ Use `arr[0]` to return the first element of the passed array. ```js const head = arr => arr[0]; -// head([1,2,3]) -> 1 +``` + +```js +head([1,2,3]) -> 1 ``` diff --git a/snippets/hexToRGB.md b/snippets/hexToRGB.md index 66c95fd3a..d3fab8a8d 100644 --- a/snippets/hexToRGB.md +++ b/snippets/hexToRGB.md @@ -16,8 +16,10 @@ const hexToRGB = hex => { + ((h & (alpha ? 0x0000ff00 : 0x0000ff)) >>> (alpha ? 8 : 0)) + (alpha ? `, ${(h & 0x000000ff)}` : '') + ')'; }; -// hexToRGB('#27ae60ff') -> 'rgba(39, 174, 96, 255)' -// hexToRGB('27ae60') -> 'rgb(39, 174, 96)' -// hexToRGB('#fff') -> 'rgb(255, 255, 255)' - +``` + +```js +hexToRGB('#27ae60ff') -> 'rgba(39, 174, 96, 255)' +hexToRGB('27ae60') -> 'rgb(39, 174, 96)' +hexToRGB('#fff') -> 'rgb(255, 255, 255)' ``` diff --git a/snippets/initial.md b/snippets/initial.md index 7b1c5c68c..87827c437 100644 --- a/snippets/initial.md +++ b/snippets/initial.md @@ -6,5 +6,8 @@ Use `arr.slice(0,-1)` to return all but the last element of the array. ```js const initial = arr => arr.slice(0, -1); -// initial([1,2,3]) -> [1,2] +``` + +```js +initial([1,2,3]) -> [1,2] ``` diff --git a/snippets/initialize2DArray.md b/snippets/initialize2DArray.md index 0cb1df126..ec7bf7681 100644 --- a/snippets/initialize2DArray.md +++ b/snippets/initialize2DArray.md @@ -6,5 +6,8 @@ Use `Array.map()` to generate h rows where each is a new array of size w initial ```js const initialize2DArray = (w, h, val = null) => Array(h).fill().map(() => Array(w).fill(val)); -// initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]] +``` + +```js +initializeArrayWithRange(2, 2, 0) -> [[0,0], [0,0]] ```