This commit is contained in:
Rohit Tanwar
2018-01-01 19:18:08 +05:30
21 changed files with 661 additions and 87 deletions

417
README.md
View File

@ -54,6 +54,7 @@
* [`initializeArrayWithRange`](#initializearraywithrange)
* [`initializeArrayWithValues`](#initializearraywithvalues)
* [`intersection`](#intersection)
* [`join`](#join)
* [`last`](#last)
* [`mapObject`](#mapobject)
* [`nthElement`](#nthelement)
@ -64,8 +65,10 @@
* [`quickSort`](#quicksort)
* [`remove`](#remove)
* [`sample`](#sample)
* [`sampleSize`](#samplesize)
* [`shuffle`](#shuffle)
* [`similarity`](#similarity)
* [`sortedIndex`](#sortedindex)
* [`symmetricDifference`](#symmetricdifference)
* [`tail`](#tail)
* [`take`](#take)
@ -77,7 +80,7 @@
</details>
### 🖥️ Browser
### 🌐 Browser
<details>
<summary>View contents</summary>
@ -90,7 +93,6 @@
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
* [`getScrollPosition`](#getscrollposition)
* [`getStyle`](#getstyle)
* [`getURLParameters`](#geturlparameters)
* [`hasClass`](#hasclass)
* [`hide`](#hide)
* [`httpsRedirect`](#httpsredirect)
@ -126,6 +128,7 @@
* [`compose`](#compose)
* [`curry`](#curry)
* [`functionName`](#functionname)
* [`memoize`](#memoize)
* [`runPromisesInSeries`](#runpromisesinseries)
* [`sleep`](#sleep)
@ -150,6 +153,7 @@
* [`collatz`](#collatz)
* [`digitize`](#digitize)
* [`distance`](#distance)
* [`elo`](#elo)
* [`factorial`](#factorial)
* [`fibonacci`](#fibonacci)
* [`fibonacciCountUntilNum`](#fibonaccicountuntilnum)
@ -217,6 +221,8 @@
* [`escapeHTML`](#escapehtml)
* [`escapeRegExp`](#escaperegexp)
* [`fromCamelCase`](#fromcamelcase)
* [`isAbsoluteURL`](#isabsoluteurl)
* [`mask`](#mask)
* [`palindrome`](#palindrome)
* [`repeatString`](#repeatstring)
* [`reverseString`](#reversestring)
@ -240,14 +246,19 @@
* [`coalesceFactory`](#coalescefactory)
* [`extendHex`](#extendhex)
* [`getType`](#gettype)
* [`getURLParameters`](#geturlparameters)
* [`hexToRGB`](#hextorgb)
* [`isArray`](#isarray)
* [`isArrayLike`](#isarraylike)
* [`isBoolean`](#isboolean)
* [`isFunction`](#isfunction)
* [`isNull`](#isnull)
* [`isNumber`](#isnumber)
* [`isPrimitive`](#isprimitive)
* [`isPromiseLike`](#ispromiselike)
* [`isString`](#isstring)
* [`isSymbol`](#issymbol)
* [`isValidJSON`](#isvalidjson)
* [`randomHexColorCode`](#randomhexcolorcode)
* [`RGBToHex`](#rgbtohex)
* [`sdbm`](#sdbm)
@ -903,6 +914,40 @@ intersection([1, 2, 3], [4, 3, 2]); // [2,3]
<br>[⬆ Back to top](#table-of-contents)
### join
Joins all elements of an array into a string and returns this string. Uses a separator and an end separator.
Use `Array.reduce()` to combine elements into a string.
Omit the second argument, `separator`, to use a default separator of `','`.
Omit the third argument, `end`, to use the same value as `separator` by default.
```js
const join = (arr, separator = ',', end = separator) =>
arr.reduce(
(acc, val, i) =>
i == arr.length - 2
? acc + val + end
: i == arr.length - 1 ? acc + val : acc + val + separator,
''
);
```
<details>
<summary>Examples</summary>
```js
join(); // ''
join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### last
Returns the last element in an array.
@ -1189,6 +1234,38 @@ sample([3, 7, 9, 11]); // 9
<br>[⬆ Back to top](#table-of-contents)
### sampleSize
Gets `n` random elements at unique keys from `array` up to the size of `array`.
Shuffle the array using the [Fisher-Yates algorithm](https://github.com/chalarangelo/30-seconds-of-code#shuffle).
Use `Array.slice()` to get the first `n` elements.
Omit the second argument, `n` to get only one element at random from the array.
```js
const sampleSize = ([...arr], n = 1) => {
let m = arr.length;
while (m) {
const i = Math.floor(Math.random() * m--);
[arr[m], arr[i]] = [arr[i], arr[m]];
}
return arr.slice(0, n);
};
```
<details>
<summary>Examples</summary>
```js
sampleSize([1, 2, 3], 2); // [3,1]
sampleSize([1, 2, 3], 4); // [2,3,1]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### shuffle
Randomizes the order of the values of an array, returning a new array.
@ -1242,6 +1319,34 @@ similarity([1, 2, 3], [1, 2, 4]); // [1,2]
<br>[⬆ Back to top](#table-of-contents)
### sortedIndex
Returns the lowest index at which value should be inserted into array in order to maintain its sort order.
Check if the array is sorted in descending order (loosely).
Use `Array.findIndex()` to find the appropriate index where the element should be inserted.
```js
const sortedIndex = (arr, n) => {
const isDescending = arr[0] > arr[arr.length - 1];
const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
return index === -1 ? arr.length : index;
};
```
<details>
<summary>Examples</summary>
```js
sortedIndex([5, 3, 2, 1], 4); // 1
sortedIndex([30, 50], 40); // 1
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### symmetricDifference
Returns the symmetric difference between two arrays.
@ -1437,7 +1542,7 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
<br>[⬆ Back to top](#table-of-contents)
---
## 🖥️ Browser
## 🌐 Browser
### arrayToHtmlList
@ -1658,32 +1763,6 @@ getStyle(document.querySelector('p'), 'font-size'); // '16px'
<br>[⬆ Back to top](#table-of-contents)
### getURLParameters
Returns an object containing the parameters of the current URL.
Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object.
Pass `location.search` as the argument to apply to the current `url`.
```js
const getURLParameters = url =>
url
.match(/([^?=&]+)(=([^&]*))/g)
.reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {});
```
<details>
<summary>Examples</summary>
```js
getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'}
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### hasClass
Returns `true` if the element has the specified class, `false` otherwise.
@ -2176,6 +2255,35 @@ functionName(Math.max); // max (logged in debug channel of console)
<br>[⬆ Back to top](#table-of-contents)
### memoize
Returns the memoized (cached) function.
Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`).
Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.
```js
const memoize = fn => {
const cache = Object.create(null);
return value => cache[value] || (cache[value] = fn(value));
};
```
<details>
<summary>Examples</summary>
```js
// See the `anagrams` snippet.
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's now cached
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### runPromisesInSeries
Runs an array of promises in series.
@ -2371,6 +2479,39 @@ distance(1, 1, 2, 3); // 2.23606797749979
<br>[⬆ Back to top](#table-of-contents)
### elo
Computes the new ratings between two opponents using the [Elo rating system](https://en.wikipedia.org/wiki/Elo_rating_system). It takes an array
of two pre-ratings and returns an array containing two post-ratings.
The winner's rating is the first element of the array.
Use the exponent `**` operator and math operators to compute the expected score (chance of winning)
of each opponent and compute the new rating for each. Omit the second argument to use the default
K-factor of 32, or supply a custom K-factor value.
```js
const elo = ([a, b], kFactor = 32) => {
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
return [newRating(a, 1), newRating(b, 0)];
};
```
<details>
<summary>Examples</summary>
```js
elo([1200, 1200]); // [1216, 1184]
elo([1000, 2000]); // [1031.8991261061358, 1968.1008738938642]
elo([1500, 1000]); // [1501.7036868864648, 998.2963131135352]
elo([1200, 1200], 64); // [1232, 1168]
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### factorial
Calculates the factorial of a number.
@ -2565,11 +2706,11 @@ inrange(3, 2); // false
Checks if the given number is an Armstrong number or not.
Convert the given number into an array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
Convert the given number into an array of digits. Use the exponent operator (`**`) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
```js
const isArmstrongNumber = digits =>
(arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)(
(arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
(digits + '').split('')
);
```
@ -2913,9 +3054,7 @@ You can omit the second argument to get the sample standard deviation or set it
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr
.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) /
arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};
@ -3355,7 +3494,7 @@ byteSize('Hello World'); // 11
<br>[⬆ Back to top](#table-of-contents)
### Capitalize
### capitalize
Capitalizes the first letter of a string.
@ -3509,6 +3648,59 @@ fromCamelCase('someJavascriptProperty', '_'); // 'some_javascript_property'
<br>[⬆ Back to top](#table-of-contents)
### isAbsoluteURL
Returns `true` if the given string is an absolute URL, `false` otherwise.
Use a regular expression to test if the string is an absolute URL.
```js
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
```
<details>
<summary>Examples</summary>
```js
isAbsoluteURL('https://google.com'); // true
isAbsoluteURL('ftp://www.myserver.net'); // true
isAbsoluteURL('/foo/bar'); // false
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### mask
Replaces all but the last `num` of characters with the specified mask character.
Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regex to replace every character with the mask character.
Concatenate the masked characters with the remaining unmasked portion of the string.
Omit the second argument, `num`, to keep a default of `4` characters unmasked. If `num` is negative, the unmasked characters will be at the start of the string.
Omit the third argument, `mask`, to use a default character of `'*'` for the mask.
```js
const mask = (cc, num = 4, mask = '*') =>
('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num);
```
<details>
<summary>Examples</summary>
```js
mask(1234567890); // '******7890'
mask(1234567890, 3); // '*******890'
mask(1234567890, 4, '$'); // '$$$$$$7890'
mask(1234567890, -4, '$'); // '1234$$$$$$'
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### palindrome
Returns `true` if the given string is a palindrome, `false` otherwise.
@ -3919,6 +4111,32 @@ getType(new Set([1, 2, 3])); // "set"
<br>[⬆ Back to top](#table-of-contents)
### getURLParameters
Returns an object containing the parameters of the current URL.
Use `match()` with an appropriate regular expression to get all key-value pairs, `Array.reduce()` to map and combine them into a single object.
Pass `location.search` as the argument to apply to the current `url`.
```js
const getURLParameters = url =>
url
.match(/([^?=&]+)(=([^&]*))/g)
.reduce((a, v) => ((a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1)), a), {});
```
<details>
<summary>Examples</summary>
```js
getURLParameters('http://url.com/page?name=Adam&surname=Smith'); // {name: 'Adam', surname: 'Smith'}
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### hexToRGB
Converts a color code to a `rgb()` or `rgba()` string if alpha value is provided.
@ -3984,6 +4202,35 @@ isArray([1]); // true
<br>[⬆ Back to top](#table-of-contents)
### isArrayLike
Checks if the provided argument is array-like (i.e. is iterable).
Use the spread operator (`...`) to check if the provided argument is iterable inside a `try... catch` block and the comma operator (`,`) to return the appropriate value.
```js
const isArrayLike = val =>
try {return [...val], true; }
catch (e) { return false; }
};
```
<details>
<summary>Examples</summary>
```js
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### isBoolean
Checks if the given argument is a native boolean element.
@ -4075,6 +4322,68 @@ isNumber(1); // true
<br>[⬆ Back to top](#table-of-contents)
### isPrimitive
Returns a boolean determining if the supplied value is primitive or not.
Use `Array.includes()` on an array of type strings which are not primitive,
supplying the type using `typeof`.
Since `typeof null` evaluates to `'object'`, it needs to be directly compared.
```js
const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
```
<details>
<summary>Examples</summary>
```js
isPrimitive(window.someNonExistentProperty); // true
isPrimitive(null); // true
isPrimitive(50); // true
isPrimitive('Hello!'); // true
isPrimitive(false); // true
isPrimitive(Symbol()); // true
isPrimitive([]); // false
isPrimitive(new String('Hello!')); // false
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### isPromiseLike
Returns `true` if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), `false` otherwise.
Check if the object is not `null`, its `typeof` matches either `object` or `function` and if it has a `.then` property, which is also a `function`.
```js
const isPromiseLike = obj =>
obj !== null &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
```
<details>
<summary>Examples</summary>
```js
isPromiseLike({
then: function() {
return '';
}
}); // true
isPromiseLike(null); // false
isPromiseLike({}); // false
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### isString
Checks if the given argument is a string.
@ -4121,6 +4430,37 @@ isSymbol(Symbol('x')); // true
<br>[⬆ Back to top](#table-of-contents)
### isValidJSON
Checks if the provided argument is a valid JSON.
Use `JSON.parse()` and a `try... catch` block to check if the provided argument is a valid JSON.
```js
const isValidJSON = obj => {
try {
JSON.parse(obj);
return true;
} catch (e) {
return false;
}
};
```
<details>
<summary>Examples</summary>
```js
isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true
```
</details>
<br>[⬆ Back to top](#table-of-contents)
### randomHexColorCode
Generates a random hexadecimal color code.
@ -4332,6 +4672,13 @@ yesNo('Foo', true); // true
<br>[⬆ Back to top](#table-of-contents)
## Collaborators
| [<img src="https://github.com/Chalarangelo.png" width="100px;"/>](https://github.com/Chalarangelo)<br/> [<sub>Angelos Chalaris</sub>](https://github.com/Chalarangelo) | [<img src="https://github.com/Pl4gue.png" width="100px;"/>](https://github.com/Pl4gue)<br/> [<sub>David Wu</sub>](https://github.com/Pl4gue) | [<img src="https://github.com/fejes713.png" width="100px;"/>](https://github.com/fejes713)<br/> [<sub>Stefan Feješ</sub>](https://github.com/fejes713) | [<img src="https://github.com/kingdavidmartins.png" width="100px;"/>](https://github.com/kingdavidmartins)<br/> [<sub>King David Martins</sub>](https://github.com/iamsoorena) | [<img src="https://github.com/iamsoorena.png" width="100px;"/>](https://github.com/iamsoorena)<br/> [<sub>Soorena Soleimani</sub>](https://github.com/iamsoorena) |
| --- | --- | --- | --- | --- |
| [<img src="https://github.com/elderhsouza.png" width="100px;"/>](https://github.com/elderhsouza)<br/> [<sub>Elder Henrique Souza</sub>](https://github.com/elderhsouza) | [<img src="https://github.com/skatcat31.png" width="100px;"/>](https://github.com/skatcat31)<br/> [<sub>Robert Mennell</sub>](https://github.com/skatcat31) | [<img src="https://github.com/atomiks.png" width="100px;"/>](https://github.com/atomiks)<br/> [<sub>atomiks</sub>](https://github.com/atomiks) |
## Credits
*Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).*

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -121,7 +121,7 @@ code {
transform: scale(1); /* Deals with the issue described in #243 */
}
pre { font-size: 1rem; border: 0.0625rem solid var(--secondary-border-color); border-radius: var(--universal-border-radius);}
.group{position:relative;margin-top:2em;margin-bottom:-1em}
.group{position:relative;margin-top:2em;margin-bottom:1em}
.search{font-size:14px;margin-top:-.1em;display:block;width:100%;border:none;border-bottom:1px solid var(--nav-link-color)}
.search:focus{outline:none}
label#search-label{color:var(--nav-link-color);font-size:18px;font-weight:400;position:absolute;left:5px;top:10px}

View File

@ -1,4 +1,4 @@
### Capitalize
### capitalize
Capitalizes the first letter of a string.

24
snippets/elo.md Normal file
View File

@ -0,0 +1,24 @@
### elo
Computes the new ratings between two opponents using the [Elo rating system](https://en.wikipedia.org/wiki/Elo_rating_system). It takes an array
of two pre-ratings and returns an array containing two post-ratings.
The winner's rating is the first element of the array.
Use the exponent `**` operator and math operators to compute the expected score (chance of winning)
of each opponent and compute the new rating for each. Omit the second argument to use the default
K-factor of 32, or supply a custom K-factor value.
```js
const elo = ([a, b], kFactor = 32) => {
const expectedScore = (self, opponent) => 1 / (1 + 10 ** ((opponent - self) / 400));
const newRating = (rating, i) => rating + kFactor * (i - expectedScore(i ? a : b, i ? b : a));
return [newRating(a, 1), newRating(b, 0)];
};
```
```js
elo([1200, 1200]); // [1216, 1184]
elo([1000, 2000]); // [1031.8991261061358, 1968.1008738938642]
elo([1500, 1000]); // [1501.7036868864648, 998.2963131135352]
elo([1200, 1200], 64); // [1232, 1168]
```

15
snippets/isAbsoluteURL.md Normal file
View File

@ -0,0 +1,15 @@
### isAbsoluteURL
Returns `true` if the given string is an absolute URL, `false` otherwise.
Use a regular expression to test if the string is an absolute URL.
```js
const isAbsoluteURL = str => /^[a-z][a-z0-9+.-]*:/.test(str);
```
```js
isAbsoluteURL('https://google.com'); // true
isAbsoluteURL('ftp://www.myserver.net'); // true
isAbsoluteURL('/foo/bar'); // false
```

View File

@ -2,11 +2,11 @@
Checks if the given number is an Armstrong number or not.
Convert the given number into an array of digits. Use `Math.pow()` to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
Convert the given number into an array of digits. Use the exponent operator (`**`) to get the appropriate power for each digit and sum them up. If the sum is equal to the number itself, return `true` otherwise `false`.
```js
const isArmstrongNumber = digits =>
(arr => arr.reduce((a, d) => a + Math.pow(parseInt(d), arr.length), 0) == digits)(
(arr => arr.reduce((a, d) => a + parseInt(d) ** arr.length, 0) == digits)(
(digits + '').split('')
);
```

View File

@ -2,22 +2,19 @@
Checks if the provided argument is array-like (i.e. is iterable).
Use `Array.from()` and a `try... catch` block to check if the provided argument is array-like.
Use the spread operator (`...`) to check if the provided argument is iterable inside a `try... catch` block and the comma operator (`,`) to return the appropriate value.
```js
const isArrayLike = arr => {
try{
Array.from(arr);
return true;
}
catch(e){
return false;
}
}
const isArrayLike = val =>
try {return [...val], true; }
catch (e) { return false; }
};
```
```js
isArrayLike(document.querySelector('.className')) // true
isArrayLike('abc') // true
isArrayLike(null) // false
isArrayLike(document.querySelectorAll('.className')); // true
isArrayLike('abc'); // true
isArrayLike(null); // false
```

22
snippets/isPrimitive.md Normal file
View File

@ -0,0 +1,22 @@
### isPrimitive
Returns a boolean determining if the supplied value is primitive or not.
Use `Array.includes()` on an array of type strings which are not primitive,
supplying the type using `typeof`.
Since `typeof null` evaluates to `'object'`, it needs to be directly compared.
```js
const isPrimitive = val => !['object', 'function'].includes(typeof val) || val === null;
```
```js
isPrimitive(window.someNonExistentProperty); // true
isPrimitive(null); // true
isPrimitive(50); // true
isPrimitive('Hello!'); // true
isPrimitive(false); // true
isPrimitive(Symbol()); // true
isPrimitive([]); // false
isPrimitive(new String('Hello!')); // false
```

22
snippets/isPromiseLike.md Normal file
View File

@ -0,0 +1,22 @@
### isPromiseLike
Returns `true` if an object looks like a [`Promise`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise), `false` otherwise.
Check if the object is not `null`, its `typeof` matches either `object` or `function` and if it has a `.then` property, which is also a `function`.
```js
const isPromiseLike = obj =>
obj !== null &&
(typeof obj === 'object' || typeof obj === 'function') &&
typeof obj.then === 'function';
```
```js
isPromiseLike({
then: function() {
return '';
}
}); // true
isPromiseLike(null); // false
isPromiseLike({}); // false
```

View File

@ -6,17 +6,17 @@ Use `JSON.parse()` and a `try... catch` block to check if the provided argument
```js
const isValidJSON = obj => {
try{
try {
JSON.parse(obj);
return true;
}
catch(e){
} catch (e) {
return false;
}
}
};
```
```js
isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // true
```

View File

@ -15,12 +15,11 @@ const join = (arr, separator = ',', end = separator) =>
: i == arr.length - 1 ? acc + val : acc + val + separator,
''
);
```
```js
join(); // ''
join(['pen','pineapple','apple','pen'],',','&'); //"pen,pineapple,apple&pen"
join(['pen','pineapple','apple','pen'],','); //"pen,pineapple,apple,pen"
join(['pen','pineapple','apple','pen']); //"pen,pineapple,apple,pen"
join(['pen', 'pineapple', 'apple', 'pen'], ',', '&'); //"pen,pineapple,apple&pen"
join(['pen', 'pineapple', 'apple', 'pen'], ','); //"pen,pineapple,apple,pen"
join(['pen', 'pineapple', 'apple', 'pen']); //"pen,pineapple,apple,pen"
```

View File

@ -0,0 +1,20 @@
### mask
Replaces all but the last `num` of characters with the specified mask character.
Use `String.slice()` to grab the portion of the characters that need to be masked and use `String.replace()` with a regex to replace every character with the mask character.
Concatenate the masked characters with the remaining unmasked portion of the string.
Omit the second argument, `num`, to keep a default of `4` characters unmasked. If `num` is negative, the unmasked characters will be at the start of the string.
Omit the third argument, `mask`, to use a default character of `'*'` for the mask.
```js
const mask = (cc, num = 4, mask = '*') =>
('' + cc).slice(0, -num).replace(/./g, mask) + ('' + cc).slice(-num);
```
```js
mask(1234567890); // '******7890'
mask(1234567890, 3); // '*******890'
mask(1234567890, 4, '$'); // '$$$$$$7890'
mask(1234567890, -4, '$'); // '1234$$$$$$'
```

20
snippets/memoize.md Normal file
View File

@ -0,0 +1,20 @@
### memoize
Returns the memoized (cached) function.
Use `Object.create(null)` to create an empty object without `Object.prototype` (so that those properties are not resolved if the input value is something like `'hasOwnProperty'`).
Return a function which takes a single argument to be supplied to the memoized function by first checking if the function's output for that specific input value is already cached, or store and return it if not.
```js
const memoize = fn => {
const cache = Object.create(null);
return value => cache[value] || (cache[value] = fn(value));
};
```
```js
// See the `anagrams` snippet.
const anagramsCached = memoize(anagrams);
anagramsCached('javascript'); // takes a long time
anagramsCached('javascript'); // returns virtually instantly since it's now cached
```

View File

@ -7,17 +7,17 @@ Use `Array.slice()` to get the first `n` elements.
Omit the second argument, `n` to get only one element at random from the array.
```js
const sampleSize = ([...arr],n=1) => {
const sampleSize = ([...arr], n = 1) => {
let m = arr.length;
while (m) {
const i = Math.floor(Math.random() * m--);
[arr[m], arr[i]] = [arr[i], arr[m]];
}
return arr.slice(0,n)
}
return arr.slice(0, n);
};
```
```js
sampleSize([1,2,3],2); // [3,1]
sampleSize([1,2,3],4); // [2,3,1]
sampleSize([1, 2, 3], 2); // [3,1]
sampleSize([1, 2, 3], 4); // [2,3,1]
```

View File

@ -8,12 +8,12 @@ Use `Array.findIndex()` to find the appropriate index where the element should b
```js
const sortedIndex = (arr, n) => {
const isDescending = arr[0] > arr[arr.length - 1];
const index = arr.findIndex(el => isDescending ? n >= el : n <= el);
const index = arr.findIndex(el => (isDescending ? n >= el : n <= el));
return index === -1 ? arr.length : index;
};
```
```js
sortedIndex([5,3,2,1], 4); // 1
sortedIndex([30,50],40); // 1
sortedIndex([5, 3, 2, 1], 4); // 1
sortedIndex([30, 50], 40); // 1
```

View File

@ -10,9 +10,7 @@ You can omit the second argument to get the sample standard deviation or set it
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr
.reduce((acc, val) => acc.concat(Math.pow(val - mean, 2)), [])
.reduce((acc, val) => acc + val, 0) /
arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc + val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};

View File

@ -1,3 +1,10 @@
## Collaborators
| [<img src="https://github.com/Chalarangelo.png" width="100px;"/>](https://github.com/Chalarangelo)<br/> [<sub>Angelos Chalaris</sub>](https://github.com/Chalarangelo) | [<img src="https://github.com/Pl4gue.png" width="100px;"/>](https://github.com/Pl4gue)<br/> [<sub>David Wu</sub>](https://github.com/Pl4gue) | [<img src="https://github.com/fejes713.png" width="100px;"/>](https://github.com/fejes713)<br/> [<sub>Stefan Feješ</sub>](https://github.com/fejes713) | [<img src="https://github.com/kingdavidmartins.png" width="100px;"/>](https://github.com/kingdavidmartins)<br/> [<sub>King David Martins</sub>](https://github.com/iamsoorena) | [<img src="https://github.com/iamsoorena.png" width="100px;"/>](https://github.com/iamsoorena)<br/> [<sub>Soorena Soleimani</sub>](https://github.com/iamsoorena) |
| --- | --- | --- | --- | --- |
| [<img src="https://github.com/elderhsouza.png" width="100px;"/>](https://github.com/elderhsouza)<br/> [<sub>Elder Henrique Souza</sub>](https://github.com/elderhsouza) | [<img src="https://github.com/skatcat31.png" width="100px;"/>](https://github.com/skatcat31)<br/> [<sub>Robert Mennell</sub>](https://github.com/skatcat31) | [<img src="https://github.com/atomiks.png" width="100px;"/>](https://github.com/atomiks)<br/> [<sub>atomiks</sub>](https://github.com/atomiks) |
## Credits
*Icons made by [Smashicons](https://www.flaticon.com/authors/smashicons) from [www.flaticon.com](https://www.flaticon.com/) is licensed by [CC 3.0 BY](http://creativecommons.org/licenses/by/3.0/).*

View File

@ -92,8 +92,6 @@
<nav class="col-md-4 col-lg-3" style="border-top: 0">
<div class="group">
<input class="search" type="text" id="searchInput" onkeyup="search(this)">
<span class="highlight"></span>
<span class="bar"></span>
<label id="search-label">Search for snippet...</label>
</div>
<label for="doc-drawer-checkbox" class="button drawer-close"></label>

View File

@ -31,6 +31,7 @@ distinctValuesOfArray:array
dropElements:array
dropRight:array
elementIsVisibleInViewport:browser
elo:math
escapeHTML:string
escapeRegExp:string
everyNth:array
@ -50,7 +51,7 @@ getDaysDiffBetweenDates:date
getScrollPosition:browser
getStyle:browser
getType:utility
getURLParameters:browser
getURLParameters:utility
groupBy:array
hammingDistance:math
hasClass:browser
@ -64,8 +65,10 @@ initializeArrayWithRange:array
initializeArrayWithValues:array
inRange:math
intersection:array
isAbsoluteURL:string
isArmstrongNumber:math
isArray:utility
isArrayLike:utility
isBoolean:utility
isDivisible:math
isEven:math
@ -73,16 +76,22 @@ isFunction:utility
isNull:utility
isNumber:utility
isPrime:math
isPrimitive:utility
isPromiseLike:utility
isString:utility
isSymbol:utility
isValidJSON:utility
join:array
JSONToDate:date
JSONToFile:node
last:array
lcm:math
lowercaseKeys:object
mapObject:array
mask:string
max:math
median:math
memoize:function
min:math
negate:logic
nthElement:array
@ -113,6 +122,7 @@ RGBToHex:utility
round:math
runPromisesInSeries:function
sample:array
sampleSize:array
scrollToTop:browser
sdbm:utility
select:object
@ -124,6 +134,7 @@ similarity:array
size:object
sleep:function
sortCharactersInString:string
sortedIndex:array
speechSynthesis:browser
splitLines:string
spreadOver:adapter