diff --git a/.prettierignore b/.prettierignore index aec230a9..8be6691c 100644 --- a/.prettierignore +++ b/.prettierignore @@ -1,5 +1,4 @@ .netlify .next -snippets node_modules out diff --git a/public/sitemap.xml b/public/sitemap.xml index d9d79cb8..67a3b3ac 100644 --- a/public/sitemap.xml +++ b/public/sitemap.xml @@ -35,6 +35,7 @@ + diff --git a/snippets/array/cast-a-value-as-an-array.md b/snippets/array/cast-a-value-as-an-array.md index 4d4cb501..236844a3 100644 --- a/snippets/array/cast-a-value-as-an-array.md +++ b/snippets/array/cast-a-value-as-an-array.md @@ -12,7 +12,7 @@ const castArray = (value) => (Array.isArray(value) ? value : [value]); **TypeScript version** ```js -const castArray = (value: T | T[]): T[] => (Array.isArray(value) ? value : [value]); +const castArray = (value: T | T[]): T[] => (Array.isArray(value) ? value : [value]); ``` **Examples** diff --git a/snippets/array/check-if-an-array-is-empty.md b/snippets/array/check-if-an-array-is-empty.md index abc8e236..cef0b610 100644 --- a/snippets/array/check-if-an-array-is-empty.md +++ b/snippets/array/check-if-an-array-is-empty.md @@ -13,7 +13,7 @@ const isEmpty = (arr) => !Array.isArray(arr) || arr.length === 0; **TypeScript version** ```js -const isEmpty = (arr: T[]): boolean => !Array.isArray(arr) || arr.length === 0; +const isEmpty = (arr: T[]): boolean => !Array.isArray(arr) || arr.length === 0; ``` **Examples** diff --git a/snippets/array/clone-an-array.md b/snippets/array/clone-an-array.md index 46c81c59..f4c38897 100644 --- a/snippets/array/clone-an-array.md +++ b/snippets/array/clone-an-array.md @@ -29,20 +29,20 @@ const clone = (arr) => arr.concat([]); ```js // `arr` is an array -const clone = (arr: T[]): T[] => arr.slice(0); +const clone = (arr: T[]): T[] => arr.slice(0); // Or -const clone = (arr: T[]): T[] => [...arr]; +const clone = (arr: T[]): T[] => [...arr]; // Or -const clone = (arr: T[]): T[] => Array.from(arr); +const clone = (arr: T[]): T[] => Array.from(arr); // Or -const clone = (arr: T[]): T[] => arr.map((x) => x); +const clone = (arr: T[]): T[] => arr.map((x) => x); // Or -const clone = (arr: T[]): T[] => JSON.parse(JSON.stringify(arr)); +const clone = (arr: T[]): T[] => JSON.parse(JSON.stringify(arr)); // Or -const clone = (arr: T[]): T[] => arr.concat([]); +const clone = (arr: T[]): T[] => arr.concat([]); ``` diff --git a/snippets/array/compare-two-arrays-regardless-of-order.md b/snippets/array/compare-two-arrays-regardless-of-order.md index beb8699f..428a532a 100644 --- a/snippets/array/compare-two-arrays-regardless-of-order.md +++ b/snippets/array/compare-two-arrays-regardless-of-order.md @@ -13,7 +13,7 @@ const isEqual = (a, b) => JSON.stringify(a.sort()) === JSON.stringify(b.sort()); **TypeScript version** ```js -const isEqual = (a: T[], b: T[]): boolean => JSON.stringify(a.sort()) === JSON.stringify(b.sort()); +const isEqual = (a: T[], b: T[]): boolean => JSON.stringify(a.sort()) === JSON.stringify(b.sort()); ``` **Examples** diff --git a/snippets/array/compare-two-arrays.md b/snippets/array/compare-two-arrays.md index a53c3dcb..e05d0209 100644 --- a/snippets/array/compare-two-arrays.md +++ b/snippets/array/compare-two-arrays.md @@ -16,10 +16,10 @@ const isEqual = (a, b) => a.length === b.length && a.every((v, i) => v === b[i]) **TypeScript version** ```js -const isEqual = (a: T[], b: T[]): boolean => JSON.stringify(a) === JSON.stringify(b); +const isEqual = (a: T[], b: T[]): boolean => JSON.stringify(a) === JSON.stringify(b); // Or -const isEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((v, i) => v === b[i]); +const isEqual = (a: T[], b: T[]): boolean => a.length === b.length && a.every((v, i) => v === b[i]); ``` **Examples** diff --git a/snippets/array/count-the-occurrences-of-a-value-in-an-array.md b/snippets/array/count-the-occurrences-of-a-value-in-an-array.md index ab76df02..cad4d9f0 100644 --- a/snippets/array/count-the-occurrences-of-a-value-in-an-array.md +++ b/snippets/array/count-the-occurrences-of-a-value-in-an-array.md @@ -15,10 +15,10 @@ const countOccurrences = (arr, val) => arr.filter((item) => item === val).length **TypeScript version** ```js -const countOccurrences = (arr: T[], val: T): number => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); +const countOccurrences = (arr: T[], val: T): number => arr.reduce((a, v) => (v === val ? a + 1 : a), 0); // Or -const countOccurrences = (arr: T[], val: T): number => arr.filter((item) => item === val).length; +const countOccurrences = (arr: T[], val: T): number => arr.filter((item) => item === val).length; ``` **Examples** diff --git a/snippets/array/empty-an-array.md b/snippets/array/empty-an-array.md index 2bf7ae81..cfab8fc8 100644 --- a/snippets/array/empty-an-array.md +++ b/snippets/array/empty-an-array.md @@ -15,7 +15,7 @@ arr = []; **TypeScript version** ```js -const empty = (arr: T[]) => (arr.length = 0); +const empty = (arr: T[]) => (arr.length = 0); // Or arr = []; diff --git a/snippets/array/find-the-index-of-the-last-matching-item-of-an-array.md b/snippets/array/find-the-index-of-the-last-matching-item-of-an-array.md index 1a6f58e8..91cfae55 100644 --- a/snippets/array/find-the-index-of-the-last-matching-item-of-an-array.md +++ b/snippets/array/find-the-index-of-the-last-matching-item-of-an-array.md @@ -15,10 +15,10 @@ const lastIndex = (arr, predicate) => arr.map((item) => predicate(item)).lastInd **TypeScript version** ```js -const lastIndex = (arr: T[], predicate: (a: T) => boolean): number => arr.reduce((prev, curr, index) => (predicate(curr) ? index : prev), -1); +const lastIndex = (arr: T[], predicate: (a: T) => boolean): number => arr.reduce((prev, curr, index) => (predicate(curr) ? index : prev), -1); // Or -const lastIndex = (arr: T[], predicate: (a: T) => boolean): number => arr.map((item) => predicate(item)).lastIndexOf(true); +const lastIndex = (arr: T[], predicate: (a: T) => boolean): number => arr.map((item) => predicate(item)).lastIndexOf(true); ``` **Examples** diff --git a/snippets/array/generate-an-array-of-alphabet-characters.md b/snippets/array/generate-an-array-of-alphabet-characters.md new file mode 100644 index 00000000..d68f2751 --- /dev/null +++ b/snippets/array/generate-an-array-of-alphabet-characters.md @@ -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(''); +``` diff --git a/snippets/array/get-all-arrays-of-consecutive-elements.md b/snippets/array/get-all-arrays-of-consecutive-elements.md index f89a42b3..bc77d2be 100644 --- a/snippets/array/get-all-arrays-of-consecutive-elements.md +++ b/snippets/array/get-all-arrays-of-consecutive-elements.md @@ -12,7 +12,7 @@ const getConsecutiveArrays = (arr, size) => (size > arr.length ? [] : arr.slice( **TypeScript version** ```js -const getConsecutiveArrays = (arr: T[], size: number): T[][] => (size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i))); +const getConsecutiveArrays = (arr: T[], size: number): T[][] => (size > arr.length ? [] : arr.slice(size - 1).map((_, i) => arr.slice(i, size + i))); ``` **Examples** diff --git a/snippets/array/get-all-nth-items-of-an-array.md b/snippets/array/get-all-nth-items-of-an-array.md index b31f50bd..5d48f8c0 100644 --- a/snippets/array/get-all-nth-items-of-an-array.md +++ b/snippets/array/get-all-nth-items-of-an-array.md @@ -12,7 +12,7 @@ const getNthItems = (arr, nth) => arr.filter((_, i) => i % nth === nth - 1); **TypeScript version** ```js -const getNthItems = (arr: T[], nth: number): T[] => arr.filter((_, i) => i % nth === nth - 1); +const getNthItems = (arr: T[], nth: number): T[] => arr.filter((_, i) => i % nth === nth - 1); ``` **Examples** diff --git a/snippets/array/get-the-intersection-of-arrays.md b/snippets/array/get-the-intersection-of-arrays.md index 13a19100..c2cdd887 100644 --- a/snippets/array/get-the-intersection-of-arrays.md +++ b/snippets/array/get-the-intersection-of-arrays.md @@ -12,7 +12,7 @@ const getIntersection = (a, ...arr) => [...new Set(a)].filter((v) => arr.every(( **TypeScript version** ```js -const getIntersection = (a: T[], ...arr: T[][]): T[] => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v))); +const getIntersection = (a: T[], ...arr: T[][]): T[] => [...new Set(a)].filter((v) => arr.every((b) => b.includes(v))); ``` **Examples** diff --git a/snippets/array/get-union-of-arrays.md b/snippets/array/get-union-of-arrays.md index c596a6ca..b475a49a 100644 --- a/snippets/array/get-union-of-arrays.md +++ b/snippets/array/get-union-of-arrays.md @@ -12,7 +12,7 @@ const union = (...arr) => [...new Set(arr.flat())]; **TypeScript version** ```js -const union = (...arr: T[][]): T[] => [...new Set(arr.flat())]; +const union = (...arr: T[][]): T[] => [...new Set(arr.flat())]; ``` **Example** diff --git a/snippets/array/merge-two-arrays.md b/snippets/array/merge-two-arrays.md index 58bebe8c..95656145 100644 --- a/snippets/array/merge-two-arrays.md +++ b/snippets/array/merge-two-arrays.md @@ -21,12 +21,12 @@ const merge = (a, b) => [...new Set([...a, ...b])]; ```js // Merge but don't remove the duplications -const merge = (a: T[], b: T[]): T[] => a.concat(b); +const merge = (a: T[], b: T[]): T[] => a.concat(b); // Or -const merge = (a: T[], b: T[]): T[] => [...a, ...b]; +const merge = (a: T[], b: T[]): T[] => [...a, ...b]; // Merge and remove the duplications -const merge = (a: T[], b: T[]): T[] => [...new Set(a.concat(b))]; +const merge = (a: T[], b: T[]): T[] => [...new Set(a.concat(b))]; // Or -const merge = (a: T[], b: T[]): T[] => [...new Set([...a, ...b])]; +const merge = (a: T[], b: T[]): T[] => [...new Set([...a, ...b])]; ``` diff --git a/snippets/array/partition-an-array-based-on-a-condition.md b/snippets/array/partition-an-array-based-on-a-condition.md index 7177e215..1d7451c9 100644 --- a/snippets/array/partition-an-array-based-on-a-condition.md +++ b/snippets/array/partition-an-array-based-on-a-condition.md @@ -12,7 +12,7 @@ const partition = (arr, criteria) => arr.reduce((acc, i) => (acc[criteria(i) ? 0 **TypeScript version** ```js -const partition = (arr: T[], criteria: (a: T) => boolean): T[][] => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]); +const partition = (arr: T[], criteria: (a: T) => boolean): T[][] => arr.reduce((acc, i) => (acc[criteria(i) ? 0 : 1].push(i), acc), [[], []]); ``` **Example** diff --git a/snippets/array/remove-duplicate-values-in-an-array.md b/snippets/array/remove-duplicate-values-in-an-array.md index 12bfa884..4edb5070 100644 --- a/snippets/array/remove-duplicate-values-in-an-array.md +++ b/snippets/array/remove-duplicate-values-in-an-array.md @@ -12,7 +12,7 @@ const removeDuplicate = (arr) => arr.filter((i) => arr.indexOf(i) === arr.lastIn **TypeScript version** ```js -const removeDuplicate = (arr: T[]): T[] => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i)); +const removeDuplicate = (arr: T[]): T[] => arr.filter((i) => arr.indexOf(i) === arr.lastIndexOf(i)); ``` **Example** diff --git a/snippets/array/remove-falsy-values-from-array.md b/snippets/array/remove-falsy-values-from-array.md index 7383ad44..a984fdbe 100644 --- a/snippets/array/remove-falsy-values-from-array.md +++ b/snippets/array/remove-falsy-values-from-array.md @@ -12,7 +12,7 @@ const removeFalsy = (arr) => arr.filter(Boolean); **TypeScript version** ```js -const removeFalsy = (arr: T[]): T[] => arr.filter(Boolean); +const removeFalsy = (arr: T[]): T[] => arr.filter(Boolean); ``` **Example** diff --git a/snippets/array/repeat-an-array.md b/snippets/array/repeat-an-array.md index 7cfe90fe..98966dfe 100644 --- a/snippets/array/repeat-an-array.md +++ b/snippets/array/repeat-an-array.md @@ -25,19 +25,19 @@ const repeat = (arr, n) => Array.from({ length: arr.length * n }, (_, i) => arr[ **TypeScript version** ```js -const repeat = (arr: T[], n: number): T[] => [].concat(...Array(n).fill(arr)); +const repeat = (arr: T[], n: number): T[] => [].concat(...Array(n).fill(arr)); // Or -const repeat = (arr: T[], n: number): T[] => Array(n).fill(arr).flat(); +const repeat = (arr: T[], n: number): T[] => Array(n).fill(arr).flat(); // Or -const repeat = (arr: T[], n: number): T[] => +const repeat = (arr: T[], n: number): T[] => Array(arr.length * n) .fill(0) .map((_, i) => arr[i % arr.length]); // Or -const repeat = (arr: T[], n: number): T[] => Array.from({ length: arr.length * n }, (_, i) => arr[i % arr.length]); +const repeat = (arr: T[], n: number): T[] => Array.from({ length: arr.length * n }, (_, i) => arr[i % arr.length]); ``` **Example** diff --git a/snippets/array/shuffle-an-array.md b/snippets/array/shuffle-an-array.md index af04a21c..054a9780 100644 --- a/snippets/array/shuffle-an-array.md +++ b/snippets/array/shuffle-an-array.md @@ -19,14 +19,14 @@ const shuffle = (arr) => arr.sort(() => 0.5 - Math.random()); **TypeScript version** ```js -const shuffle = (arr: T[]): T[] => +const shuffle = (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 = (arr: T[]): T[] => arr.sort(() => 0.5 - Math.random()); +const shuffle = (arr: T[]): T[] => arr.sort(() => 0.5 - Math.random()); ``` **Example** diff --git a/snippets/array/swap-two-array-items.md b/snippets/array/swap-two-array-items.md index 21908825..5bc37161 100644 --- a/snippets/array/swap-two-array-items.md +++ b/snippets/array/swap-two-array-items.md @@ -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 = (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 = (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** diff --git a/snippets/random/get-a-random-item-from-an-array.md b/snippets/random/get-a-random-item-from-an-array.md index ffa3dc42..71e5fbd7 100644 --- a/snippets/random/get-a-random-item-from-an-array.md +++ b/snippets/random/get-a-random-item-from-an-array.md @@ -12,5 +12,5 @@ const randomItem = (arr) => arr[(Math.random() * arr.length) | 0]; **TypeScript version** ```js -const randomItem = (arr: T[]): T => arr[(Math.random() * arr.length) | 0]; +const randomItem = (arr: T[]): T => arr[(Math.random() * arr.length) | 0]; ``` diff --git a/snippets/validator/check-if-a-flat-array-has-duplicate-values.md b/snippets/validator/check-if-a-flat-array-has-duplicate-values.md index 31942484..0101db79 100644 --- a/snippets/validator/check-if-a-flat-array-has-duplicate-values.md +++ b/snippets/validator/check-if-a-flat-array-has-duplicate-values.md @@ -12,7 +12,7 @@ const hasDuplicateValues = (arr) => new Set(arr).size !== arr.length; **TypeScript version** ```js -const hasDuplicateValues = (arr: T[]): boolean => new Set(arr).size !== arr.length; +const hasDuplicateValues = (arr: T[]): boolean => new Set(arr).size !== arr.length; ``` **Examples** diff --git a/snippets/validator/check-if-all-array-elements-are-equal-to-a-given-value.md b/snippets/validator/check-if-all-array-elements-are-equal-to-a-given-value.md index 29044071..e052fe82 100644 --- a/snippets/validator/check-if-all-array-elements-are-equal-to-a-given-value.md +++ b/snippets/validator/check-if-all-array-elements-are-equal-to-a-given-value.md @@ -16,10 +16,10 @@ const isEqual = (arr, value) => !arr.some((item) => item !== value); **TypeScript version** ```js -const isEqual = (arr: T[], value: T): boolean => arr.every((item) => item === value); +const isEqual = (arr: T[], value: T): boolean => arr.every((item) => item === value); // Or -const isEqual = (arr: T[], value: T): boolean => !arr.some((item) => item !== value); +const isEqual = (arr: T[], value: T): boolean => !arr.some((item) => item !== value); ``` **Examples** diff --git a/snippets/validator/check-if-all-items-in-an-array-are-equal.md b/snippets/validator/check-if-all-items-in-an-array-are-equal.md index 48b1679e..3de7541e 100644 --- a/snippets/validator/check-if-all-items-in-an-array-are-equal.md +++ b/snippets/validator/check-if-all-items-in-an-array-are-equal.md @@ -15,10 +15,10 @@ const areEqual = (arr) => new Set(arr).size === 1; **TypeScript version** ```js -const areEqual = (arr: T[]): boolean => arr.length > 0 && arr.every((item) => item === arr[0]); +const areEqual = (arr: T[]): boolean => arr.length > 0 && arr.every((item) => item === arr[0]); // Or -const areEqual = (arr: T[]): boolean => new Set(arr).size === 1; +const areEqual = (arr: T[]): boolean => new Set(arr).size === 1; ``` **Examples** diff --git a/snippets/validator/check-if-an-array-contains-a-value-matching-some-criterias.md b/snippets/validator/check-if-an-array-contains-a-value-matching-some-criterias.md index d37ada21..2035d156 100644 --- a/snippets/validator/check-if-an-array-contains-a-value-matching-some-criterias.md +++ b/snippets/validator/check-if-an-array-contains-a-value-matching-some-criterias.md @@ -18,13 +18,13 @@ const contains = (arr, criteria) => arr.filter(criteria).length > 0; **TypeScript version** ```js -const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.some((v) => criteria(v)); +const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.some((v) => criteria(v)); // Or -const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.some(criteria); +const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.some(criteria); // Or -const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.filter(criteria).length > 0; +const contains = (arr: T[], criteria: (a: T) => boolean): boolean => arr.filter(criteria).length > 0; ``` **Examples** diff --git a/snippets/validator/check-if-an-array-is-subset-of-other-array.md b/snippets/validator/check-if-an-array-is-subset-of-other-array.md index b68d5ca8..42ea89ca 100644 --- a/snippets/validator/check-if-an-array-is-subset-of-other-array.md +++ b/snippets/validator/check-if-an-array-is-subset-of-other-array.md @@ -16,10 +16,10 @@ const isSubset = (a, b) => b.join('|').includes(a.join('|')); **TypeScript version** ```js -const isSubset = (a: T[], b: T[]): boolean => new Set(b).size === new Set(b.concat(a)).size; +const isSubset = (a: T[], b: T[]): boolean => new Set(b).size === new Set(b.concat(a)).size; // Or -const isSubset = (a: T[], b: T[]): boolean => b.join('|').includes(a.join('|')); +const isSubset = (a: T[], b: T[]): boolean => b.join('|').includes(a.join('|')); ``` **Examples** diff --git a/styles/blocks/_header.scss b/styles/blocks/_header.scss index 2a9c3653..67c67f93 100644 --- a/styles/blocks/_header.scss +++ b/styles/blocks/_header.scss @@ -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;