Merge branch 'master' into shuffle
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@ -1 +1,3 @@
|
||||
node_modules/
|
||||
|
||||
currentSnippet\.js
|
||||
|
||||
@ -1,13 +1,74 @@
|
||||
## Contributing
|
||||
|
||||
You can contribute to **30 seconds of code** by sending pull requests for snippets that you find useful, reporting issues with current snippets or suggesting changes and/or additions.
|
||||
**30 seconds of code** is a community effort, so feel free to contribute in any way you can. Every contribution helps!
|
||||
|
||||
### Guidelines for new snippets
|
||||
Here's what you can do to help:
|
||||
|
||||
- Snippets must be short. Usually anything above 10 lines would be considered too long, but you can still submit it as it might be possible to shorten it or it might still prove useful regardless of its length.
|
||||
- Snippets must be explained to a certain extent in the description above them. Make sure to include what functions you are using and why.
|
||||
- Snippets must solve real-world problems and should be abstract enough to use in different scenarios. This is highly subjective, so send them in anyways.
|
||||
- Snippets *should* be written in ES6 if possible.
|
||||
- Snippet files must follow the anchor name conventions of [GitHub Flavored Markdown](https://github.github.com/gfm/), so that the `builder.js` can build the links for the list.
|
||||
- Use the [template](snippet-template.md) to format your snippets.
|
||||
- If possible, provide test cases in your Pull Request (link or comment), so that it's easier to verify that each snippet is working.
|
||||
- [Open issues](https://github.com/Chalarangelo/30-seconds-of-code/issues/new) for things you want to see added or modified.
|
||||
- Be part of the discussion by helping out with [existing issues](https://github.com/Chalarangelo/30-seconds-of-code/issues) or talking on our [gitter channel](https://gitter.im/30-seconds-of-code/Lobby).
|
||||
- Submit [pull requests](https://github.com/Chalarangelo/30-seconds-of-code/pulls) with snippets you have created (see below for guidelines).
|
||||
- Fix typos in existing snippets or run `npm run lint "snippet-name.md"` on unlinted snippets (yes, this is something we actually want help with).
|
||||
|
||||
### Snippet submission and Pull request guidelines
|
||||
|
||||
- **DO NOT MODIFY THE README.md FILE!** Make changes to individual snippet files. You can optionally run `npm run build-list` to update the README.md file automatically, based on the changes you have made.
|
||||
- **Snippet filenames** must correspond to the title of the snippet. For example if your snippet is titled `### Awesome snippet` the filename should be `awesome-snippet.md`.
|
||||
- Use `kebab-case`, not `snake_case`.
|
||||
- Avoid capitalization of words, except if the whole word is capitalized (e.g `URL` should be capitalized in the filename and the snippet title).
|
||||
- If there are parentheses in the title, add them to the filename (e.g. `awesome-snippet-(extra-awesome).md` if your snippet's title is `Awesome snippet (extra awesome)`).
|
||||
- **Snippet titles** should have only the first letter of the first word capitalized. Certain words can be in capitals (e.g. `URL`, `RGB`), but this is on a per-snippet basis.
|
||||
- All snippet titles must be prefixed with `###` and be at the very first line of your snippet.
|
||||
- Snippet titles must be unique (although if you cannot find a better title, just add some placeholder at the end of the filename and title and we will figure it out).
|
||||
- Follow snippet titles with an empty line.
|
||||
- **Snippet descriptions** must be short and to the point. Try to explain *how* the snippet works and what Javascript features are used. Remember to include what functions you are using and why.
|
||||
- Follow snippet descriptions with an empty line.
|
||||
- **Snippet code** must be enclosed inside ` ```js ` and ` ``` `.
|
||||
- Remember to start your snippet's code on a new line below the opening backticks.
|
||||
- Use ES6 notation to define your function. For example `const myFunction = arg1, arg2 => { }`.
|
||||
- Try to keep your snippets' code short and to the point. Use modern techniques and features. Make sure to test your code before submitting.
|
||||
- All snippets must be followed by one (more if necessary) test case after the code, on a new line, in the form of a comment, along with the expected output. The syntax for this is `myFunction('testInput') -> 'testOutput'`. Use multiline comments only if necessary.
|
||||
- Try to make your function name unique, so that it does not conflict with existing snippets.
|
||||
- Snippets should be short (usually below 10 lines). If your snippet is longer than that, you can still submit it and we can help you shorten it or figure out ways to improve it.
|
||||
- Snippets *should* solve real-world problems, no matter how simple.
|
||||
- Snippets *should* be abstract enough to be applied to different scenarios.
|
||||
- It is not mandatory, but highly appreciated if you provide **test cases** and/or performance tests (we recommend using [jsPerf](https://jsperf.com/)).
|
||||
- You can start creating a new snippet, by using the [snippet template](snippet-template.md) to format your snippets.
|
||||
|
||||
### Additional guidelines and conventions regarding snippets
|
||||
|
||||
- When describing snippets, refer to methods, using their full name. For example, use `Array.reduce()`, instead of `reduce()`.
|
||||
- If your snippet contains argument with default parameters, explain what happens if they are ommited when calling the function and what the default case is.
|
||||
- If your snippet uses recursion, explain the base cases.
|
||||
- Always use `const functionName` for function definitions.
|
||||
- Use variables only when necessary. Prefer `const` when the values are not altered after assignment, otherwise use `let`. Avoid using `var`.
|
||||
- Use `camelCase` for function and variable names if they consist of more than one word.
|
||||
- Try to give meaningful names to variables. For example use `letter`, instead of `lt`. Some exceptions by convention are:
|
||||
- `arr` for arrays (usually as the snippet function's argument).
|
||||
- `str` for strings.
|
||||
- `val` or `v` for value (usually when iterating a list, mapping, sorting etc.).
|
||||
- `acc` for accumulators in `Array.reduce()`.
|
||||
- `(a,b)` for the two values compared when using `Array.sort()`.
|
||||
- `i` for indexes.
|
||||
- `func` for function arguments.
|
||||
- `nums` for arrays of numbers.
|
||||
- Use `_` if your function takes no arguments or if an argument inside some function (e.g. `Array.reduce()`) is not used anywhere in your code.
|
||||
- Specify default parameters for arguments, if necessary. It is preferred to put default parameters last, unless you have pretty good reason not to.
|
||||
- If your snippet's function takes variadic arguments, use `..args` (although in certain cases, it might be needed to use a different name).
|
||||
- If your snippet function's body is a single statement, omit the `return` keyword and use an expression instead.
|
||||
- Always use soft tabs (2 spaces), never hard tabs.
|
||||
- Omit curly braces (`{` and `}`) whenever possible.
|
||||
- Always use single quotes for string literals. Use template literals, instead, if necessary.
|
||||
- If your snippet's code is short enough (around 80 characters), you can make it a single-line function (although not mandatory). Otherwise, use multiple lines.
|
||||
- Prefer using `Array` methods whenever possible.
|
||||
- Prefer `Array.concat()` instead of `Array.push()` when working with `Array.reduce()`.
|
||||
- Use strict equality checking (`===` and `!==` instead of `==` and `!=`), unless you specificly have reason not to.
|
||||
- Prefer using the ternary operator (`condition ? trueResult : falseResult`) instead of `if else` statements whenever possible.
|
||||
- Avoid nesting ternary operators (but you can do it if you feel like you should).
|
||||
- You should define multiple variables on the same line (e.g. `const x = 0, y = 0`) on the same line whenever possible.
|
||||
- Do not use trailing or leading underscores in variable names.
|
||||
- Use dot notation (`object.property`) for object properties, when possible. Use bracket notation (`object[variable]`) when accessing object properties using a variable.
|
||||
- Use arrow functions as much as possible, except when you can't.
|
||||
- Use semicolons whenever necessary. If your snippet function's body is a single statement, return an expression and add a semicolon at the end.
|
||||
- Leave a single space after a comma (`,`) character.
|
||||
- Try to strike a balance between readability, brevity and performance.
|
||||
- Never use `eval()`. Your snippet will be disqualified immediately.
|
||||
|
||||
234
README.md
234
README.md
@ -10,6 +10,9 @@
|
||||
## Contents
|
||||
|
||||
* [Anagrams of string (with duplicates)](#anagrams-of-string-with-duplicates)
|
||||
* [Array difference](#array-difference)
|
||||
* [Array intersection](#array-intersection)
|
||||
* [Array union](#array-union)
|
||||
* [Average of array of numbers](#average-of-array-of-numbers)
|
||||
* [Bottom visible](#bottom-visible)
|
||||
* [Capitalize first letter of every word](#capitalize-first-letter-of-every-word)
|
||||
@ -17,11 +20,11 @@
|
||||
* [Chain asynchronous functions](#chain-asynchronous-functions)
|
||||
* [Check for palindrome](#check-for-palindrome)
|
||||
* [Chunk array](#chunk-array)
|
||||
* [Compact](#compact)
|
||||
* [Count occurrences of a value in array](#count-occurrences-of-a-value-in-array)
|
||||
* [Current URL](#current-url)
|
||||
* [Curry](#curry)
|
||||
* [Deep flatten array](#deep-flatten-array)
|
||||
* [Difference between arrays](#difference-between-arrays)
|
||||
* [Distance between two points](#distance-between-two-points)
|
||||
* [Divisible by number](#divisible-by-number)
|
||||
* [Escape regular expression](#escape-regular-expression)
|
||||
@ -35,6 +38,7 @@
|
||||
* [Get native type of value](#get-native-type-of-value)
|
||||
* [Get scroll position](#get-scroll-position)
|
||||
* [Greatest common divisor (GCD)](#greatest-common-divisor-gcd)
|
||||
* [Group by](#group-by)
|
||||
* [Hamming distance](#hamming-distance)
|
||||
* [Head of list](#head-of-list)
|
||||
* [Initial of list](#initial-of-list)
|
||||
@ -44,19 +48,24 @@
|
||||
* [Measure time taken by function](#measure-time-taken-by-function)
|
||||
* [Median of array of numbers](#median-of-array-of-numbers)
|
||||
* [Object from key value pairs](#object-from-key-value-pairs)
|
||||
* [Object to key value pairs](#object-to-key-value-pairs)
|
||||
* [Ordinal suffix of number](#ordinal-suffix-of-number)
|
||||
* [Percentile](#percentile)
|
||||
* [Pick](#pick)
|
||||
* [Pipe](#pipe)
|
||||
* [Powerset](#powerset)
|
||||
* [Promisify](#promisify)
|
||||
* [Random integer in range](#random-integer-in-range)
|
||||
* [Random number in range](#random-number-in-range)
|
||||
* [Redirect to url](#redirect-to-url)
|
||||
* [Randomize order of array](#randomize-order-of-array)
|
||||
* [Redirect to URL](#redirect-to-url)
|
||||
* [Reverse a string](#reverse-a-string)
|
||||
* [RGB to hexadecimal](#rgb-to-hexadecimal)
|
||||
* [Run promises in series](#run-promises-in-series)
|
||||
* [Scroll to top](#scroll-to-top)
|
||||
* [Shuffle array](#shuffle-array)
|
||||
* [Similarity between arrays](#similarity-between-arrays)
|
||||
* [Sleep](#sleep)
|
||||
* [Sort characters in string (alphabetical)](#sort-characters-in-string-alphabetical)
|
||||
* [Sum of array of numbers](#sum-of-array-of-numbers)
|
||||
* [Swap values of two variables](#swap-values-of-two-variables)
|
||||
@ -77,20 +86,46 @@ 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 )), []);
|
||||
}
|
||||
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)), []);
|
||||
};
|
||||
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
|
||||
```
|
||||
|
||||
### Array difference
|
||||
|
||||
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
|
||||
|
||||
```js
|
||||
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); }
|
||||
// difference([1,2,3], [1,2]) -> [3]
|
||||
```
|
||||
|
||||
### Array intersection
|
||||
|
||||
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
|
||||
|
||||
```js
|
||||
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); }
|
||||
// intersection([1,2,3], [4,3,2]) -> [2,3]
|
||||
```
|
||||
|
||||
### Array union
|
||||
|
||||
Create a `Set` with all values of `a` and `b` and convert to an array.
|
||||
|
||||
```js
|
||||
const union = (a, b) => Array.from(new Set([...a, ...b]))
|
||||
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
|
||||
```
|
||||
|
||||
### Average of array of numbers
|
||||
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||
|
||||
```js
|
||||
const average = arr =>
|
||||
arr.reduce( (acc , val) => acc + val, 0) / arr.length;
|
||||
const average = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
|
||||
// average([1,2,3]) -> 2
|
||||
```
|
||||
|
||||
@ -99,7 +134,7 @@ const average = arr =>
|
||||
Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible.
|
||||
|
||||
```js
|
||||
const bottomVisible = _ =>
|
||||
const bottomVisible = _ =>
|
||||
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
|
||||
// bottomVisible() -> true
|
||||
```
|
||||
@ -120,7 +155,7 @@ Omit the `lowerRest` parameter to keep the rest of the string intact, or set it
|
||||
|
||||
```js
|
||||
const capitalize = (str, lowerRest = false) =>
|
||||
str.slice(0, 1).toUpperCase() + (lowerRest? str.slice(1).toLowerCase() : str.slice(1));
|
||||
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
|
||||
// capitalize('myName', true) -> 'Myname'
|
||||
```
|
||||
|
||||
@ -129,7 +164,7 @@ const capitalize = (str, lowerRest = false) =>
|
||||
Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.
|
||||
|
||||
```js
|
||||
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }
|
||||
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
|
||||
/*
|
||||
chainAsync([
|
||||
next => { console.log('0 seconds'); setTimeout(next, 1000); },
|
||||
@ -145,23 +180,34 @@ Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric ch
|
||||
Then, `split('')` into individual characters, `reverse()`, `join('')` and compare to the original, unreversed string, after converting it `tolowerCase()`.
|
||||
|
||||
```js
|
||||
const palindrome = str =>
|
||||
str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('') === str.toLowerCase().replace(/[\W_]/g,'');
|
||||
const palindrome = str => {
|
||||
const s = str.toLowerCase().replace(/[\W_]/g,'');
|
||||
return s === s.split('').reverse().join('');
|
||||
}
|
||||
// palindrome('taco cat') -> true
|
||||
```
|
||||
|
||||
### Chunk array
|
||||
|
||||
Use `Array.apply()` to create a new array, that fits the number of chunks that will be produced.
|
||||
Use `Array.map()` to map each element of the new array to a chunk the length of `size`.
|
||||
Use `Array.from()` to create a new array, that fits the number of chunks that will be produced.
|
||||
Use `Array.slice()` to map each element of the new array to a chunk the length of `size`.
|
||||
If the original array can't be split evenly, the final chunk will contain the remaining elements.
|
||||
|
||||
```js
|
||||
const chunk = (arr, size) =>
|
||||
Array.apply(null, {length: Math.ceil(arr.length/size)}).map((v, i) => arr.slice(i*size, i*size+size));
|
||||
Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size));
|
||||
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5]
|
||||
```
|
||||
|
||||
### Compact
|
||||
|
||||
Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||
|
||||
```js
|
||||
const compact = (arr) => arr.filter(v => v);
|
||||
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
|
||||
### Count occurrences of a value in array
|
||||
|
||||
Use `Array.reduce()` to increment a counter each time you encounter the specific value inside the array.
|
||||
@ -206,19 +252,10 @@ Use `Array.reduce()` to get all elements that are not arrays, flatten each eleme
|
||||
|
||||
```js
|
||||
const deepFlatten = arr =>
|
||||
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? deepFlatten(v) : v ), []);
|
||||
arr.reduce((a, v) => a.concat(Array.isArray(v) ? deepFlatten(v) : v), []);
|
||||
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
||||
```
|
||||
|
||||
### Difference between arrays
|
||||
|
||||
Use `filter()` to remove values that are part of `values`, determined using `includes()`.
|
||||
|
||||
```js
|
||||
const difference = (arr, values) => arr.filter(v => !values.includes(v));
|
||||
// difference([1,2,3], [1,2]) -> [3]
|
||||
```
|
||||
|
||||
### Distance between two points
|
||||
|
||||
Use `Math.hypot()` to calculate the Euclidean distance between two points.
|
||||
@ -248,11 +285,11 @@ const escapeRegExp = str => str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
|
||||
|
||||
### Even or odd number
|
||||
|
||||
Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator.
|
||||
Return `true` if the number is even, `false` if the number is odd.
|
||||
Checks whether a number is odd or even using the modulo (`%`) operator.
|
||||
Returns `true` if the number is even, `false` if the number is odd.
|
||||
|
||||
```js
|
||||
const isEven = num => Math.abs(num) % 2 === 0;
|
||||
const isEven = num => num % 2 === 0;
|
||||
// isEven(3) -> false
|
||||
```
|
||||
|
||||
@ -273,8 +310,8 @@ Create an empty array of the specific length, initializing the first two values
|
||||
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
||||
|
||||
```js
|
||||
const fibonacci = n =>
|
||||
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),[]);
|
||||
const fibonacci = n =>
|
||||
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||
// fibonacci(5) -> [0,1,1,2,3]
|
||||
```
|
||||
|
||||
@ -292,7 +329,7 @@ const filterNonUnique = arr => arr.filter(i => arr.indexOf(i) === arr.lastIndexO
|
||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
|
||||
```js
|
||||
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
|
||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -320,7 +357,7 @@ Returns lower-cased constructor name of value, "undefined" or "null" if value is
|
||||
|
||||
```js
|
||||
const getType = v =>
|
||||
v === undefined ? "undefined" : v === null ? "null" : v.constructor.name.toLowerCase();
|
||||
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
|
||||
// getType(new Set([1,2,3])) -> "set"
|
||||
```
|
||||
|
||||
@ -331,8 +368,8 @@ You can omit `el` to use a default value of `window`.
|
||||
|
||||
```js
|
||||
const getScrollPos = (el = window) =>
|
||||
( {x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop} );
|
||||
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
|
||||
// getScrollPos() -> {x: 0, y: 200}
|
||||
```
|
||||
|
||||
@ -343,10 +380,25 @@ Base case is when `y` equals `0`. In this case, return `x`.
|
||||
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
||||
|
||||
```js
|
||||
const gcd = (x , y) => !y ? x : gcd(y, x % y);
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
// gcd (8, 36) -> 4
|
||||
```
|
||||
|
||||
### Group by
|
||||
|
||||
Use `Array.map()` to map the values of an array to a function or property name.
|
||||
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
|
||||
|
||||
```js
|
||||
const groupBy = (arr, func) =>
|
||||
(typeof func === 'function' ? arr.map(func) : arr.map(val => val[func]))
|
||||
.reduce((acc, val, i) => {
|
||||
acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc;
|
||||
}, {});
|
||||
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
|
||||
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
|
||||
```
|
||||
|
||||
### Hamming distance
|
||||
|
||||
Use XOR operator (`^`) to find the bit difference between the two numbers, convert to binary string using `toString(2)`.
|
||||
@ -354,13 +406,13 @@ Count and return the number of `1`s in the string, using `match(/1/g)`.
|
||||
|
||||
```js
|
||||
const hammingDistance = (num1, num2) =>
|
||||
((num1^num2).toString(2).match(/1/g) || '').length;
|
||||
((num1 ^ num2).toString(2).match(/1/g) || '').length;
|
||||
// hammingDistance(2,3) -> 1
|
||||
```
|
||||
|
||||
### Head of list
|
||||
|
||||
Return `arr[0]`.
|
||||
Use `arr[0]` to return the first element of the passed array.
|
||||
|
||||
```js
|
||||
const head = arr => arr[0];
|
||||
@ -369,10 +421,10 @@ const head = arr => arr[0];
|
||||
|
||||
### Initial of list
|
||||
|
||||
Return `arr.slice(0,-1)`.
|
||||
Use `arr.slice(0,-1)`to return all but the last element of the array.
|
||||
|
||||
```js
|
||||
const initial = arr => arr.slice(0,-1);
|
||||
const initial = arr => arr.slice(0, -1);
|
||||
// initial([1,2,3]) -> [1,2]
|
||||
```
|
||||
|
||||
@ -383,7 +435,7 @@ You can omit `start` to use a default value of `0`.
|
||||
|
||||
```js
|
||||
const initializeArrayRange = (end, start = 0) =>
|
||||
Array.apply(null, Array(end-start)).map( (v,i) => i + start );
|
||||
Array.apply(null, Array(end - start)).map((v, i) => i + start);
|
||||
// initializeArrayRange(5) -> [0,1,2,3,4]
|
||||
```
|
||||
|
||||
@ -399,7 +451,7 @@ const initializeArray = (n, value = 0) => Array(n).fill(value);
|
||||
|
||||
### Last of list
|
||||
|
||||
Return `arr.slice(-1)[0]`.
|
||||
Use `arr.slice(-1)[0]` to get the last element of the given array.
|
||||
|
||||
```js
|
||||
const last = arr => arr.slice(-1)[0];
|
||||
@ -416,7 +468,7 @@ const timeTaken = callback => {
|
||||
const t0 = performance.now(), r = callback();
|
||||
console.log(performance.now() - t0);
|
||||
return r;
|
||||
}
|
||||
};
|
||||
// timeTaken(() => Math.pow(2, 10)) -> 1024 (0.010000000009313226 logged in console)
|
||||
```
|
||||
|
||||
@ -427,9 +479,9 @@ Return the number at the midpoint if `length` is odd, otherwise the average of t
|
||||
|
||||
```js
|
||||
const median = arr => {
|
||||
const mid = Math.floor(arr.length / 2), nums = arr.sort((a,b) => a - b);
|
||||
const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b);
|
||||
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
|
||||
}
|
||||
};
|
||||
// median([5,6,50,1,-5]) -> 5
|
||||
// median([0,10,-2,7]) -> 3.5
|
||||
```
|
||||
@ -439,10 +491,35 @@ const median = arr => {
|
||||
Use `Array.reduce()` to create and combine key-value pairs.
|
||||
|
||||
```js
|
||||
const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {});
|
||||
const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
|
||||
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
||||
```
|
||||
|
||||
### Object to key-value pairs
|
||||
|
||||
Use `Object.keys()` and `Array.map()` to iterate over the object's keys and produce an array with key-value pairs.
|
||||
|
||||
```js
|
||||
const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
||||
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
||||
```
|
||||
|
||||
### Ordinal suffix of number
|
||||
|
||||
Use the modulo operator (`%`) to find values of single and tens digits.
|
||||
Find which ordinal pattern digits match.
|
||||
If digit is found in teens pattern, use teens ordinal.
|
||||
|
||||
```js
|
||||
const toOrdinalSuffix = num => {
|
||||
const int = parseInt(num), digits = [(int % 10), (int % 100)],
|
||||
ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4],
|
||||
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3];
|
||||
}
|
||||
// toOrdinalSuffix("123") -> "123rd"
|
||||
```
|
||||
|
||||
### Percentile
|
||||
|
||||
Use `Array.reduce()` to calculate how many numbers are below the value and how many are the same value and
|
||||
@ -454,6 +531,17 @@ const percentile = (arr, val) =>
|
||||
// percentile([1,2,3,4,5,6,7,8,9,10], 6) -> 55
|
||||
```
|
||||
|
||||
### Pick
|
||||
|
||||
Use `Array.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj.
|
||||
|
||||
```js
|
||||
const pick = (obj, arr) =>
|
||||
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
|
||||
// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
|
||||
// pick(object, ['a', 'c'])['a'] -> 1
|
||||
```
|
||||
|
||||
### Pipe
|
||||
|
||||
Use `Array.reduce()` to pass value through functions.
|
||||
@ -469,7 +557,7 @@ Use `Array.reduce()` combined with `Array.map()` to iterate over elements and co
|
||||
|
||||
```js
|
||||
const powerset = arr =>
|
||||
arr.reduce( (a,v) => a.concat(a.map( r => [v].concat(r) )), [[]]);
|
||||
arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
|
||||
// powerset([1,2]) -> [[], [1], [2], [2,1]]
|
||||
```
|
||||
|
||||
@ -509,6 +597,15 @@ const randomInRange = (min, max) => Math.random() * (max - min) + min;
|
||||
// randomInRange(2,10) -> 6.0211363285087005
|
||||
```
|
||||
|
||||
### Randomize order of array
|
||||
|
||||
Use `Array.sort()` to reorder elements, utilizing `Math.random()` to randomize the sorting.
|
||||
|
||||
```js
|
||||
const randomizeOrder = arr => arr.sort((a, b) => Math.random() >= 0.5 ? -1 : 1);
|
||||
// randomizeOrder([1,2,3]) -> [1,3,2]
|
||||
```
|
||||
|
||||
### Redirect to URL
|
||||
|
||||
Use `window.location.href` or `window.location.replace()` to redirect to `url`.
|
||||
@ -557,11 +654,11 @@ Scroll by a fraction of the distance from top. Use `window.requestAnimationFrame
|
||||
```js
|
||||
const scrollToTop = _ => {
|
||||
const c = document.documentElement.scrollTop || document.body.scrollTop;
|
||||
if(c > 0) {
|
||||
if (c > 0) {
|
||||
window.requestAnimationFrame(scrollToTop);
|
||||
window.scrollTo(0, c - c/8);
|
||||
window.scrollTo(0, c - c / 8);
|
||||
}
|
||||
}
|
||||
};
|
||||
// scrollToTop()
|
||||
```
|
||||
|
||||
@ -570,8 +667,11 @@ const scrollToTop = _ => {
|
||||
Use `Array.sort()` to reorder elements, using `Math.random()` in the comparator.
|
||||
|
||||
```js
|
||||
const shuffle = arr => arr.sort(() => Math.random() - 0.5);
|
||||
// shuffle([1,2,3]) -> [2,3,1]
|
||||
const shuffle = arr => {
|
||||
let r = arr.map(Math.random);
|
||||
return arr.sort((a, b) => r[a] - r[b]);
|
||||
};
|
||||
// shuffle([1,2,3]) -> [2, 1, 3]
|
||||
```
|
||||
|
||||
### Similarity between arrays
|
||||
@ -583,13 +683,28 @@ const similarity = (arr, values) => arr.filter(v => values.includes(v));
|
||||
// similarity([1,2,3], [1,2,4]) -> [1,2]
|
||||
```
|
||||
|
||||
### Sleep
|
||||
|
||||
Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`.
|
||||
|
||||
```js
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
/*
|
||||
async function sleepyWork() {
|
||||
console.log('I\'m going to sleep for 1 second.');
|
||||
await sleep(1000);
|
||||
console.log('I woke up after 1 second.');
|
||||
}
|
||||
*/
|
||||
```
|
||||
|
||||
### Sort characters in string (alphabetical)
|
||||
|
||||
Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`, recombine using `join('')`.
|
||||
|
||||
```js
|
||||
const sortCharactersInString = str =>
|
||||
str.split('').sort( (a,b) => a.localeCompare(b) ).join('');
|
||||
str.split('').sort((a, b) => a.localeCompare(b)).join('');
|
||||
// sortCharactersInString('cabbage') -> 'aabbceg'
|
||||
```
|
||||
|
||||
@ -598,7 +713,7 @@ const sortCharactersInString = str =>
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
```js
|
||||
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||
const sum = arr => arr.reduce((acc, val) => acc + val, 0);
|
||||
// sum([1,2,3,4]) -> 10
|
||||
```
|
||||
|
||||
@ -628,7 +743,7 @@ Return the string truncated to the desired length, with `...` appended to the en
|
||||
|
||||
```js
|
||||
const truncate = (str, num) =>
|
||||
str.length > num ? str.slice(0, num > 3 ? num-3 : num) + '...' : str;
|
||||
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
|
||||
// truncate('boomerang', 7) -> 'boom...'
|
||||
```
|
||||
|
||||
@ -648,8 +763,8 @@ 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), {}
|
||||
url.match(/([^?=&]+)(=([^&]*))/g).reduce(
|
||||
(a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
|
||||
);
|
||||
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
|
||||
```
|
||||
@ -660,7 +775,7 @@ Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.o
|
||||
|
||||
```js
|
||||
const uuid = _ =>
|
||||
( [1e7]+-1e3+-4e3+-8e3+-1e11 ).replace( /[018]/g, c =>
|
||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
@ -670,9 +785,10 @@ const uuid = _ =>
|
||||
|
||||
Use `!isNaN` in combination with `parseFloat()` to check if the argument is a number.
|
||||
Use `isFinite()` to check if the number is finite.
|
||||
Use `Number()` to check if the coercion holds.
|
||||
|
||||
```js
|
||||
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n);
|
||||
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
|
||||
// validateNumber('10') -> true
|
||||
```
|
||||
|
||||
|
||||
1421
package-lock.json
generated
1421
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -4,7 +4,8 @@
|
||||
"fs-extra": "^4.0.2",
|
||||
"live-server": "^1.2.0",
|
||||
"markdown-it": "^8.4.0",
|
||||
"nodemon": "^1.12.1"
|
||||
"nodemon": "^1.12.1",
|
||||
"semistandard": "^11.0.0"
|
||||
},
|
||||
"name": "30-seconds-of-code",
|
||||
"description": "A collection of useful Javascript snippets.",
|
||||
@ -13,6 +14,7 @@
|
||||
"devDependencies": {},
|
||||
"scripts": {
|
||||
"build-list": "node ./scripts/builder.js",
|
||||
"lint": "node ./scripts/lintSnippet.js",
|
||||
"start": "concurrently --kill-others \"nodemon -e js,md -i README.md -x \\\"npm run build-list\\\"\" \"live-server ./build\""
|
||||
},
|
||||
"repository": {
|
||||
|
||||
@ -6,6 +6,8 @@ var staticPartsPath = './static-parts';
|
||||
|
||||
var snippets = {}, startPart = '', endPart = '', output = '';
|
||||
|
||||
console.time('Builder');
|
||||
|
||||
try {
|
||||
var snippetFilenames = fs.readdirSync(snippetsPath);
|
||||
snippetFilenames.sort((a, b) => {
|
||||
@ -51,3 +53,5 @@ catch (err){
|
||||
console.log('Error during README generation: '+err);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
console.timeEnd('Builder');
|
||||
|
||||
31
scripts/lintSnippet.js
Normal file
31
scripts/lintSnippet.js
Normal file
@ -0,0 +1,31 @@
|
||||
var fs = require('fs-extra');
|
||||
var cp = require('child_process');
|
||||
var path = require('path');
|
||||
|
||||
var snippetsPath = './snippets';
|
||||
var snippetFilename = '';
|
||||
|
||||
console.time('Linter');
|
||||
|
||||
if(process.argv.length < 3){
|
||||
console.log('Please specify the filename of a snippet to be linted.');
|
||||
console.log('Example usage: npm run lint "snippet-file.md"');
|
||||
process.exit(0);
|
||||
}
|
||||
else {
|
||||
snippetFilename = process.argv[2];
|
||||
let snippetData = fs.readFileSync(path.join(snippetsPath,snippetFilename),'utf8');
|
||||
try {
|
||||
let originalCode = snippetData.slice(snippetData.indexOf('```js')+5,snippetData.lastIndexOf('```'));
|
||||
fs.writeFileSync('currentSnippet.js',`${originalCode}`);
|
||||
cp.exec('semistandard "currentSnippet.js" --fix',{},(error, stdOut, stdErr) => {
|
||||
let lintedCode = fs.readFileSync('currentSnippet.js','utf8');
|
||||
fs.writeFile(path.join(snippetsPath,snippetFilename), `${snippetData.slice(0, snippetData.indexOf('```js')+5)+lintedCode+'```\n'}`);
|
||||
console.timeEnd('Linter');
|
||||
});
|
||||
}
|
||||
catch (err){
|
||||
console.log('Error during snippet loading: '+err);
|
||||
process.exit(1);
|
||||
}
|
||||
}
|
||||
@ -1,9 +1,9 @@
|
||||
### Snippet title
|
||||
|
||||
Explain briefly how the snippet works
|
||||
Explain briefly how the snippet works.
|
||||
|
||||
```js
|
||||
const functionName = arguments =>
|
||||
const functionName = arguments =>
|
||||
{functionBody}
|
||||
// functionName(sampleInput) -> sampleOutput
|
||||
```
|
||||
|
||||
@ -5,8 +5,8 @@ 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), {}
|
||||
url.match(/([^?=&]+)(=([^&]*))/g).reduce(
|
||||
(a, v) => (a[v.slice(0, v.indexOf('='))] = v.slice(v.indexOf('=') + 1), a), {}
|
||||
);
|
||||
// getUrlParameters('http://url.com/page?name=Adam&surname=Smith') -> {name: 'Adam', surname: 'Smith'}
|
||||
```
|
||||
|
||||
@ -4,7 +4,7 @@ Use `crypto` API to generate a UUID, compliant with [RFC4122](https://www.ietf.o
|
||||
|
||||
```js
|
||||
const uuid = _ =>
|
||||
( [1e7]+-1e3+-4e3+-8e3+-1e11 ).replace( /[018]/g, c =>
|
||||
([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
|
||||
(c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
|
||||
);
|
||||
// uuid() -> '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
|
||||
@ -7,9 +7,9 @@ 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 )), []);
|
||||
}
|
||||
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)), []);
|
||||
};
|
||||
// anagrams('abc') -> ['abc','acb','bac','bca','cab','cba']
|
||||
```
|
||||
|
||||
8
snippets/array-difference.md
Normal file
8
snippets/array-difference.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Array difference
|
||||
|
||||
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values not contained in `b`.
|
||||
|
||||
```js
|
||||
const difference = (a, b) => { const s = new Set(b); return a.filter(x => !s.has(x)); }
|
||||
// difference([1,2,3], [1,2]) -> [3]
|
||||
```
|
||||
8
snippets/array-intersection.md
Normal file
8
snippets/array-intersection.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Array intersection
|
||||
|
||||
Create a `Set` from `b`, then use `Array.filter()` on `a` to only keep values contained in `b`.
|
||||
|
||||
```js
|
||||
const intersection = (a, b) => { const s = new Set(b); return a.filter(x => s.has(x)); }
|
||||
// intersection([1,2,3], [4,3,2]) -> [2,3]
|
||||
```
|
||||
8
snippets/array-union.md
Normal file
8
snippets/array-union.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Array union
|
||||
|
||||
Create a `Set` with all values of `a` and `b` and convert to an array.
|
||||
|
||||
```js
|
||||
const union = (a, b) => Array.from(new Set([...a, ...b]))
|
||||
// union([1,2,3], [4,3,2]) -> [1,2,3,4]
|
||||
```
|
||||
@ -3,7 +3,6 @@
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`, divide by the `length` of the array.
|
||||
|
||||
```js
|
||||
const average = arr =>
|
||||
arr.reduce( (acc , val) => acc + val, 0) / arr.length;
|
||||
const average = arr => arr.reduce((acc, val) => acc + val, 0) / arr.length;
|
||||
// average([1,2,3]) -> 2
|
||||
```
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Use `scrollY`, `scrollHeight` and `clientHeight` to determine if the bottom of the page is visible.
|
||||
|
||||
```js
|
||||
const bottomVisible = _ =>
|
||||
const bottomVisible = _ =>
|
||||
document.documentElement.clientHeight + window.scrollY >= document.documentElement.scrollHeight || document.documentElement.clientHeight;
|
||||
// bottomVisible() -> true
|
||||
```
|
||||
|
||||
@ -5,6 +5,6 @@ Omit the `lowerRest` parameter to keep the rest of the string intact, or set it
|
||||
|
||||
```js
|
||||
const capitalize = (str, lowerRest = false) =>
|
||||
str.slice(0, 1).toUpperCase() + (lowerRest? str.slice(1).toLowerCase() : str.slice(1));
|
||||
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
|
||||
// capitalize('myName', true) -> 'Myname'
|
||||
```
|
||||
|
||||
@ -3,7 +3,7 @@
|
||||
Loop through an array of functions containing asynchronous events, calling `next` when each asynchronous event has completed.
|
||||
|
||||
```js
|
||||
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); }
|
||||
const chainAsync = fns => { let curr = 0; const next = () => fns[curr++](next); next(); };
|
||||
/*
|
||||
chainAsync([
|
||||
next => { console.log('0 seconds'); setTimeout(next, 1000); },
|
||||
|
||||
@ -4,7 +4,9 @@ Convert string `toLowerCase()` and use `replace()` to remove non-alphanumeric ch
|
||||
Then, `split('')` into individual characters, `reverse()`, `join('')` and compare to the original, unreversed string, after converting it `tolowerCase()`.
|
||||
|
||||
```js
|
||||
const palindrome = str =>
|
||||
str.toLowerCase().replace(/[\W_]/g,'').split('').reverse().join('') === str.toLowerCase().replace(/[\W_]/g,'');
|
||||
const palindrome = str => {
|
||||
const s = str.toLowerCase().replace(/[\W_]/g,'');
|
||||
return s === s.split('').reverse().join('');
|
||||
}
|
||||
// palindrome('taco cat') -> true
|
||||
```
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
### Chunk array
|
||||
|
||||
Use `Array.apply()` to create a new array, that fits the number of chunks that will be produced.
|
||||
Use `Array.map()` to map each element of the new array to a chunk the length of `size`.
|
||||
Use `Array.from()` to create a new array, that fits the number of chunks that will be produced.
|
||||
Use `Array.slice()` to map each element of the new array to a chunk the length of `size`.
|
||||
If the original array can't be split evenly, the final chunk will contain the remaining elements.
|
||||
|
||||
```js
|
||||
const chunk = (arr, size) =>
|
||||
Array.apply(null, {length: Math.ceil(arr.length/size)}).map((v, i) => arr.slice(i*size, i*size+size));
|
||||
Array.from({length: Math.ceil(arr.length / size)}, (v, i) => arr.slice(i * size, i * size + size));
|
||||
// chunk([1,2,3,4,5], 2) -> [[1,2],[3,4],5]
|
||||
```
|
||||
|
||||
8
snippets/compact.md
Normal file
8
snippets/compact.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Compact
|
||||
|
||||
Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||
|
||||
```js
|
||||
const compact = (arr) => arr.filter(v => v);
|
||||
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
@ -5,6 +5,6 @@ Use `Array.reduce()` to get all elements that are not arrays, flatten each eleme
|
||||
|
||||
```js
|
||||
const deepFlatten = arr =>
|
||||
arr.reduce( (a, v) => a.concat( Array.isArray(v) ? deepFlatten(v) : v ), []);
|
||||
arr.reduce((a, v) => a.concat(Array.isArray(v) ? deepFlatten(v) : v), []);
|
||||
// deepFlatten([1,[2],[[3],4],5]) -> [1,2,3,4,5]
|
||||
```
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
### Difference between arrays
|
||||
|
||||
Use `filter()` to remove values that are part of `values`, determined using `includes()`.
|
||||
|
||||
```js
|
||||
const difference = (arr, values) => arr.filter(v => !values.includes(v));
|
||||
// difference([1,2,3], [1,2]) -> [3]
|
||||
```
|
||||
@ -1,9 +1,9 @@
|
||||
### Even or odd number
|
||||
|
||||
Use `Math.abs()` to extend logic to negative numbers, check using the modulo (`%`) operator.
|
||||
Return `true` if the number is even, `false` if the number is odd.
|
||||
Checks whether a number is odd or even using the modulo (`%`) operator.
|
||||
Returns `true` if the number is even, `false` if the number is odd.
|
||||
|
||||
```js
|
||||
const isEven = num => Math.abs(num) % 2 === 0;
|
||||
const isEven = num => num % 2 === 0;
|
||||
// isEven(3) -> false
|
||||
```
|
||||
|
||||
@ -4,7 +4,7 @@ Create an empty array of the specific length, initializing the first two values
|
||||
Use `Array.reduce()` to add values into the array, using the sum of the last two values, except for the first two.
|
||||
|
||||
```js
|
||||
const fibonacci = n =>
|
||||
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i),[]);
|
||||
const fibonacci = n =>
|
||||
Array(n).fill(0).reduce((acc, val, i) => acc.concat(i > 1 ? acc[i - 1] + acc[i - 2] : i), []);
|
||||
// fibonacci(5) -> [0,1,1,2,3]
|
||||
```
|
||||
|
||||
@ -3,6 +3,6 @@
|
||||
Use `Array.reduce()` to get all elements inside the array and `concat()` to flatten them.
|
||||
|
||||
```js
|
||||
const flatten = arr => arr.reduce( (a, v) => a.concat(v), []);
|
||||
const flatten = arr => arr.reduce((a, v) => a.concat(v), []);
|
||||
// flatten([1,[2],3,4]) -> [1,2,3,4]
|
||||
```
|
||||
|
||||
@ -4,6 +4,6 @@ Returns lower-cased constructor name of value, "undefined" or "null" if value is
|
||||
|
||||
```js
|
||||
const getType = v =>
|
||||
v === undefined ? "undefined" : v === null ? "null" : v.constructor.name.toLowerCase();
|
||||
v === undefined ? 'undefined' : v === null ? 'null' : v.constructor.name.toLowerCase();
|
||||
// getType(new Set([1,2,3])) -> "set"
|
||||
```
|
||||
|
||||
@ -5,7 +5,7 @@ You can omit `el` to use a default value of `window`.
|
||||
|
||||
```js
|
||||
const getScrollPos = (el = window) =>
|
||||
( {x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop} );
|
||||
({x: (el.pageXOffset !== undefined) ? el.pageXOffset : el.scrollLeft,
|
||||
y: (el.pageYOffset !== undefined) ? el.pageYOffset : el.scrollTop});
|
||||
// getScrollPos() -> {x: 0, y: 200}
|
||||
```
|
||||
|
||||
@ -5,6 +5,6 @@ Base case is when `y` equals `0`. In this case, return `x`.
|
||||
Otherwise, return the GCD of `y` and the remainder of the division `x/y`.
|
||||
|
||||
```js
|
||||
const gcd = (x , y) => !y ? x : gcd(y, x % y);
|
||||
const gcd = (x, y) => !y ? x : gcd(y, x % y);
|
||||
// gcd (8, 36) -> 4
|
||||
```
|
||||
|
||||
14
snippets/group-by.md
Normal file
14
snippets/group-by.md
Normal file
@ -0,0 +1,14 @@
|
||||
### Group by
|
||||
|
||||
Use `Array.map()` to map the values of an array to a function or property name.
|
||||
Use `Array.reduce()` to create an object, where the keys are produced from the mapped results.
|
||||
|
||||
```js
|
||||
const groupBy = (arr, func) =>
|
||||
(typeof func === 'function' ? arr.map(func) : arr.map(val => val[func]))
|
||||
.reduce((acc, val, i) => {
|
||||
acc[val] = acc[val] === undefined ? [arr[i]] : acc[val].concat(arr[i]); return acc;
|
||||
}, {});
|
||||
// groupBy([6.1, 4.2, 6.3], Math.floor) -> {4: [4.2], 6: [6.1, 6.3]}
|
||||
// groupBy(['one', 'two', 'three'], 'length') -> {3: ['one', 'two'], 5: ['three']}
|
||||
```
|
||||
@ -5,6 +5,6 @@ Count and return the number of `1`s in the string, using `match(/1/g)`.
|
||||
|
||||
```js
|
||||
const hammingDistance = (num1, num2) =>
|
||||
((num1^num2).toString(2).match(/1/g) || '').length;
|
||||
((num1 ^ num2).toString(2).match(/1/g) || '').length;
|
||||
// hammingDistance(2,3) -> 1
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Head of list
|
||||
|
||||
Return `arr[0]`.
|
||||
Use `arr[0]` to return the first element of the passed array.
|
||||
|
||||
```js
|
||||
const head = arr => arr[0];
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
### Initial of list
|
||||
|
||||
Return `arr.slice(0,-1)`.
|
||||
Use `arr.slice(0,-1)`to return all but the last element of the array.
|
||||
|
||||
```js
|
||||
const initial = arr => arr.slice(0,-1);
|
||||
const initial = arr => arr.slice(0, -1);
|
||||
// initial([1,2,3]) -> [1,2]
|
||||
```
|
||||
|
||||
@ -5,6 +5,6 @@ You can omit `start` to use a default value of `0`.
|
||||
|
||||
```js
|
||||
const initializeArrayRange = (end, start = 0) =>
|
||||
Array.apply(null, Array(end-start)).map( (v,i) => i + start );
|
||||
Array.apply(null, Array(end - start)).map((v, i) => i + start);
|
||||
// initializeArrayRange(5) -> [0,1,2,3,4]
|
||||
```
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
### Last of list
|
||||
|
||||
Return `arr.slice(-1)[0]`.
|
||||
Use `arr.slice(-1)[0]` to get the last element of the given array.
|
||||
|
||||
```js
|
||||
const last = arr => arr.slice(-1)[0];
|
||||
|
||||
@ -8,6 +8,6 @@ const timeTaken = callback => {
|
||||
const t0 = performance.now(), r = callback();
|
||||
console.log(performance.now() - t0);
|
||||
return r;
|
||||
}
|
||||
};
|
||||
// timeTaken(() => Math.pow(2, 10)) -> 1024 (0.010000000009313226 logged in console)
|
||||
```
|
||||
|
||||
@ -5,9 +5,9 @@ Return the number at the midpoint if `length` is odd, otherwise the average of t
|
||||
|
||||
```js
|
||||
const median = arr => {
|
||||
const mid = Math.floor(arr.length / 2), nums = arr.sort((a,b) => a - b);
|
||||
const mid = Math.floor(arr.length / 2), nums = arr.sort((a, b) => a - b);
|
||||
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
|
||||
}
|
||||
};
|
||||
// median([5,6,50,1,-5]) -> 5
|
||||
// median([0,10,-2,7]) -> 3.5
|
||||
```
|
||||
|
||||
@ -3,6 +3,6 @@
|
||||
Use `Array.reduce()` to create and combine key-value pairs.
|
||||
|
||||
```js
|
||||
const objectFromPairs = arr => arr.reduce((a,v) => (a[v[0]] = v[1], a), {});
|
||||
const objectFromPairs = arr => arr.reduce((a, v) => (a[v[0]] = v[1], a), {});
|
||||
// objectFromPairs([['a',1],['b',2]]) -> {a: 1, b: 2}
|
||||
```
|
||||
|
||||
8
snippets/object-to-key-value-pairs.md
Normal file
8
snippets/object-to-key-value-pairs.md
Normal file
@ -0,0 +1,8 @@
|
||||
### Object to key-value pairs
|
||||
|
||||
Use `Object.keys()` and `Array.map()` to iterate over the object's keys and produce an array with key-value pairs.
|
||||
|
||||
```js
|
||||
const objectToPairs = obj => Object.keys(obj).map(k => [k, obj[k]]);
|
||||
// objectToPairs({a: 1, b: 2}) -> [['a',1],['b',2]])
|
||||
```
|
||||
15
snippets/ordinal-suffix-of-number.md
Normal file
15
snippets/ordinal-suffix-of-number.md
Normal file
@ -0,0 +1,15 @@
|
||||
### Ordinal suffix of number
|
||||
|
||||
Use the modulo operator (`%`) to find values of single and tens digits.
|
||||
Find which ordinal pattern digits match.
|
||||
If digit is found in teens pattern, use teens ordinal.
|
||||
|
||||
```js
|
||||
const toOrdinalSuffix = num => {
|
||||
const int = parseInt(num), digits = [(int % 10), (int % 100)],
|
||||
ordinals = ["st", "nd", "rd", "th"], oPattern = [1,2,3,4],
|
||||
tPattern = [11, 12, 13, 14, 15, 16, 17, 18, 19]
|
||||
return oPattern.includes(digits[0]) && !tPattern.includes(digits[1]) ? int + ordinals[digits[0]-1] : int + ordinals[3];
|
||||
}
|
||||
// toOrdinalSuffix("123") -> "123rd"
|
||||
```
|
||||
10
snippets/pick.md
Normal file
10
snippets/pick.md
Normal file
@ -0,0 +1,10 @@
|
||||
### Pick
|
||||
|
||||
Use `Array.reduce()` to convert the filtered/picked keys back to a object with the corresponding key:value pair if the key exist in the obj.
|
||||
|
||||
```js
|
||||
const pick = (obj, arr) =>
|
||||
arr.reduce((acc, curr) => (curr in obj && (acc[curr] = obj[curr]), acc), {});
|
||||
// pick({ 'a': 1, 'b': '2', 'c': 3 }, ['a', 'c']) -> { 'a': 1, 'c': 3 }
|
||||
// pick(object, ['a', 'c'])['a'] -> 1
|
||||
```
|
||||
@ -4,6 +4,6 @@ Use `Array.reduce()` combined with `Array.map()` to iterate over elements and co
|
||||
|
||||
```js
|
||||
const powerset = arr =>
|
||||
arr.reduce( (a,v) => a.concat(a.map( r => [v].concat(r) )), [[]]);
|
||||
arr.reduce((a, v) => a.concat(a.map(r => [v].concat(r))), [[]]);
|
||||
// powerset([1,2]) -> [[], [1], [2], [2,1]]
|
||||
```
|
||||
|
||||
@ -6,10 +6,10 @@ Scroll by a fraction of the distance from top. Use `window.requestAnimationFrame
|
||||
```js
|
||||
const scrollToTop = _ => {
|
||||
const c = document.documentElement.scrollTop || document.body.scrollTop;
|
||||
if(c > 0) {
|
||||
if (c > 0) {
|
||||
window.requestAnimationFrame(scrollToTop);
|
||||
window.scrollTo(0, c - c/8);
|
||||
window.scrollTo(0, c - c / 8);
|
||||
}
|
||||
}
|
||||
};
|
||||
// scrollToTop()
|
||||
```
|
||||
|
||||
14
snippets/sleep.md
Normal file
14
snippets/sleep.md
Normal file
@ -0,0 +1,14 @@
|
||||
### Sleep
|
||||
|
||||
Delay executing part of an `async` function, by putting it to sleep, returning a `Promise`.
|
||||
|
||||
```js
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
/*
|
||||
async function sleepyWork() {
|
||||
console.log('I\'m going to sleep for 1 second.');
|
||||
await sleep(1000);
|
||||
console.log('I woke up after 1 second.');
|
||||
}
|
||||
*/
|
||||
```
|
||||
@ -4,6 +4,6 @@ Split the string using `split('')`, `Array.sort()` utilizing `localeCompare()`,
|
||||
|
||||
```js
|
||||
const sortCharactersInString = str =>
|
||||
str.split('').sort( (a,b) => a.localeCompare(b) ).join('');
|
||||
str.split('').sort((a, b) => a.localeCompare(b)).join('');
|
||||
// sortCharactersInString('cabbage') -> 'aabbceg'
|
||||
```
|
||||
|
||||
@ -3,6 +3,6 @@
|
||||
Use `Array.reduce()` to add each value to an accumulator, initialized with a value of `0`.
|
||||
|
||||
```js
|
||||
const sum = arr => arr.reduce( (acc , val) => acc + val, 0);
|
||||
const sum = arr => arr.reduce((acc, val) => acc + val, 0);
|
||||
// sum([1,2,3,4]) -> 10
|
||||
```
|
||||
|
||||
@ -5,6 +5,6 @@ Return the string truncated to the desired length, with `...` appended to the en
|
||||
|
||||
```js
|
||||
const truncate = (str, num) =>
|
||||
str.length > num ? str.slice(0, num > 3 ? num-3 : num) + '...' : str;
|
||||
str.length > num ? str.slice(0, num > 3 ? num - 3 : num) + '...' : str;
|
||||
// truncate('boomerang', 7) -> 'boom...'
|
||||
```
|
||||
|
||||
@ -2,8 +2,9 @@
|
||||
|
||||
Use `!isNaN` in combination with `parseFloat()` to check if the argument is a number.
|
||||
Use `isFinite()` to check if the number is finite.
|
||||
Use `Number()` to check if the coercion holds.
|
||||
|
||||
```js
|
||||
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n);
|
||||
const validateNumber = n => !isNaN(parseFloat(n)) && isFinite(n) && Number(n) == n;
|
||||
// validateNumber('10') -> true
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user