Format snippets

This commit is contained in:
Isabelle Viktoria Maciohsek
2020-11-03 22:11:18 +02:00
parent 15dcd4d184
commit 05e3f6dc07
13 changed files with 42 additions and 26 deletions

View File

@ -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'}];
```

View File

@ -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

View File

@ -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.
```

View File

@ -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)
```

View File

@ -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}
```

View File

@ -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

View File

@ -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

View File

@ -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;
};

View File

@ -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
```

View File

@ -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'}
```

View File

@ -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}
```

View File

@ -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);
```

View File

@ -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