Travis build: 475 [cron]

This commit is contained in:
30secondsofcode
2018-09-18 20:12:19 +00:00
parent 17a3db8f19
commit 8cf111559d
4 changed files with 1907 additions and 1894 deletions

View File

@ -1,7 +1,7 @@
![Logo](/logo.png)
# Snippets Archive
These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.
## Table of Contents
![Logo](/logo.png)
# Snippets Archive
These snippets, while useful and interesting, didn't quite make it into the repository due to either having very specific use-cases or being outdated. However we felt like they might still be useful to some readers, so here they are.
## Table of Contents
* [`JSONToDate`](#jsontodate)
* [`speechSynthesis`](#speechsynthesis)
* [`binarySearch`](#binarysearch)
@ -20,8 +20,8 @@ These snippets, while useful and interesting, didn't quite make it into the repo
* [`removeVowels`](#removevowels)
* [`solveRPN`](#solverpn)
* [`howManyTimes`](#howmanytimes)
---
---
### JSONToDate
Converts a JSON object to a date.
@ -33,18 +33,18 @@ const JSONToDate = arr => {
const dt = new Date(parseInt(arr.toString().substr(6)));
return `${dt.getDate()}/${dt.getMonth() + 1}/${dt.getFullYear()}`;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
JSONToDate(/Date(1489525200000)/); // "14/3/2017"
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### speechSynthesis
@ -61,18 +61,18 @@ const speechSynthesis = message => {
msg.voice = window.speechSynthesis.getVoices()[0];
window.speechSynthesis.speak(msg);
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
speechSynthesis('Hello, World'); // // plays the message
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### binarySearch
@ -92,19 +92,19 @@ const binarySearch = (arr, val, start = 0, end = arr.length - 1) => {
if (arr[mid] < val) return binarySearch(arr, val, mid + 1, end);
return mid;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 6); // 2
binarySearch([1, 4, 6, 7, 12, 13, 15, 18, 19, 20, 22, 24], 21); // -1
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### cleanObj
@ -124,19 +124,19 @@ const cleanObj = (obj, keysToKeep = [], childIndicator) => {
});
return obj;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
const testObj = { a: 1, b: 2, children: { a: 1, b: 2 } };
cleanObj(testObj, ['a'], 'children'); // { a: 1, children : { a: 1}}
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### collatz
@ -146,18 +146,18 @@ If `n` is even, return `n/2`. Otherwise, return `3n+1`.
```js
const collatz = n => (n % 2 === 0 ? n / 2 : 3 * n + 1);
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
collatz(8); // 4
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### countVowels
@ -167,19 +167,19 @@ Use a regular expression to count the number of vowels `(A, E, I, O, U)` in a `s
```js
const countVowels = str => (str.match(/[aeiou]/gi) || []).length;
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
countVowels('foobar'); // 3
countVowels('gym'); // 0
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### factors
@ -215,12 +215,12 @@ const factors = (num, primes = false) => {
}, []);
return primes ? array.filter(isPrime) : array;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
factors(12); // [2,3,4,6,12]
factors(12, true); // [2,3]
factors(-12); // [2, -2, 3, -3, 4, -4, 6, -6, 12, -12]
@ -228,8 +228,8 @@ factors(-12, true); // [2,3]
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### fibonacciCountUntilNum
@ -240,18 +240,18 @@ Use a mathematical formula to calculate the number of fibonacci numbers until `n
```js
const fibonacciCountUntilNum = num =>
Math.ceil(Math.log(num * Math.sqrt(5) + 1 / 2) / Math.log((Math.sqrt(5) + 1) / 2));
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
fibonacciCountUntilNum(10); // 7
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### fibonacciUntilNum
@ -269,18 +269,18 @@ const fibonacciUntilNum = num => {
[]
);
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### httpDelete
@ -294,25 +294,25 @@ Omit the third argument, `err` to log the request to the console's error stream
```js
const httpDelete = (url, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open("DELETE", url, true);
request.open('DELETE', url, true);
request.onload = () => callback(request);
request.onerror = () => err(request);
request.send();
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
httpDelete('https://website.com/users/123', request => {
console.log(request.responseText);
}); // 'Deletes a user from the database'
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### httpPut
@ -326,19 +326,19 @@ Omit the last argument, `err` to log the request to the console's error stream b
```js
const httpPut = (url, data, callback, err = console.error) => {
const request = new XMLHttpRequest();
request.open("PUT", url, true);
request.setRequestHeader('Content-type','application/json; charset=utf-8');
request.onload = () => callback(request);
request.onerror = () => err(request);
request.send(data);
const request = new XMLHttpRequest();
request.open("PUT", url, true);
request.setRequestHeader('Content-type','application/json; charset=utf-8');
request.onload = () => callback(request);
request.onerror = () => err(request);
request.send(data);
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
const password = "fooBaz";
const data = JSON.stringify(password);
httpPut('https://website.com/users/123', data, request => {
@ -347,8 +347,8 @@ httpPut('https://website.com/users/123', data, request => {
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### isArmstrongNumber
@ -361,19 +361,19 @@ const isArmstrongNumber = digits =>
(arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
(digits + '').split('')
);
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
isArmstrongNumber(1634); // true
isArmstrongNumber(56); // false
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### isSimilar
@ -384,22 +384,28 @@ Adapted from [here](https://github.com/forrestthewoods/lib_fts/blob/80f3f8c52db5
```js
const isSimilar = (pattern, str) =>
[...str].reduce(
(matchIndex, char) => char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase() ? matchIndex + 1 : matchIndex, 0
) === pattern.length ? true : false;
```
[...str].reduce(
(matchIndex, char) =>
char.toLowerCase() === (pattern[matchIndex] || '').toLowerCase()
? matchIndex + 1
: matchIndex,
0
) === pattern.length
? true
: false;
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
isSimilar('rt','Rohit'); // true
isSimilar('tr','Rohit'); // false
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### levenshteinDistance
@ -410,36 +416,43 @@ Can also be used to compare two strings as shown in the second example.
``` js
const levenshteinDistance = (string1, string2) => {
if(string1.length === 0) return string2.length;
if(string2.length === 0) return string1.length;
let matrix = Array(string2.length + 1).fill(0).map((x,i) => [i]);
matrix[0] = Array(string1.length + 1).fill(0).map((x,i) => i);
for(let i = 1; i <= string2.length; i++) {
for(let j = 1; j<=string1.length; j++) {
if(string2[i-1] === string1[j-1]) {
matrix[i][j] = matrix[i-1][j-1];
}
else{
matrix[i][j] = Math.min(matrix[i-1][j-1]+1, matrix[i][j-1]+1, matrix[i-1][j]+1);
}
}
if (string1.length === 0) return string2.length;
if (string2.length === 0) return string1.length;
let matrix = Array(string2.length + 1)
.fill(0)
.map((x, i) => [i]);
matrix[0] = Array(string1.length + 1)
.fill(0)
.map((x, i) => i);
for (let i = 1; i <= string2.length; i++) {
for (let j = 1; j <= string1.length; j++) {
if (string2[i - 1] === string1[j - 1]) {
matrix[i][j] = matrix[i - 1][j - 1];
} else {
matrix[i][j] = Math.min(
matrix[i - 1][j - 1] + 1,
matrix[i][j - 1] + 1,
matrix[i - 1][j] + 1
);
}
}
return matrix[string2.length][string1.length];
}
return matrix[string2.length][string1.length];
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
levenshteinDistance('30-seconds-of-code','30-seconds-of-python-code'); // 7
const compareStrings = (string1,string2) => (100 - levenshteinDistance(string1,string2) / Math.max(string1.length,string2.length));
compareStrings('30-seconds-of-code', '30-seconds-of-python-code'); // 99.72 (%)
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### quickSort
@ -458,19 +471,19 @@ const quickSort = ([n, ...nums], desc) =>
n,
...quickSort(nums.filter(v => (!desc ? v > n : v <= n)), desc)
];
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
quickSort([4, 1, 3, 2]); // [1,2,3,4]
quickSort([4, 1, 3, 2], true); // [4,3,2,1]
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### removeVowels
@ -480,20 +493,20 @@ Use `String.replace()` with a regexp to replace all vowels in `str`.
Omot `repl` to use a default value of `''`.
```js
const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi,repl);
```
const removeVowels = (str, repl = '') => str.replace(/[aeiou]/gi, repl);
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
removeVowels("foobAr"); // "fbr"
removeVowels("foobAr","*"); // "f**b*r"
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### solveRPN
@ -534,19 +547,19 @@ const solveRPN = rpn => {
if (stack.length === 1) return stack.pop();
else throw `${rpn} is not a proper RPN. Please check it and try again`;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
solveRPN('15 7 1 1 + - / 3 * 2 1 1 + + -'); // 5
solveRPN('2 3 ^'); // 8
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)
### howManyTimes
@ -569,12 +582,12 @@ const howManyTimes = (num, divisor) => {
}
return i;
};
```
```
<details>
<summary>Examples</summary>
```js
<summary>Examples</summary>
```js
howManyTimes(100, 2); // 2
howManyTimes(100, 2.5); // 2
howManyTimes(100, 0); // 0
@ -582,6 +595,6 @@ howManyTimes(100, -1); // Infinity
```
</details>
[⬆ Back to top](#table-of-contents)
<br>[⬆ Back to top](#table-of-contents)