Add 6 new snippets
This commit is contained in:
21
snippets/callOrReturn.md
Normal file
21
snippets/callOrReturn.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
title: Call or return
|
||||||
|
tags: function
|
||||||
|
expertise: beginner
|
||||||
|
firstSeen: 2022-04-04T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Calls the argument if it's a function, otherwise returns it.
|
||||||
|
|
||||||
|
- Use the `typeof` operator to check if the given argument is a function.
|
||||||
|
- If it is, use the spread operator (`...`) to call it with the rest of the given arguments. Otherwise, return it.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const callOrReturn = (fn, ...args) =>
|
||||||
|
typeof fn === 'function' ? fn(...args) : fn;
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
callOrReturn(x => x + 1, 1); // 2
|
||||||
|
callOrReturn(1, 1); // 1
|
||||||
|
```
|
||||||
21
snippets/commonKeys.md
Normal file
21
snippets/commonKeys.md
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
---
|
||||||
|
title: Common keys
|
||||||
|
tags: object
|
||||||
|
expertise: intermediate
|
||||||
|
firstSeen: 2022-04-23T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Finds the common keys between two objects.
|
||||||
|
|
||||||
|
- Use `Object.keys()` to get the keys of the first object.
|
||||||
|
- Use `Object.prototype.hasOwnProperty()` to check if the second object has a key that's in the first object.
|
||||||
|
- Use `Array.prototype.filter()` to filter out keys that aren't in both objects.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const commonKeys = (obj1, obj2) =>
|
||||||
|
Object.keys(obj1).filter(key => obj2.hasOwnProperty(key));
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
commonKeys({ a: 1, b: 2 }, { a: 2, c: 1 }); // ['a']
|
||||||
|
```
|
||||||
19
snippets/compactJoin.md
Normal file
19
snippets/compactJoin.md
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
---
|
||||||
|
title: Compact and join array
|
||||||
|
tags: array
|
||||||
|
expertise: intermediate
|
||||||
|
firstSeen: 2022-04-08T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Removes falsy values from an array and combines the remaining values into a string.
|
||||||
|
|
||||||
|
- Use `Array.prototype.filter()` to filter out falsy values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||||
|
- Use `Array.prototype.join()` to join the remaining values into a string.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const compactJoin = (arr, delim = ',') => arr.filter(Boolean).join(delim);
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
compactJoin(['a', '', 'b', 'c']); // 'a,b,c'
|
||||||
|
```
|
||||||
18
snippets/flags.md
Normal file
18
snippets/flags.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
---
|
||||||
|
title: Array to flags object
|
||||||
|
tags: array,object
|
||||||
|
expertise: intermediate
|
||||||
|
firstSeen: 2022-04-12T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Converts an array of strings into an object mapping to true.
|
||||||
|
|
||||||
|
- Use `Array.prototype.reduce()` to convert the array into an object, where each array value is used as a key whose value is set to `true`.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const flags = arr => arr.reduce((acc, str) => ({...acc, [str]: true }), {});
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
flags(['red', 'green']); // { red: true, green: true }
|
||||||
|
```
|
||||||
23
snippets/toggleElement.md
Normal file
23
snippets/toggleElement.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
title: Toggle element in array
|
||||||
|
tags: array
|
||||||
|
expertise: beginner
|
||||||
|
firstSeen: 2022-04-15T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Removes an element from an array if it's included in the array, or pushes it to the array if it isn't.
|
||||||
|
|
||||||
|
- Use `Array.prototype.includes()` to check if the given element is in the array.
|
||||||
|
- Use `Array.prototype.filter()` to remove the element if it's in the array.
|
||||||
|
- Use the spread operator (`...`) to push the element if it's not in the array.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const toggleElement = (arr, val) =>
|
||||||
|
arr.includes(val) ? arr.filter(el => el !== val) : [...arr, val];
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
toggleElement([1, 2, 3], 2); // [1, 3]
|
||||||
|
toggleElement([1, 2, 3], 4); // [1, 2, 3, 4]
|
||||||
|
```
|
||||||
23
snippets/unwind.md
Normal file
23
snippets/unwind.md
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
---
|
||||||
|
title: Unwind object
|
||||||
|
tags: object
|
||||||
|
expertise: intermediate
|
||||||
|
firstSeen: 2022-04-18T05:00:00-04:00
|
||||||
|
---
|
||||||
|
|
||||||
|
Produces an array of objects from an object and one of its array-valued properties.
|
||||||
|
|
||||||
|
- Use object destructuring to exclude the key-value pair for the specified `key` from the object.
|
||||||
|
- Use `Array.prototype.map()` for the values of the given `key` to create an array of objects.
|
||||||
|
- Each object contains the original object's values, except for `key` which is mapped to its individual values.
|
||||||
|
|
||||||
|
```js
|
||||||
|
const unwind = (key, obj) => {
|
||||||
|
const { [key]: _, ...rest } = obj;
|
||||||
|
return obj[key].map(val => ({ ...rest, [key]: val }));
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
unwind('b', { a: true, b: [1, 2] }); // [{ a: true, b: 1 }, { a: true, b: 2 }]
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user