Travis build: 1702
This commit is contained in:
143
README.md
143
README.md
@ -141,6 +141,7 @@ average(1, 2, 3);
|
||||
* [`none`](#none)
|
||||
* [`nthElement`](#nthelement)
|
||||
* [`partition`](#partition)
|
||||
* [`permutations`](#permutations)
|
||||
* [`pull`](#pull)
|
||||
* [`pullAtIndex`](#pullatindex)
|
||||
* [`pullAtValue`](#pullatvalue)
|
||||
@ -363,7 +364,6 @@ average(1, 2, 3);
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
|
||||
* [`anagrams`](#anagrams)
|
||||
* [`byteSize`](#bytesize)
|
||||
* [`capitalize`](#capitalize)
|
||||
* [`capitalizeEveryWord`](#capitalizeeveryword)
|
||||
@ -372,6 +372,7 @@ average(1, 2, 3);
|
||||
* [`escapeRegExp`](#escaperegexp)
|
||||
* [`fromCamelCase`](#fromcamelcase)
|
||||
* [`isAbsoluteURL`](#isabsoluteurl)
|
||||
* [`isAnagram`](#isanagram)
|
||||
* [`isLowerCase`](#islowercase)
|
||||
* [`isUpperCase`](#isuppercase)
|
||||
* [`mask`](#mask)
|
||||
@ -381,6 +382,7 @@ average(1, 2, 3);
|
||||
* [`reverseString`](#reversestring)
|
||||
* [`sortCharactersInString`](#sortcharactersinstring)
|
||||
* [`splitLines`](#splitlines)
|
||||
* [`stringPermutations`](#stringpermutations)
|
||||
* [`stripHTMLTags`](#striphtmltags)
|
||||
* [`toCamelCase`](#tocamelcase)
|
||||
* [`toKebabCase`](#tokebabcase)
|
||||
@ -1851,6 +1853,42 @@ partition(users, o => o.active); // [[{ 'user': 'fred', 'age': 40, 'active':
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### permutations
|
||||
|
||||
⚠️ **WARNING**: This function's execution time increases exponentially with each array element. Anything more than 8 to 10 entries will cause your browser to hang as it tries to solve all the different combinations.
|
||||
|
||||
Generates all permutations of an array's elements (contains duplicates).
|
||||
|
||||
Use recursion.
|
||||
For each element in the given array, create all the partial permutations for the rest of its elements.
|
||||
Use `Array.map()` to combine the element with each partial permutation, then `Array.reduce()` to combine all permutations in one array.
|
||||
Base cases are for array `length` equal to `2` or `1`.
|
||||
|
||||
```js
|
||||
const permutations = arr => {
|
||||
if (arr.length <= 2) return arr.length === 2 ? [arr, [arr[1], arr[0]]] : arr;
|
||||
return arr.reduce(
|
||||
(acc, item, i) =>
|
||||
acc.concat(
|
||||
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [item, ...val])
|
||||
),
|
||||
[]
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
permutations([1, 33, 5]); // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### pull
|
||||
|
||||
Mutates the original array to filter out the values specified.
|
||||
@ -6549,42 +6587,6 @@ unflattenObject({ 'a.b.c': 1, d: 1 }); // { a: { b: { c: 1 } }, d: 1 }
|
||||
---
|
||||
## 📜 String
|
||||
|
||||
### anagrams
|
||||
|
||||
⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
|
||||
|
||||
Generates all anagrams of a string (contains duplicates).
|
||||
|
||||
Use recursion.
|
||||
For each letter in the given string, create all the partial anagrams for the rest of its letters.
|
||||
Use `Array.map()` to combine the letter with each partial anagram, then `Array.reduce()` to combine all anagrams in one array.
|
||||
Base cases are for string `length` equal to `2` or `1`.
|
||||
|
||||
```js
|
||||
const anagrams = str => {
|
||||
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
|
||||
return str
|
||||
.split('')
|
||||
.reduce(
|
||||
(acc, letter, i) =>
|
||||
acc.concat(anagrams(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
|
||||
[]
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
anagrams('abc'); // ['abc','acb','bac','bca','cab','cba']
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### byteSize
|
||||
|
||||
Returns the length of a string in bytes.
|
||||
@ -6788,6 +6790,37 @@ isAbsoluteURL('/foo/bar'); // false
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### isAnagram
|
||||
|
||||
Checks if a string is an anagram of another string (case-insensitive, ignores spaces, punctuation and special characters).
|
||||
|
||||
Use `String.toLowerCase()`, `String.replace()` with an appropriate regular expression to remove unnecessary characters, `String.split('')`, `Array.sort()` and `Array.join('')` on both strings to normalize them, then check if their normalized forms are equal.
|
||||
|
||||
```js
|
||||
const isAnagram = (str1, str2) => {
|
||||
const normalize = str =>
|
||||
str
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]/gi, '')
|
||||
.split('')
|
||||
.sort()
|
||||
.join('');
|
||||
return normalize(str1) === normalize(str2);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
isAnagram('iceman', 'cinema'); // true
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### isLowerCase
|
||||
|
||||
Checks if a string is lower case.
|
||||
@ -7023,6 +7056,42 @@ splitLines('This\nis a\nmultiline\nstring.\n'); // ['This', 'is a', 'multiline',
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### stringPermutations
|
||||
|
||||
⚠️ **WARNING**: This function's execution time increases exponentially with each character. Anything more than 8 to 10 characters will cause your browser to hang as it tries to solve all the different combinations.
|
||||
|
||||
Generates all permutations of a string (contains duplicates).
|
||||
|
||||
Use recursion.
|
||||
For each letter in the given string, create all the partial permutations for the rest of its letters.
|
||||
Use `Array.map()` to combine the letter with each partial permutation, then `Array.reduce()` to combine all permutations in one array.
|
||||
Base cases are for string `length` equal to `2` or `1`.
|
||||
|
||||
```js
|
||||
const stringPermutations = str => {
|
||||
if (str.length <= 2) return str.length === 2 ? [str, str[1] + str[0]] : [str];
|
||||
return str
|
||||
.split('')
|
||||
.reduce(
|
||||
(acc, letter, i) =>
|
||||
acc.concat(stringPermutations(str.slice(0, i) + str.slice(i + 1)).map(val => letter + val)),
|
||||
[]
|
||||
);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
stringPermutations('abc'); // ['abc','acb','bac','bca','cab','cba']
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### stripHTMLTags
|
||||
|
||||
Removes HTML/XML tags from string.
|
||||
|
||||
File diff suppressed because one or more lines are too long
@ -6,7 +6,13 @@ Use `String.toLowerCase()`, `String.replace()` with an appropriate regular expre
|
||||
|
||||
```js
|
||||
const isAnagram = (str1, str2) => {
|
||||
const normalize = str => str.toLowerCase().replace(/[^a-z0-9]/gi, '').split('').sort().join('');
|
||||
const normalize = str =>
|
||||
str
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9]/gi, '')
|
||||
.split('')
|
||||
.sort()
|
||||
.join('');
|
||||
return normalize(str1) === normalize(str2);
|
||||
};
|
||||
```
|
||||
|
||||
@ -15,10 +15,7 @@ const permutations = arr => {
|
||||
return arr.reduce(
|
||||
(acc, item, i) =>
|
||||
acc.concat(
|
||||
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [
|
||||
item,
|
||||
...val,
|
||||
])
|
||||
permutations([...arr.slice(0, i), ...arr.slice(i + 1)]).map(val => [item, ...val])
|
||||
),
|
||||
[]
|
||||
);
|
||||
@ -26,5 +23,5 @@ const permutations = arr => {
|
||||
```
|
||||
|
||||
```js
|
||||
permutations([1, 33, 5]) // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
|
||||
permutations([1, 33, 5]); // [ [ 1, 33, 5 ], [ 1, 5, 33 ], [ 33, 1, 5 ], [ 33, 5, 1 ], [ 5, 1, 33 ], [ 5, 33, 1 ] ]
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user