Format snippets
This commit is contained in:
@ -28,6 +28,8 @@ const CSVToJSON = (data, delimiter = ',') => {
|
||||
```
|
||||
|
||||
```js
|
||||
CSVToJSON('col1,col2\na,b\nc,d'); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
||||
CSVToJSON('col1;col2\na;b\nc;d', ';'); // [{'col1': 'a', 'col2': 'b'}, {'col1': 'c', 'col2': 'd'}];
|
||||
CSVToJSON('col1,col2\na,b\nc,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.
|
||||
|
||||
```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
|
||||
|
||||
@ -28,5 +28,6 @@ var view = {
|
||||
}
|
||||
};
|
||||
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.
|
||||
```
|
||||
|
||||
@ -32,6 +32,6 @@ const colorize = (...args) => ({
|
||||
```js
|
||||
console.log(colorize('foo').red); // 'foo' (red letters)
|
||||
console.log(colorize('foo', 'bar').bgBlue); // 'foo bar' (blue background)
|
||||
console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite);
|
||||
console.log(colorize(colorize('foo').yellow, colorize('foo').green).bgWhite);
|
||||
// 'foo bar' (first word in yellow letters, second word in green letters, white background for both)
|
||||
```
|
||||
|
||||
@ -19,5 +19,6 @@ const countBy = (arr, fn) =>
|
||||
```js
|
||||
countBy([6.1, 4.2, 6.3], Math.floor); // {4: 1, 6: 2}
|
||||
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
|
||||
elementContains(document.querySelector('head'), document.querySelector('title'));
|
||||
elementContains(
|
||||
document.querySelector('head'),
|
||||
document.querySelector('title')
|
||||
);
|
||||
// true
|
||||
elementContains(document.querySelector('body'), document.querySelector('body'));
|
||||
// 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.
|
||||
|
||||
```js
|
||||
const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();
|
||||
const isSameDate = (dateA, dateB) =>
|
||||
dateA.toISOString() === dateB.toISOString();
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
@ -17,7 +17,10 @@ const luhnCheck = num => {
|
||||
.reverse()
|
||||
.map(x => parseInt(x));
|
||||
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;
|
||||
return sum % 10 === 0;
|
||||
};
|
||||
|
||||
@ -16,8 +16,8 @@ const matches = (obj, source) =>
|
||||
```
|
||||
|
||||
```js
|
||||
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true });
|
||||
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true });
|
||||
// true
|
||||
matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true });
|
||||
matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true });
|
||||
// false
|
||||
```
|
||||
|
||||
@ -18,5 +18,6 @@ const queryStringToObject = url =>
|
||||
```
|
||||
|
||||
```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], (a, b) => b - a); // 3
|
||||
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
|
||||
); // {name: 'Lucy', age: 9}
|
||||
```
|
||||
|
||||
@ -22,16 +22,18 @@ const renderElement = ({ type, props = {} }, container) => {
|
||||
const isAttribute = p => !isListener(p) && p !== 'children';
|
||||
|
||||
Object.keys(props).forEach(p => {
|
||||
if(isAttribute(p)) element[p] = props[p];
|
||||
if(!isTextElement && isListener(p))
|
||||
if (isAttribute(p)) element[p] = props[p];
|
||||
if (!isTextElement && isListener(p))
|
||||
element.addEventListener(p.toLowerCase().slice(2), props[p]);
|
||||
});
|
||||
|
||||
if(!isTextElement && props.children && props.children.length)
|
||||
props.children.forEach(childElement => renderElement(childElement, element));
|
||||
if (!isTextElement && props.children && props.children.length)
|
||||
props.children.forEach(childElement =>
|
||||
renderElement(childElement, element)
|
||||
);
|
||||
|
||||
container.appendChild(element);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
@ -41,14 +43,9 @@ const myElement = {
|
||||
type: 'button',
|
||||
className: 'btn',
|
||||
onClick: () => alert('Clicked'),
|
||||
children: [
|
||||
{ props: { nodeValue: 'Click me' } }
|
||||
]
|
||||
children: [{ props: { nodeValue: 'Click me' } }]
|
||||
}
|
||||
};
|
||||
|
||||
renderElement(
|
||||
myElement,
|
||||
document.body
|
||||
);
|
||||
renderElement(myElement, document.body);
|
||||
```
|
||||
|
||||
@ -10,7 +10,9 @@ Calculates the sum of an array, after mapping each element to a value using the
|
||||
|
||||
```js
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user