Skip to content

Commit

Permalink
Merge pull request phuocng#482 from 1milligram/alphabet
Browse files Browse the repository at this point in the history
New snippet: Generate an array of alphabet characters
  • Loading branch information
phuocng authored Oct 22, 2021
2 parents 27440b4 + 3ee1d0a commit ba1e283
Show file tree
Hide file tree
Showing 29 changed files with 107 additions and 47 deletions.
1 change: 0 additions & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.netlify
.next
snippets
node_modules
out
1 change: 1 addition & 0 deletions public/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
<url><loc><![CDATA[https://1loc.dev/array/find-the-minimum-item-of-an-array-by-given-key]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/find-the-minimum-item-of-an-array]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/flatten-an-array]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/generate-an-array-of-alphabet-characters]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/get-all-arrays-of-consecutive-elements]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/get-all-nth-items-of-an-array]]></loc></url>
<url><loc><![CDATA[https://1loc.dev/array/get-all-subsets-of-an-array]]></loc></url>
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/cast-a-value-as-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const castArray = (value) => (Array.isArray(value) ? value : [value]);
**TypeScript version**

```js
const castArray = <T,>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);
const castArray = <T,_>(value: T | T[]): T[] => (Array.isArray(value) ? value : [value]);
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/check-if-an-array-is-empty.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const isEmpty = (arr) => !Array.isArray(arr) || arr.length === 0;
**TypeScript version**

```js
const isEmpty = <T,>(arr: T[]): boolean => !Array.isArray(arr) || arr.length === 0;
const isEmpty = <T,_>(arr: T[]): boolean => !Array.isArray(arr) || arr.length === 0;
```

**Examples**
Expand Down
12 changes: 6 additions & 6 deletions snippets/array/clone-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,20 @@ const clone = (arr) => arr.concat([]);

```js
// `arr` is an array
const clone = <T,>(arr: T[]): T[] => arr.slice(0);
const clone = <T,_>(arr: T[]): T[] => arr.slice(0);

// Or
const clone = <T,>(arr: T[]): T[] => [...arr];
const clone = <T,_>(arr: T[]): T[] => [...arr];

// Or
const clone = <T,>(arr: T[]): T[] => Array.from(arr);
const clone = <T,_>(arr: T[]): T[] => Array.from(arr);

// Or
const clone = <T,>(arr: T[]): T[] => arr.map((x) => x);
const clone = <T,_>(arr: T[]): T[] => arr.map((x) => x);

// Or
const clone = <T,>(arr: T[]): T[] => JSON.parse(JSON.stringify(arr));
const clone = <T,_>(arr: T[]): T[] => JSON.parse(JSON.stringify(arr));

// Or
const clone = <T,>(arr: T[]): T[] => arr.concat([]);
const clone = <T,_>(arr: T[]): T[] => arr.concat([]);
```
2 changes: 1 addition & 1 deletion snippets/array/compare-two-arrays-regardless-of-order.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
**TypeScript version**

```js
const isEqual = <T,>(a: T[], b: T[]): boolean => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
const isEqual = <T,_>(a: T[], b: T[]): boolean => JSON.stringify(a.sort()) === JSON.stringify(b.sort());
```

**Examples**
Expand Down
4 changes: 2 additions & 2 deletions snippets/array/compare-two-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const isEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i])
**TypeScript version**

```js
const isEqual = <T,>(a: T[], b: T[]): boolean => JSON.stringify(a) === JSON.stringify(b);
const isEqual = <T,_>(a: T[], b: T[]): boolean => JSON.stringify(a) === JSON.stringify(b);

// Or
const isEqual = <T,>(a: T[], b: T[]): boolean => a.length === b.length && a.every((v, i) => v === b[i]);
const isEqual = <T,_>(a: T[], b: T[]): boolean => a.length === b.length && a.every((v, i) => v === b[i]);
```

**Examples**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const countOccurrences = (arr, val) => arr.filter((item) => item === val).length
**TypeScript version**

```js
const countOccurrences = <T,>(arr: T[], val: T): number => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);
const countOccurrences = <T,_>(arr: T[], val: T): number => arr.reduce((a, v) => (v === val ? a + 1 : a), 0);

// Or
const countOccurrences = <T,>(arr: T[], val: T): number => arr.filter((item) => item === val).length;
const countOccurrences = <T,_>(arr: T[], val: T): number => arr.filter((item) => item === val).length;
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/empty-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ arr = [];
**TypeScript version**

```js
const empty = <T,>(arr: T[]) => (arr.length = 0);
const empty = <T,_>(arr: T[]) => (arr.length = 0);

// Or
arr = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const lastIndex = (arr, predicate) => arr.map((item) => predicate(item)).lastInd
**TypeScript version**

```js
const lastIndex = <T,>(arr: T[], predicate: (a: T) => boolean): number => arr.reduce((prev, curr, index) => (predicate(curr) ? index : prev), -1);
const lastIndex = <T,_>(arr: T[], predicate: (a: T) => boolean): number => arr.reduce((prev, curr, index) => (predicate(curr) ? index : prev), -1);

// Or
const lastIndex = <T,>(arr: T[], predicate: (a: T) => boolean): number => arr.map((item) => predicate(item)).lastIndexOf(true);
const lastIndex = <T,_>(arr: T[], predicate: (a: T) => boolean): number => arr.map((item) => predicate(item)).lastIndexOf(true);
```

**Examples**
Expand Down
60 changes: 60 additions & 0 deletions snippets/array/generate-an-array-of-alphabet-characters.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Generate an array of alphabet characters
category: Array
---

**JavaScript version**

```js
const alphabet = 'abcdefghijklmnopqrstuvwxyz'.split('');

// Or
const alphabet = [...'abcdefghijklmnopqrstuvwxyz'];

// Or
const alphabet = Array(26)
.fill(0)
.map((_, i) => String.fromCharCode(i + 97));

// Or
const alphabet = [...Array(26).keys()].map((i) => String.fromCharCode(i + 97));

// Or
const alphabet = [...Array(26)].map((_, i) => (i + 10).toString(36));

// Or
const alphabet = String.fromCharCode(
...' '
.repeat(26)
.split('')
.map((_, i) => i + 97)
).split('');
```

**TypeScript version**

```js
const alphabet: string[] = 'abcdefghijklmnopqrstuvwxyz'.split('');

// Or
const alphabet: string[] = [...'abcdefghijklmnopqrstuvwxyz'];

// Or
const alphabet: string[] = Array(26)
.fill(0)
.map((_, i) => String.fromCharCode(i + 97));

// Or
const alphabet: string[] = [...Array(26).keys()].map((i) => String.fromCharCode(i + 97));

// Or
const alphabet: string[] = [...Array(26)].map((_, i) => (i + 10).toString(36));

// Or
const alphabet: string[] = String.fromCharCode(
...' '
.repeat(26)
.split('')
.map((_, i) => i + 97)
).split('');
```
2 changes: 1 addition & 1 deletion snippets/array/get-all-arrays-of-consecutive-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getConsecutiveArrays = (arr, size) => (size > arr.length ? [] : arr.slice(
**TypeScript version**

```js
const getConsecutiveArrays = <T,>(arr: T[], size: number): T[][] => (size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i)));
const getConsecutiveArrays = <T,_>(arr: T[], size: number): T[][] => (size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i)));
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/get-all-nth-items-of-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getNthItems = (arr, nth) => arr.filter((_, i) => i % nth === nth - 1);
**TypeScript version**

```js
const getNthItems = <T,>(arr: T[], nth: number): T[] => arr.filter((_, i) => i % nth === nth - 1);
const getNthItems = <T,_>(arr: T[], nth: number): T[] => arr.filter((_, i) => i % nth === nth - 1);
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/get-the-intersection-of-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const getIntersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every((
**TypeScript version**

```js
const getIntersection = <T,>(a: T[], ...arr: T[][]): T[] => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)));
const getIntersection = <T,_>(a: T[], ...arr: T[][]): T[] => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v)));
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/get-union-of-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const union = (...arr) => [...new Set(arr.flat())];
**TypeScript version**

```js
const union = <T,>(...arr: T[][]): T[] => [...new Set(arr.flat())];
const union = <T,_>(...arr: T[][]): T[] => [...new Set(arr.flat())];
```

**Example**
Expand Down
8 changes: 4 additions & 4 deletions snippets/array/merge-two-arrays.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ const merge = (a, b) => [...new Set([...a, ...b])];

```js
// Merge but don't remove the duplications
const merge = <T,>(a: T[], b: T[]): T[] => a.concat(b);
const merge = <T,_>(a: T[], b: T[]): T[] => a.concat(b);
// Or
const merge = <T,>(a: T[], b: T[]): T[] => [...a, ...b];
const merge = <T,_>(a: T[], b: T[]): T[] => [...a, ...b];

// Merge and remove the duplications
const merge = <T,>(a: T[], b: T[]): T[] => [...new Set(a.concat(b))];
const merge = <T,_>(a: T[], b: T[]): T[] => [...new Set(a.concat(b))];
// Or
const merge = <T,>(a: T[], b: T[]): T[] => [...new Set([...a, ...b])];
const merge = <T,_>(a: T[], b: T[]): T[] => [...new Set([...a, ...b])];
```
2 changes: 1 addition & 1 deletion snippets/array/partition-an-array-based-on-a-condition.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const partition = (arr, criteria) => arr.reduce((acc, i) => (acc[criteria(i) ? 0
**TypeScript version**

```js
const partition = <T,>(arr: T[], criteria: (a: T) => boolean): T[][] => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
const partition = <T,_>(arr: T[], criteria: (a: T) => boolean): T[][] => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]);
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/remove-duplicate-values-in-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const removeDuplicate = (arr) => arr.filter((i) => arr.indexOf(i) === arr.lastIn
**TypeScript version**

```js
const removeDuplicate = <T,>(arr: T[]): T[] => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i));
const removeDuplicate = <T,_>(arr: T[]): T[] => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i));
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/remove-falsy-values-from-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const removeFalsy = (arr) => arr.filter(Boolean);
**TypeScript version**

```js
const removeFalsy = <T,>(arr: T[]): T[] => arr.filter(Boolean);
const removeFalsy = <T,_>(arr: T[]): T[] => arr.filter(Boolean);
```

**Example**
Expand Down
8 changes: 4 additions & 4 deletions snippets/array/repeat-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ const repeat = (arr, n) => Array.from({ length: arr.length * n }, (_, i) => arr[
**TypeScript version**

```js
const repeat = <T,>(arr: T[], n: number): T[] => [].concat(...Array(n).fill(arr));
const repeat = <T,_>(arr: T[], n: number): T[] => [].concat(...Array(n).fill(arr));

// Or
const repeat = <T,>(arr: T[], n: number): T[] => Array(n).fill(arr).flat();
const repeat = <T,_>(arr: T[], n: number): T[] => Array(n).fill(arr).flat();

// Or
const repeat = <T,>(arr: T[], n: number): T[] =>
const repeat = <T,_>(arr: T[], n: number): T[] =>
Array(arr.length * n)
.fill(0)
.map((_, i) => arr[i % arr.length]);

// Or
const repeat = <T,>(arr: T[], n: number): T[] => Array.from({ length: arr.length * n }, (_, i) => arr[i % arr.length]);
const repeat = <T,_>(arr: T[], n: number): T[] => Array.from({ length: arr.length * n }, (_, i) => arr[i % arr.length]);
```

**Example**
Expand Down
4 changes: 2 additions & 2 deletions snippets/array/shuffle-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ const shuffle = (arr) => arr.sort(() => 0.5 - Math.random());
**TypeScript version**

```js
const shuffle = <T,>(arr: T[]): T[] =>
const shuffle = <T,_>(arr: T[]): T[] =>
arr
.map((a) => ({ sort: Math.random(), value: a }))
.sort((a, b) => a.sort - b.sort)
.map((a) => a.value);

// Or
const shuffle = <T,>(arr: T[]): T[] => arr.sort(() => 0.5 - Math.random());
const shuffle = <T,_>(arr: T[]): T[] => arr.sort(() => 0.5 - Math.random());
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion snippets/array/swap-two-array-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const swapItems = (a, i, j) => (a[i] && a[j] && [...a.slice(0, i), a[j], ...a.sl
**TypeScript version**

```js
const swapItems = <T,>(a: T[], i: number, j: number): T[] => (a[i] && a[j] && [...a.slice(0, i), a[j], ...a.slice(i + 1, j), a[i], ...a.slice(j + 1)]) || a;
const swapItems = <T,_>(a: T[], i: number, j: number): T[] => (a[i] && a[j] && [...a.slice(0, i), a[j], ...a.slice(i + 1, j), a[i], ...a.slice(j + 1)]) || a;
```

**Example**
Expand Down
2 changes: 1 addition & 1 deletion snippets/random/get-a-random-item-from-an-array.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ const randomItem = (arr) => arr[(Math.random() * arr.length) | 0];
**TypeScript version**

```js
const randomItem = <T,>(arr: T[]): T => arr[(Math.random() * arr.length) | 0];
const randomItem = <T,_>(arr: T[]): T => arr[(Math.random() * arr.length) | 0];
```
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const hasDuplicateValues = (arr) => new Set(arr).size !== arr.length;
**TypeScript version**

```js
const hasDuplicateValues = <T,>(arr: T[]): boolean => new Set(arr).size !== arr.length;
const hasDuplicateValues = <T,_>(arr: T[]): boolean => new Set(arr).size !== arr.length;
```

**Examples**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const isEqual = (arr, value) => !arr.some((item) => item !== value);
**TypeScript version**

```js
const isEqual = <T,>(arr: T[], value: T): boolean => arr.every((item) => item === value);
const isEqual = <T,_>(arr: T[], value: T): boolean => arr.every((item) => item === value);

// Or
const isEqual = <T,>(arr: T[], value: T): boolean => !arr.some((item) => item !== value);
const isEqual = <T,_>(arr: T[], value: T): boolean => !arr.some((item) => item !== value);
```

**Examples**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const areEqual = (arr) => new Set(arr).size === 1;
**TypeScript version**

```js
const areEqual = <T,>(arr: T[]): boolean => arr.length > 0 && arr.every((item) => item === arr[0]);
const areEqual = <T,_>(arr: T[]): boolean => arr.length > 0 && arr.every((item) => item === arr[0]);

// Or
const areEqual = <T,>(arr: T[]): boolean => new Set(arr).size === 1;
const areEqual = <T,_>(arr: T[]): boolean => new Set(arr).size === 1;
```

**Examples**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const contains = (arr, criteria) => arr.filter(criteria).length > 0;
**TypeScript version**

```js
const contains = <T,>(arr: T[], criteria: (a: T) => boolean): boolean => arr.some((v) => criteria(v));
const contains = <T,_>(arr: T[], criteria: (a: T) => boolean): boolean => arr.some((v) => criteria(v));

// Or
const contains = <T,>(arr: T[], criteria: (a: T) => boolean): boolean => arr.some(criteria);
const contains = <T,_>(arr: T[], criteria: (a: T) => boolean): boolean => arr.some(criteria);

// Or
const contains = <T,>(arr: T[], criteria: (a: T) => boolean): boolean => arr.filter(criteria).length > 0;
const contains = <T,_>(arr: T[], criteria: (a: T) => boolean): boolean => arr.filter(criteria).length > 0;
```

**Examples**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ const isSubset = (a, b) => b.join('|').includes(a.join('|'));
**TypeScript version**

```js
const isSubset = <T,>(a: T[], b: T[]): boolean => new Set(b).size === new Set(b.concat(a)).size;
const isSubset = <T,_>(a: T[], b: T[]): boolean => new Set(b).size === new Set(b.concat(a)).size;

// Or
const isSubset = <T,>(a: T[], b: T[]): boolean => b.join('|').includes(a.join('|'));
const isSubset = <T,_>(a: T[], b: T[]): boolean => b.join('|').includes(a.join('|'));
```

**Examples**
Expand Down
2 changes: 1 addition & 1 deletion styles/blocks/_header.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
.block-header__cta {
align-items: center;
background-color: var(--mgd-color-primary);
border-radius: 9999px;
border-radius: 0.25rem;
color: #fff;
display: flex;
font-weight: 700;
Expand Down

0 comments on commit ba1e283

Please sign in to comment.