Merge branch 'patch-1' of https://github.com/kriadmin/30-seconds-of-code
This commit is contained in:
@ -4,14 +4,23 @@ setup_git() {
|
||||
}
|
||||
|
||||
commit_website_files() {
|
||||
git checkout master
|
||||
git add *
|
||||
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER"
|
||||
if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then
|
||||
if [ $TRAVIS_BRANCH == "master" ]; then
|
||||
echo "Commiting to master branch..."
|
||||
git checkout master
|
||||
git add *
|
||||
git commit --message "Travis build: $TRAVIS_BUILD_NUMBER [ci skip]"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
upload_files() {
|
||||
echo "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git"
|
||||
git push --force "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git" master > /dev/null 2>&1
|
||||
if [ $TRAVIS_EVENT_TYPE != "pull_request" ]; then
|
||||
if [ $TRAVIS_BRANCH == "master" ]; then
|
||||
echo "Pushing to master branch..."
|
||||
git push --force --quiet "https://${GH_TOKEN}@github.com/Chalarangelo/30-seconds-of-code.git" master > /dev/null 2>&1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
setup_git
|
||||
|
||||
271
README.md
271
README.md
@ -9,11 +9,13 @@
|
||||
- Use <kbd>Ctrl</kbd> + <kbd>F</kbd> or <kbd>command</kbd> + <kbd>F</kbd> to search for a snippet.
|
||||
- Contributions welcome, please read the [contribution guide](CONTRIBUTING.md).
|
||||
- Snippets are written in ES6, use the [Babel transpiler](https://babeljs.io/) to ensure backwards-compatibility.
|
||||
- You can import these snippets into your text editor of choice (VSCode, Atom, Sublime) using the files found in [this repo](https://github.com/Rob-Rychs/30-seconds-of-code-texteditorsnippets).
|
||||
- You can import these snippets into Alfred 3, using [this file](https://github.com/lslvxy/30-seconds-of-code-alfredsnippets).
|
||||
- You can find a package with all the snippets on [npm](https://www.npmjs.com/package/tsoc). Bear in mind that most of these snippets are not production-ready.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
### Adapter
|
||||
### 🔌 Adapter
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -27,7 +29,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Array
|
||||
### 📚 Array
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -75,13 +77,14 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Browser
|
||||
### 🖥️ Browser
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
|
||||
* [`arrayToHtmlList`](#arraytohtmllist)
|
||||
* [`bottomVisible`](#bottomvisible)
|
||||
* [`copyToClipboard`](#copytoclipboard)
|
||||
* [`currentURL`](#currenturl)
|
||||
* [`detectDeviceType`](#detectdevicetype)
|
||||
* [`elementIsVisibleInViewport`](#elementisvisibleinviewport)
|
||||
@ -96,12 +99,13 @@
|
||||
* [`scrollToTop`](#scrolltotop)
|
||||
* [`setStyle`](#setstyle)
|
||||
* [`show`](#show)
|
||||
* [`speechSynthesis`](#speechsynthesis)
|
||||
* [`toggleClass`](#toggleclass)
|
||||
* [`UUIDGeneratorBrowser`](#uuidgeneratorbrowser)
|
||||
|
||||
</details>
|
||||
|
||||
### Date
|
||||
### ⏱️ Date
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -113,7 +117,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Function
|
||||
### 🎛️ Function
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -127,7 +131,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Logic
|
||||
### 🔮 Logic
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -136,7 +140,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Math
|
||||
### ➗ Math
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -172,16 +176,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Media
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
|
||||
* [`speechSynthesis`](#speechsynthesis)
|
||||
|
||||
</details>
|
||||
|
||||
### Node
|
||||
### 📦 Node
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -192,7 +187,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Object
|
||||
### 🗃️ Object
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -204,11 +199,12 @@
|
||||
* [`orderBy`](#orderby)
|
||||
* [`select`](#select)
|
||||
* [`shallowClone`](#shallowclone)
|
||||
* [`size`](#size)
|
||||
* [`truthCheckCollection`](#truthcheckcollection)
|
||||
|
||||
</details>
|
||||
|
||||
### String
|
||||
### 📜 String
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -235,7 +231,7 @@
|
||||
|
||||
</details>
|
||||
|
||||
### Utility
|
||||
### 💎 Utility
|
||||
|
||||
<details>
|
||||
<summary>View contents</summary>
|
||||
@ -248,6 +244,7 @@
|
||||
* [`isArray`](#isarray)
|
||||
* [`isBoolean`](#isboolean)
|
||||
* [`isFunction`](#isfunction)
|
||||
* [`isNull`](#isnull)
|
||||
* [`isNumber`](#isnumber)
|
||||
* [`isString`](#isstring)
|
||||
* [`isSymbol`](#issymbol)
|
||||
@ -258,10 +255,12 @@
|
||||
* [`toDecimalMark`](#todecimalmark)
|
||||
* [`toOrdinalSuffix`](#toordinalsuffix)
|
||||
* [`validateNumber`](#validatenumber)
|
||||
* [`yesNo`](#yesno)
|
||||
|
||||
</details>
|
||||
|
||||
## Adapter
|
||||
---
|
||||
## 🔌 Adapter
|
||||
|
||||
### call
|
||||
|
||||
@ -423,7 +422,8 @@ arrayMax([1, 2, 4]); // 4
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Array
|
||||
---
|
||||
## 📚 Array
|
||||
|
||||
### chunk
|
||||
|
||||
@ -1436,7 +1436,8 @@ zipObject(['a', 'b'], [1, 2, 3]); // {a: 1, b: 2}
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Browser
|
||||
---
|
||||
## 🖥️ Browser
|
||||
|
||||
### arrayToHtmlList
|
||||
|
||||
@ -1485,6 +1486,48 @@ bottomVisible(); // true
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### copyToClipboard
|
||||
|
||||
Copy a string to the clipboard. Only works as a result of user action (i.e. inside a `click` event listener).
|
||||
|
||||
Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
|
||||
Use `Selection.getRangeAt()`to store the selected range (if any).
|
||||
Use `document.execCommand('copy')` to copy to the clipboard.
|
||||
Remove the `<textarea>` element from the HTML document.
|
||||
Finally, use `Selection().addRange()` to recover the original selected range (if any).
|
||||
|
||||
```js
|
||||
const copyToClipboard = str => {
|
||||
const el = document.createElement('textarea');
|
||||
el.value = str;
|
||||
el.setAttribute('readonly', '');
|
||||
el.style.position = 'absolute';
|
||||
el.style.left = '-9999px';
|
||||
document.body.appendChild(el);
|
||||
const selected =
|
||||
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||
el.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(el);
|
||||
if (selected) {
|
||||
document.getSelection().removeAllRanges();
|
||||
document.getSelection().addRange(selected);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### currentURL
|
||||
|
||||
Returns the current URL.
|
||||
@ -1841,6 +1884,35 @@ show(document.querySelectorAll('img')); // Shows all <img> elements on the page
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### speechSynthesis
|
||||
|
||||
Performs speech synthesis (experimental).
|
||||
|
||||
Use `SpeechSynthesisUtterance.voice` and `window.speechSynthesis.getVoices()` to convert a message to speech.
|
||||
Use `window.speechSynthesis.speak()` to play the message.
|
||||
|
||||
Learn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).
|
||||
|
||||
```js
|
||||
const speechSynthesis = message => {
|
||||
const msg = new SpeechSynthesisUtterance(message);
|
||||
msg.voice = window.speechSynthesis.getVoices()[0];
|
||||
window.speechSynthesis.speak(msg);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
speechSynthesis('Hello, World'); // // plays the message
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### toggleClass
|
||||
|
||||
Toggle a class for an element.
|
||||
@ -1887,7 +1959,8 @@ UUIDGeneratorBrowser(); // '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Date
|
||||
---
|
||||
## ⏱️ Date
|
||||
|
||||
### getDaysDiffBetweenDates
|
||||
|
||||
@ -1987,7 +2060,8 @@ tomorrow(); // 2017-12-27 (if current date is 2017-12-26)
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Function
|
||||
---
|
||||
## 🎛️ Function
|
||||
|
||||
### chainAsync
|
||||
|
||||
@ -2150,7 +2224,8 @@ async function sleepyWork() {
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Logic
|
||||
---
|
||||
## 🔮 Logic
|
||||
|
||||
### negate
|
||||
|
||||
@ -2174,7 +2249,8 @@ negate(isOdd)(1); // false
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Math
|
||||
---
|
||||
## ➗ Math
|
||||
|
||||
### average
|
||||
|
||||
@ -2397,7 +2473,7 @@ const fibonacciUntilNum = num => {
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
fibonacciCountUntilNum(10); // 7
|
||||
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
||||
```
|
||||
|
||||
</details>
|
||||
@ -2567,7 +2643,7 @@ Return `false` if any of them divides the given number, else return `true`, unle
|
||||
```js
|
||||
const isPrime = num => {
|
||||
const boundary = Math.floor(Math.sqrt(num));
|
||||
for (var i = 2; i * i <= boundary; i++) if (num % i == 0) return false;
|
||||
for (var i = 2; i <= boundary; i++) if (num % i == 0) return false;
|
||||
return num >= 2;
|
||||
};
|
||||
```
|
||||
@ -2879,37 +2955,8 @@ sum([1, 2, 3, 4]); // 10
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Media
|
||||
|
||||
### speechSynthesis
|
||||
|
||||
Performs speech synthesis (experimental).
|
||||
|
||||
Use `SpeechSynthesisUtterance.voice` and `window.speechSynthesis.getVoices()` to convert a message to speech.
|
||||
Use `window.speechSynthesis.speak()` to play the message.
|
||||
|
||||
Learn more about the [SpeechSynthesisUtterance interface of the Web Speech API](https://developer.mozilla.org/en-US/docs/Web/API/SpeechSynthesisUtterance).
|
||||
|
||||
```js
|
||||
const speechSynthesis = message => {
|
||||
const msg = new SpeechSynthesisUtterance(message);
|
||||
msg.voice = window.speechSynthesis.getVoices()[0];
|
||||
window.speechSynthesis.speak(msg);
|
||||
};
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
speechSynthesis('Hello, World'); // // plays the message
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Node
|
||||
---
|
||||
## 📦 Node
|
||||
|
||||
### JSONToFile
|
||||
|
||||
@ -2998,7 +3045,8 @@ UUIDGeneratorNode(); // '79c7c136-60ee-40a2-beb2-856f1feabefc'
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Object
|
||||
---
|
||||
## 🗃️ Object
|
||||
|
||||
### cleanObj
|
||||
|
||||
@ -3192,6 +3240,40 @@ a === b; // false
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### size
|
||||
|
||||
Get size of arrays, objects or strings.
|
||||
|
||||
Get type of `value` (`array`, `object` or `string`).
|
||||
Use `length` property for arrays.
|
||||
Use `length` or `size` value if available or number of keys for objects.
|
||||
Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `value` for strings.
|
||||
|
||||
Split strings into array of characters with `split('')` and return its length.
|
||||
|
||||
```js
|
||||
const size = value =>
|
||||
Array.isArray(value)
|
||||
? value.length
|
||||
: value && typeof value === 'object'
|
||||
? value.size || value.length || Object.keys(value).length
|
||||
: typeof value === 'string' ? new Blob([value]).size : 0;
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
size([1, 2, 3, 4, 5]); // 5
|
||||
size('size'); // 4
|
||||
size({ one: 1, two: 2, three: 3 }); // 3
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### truthCheckCollection
|
||||
|
||||
Checks if the predicate (second argument) is truthy on all elements of a collection (first argument).
|
||||
@ -3213,7 +3295,8 @@ truthCheckCollection([{ user: 'Tinky-Winky', sex: 'male' }, { user: 'Dipsy', sex
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## String
|
||||
---
|
||||
## 📜 String
|
||||
|
||||
### anagrams
|
||||
|
||||
@ -3632,13 +3715,12 @@ Break the string into words and combine them using `_` as a separator.
|
||||
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
||||
|
||||
```js
|
||||
const toSnakeCase = str => {
|
||||
const toSnakeCase = str =>
|
||||
str &&
|
||||
str
|
||||
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||
.map(x => x.toLowerCase())
|
||||
.join('_');
|
||||
};
|
||||
str
|
||||
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||
.map(x => x.toLowerCase())
|
||||
.join('_');
|
||||
```
|
||||
|
||||
<details>
|
||||
@ -3737,7 +3819,8 @@ words('python, javaScript & coffee'); // ["python", "javaScript", "coffee"]
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
## Utility
|
||||
---
|
||||
## 💎 Utility
|
||||
|
||||
### coalesce
|
||||
|
||||
@ -3947,6 +4030,29 @@ isFunction(x => x); // true
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### isNull
|
||||
|
||||
Returns `true` if the specified value is `null`, `false` otherwise.
|
||||
|
||||
Use the strict equality operator to check if the value and of `val` are equal to `null`.
|
||||
|
||||
```js
|
||||
const isNull = val => val === null;
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
isNull(null); // true
|
||||
isNull('null'); // false
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### isNumber
|
||||
|
||||
Checks if the given argument is a number.
|
||||
@ -4199,6 +4305,33 @@ validateNumber('10'); // true
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
### yesNo
|
||||
|
||||
Returns `true` if the string is `y`/`yes` or `false` if the string is `n`/`no`.
|
||||
|
||||
Use `RegExp.test()` to check if the string evaluates to `y/yes` or `n/no`.
|
||||
Omit the second argument, `def` to set the default answer as `no`.
|
||||
|
||||
```js
|
||||
const yesNo = (val, def = false) =>
|
||||
/^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
|
||||
```
|
||||
|
||||
<details>
|
||||
<summary>Examples</summary>
|
||||
|
||||
```js
|
||||
yesNo('Y'); // true
|
||||
yesNo('yes'); // true
|
||||
yesNo('No'); // false
|
||||
yesNo('Foo', true); // true
|
||||
```
|
||||
|
||||
</details>
|
||||
|
||||
<br>[⬆ Back to top](#table-of-contents)
|
||||
|
||||
|
||||
## 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
122
scripts/build.js
122
scripts/build.js
@ -3,51 +3,64 @@
|
||||
Run using `npm run builder`.
|
||||
*/
|
||||
// Load modules
|
||||
const fs = require('fs-extra'),
|
||||
path = require('path'),
|
||||
chalk = require('chalk');
|
||||
// Set variables for paths
|
||||
const snippetsPath = './snippets',
|
||||
staticPartsPath = './static-parts';
|
||||
// Set variables for script
|
||||
let snippets = {},
|
||||
startPart = '',
|
||||
const fs = require('fs-extra');
|
||||
const path = require('path');
|
||||
const chalk = require('chalk');
|
||||
|
||||
const SNIPPETS_PATH = './snippets';
|
||||
const STATIC_PARTS_PATH = './static-parts';
|
||||
|
||||
const snippets = {};
|
||||
const EMOJIS = {
|
||||
adapter: '🔌',
|
||||
array: '📚',
|
||||
browser: '🌐',
|
||||
date: '⏱️',
|
||||
function: '🎛️',
|
||||
logic: '🔮',
|
||||
math: '➗',
|
||||
media: '📺',
|
||||
node: '📦',
|
||||
object: '🗃️',
|
||||
string: '📜',
|
||||
utility: '💎'
|
||||
};
|
||||
|
||||
let startPart = '',
|
||||
endPart = '',
|
||||
output = '',
|
||||
tagDbData = {};
|
||||
|
||||
// Load helper functions (these are from existing snippets in 30 seconds of code!)
|
||||
const objectFromPairs = arr => arr.reduce((a, v) => ((a[v[0]] = v[1]), a), {});
|
||||
const capitalize = (str, lowerRest = false) =>
|
||||
str.slice(0, 1).toUpperCase() + (lowerRest ? str.slice(1).toLowerCase() : str.slice(1));
|
||||
// Start the timer of the script
|
||||
|
||||
console.time('Builder');
|
||||
|
||||
// Synchronously read all snippets and sort them as necessary (case-insensitive)
|
||||
try {
|
||||
let snippetFilenames = fs.readdirSync(snippetsPath);
|
||||
snippetFilenames.sort((a, b) => {
|
||||
a = a.toLowerCase();
|
||||
b = b.toLowerCase();
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
});
|
||||
const snippetFilenames = fs
|
||||
.readdirSync(SNIPPETS_PATH)
|
||||
.sort((a, b) => a.toLowerCase() - b.toLowerCase());
|
||||
// Store the data read from each snippet in the appropriate object
|
||||
for (let snippet of snippetFilenames)
|
||||
snippets[snippet] = fs.readFileSync(path.join(snippetsPath, snippet), 'utf8');
|
||||
for (const name of snippetFilenames) {
|
||||
snippets[name] = fs.readFileSync(path.join(SNIPPETS_PATH, name), 'utf8');
|
||||
}
|
||||
} catch (err) {
|
||||
// Handle errors (hopefully not!)
|
||||
console.log(`${chalk.red('ERROR!')} During snippet loading: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Load static parts for the README file
|
||||
try {
|
||||
startPart = fs.readFileSync(path.join(staticPartsPath, 'README-start.md'), 'utf8');
|
||||
endPart = fs.readFileSync(path.join(staticPartsPath, 'README-end.md'), 'utf8');
|
||||
startPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-start.md'), 'utf8');
|
||||
endPart = fs.readFileSync(path.join(STATIC_PARTS_PATH, 'README-end.md'), 'utf8');
|
||||
} catch (err) {
|
||||
// Handle errors (hopefully not!)
|
||||
console.log(`${chalk.red('ERROR!')} During static part loading: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Load tag data from the database
|
||||
try {
|
||||
tagDbData = objectFromPairs(
|
||||
@ -58,48 +71,62 @@ try {
|
||||
.map(v => v.split(':').slice(0, 2))
|
||||
);
|
||||
} catch (err) {
|
||||
// Handle errors (hopefully not!)
|
||||
console.log(`${chalk.red('ERROR!')} During tag database loading: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
// Create the output for the README file
|
||||
try {
|
||||
const tags = [
|
||||
...new Set(
|
||||
Object.entries(tagDbData)
|
||||
.map(t => t[1])
|
||||
.filter(v => v)
|
||||
.sort((a, b) => a.localeCompare(b))
|
||||
)
|
||||
];
|
||||
|
||||
// Add the start static part
|
||||
output += `${startPart + '\n'}`;
|
||||
// Loop over tags and snippets to create the table of contents
|
||||
let uncategorizedOutput = '';
|
||||
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1]))]
|
||||
.filter(v => v)
|
||||
.sort((a, b) => a.localeCompare(b))) {
|
||||
if (capitalize(tag, true) == 'Uncategorized') {
|
||||
uncategorizedOutput += `### _${capitalize(
|
||||
tag,
|
||||
true
|
||||
)}_\n\n<details>\n<summary>View contents</summary>\n\n`;
|
||||
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag))
|
||||
|
||||
// Loop over tags and snippets to create the table of contents
|
||||
for (const tag of tags) {
|
||||
const capitalizedTag = capitalize(tag, true);
|
||||
|
||||
if (capitalizedTag === 'Uncategorized') {
|
||||
uncategorizedOutput += `### _${capitalizedTag}_\n\n<details>\n<summary>View contents</summary>\n\n`;
|
||||
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) {
|
||||
uncategorizedOutput += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()})\n`;
|
||||
}
|
||||
uncategorizedOutput += '\n</details>\n\n';
|
||||
} else {
|
||||
output += `### ${capitalize(tag, true)}\n\n<details>\n<summary>View contents</summary>\n\n`;
|
||||
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag))
|
||||
output += `### ${
|
||||
EMOJIS[tag] || ''
|
||||
} ${capitalizedTag}\n\n<details>\n<summary>View contents</summary>\n\n`;
|
||||
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) {
|
||||
output += `* [\`${taggedSnippet[0]}\`](#${taggedSnippet[0].toLowerCase()})\n`;
|
||||
}
|
||||
output += '\n</details>\n\n';
|
||||
}
|
||||
}
|
||||
|
||||
output += uncategorizedOutput;
|
||||
uncategorizedOutput = '';
|
||||
|
||||
// Loop over tags and snippets to create the list of snippets
|
||||
for (let tag of [...new Set(Object.entries(tagDbData).map(t => t[1]))]
|
||||
.filter(v => v)
|
||||
.sort((a, b) => a.localeCompare(b))) {
|
||||
if (capitalize(tag, true) == 'Uncategorized') {
|
||||
uncategorizedOutput += `## _${capitalize(tag, true)}_\n`;
|
||||
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag))
|
||||
for (const tag of tags) {
|
||||
const capitalizedTag = capitalize(tag, true);
|
||||
|
||||
if (capitalizedTag == 'Uncategorized') {
|
||||
uncategorizedOutput += `---\n ## _${capitalizedTag}_\n`;
|
||||
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) {
|
||||
uncategorizedOutput += `\n${snippets[taggedSnippet[0] + '.md'] +
|
||||
'\n<br>[⬆ back to top](#table-of-contents)\n\n'}`;
|
||||
}
|
||||
} else {
|
||||
output += `## ${capitalize(tag, true)}\n`;
|
||||
for (let taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) {
|
||||
output += `---\n ## ${EMOJIS[tag] || ''} ${capitalizedTag}\n`;
|
||||
for (const taggedSnippet of Object.entries(tagDbData).filter(v => v[1] === tag)) {
|
||||
let data = snippets[taggedSnippet[0] + '.md'];
|
||||
data =
|
||||
data.slice(0, data.lastIndexOf('```js')) +
|
||||
@ -111,17 +138,16 @@ try {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
output += uncategorizedOutput;
|
||||
// Add the ending static part
|
||||
output += `\n${endPart + '\n'}`;
|
||||
// Write to the README file
|
||||
fs.writeFileSync('README.md', output);
|
||||
} catch (err) {
|
||||
// Handle errors (hopefully not!)
|
||||
console.log(`${chalk.red('ERROR!')} During README generation: ${err}`);
|
||||
process.exit(1);
|
||||
}
|
||||
// Log a success message
|
||||
|
||||
console.log(`${chalk.green('SUCCESS!')} README file generated!`);
|
||||
// Log the time taken
|
||||
console.timeEnd('Builder');
|
||||
|
||||
33
snippets/copyToClipboard.md
Normal file
33
snippets/copyToClipboard.md
Normal file
@ -0,0 +1,33 @@
|
||||
### copyToClipboard
|
||||
|
||||
Copy a string to the clipboard. Only works as a result of user action (i.e. inside a `click` event listener).
|
||||
|
||||
Create a new `<textarea>` element, fill it with the supplied data and add it to the HTML document.
|
||||
Use `Selection.getRangeAt()`to store the selected range (if any).
|
||||
Use `document.execCommand('copy')` to copy to the clipboard.
|
||||
Remove the `<textarea>` element from the HTML document.
|
||||
Finally, use `Selection().addRange()` to recover the original selected range (if any).
|
||||
|
||||
```js
|
||||
const copyToClipboard = str => {
|
||||
const el = document.createElement('textarea');
|
||||
el.value = str;
|
||||
el.setAttribute('readonly', '');
|
||||
el.style.position = 'absolute';
|
||||
el.style.left = '-9999px';
|
||||
document.body.appendChild(el);
|
||||
const selected =
|
||||
document.getSelection().rangeCount > 0 ? document.getSelection().getRangeAt(0) : false;
|
||||
el.select();
|
||||
document.execCommand('copy');
|
||||
document.body.removeChild(el);
|
||||
if (selected) {
|
||||
document.getSelection().removeAllRanges();
|
||||
document.getSelection().addRange(selected);
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
copyToClipboard('Lorem ipsum'); // 'Lorem ipsum' copied to clipboard.
|
||||
```
|
||||
@ -17,5 +17,5 @@ const fibonacciUntilNum = num => {
|
||||
```
|
||||
|
||||
```js
|
||||
fibonacciCountUntilNum(10); // 7
|
||||
fibonacciUntilNum(10); // [ 0, 1, 1, 2, 3, 5, 8 ]
|
||||
```
|
||||
|
||||
14
snippets/isNull.md
Normal file
14
snippets/isNull.md
Normal file
@ -0,0 +1,14 @@
|
||||
### isNull
|
||||
|
||||
Returns `true` if the specified value is `null`, `false` otherwise.
|
||||
|
||||
Use the strict equality operator to check if the value and of `val` are equal to `null`.
|
||||
|
||||
```js
|
||||
const isNull = val => val === null;
|
||||
```
|
||||
|
||||
```js
|
||||
isNull(null); // true
|
||||
isNull('null'); // false
|
||||
```
|
||||
@ -8,7 +8,7 @@ Return `false` if any of them divides the given number, else return `true`, unle
|
||||
```js
|
||||
const isPrime = num => {
|
||||
const boundary = Math.floor(Math.sqrt(num));
|
||||
for (var i = 2; i * i <= boundary; i++) if (num % i == 0) return false;
|
||||
for (var i = 2; i <= boundary; i++) if (num % i == 0) return false;
|
||||
return num >= 2;
|
||||
};
|
||||
```
|
||||
|
||||
25
snippets/size.md
Normal file
25
snippets/size.md
Normal file
@ -0,0 +1,25 @@
|
||||
### size
|
||||
|
||||
Get size of arrays, objects or strings.
|
||||
|
||||
Get type of `value` (`array`, `object` or `string`).
|
||||
Use `length` property for arrays.
|
||||
Use `length` or `size` value if available or number of keys for objects.
|
||||
Use `size` of a [`Blob` object](https://developer.mozilla.org/en-US/docs/Web/API/Blob) created from `value` for strings.
|
||||
|
||||
Split strings into array of characters with `split('')` and return its length.
|
||||
|
||||
```js
|
||||
const size = value =>
|
||||
Array.isArray(value)
|
||||
? value.length
|
||||
: value && typeof value === 'object'
|
||||
? value.size || value.length || Object.keys(value).length
|
||||
: typeof value === 'string' ? new Blob([value]).size : 0;
|
||||
```
|
||||
|
||||
```js
|
||||
size([1, 2, 3, 4, 5]); // 5
|
||||
size('size'); // 4
|
||||
size({ one: 1, two: 2, three: 3 }); // 3
|
||||
```
|
||||
@ -6,13 +6,12 @@ Break the string into words and combine them using `_` as a separator.
|
||||
For more detailed explanation of this Regex, [visit this Site](https://regex101.com/r/bMCgAB/1).
|
||||
|
||||
```js
|
||||
const toSnakeCase = str => {
|
||||
const toSnakeCase = str =>
|
||||
str &&
|
||||
str
|
||||
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||
.map(x => x.toLowerCase())
|
||||
.join('_');
|
||||
};
|
||||
str
|
||||
.match(/[A-Z]{2,}(?=[A-Z][a-z]+[0-9]*|\b)|[A-Z]?[a-z]+[0-9]*|[A-Z]|[0-9]+/g)
|
||||
.map(x => x.toLowerCase())
|
||||
.join('_');
|
||||
```
|
||||
|
||||
```js
|
||||
|
||||
18
snippets/yesNo.md
Normal file
18
snippets/yesNo.md
Normal file
@ -0,0 +1,18 @@
|
||||
### yesNo
|
||||
|
||||
Returns `true` if the string is `y`/`yes` or `false` if the string is `n`/`no`.
|
||||
|
||||
Use `RegExp.test()` to check if the string evaluates to `y/yes` or `n/no`.
|
||||
Omit the second argument, `def` to set the default answer as `no`.
|
||||
|
||||
```js
|
||||
const yesNo = (val, def = false) =>
|
||||
/^(y|yes)$/i.test(val) ? true : /^(n|no)$/i.test(val) ? false : def;
|
||||
```
|
||||
|
||||
```js
|
||||
yesNo('Y'); // true
|
||||
yesNo('yes'); // true
|
||||
yesNo('No'); // false
|
||||
yesNo('Foo', true); // true
|
||||
```
|
||||
@ -9,6 +9,8 @@
|
||||
- Use <kbd>Ctrl</kbd> + <kbd>F</kbd> or <kbd>command</kbd> + <kbd>F</kbd> to search for a snippet.
|
||||
- Contributions welcome, please read the [contribution guide](CONTRIBUTING.md).
|
||||
- Snippets are written in ES6, use the [Babel transpiler](https://babeljs.io/) to ensure backwards-compatibility.
|
||||
- You can import these snippets into your text editor of choice (VSCode, Atom, Sublime) using the files found in [this repo](https://github.com/Rob-Rychs/30-seconds-of-code-texteditorsnippets).
|
||||
- You can import these snippets into Alfred 3, using [this file](https://github.com/lslvxy/30-seconds-of-code-alfredsnippets).
|
||||
- You can find a package with all the snippets on [npm](https://www.npmjs.com/package/tsoc). Bear in mind that most of these snippets are not production-ready.
|
||||
|
||||
## Table of Contents
|
||||
|
||||
@ -16,6 +16,7 @@ collatz:math
|
||||
collectInto:adapter
|
||||
compact:array
|
||||
compose:function
|
||||
copyToClipboard:browser
|
||||
countOccurrences:array
|
||||
countVowels:string
|
||||
currentURL:browser
|
||||
@ -69,6 +70,7 @@ isBoolean:utility
|
||||
isDivisible:math
|
||||
isEven:math
|
||||
isFunction:utility
|
||||
isNull:utility
|
||||
isNumber:utility
|
||||
isPrime:math
|
||||
isString:utility
|
||||
@ -119,9 +121,10 @@ shallowClone:object
|
||||
show:browser
|
||||
shuffle:array
|
||||
similarity:array
|
||||
size:object
|
||||
sleep:function
|
||||
sortCharactersInString:string
|
||||
speechSynthesis:media
|
||||
speechSynthesis:browser
|
||||
splitLines:string
|
||||
spreadOver:adapter
|
||||
standardDeviation:math
|
||||
@ -148,5 +151,6 @@ UUIDGeneratorNode:node
|
||||
validateNumber:utility
|
||||
without:array
|
||||
words:string
|
||||
yesNo:utility
|
||||
zip:array
|
||||
zipObject:array
|
||||
|
||||
Reference in New Issue
Block a user