Format snippets
This commit is contained in:
@ -28,6 +28,8 @@ const CSVToJSON = (data, delimiter = ',') => {
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
CSVToJSON('col1,col2\na,b\nc,d'); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
CSVToJSON('col1,col2\na,b\nc,d');
|
||||||
CSVToJSON('col1;col2\na;b\nc;d', ';'); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
// [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
||||||
|
CSVToJSON('col1;col2\na;b\nc;d', ';');
|
||||||
|
// [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
||||||
```
|
```
|
||||||
|
|||||||
@ -9,7 +9,8 @@ Converts the values of RGB components to a hexadecimal color code.
|
|||||||
- Use `String.prototype.padStart(6, '0')` to get a 6-digit hexadecimal value.
|
- Use `String.prototype.padStart(6, '0')` to get a 6-digit hexadecimal value.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const RGBToHex = (r, g, b) => ((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
const RGBToHex = (r, g, b) =>
|
||||||
|
((r << 16) + (g << 8) + b).toString(16).padStart(6, '0');
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -28,5 +28,6 @@ var view = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
bindAll(view, 'click');
|
bindAll(view, 'click');
|
||||||
document.body.addEventListener('click', view.click); // Log 'clicked docs' when clicked.
|
document.body.addEventListener('click', view.click);
|
||||||
|
// Log 'clicked docs' when clicked.
|
||||||
```
|
```
|
||||||
|
|||||||
@ -19,5 +19,6 @@ const countBy = (arr, fn) =>
|
|||||||
```js
|
```js
|
||||||
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}
|
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}
|
||||||
countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}
|
countBy(['one', 'two', 'three'], 'length'); // {3: 2, 5: 1}
|
||||||
countBy([{ count: 5 }, { count: 10 }, { count: 5 }], x => x.count) // {5: 2, 10: 1}
|
countBy([{ count: 5 }, { count: 10 }, { count: 5 }], x => x.count)
|
||||||
|
// {5: 2, 10: 1}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -14,7 +14,10 @@ const elementContains = (parent, child) =>
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
elementContains(document.querySelector('head'), document.querySelector('title'));
|
elementContains(
|
||||||
|
document.querySelector('head'),
|
||||||
|
document.querySelector('title')
|
||||||
|
);
|
||||||
// true
|
// true
|
||||||
elementContains(document.querySelector('body'), document.querySelector('body'));
|
elementContains(document.querySelector('body'), document.querySelector('body'));
|
||||||
// false
|
// false
|
||||||
|
|||||||
@ -8,7 +8,8 @@ Checks if a date is the same as another date.
|
|||||||
- Use `Date.prototype.toISOString()` and strict equality checking (`===`) to check if the first date is the same as the second one.
|
- Use `Date.prototype.toISOString()` and strict equality checking (`===`) to check if the first date is the same as the second one.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
|
const isSameDate = (dateA, dateB) =>
|
||||||
|
dateA.toISOString() === dateB.toISOString();
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
@ -17,7 +17,10 @@ const luhnCheck = num => {
|
|||||||
.reverse()
|
.reverse()
|
||||||
.map(x => parseInt(x));
|
.map(x => parseInt(x));
|
||||||
let lastDigit = arr.splice(0, 1)[0];
|
let lastDigit = arr.splice(0, 1)[0];
|
||||||
let sum = arr.reduce((acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9), 0);
|
let sum = arr.reduce(
|
||||||
|
(acc, val, i) => (i % 2 !== 0 ? acc + val : acc + ((val * 2) % 9) || 9),
|
||||||
|
0
|
||||||
|
);
|
||||||
sum += lastDigit;
|
sum += lastDigit;
|
||||||
return sum % 10 === 0;
|
return sum % 10 === 0;
|
||||||
};
|
};
|
||||||
|
|||||||
@ -18,5 +18,6 @@ const queryStringToObject = url =>
|
|||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
queryStringToObject('https://google.com?page=1&count=10'); // {page: '1', count: '10'}
|
queryStringToObject('https://google.com?page=1&count=10');
|
||||||
|
// {page: '1', count: '10'}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -17,7 +17,11 @@ const reduceWhich = (arr, comparator = (a, b) => a - b) =>
|
|||||||
reduceWhich([1, 3, 2]); // 1
|
reduceWhich([1, 3, 2]); // 1
|
||||||
reduceWhich([1, 3, 2], (a, b) => b - a); // 3
|
reduceWhich([1, 3, 2], (a, b) => b - a); // 3
|
||||||
reduceWhich(
|
reduceWhich(
|
||||||
[{ name: 'Tom', age: 12 }, { name: 'Jack', age: 18 }, { name: 'Lucy', age: 9 }],
|
[
|
||||||
|
{ name: 'Tom', age: 12 },
|
||||||
|
{ name: 'Jack', age: 18 },
|
||||||
|
{ name: 'Lucy', age: 9 }
|
||||||
|
],
|
||||||
(a, b) => a.age - b.age
|
(a, b) => a.age - b.age
|
||||||
); // {name: 'Lucy', age: 9}
|
); // {name: 'Lucy', age: 9}
|
||||||
```
|
```
|
||||||
|
|||||||
@ -28,10 +28,12 @@ const renderElement = ({ type, props = {} }, container) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!isTextElement && props.children && props.children.length)
|
if (!isTextElement && props.children && props.children.length)
|
||||||
props.children.forEach(childElement => renderElement(childElement, element));
|
props.children.forEach(childElement =>
|
||||||
|
renderElement(childElement, element)
|
||||||
|
);
|
||||||
|
|
||||||
container.appendChild(element);
|
container.appendChild(element);
|
||||||
}
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@ -41,14 +43,9 @@ const myElement = {
|
|||||||
type: 'button',
|
type: 'button',
|
||||||
className: 'btn',
|
className: 'btn',
|
||||||
onClick: () => alert('Clicked'),
|
onClick: () => alert('Clicked'),
|
||||||
children: [
|
children: [{ props: { nodeValue: 'Click me' } }]
|
||||||
{ props: { nodeValue: 'Click me' } }
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
renderElement(
|
renderElement(myElement, document.body);
|
||||||
myElement,
|
|
||||||
document.body
|
|
||||||
);
|
|
||||||
```
|
```
|
||||||
|
|||||||
@ -10,7 +10,9 @@ Calculates the sum of an array, after mapping each element to a value using the
|
|||||||
|
|
||||||
```js
|
```js
|
||||||
const sumBy = (arr, fn) =>
|
const sumBy = (arr, fn) =>
|
||||||
arr.map(typeof fn === 'function' ? fn : val => val[fn]).reduce((acc, val) => acc + val, 0);
|
arr
|
||||||
|
.map(typeof fn === 'function' ? fn : val => val[fn])
|
||||||
|
.reduce((acc, val) => acc + val, 0);
|
||||||
```
|
```
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
|||||||
Reference in New Issue
Block a user