update snippets 47-62
This commit is contained in:
committed by
Agamemnon Zorbas
parent
1c73c70b4a
commit
32fa5a9773
@ -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]
|
||||
```
|
||||
|
||||
@ -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
|
||||
*/
|
||||
```
|
||||
|
||||
@ -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'
|
||||
```
|
||||
|
||||
@ -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)
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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}
|
||||
```
|
||||
|
||||
@ -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"
|
||||
```
|
||||
|
||||
@ -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'}
|
||||
```
|
||||
|
||||
@ -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']}
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -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
|
||||
```
|
||||
|
||||
@ -16,7 +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)'
|
||||
```
|
||||
|
||||
@ -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]
|
||||
```
|
||||
|
||||
@ -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]]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user