Add 7 new snippets

This commit is contained in:
Angelos Chalaris
2023-01-01 23:16:49 +02:00
parent 31a1b02cef
commit a4874b6b68
7 changed files with 198 additions and 0 deletions

29
snippets/alphabetical.md Normal file
View File

@ -0,0 +1,29 @@
---
title: Sort array alphabetically
tags: array
author: chalarangelo
cover: blog_images/boutique-home-office-1.jpg
firstSeen: 2023-02-15T05:00:00-04:00
---
Sorts an array of objects alphabetically based on a given property.
- Use `Array.prototype.sort()` to sort the array based on the given property.
- Use `String.prototype.localeCompare()` to compare the values for the given property.
```js
const alphabetical = (arr, getter, order = 'asc') =>
arr.sort(
order === 'desc'
? (a, b) => getter(b).localeCompare(getter(a))
: (a, b) => getter(a).localeCompare(getter(b))
);
```
```js
const people = [ { name: 'John' }, { name: 'Adam' }, { name: 'Mary' } ];
alphabetical(people, g => g.name);
// [ { name: 'Adam' }, { name: 'John' }, { name: 'Mary' } ]
alphabetical(people, g => g.name, 'desc');
// [ { name: 'Mary' }, { name: 'John' }, { name: 'Adam' } ]
```

24
snippets/intersects.md Normal file
View File

@ -0,0 +1,24 @@
---
title: Check if two arrays intersect
tags: array
author: chalarangelo
cover: blog_images/interior-5.jpg
firstSeen: 2023-02-17T05:00:00-04:00
---
Determines if two arrays have a common item.
- Create a `Set` from `b` to get the unique values in `b`.
- Use `Array.prototype.some()` on `a` to check if any of its values are contained in `b`, using `Set.prototype.has()`.
```js
const intersects = (a, b) => {
const s = new Set(b);
return [...new Set(a)].some(x => s.has(x));
};
```
```js
intersects(['a', 'b'], ['b', 'c']); // true
intersects(['a', 'b'], ['c', 'd']); // false
```

27
snippets/listify.md Normal file
View File

@ -0,0 +1,27 @@
---
title: Map an object to an array
tags: object,array
author: chalarangelo
cover: blog_images/metro-arrival.jpg
firstSeen: 2023-02-05T05:00:00-04:00
---
Maps an object to an object array, using the provided mapping function.
- Use `Object.entries()` to get an array of the object's key-value pairs.
- Use `Array.prototype.reduce()` to map the array to an object.
- Use `mapFn` to map the keys and values of the object and `Array.prototype.push()` to add the mapped values to the array.
```js
const listify = (obj, mapFn) =>
Object.entries(obj).reduce((acc, [key, value]) => {
acc.push(mapFn(key, value));
return acc;
}, []);
```
```js
const people = { John: { age: 42 }, Adam: { age: 39 } };
listify(people, (key, value) => ({ name: key, ...value }));
// [ { name: 'John', age: 42 }, { name: 'Adam', age: 39 } ]
```

24
snippets/lowerize.md Normal file
View File

@ -0,0 +1,24 @@
---
title: Lowercase object keys
tags: object
author: chalarangelo
cover: blog_images/building-facade.jpg
firstSeen: 2023-02-12T05:00:00-04:00
---
Converts all the keys of an object to lower case.
- Use `Object.keys()` to get an array of the object's keys.
- Use `Array.prototype.reduce()` to map the array to an object, using `String.prototype.toLowerCase()` to lowercase the keys.
```js
const lowerize = obj =>
Object.keys(obj).reduce((acc, k) => {
acc[k.toLowerCase()] = obj[k];
return acc;
}, {});
```
```js
lowerize({ Name: 'John', Age: 22 }); // { name: 'John', age: 22 }
```

32
snippets/objectify.md Normal file
View File

@ -0,0 +1,32 @@
---
title: Map an array to an object
tags: array,object
author: chalarangelo
cover: blog_images/metro-tunnel.jpg
firstSeen: 2023-02-04T05:00:00-04:00
---
Maps an object array to an object, using the provided mapping functions.
- Use `Array.prototype.reduce()` to map the array to an object.
- Use `mapKey` to map the keys of the object and `mapValue` to map the values.
```js
const objectify = (arr, mapKey, mapValue = i => i) =>
arr.reduce((acc, item) => {
acc[mapKey(item)] = mapValue(item);
return acc;
}, {});
```
```js
const people = [ { name: 'John', age: 42 }, { name: 'Adam', age: 39 } ];
objectify(people, p => p.name.toLowerCase());
// { john: { name: 'John', age: 42 }, adam: { name: 'Adam', age: 39 } }
objectify(
people,
p => p.name.toLowerCase(),
p => p.age
);
// { john: 42, adam: 39 }
```

View File

@ -0,0 +1,38 @@
---
title: Replace or append array value
tags: array
author: chalarangelo
cover: blog_images/boutique-home-office-2.jpg
firstSeen: 2023-02-19T05:00:00-04:00
---
Replaces an item in an array or appends it, if it doesn't exist.
- Use the spread operator (`...`) to create a shallow copy of the array.
- Use `Array.prototype.findIndex()` to find the index of the first element that satisfies the provided comparison function, `compFn`.
- If no such element is found, use `Array.prototype.push()` to append the new value to the array.
- Otherwise, use `Array.prototype.splice()` to replace the value at the found index with the new value.
```js
const replaceOrAppend = (arr, val, compFn) => {
const res = [...arr];
const i = arr.findIndex(v => compFn(v, val));
if (i === -1) res.push(val);
else res.splice(i, 1, val);
return res;
};
```
```js
const people = [ { name: 'John', age: 30 }, { name: 'Jane', age: 28 } ];
const jane = { name: 'Jane', age: 29 };
const jack = { name: 'Jack', age: 28 };
replaceOrAppend(people, jane, (a, b) => a.name === b.name);
// [ { name: 'John', age: 30 }, { name: 'Jane', age: 29 } ]
replaceOrAppend(people, jack, (a, b) => a.name === b.name);
// [
// { name: 'John', age: 30 },
// { name: 'Jane', age: 28 },
// { name: 'Jack', age: 28 }
// ]
```

24
snippets/upperize.md Normal file
View File

@ -0,0 +1,24 @@
---
title: Uppercase object keys
tags: object
author: chalarangelo
cover: blog_images/sofia-tram.jpg
firstSeen: 2023-02-11T05:00:00-04:00
---
Converts all the keys of an object to upper case.
- Use `Object.keys()` to get an array of the object's keys.
- Use `Array.prototype.reduce()` to map the array to an object, using `String.prototype.toUpperCase()` to uppercase the keys.
```js
const upperize = obj =>
Object.keys(obj).reduce((acc, k) => {
acc[k.toUpperCase()] = obj[k];
return acc;
}, {});
```
```js
upperize({ Name: 'John', Age: 22 }); // { NAME: 'John', AGE: 22 }
```