diff --git a/snippets/callOrReturn.md b/snippets/callOrReturn.md new file mode 100644 index 000000000..5a48f3655 --- /dev/null +++ b/snippets/callOrReturn.md @@ -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 +``` diff --git a/snippets/commonKeys.md b/snippets/commonKeys.md new file mode 100644 index 000000000..99e555ff8 --- /dev/null +++ b/snippets/commonKeys.md @@ -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'] +``` diff --git a/snippets/compactJoin.md b/snippets/compactJoin.md new file mode 100644 index 000000000..984f08d5c --- /dev/null +++ b/snippets/compactJoin.md @@ -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' +``` diff --git a/snippets/flags.md b/snippets/flags.md new file mode 100644 index 000000000..fb2f891bd --- /dev/null +++ b/snippets/flags.md @@ -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 } +``` diff --git a/snippets/toggleElement.md b/snippets/toggleElement.md new file mode 100644 index 000000000..2f9a7d033 --- /dev/null +++ b/snippets/toggleElement.md @@ -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] +``` diff --git a/snippets/unwind.md b/snippets/unwind.md new file mode 100644 index 000000000..448c01d08 --- /dev/null +++ b/snippets/unwind.md @@ -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 }] +```