Merge branch 'master' of https://github.com/Chalarangelo/30-seconds-of-code
This commit is contained in:
29
README.md
29
README.md
@ -1,7 +1,7 @@
|
||||

|
||||
|
||||
# 30 seconds of code [](https://gitter.im/30-seconds-of-code/Lobby) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code)
|
||||
> Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
|
||||
> Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
|
||||
|
||||
- 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).
|
||||
@ -154,7 +154,6 @@
|
||||
* [`toDecimalMark`](#todecimalmark)
|
||||
* [`toOrdinalSuffix`](#toordinalsuffix)
|
||||
* [`UUIDGenerator`](#uuidgenerator)
|
||||
* [`validateEmail`](#validateemail)
|
||||
* [`validateNumber`](#validatenumber)
|
||||
|
||||
## Array
|
||||
@ -243,7 +242,7 @@ Removes falsey values from an array.
|
||||
Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||
|
||||
```js
|
||||
const compact = (arr) => arr.filter(Boolean);
|
||||
const compact = arr => arr.filter(Boolean);
|
||||
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
|
||||
@ -355,8 +354,8 @@ Returns every nth element in an array.
|
||||
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
||||
|
||||
```js
|
||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
|
||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
@ -1385,12 +1384,11 @@ Returns `false` if the provided number has positive divisors other than 1 and it
|
||||
|
||||
```js
|
||||
const isPrime = num => {
|
||||
for (var i = 2; i < num; i++) if (num % i == 0) return false;
|
||||
for (var i = 2; i * i <= num; i++) if (num % i == 0) return false;
|
||||
return num >= 2;
|
||||
}
|
||||
};
|
||||
// isPrime(11) -> true
|
||||
// isPrime(12) -> false
|
||||
// isPrime(1) -> false
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
@ -2167,21 +2165,6 @@ const UUIDGenerator = () =>
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
### validateEmail
|
||||
|
||||
Returns `true` if the given string is a valid email, `false` otherwise.
|
||||
|
||||
Use a regular expression to check if the email is valid.
|
||||
Returns `true` if email is valid, `false` if not.
|
||||
|
||||
```js
|
||||
const validateEmail = str =>
|
||||
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
|
||||
// validateEmail(mymail@gmail.com) -> true
|
||||
```
|
||||
|
||||
[⬆ back to top](#table-of-contents)
|
||||
|
||||
### validateNumber
|
||||
|
||||
Returns `true` if the given value is a number, `false` otherwise.
|
||||
|
||||
@ -185,7 +185,6 @@
|
||||
<a class="sublink-1" href="#todecimalmark">toDecimalMark</a>
|
||||
<a class="sublink-1" href="#toordinalsuffix">toOrdinalSuffix</a>
|
||||
<a class="sublink-1" href="#uuidgenerator">UUIDGenerator</a>
|
||||
<a class="sublink-1" href="#validateemail">validateEmail</a>
|
||||
<a class="sublink-1" href="#validatenumber">validateNumber</a>
|
||||
|
||||
</nav><main class="col-sm-12 col-md-8 col-lg-9" style="height:100%;overflow-y:auto;background:#eceef2;padding:0"><a id="top"> </a><h2 style="text-align:center">Array</h2>
|
||||
@ -234,7 +233,7 @@ If the original array can't be split evenly, the final chunk will contain the re
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="compact">compact</h3></div><div class="section double-padded">
|
||||
<p>Removes falsey values from an array.</p>
|
||||
<p>Use <code>Array.filter()</code> to filter out falsey values (<code>false</code>, <code>null</code>, <code>0</code>, <code>""</code>, <code>undefined</code>, and <code>NaN</code>).</p>
|
||||
<pre><code class="language-js">const compact = (arr) => arr.filter(Boolean);
|
||||
<pre><code class="language-js">const compact = arr => arr.filter(Boolean);
|
||||
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="countoccurrences">countOccurrences</h3></div><div class="section double-padded">
|
||||
@ -290,8 +289,8 @@ Returns the remaining elements.</p>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="everynth">everyNth</h3></div><div class="section double-padded">
|
||||
<p>Returns every nth element in an array.</p>
|
||||
<p>Use <code>Array.filter()</code> to create a new array that contains every nth element of a given array.</p>
|
||||
<pre><code class="language-js">const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
|
||||
<pre><code class="language-js">const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="filternonunique">filterNonUnique</h3></div><div class="section double-padded">
|
||||
<p>Filters out the non-unique values in an array.</p>
|
||||
@ -869,12 +868,11 @@ Returns <code>true</code> if the number is even, <code>false</code> if the numbe
|
||||
<p>Checks if the provided integer is a prime number.</p>
|
||||
<p>Returns <code>false</code> if the provided number has positive divisors other than 1 and itself or if the number itself is less than 2.</p>
|
||||
<pre><code class="language-js">const isPrime = num => {
|
||||
for (var i = 2; i < num; i++) if (num % i == 0) return false;
|
||||
for (var i = 2; i * i <= num; i++) if (num % i == 0) return false;
|
||||
return num >= 2;
|
||||
}
|
||||
};
|
||||
// isPrime(11) -> true
|
||||
// isPrime(12) -> false
|
||||
// isPrime(1) -> false
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="lcm">lcm</h3></div><div class="section double-padded">
|
||||
<p>Returns the least common multiple of two numbers.</p>
|
||||
@ -1318,14 +1316,6 @@ If digit is found in teens pattern, use teens ordinal.</p>
|
||||
);
|
||||
// UUIDGenerator() -> '7982fcfe-5721-4632-bede-6000885be57d'
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="validateemail">validateEmail</h3></div><div class="section double-padded">
|
||||
<p>Returns <code>true</code> if the given string is a valid email, <code>false</code> otherwise.</p>
|
||||
<p>Use a regular expression to check if the email is valid.
|
||||
Returns <code>true</code> if email is valid, <code>false</code> if not.</p>
|
||||
<pre><code class="language-js">const validateEmail = str =>
|
||||
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
|
||||
// validateEmail(mymail@gmail.com) -> true
|
||||
</code></pre>
|
||||
</div></div><br/><div class="card fluid"><div class="section double-padded"><h3 id="validatenumber">validateNumber</h3></div><div class="section double-padded">
|
||||
<p>Returns <code>true</code> if the given value is a number, <code>false</code> otherwise.</p>
|
||||
<p>Use <code>!isNaN</code> in combination with <code>parseFloat()</code> to check if the argument is a number.
|
||||
|
||||
@ -5,6 +5,6 @@ Removes falsey values from an array.
|
||||
Use `Array.filter()` to filter out falsey values (`false`, `null`, `0`, `""`, `undefined`, and `NaN`).
|
||||
|
||||
```js
|
||||
const compact = (arr) => arr.filter(Boolean);
|
||||
const compact = arr => arr.filter(Boolean);
|
||||
// compact([0, 1, false, 2, '', 3, 'a', 'e'*23, NaN, 's', 34]) -> [ 1, 2, 3, 'a', 's', 34 ]
|
||||
```
|
||||
|
||||
@ -5,6 +5,6 @@ Returns every nth element in an array.
|
||||
Use `Array.filter()` to create a new array that contains every nth element of a given array.
|
||||
|
||||
```js
|
||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === 0);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 1, 3, 5 ]
|
||||
const everyNth = (arr, nth) => arr.filter((e, i) => i % nth === nth - 1);
|
||||
// everyNth([1,2,3,4,5,6], 2) -> [ 2, 4, 6 ]
|
||||
```
|
||||
|
||||
@ -6,10 +6,9 @@ Returns `false` if the provided number has positive divisors other than 1 and it
|
||||
|
||||
```js
|
||||
const isPrime = num => {
|
||||
for (var i = 2; i < num; i++) if (num % i == 0) return false;
|
||||
for (var i = 2; i * i <= num; i++) if (num % i == 0) return false;
|
||||
return num >= 2;
|
||||
}
|
||||
};
|
||||
// isPrime(11) -> true
|
||||
// isPrime(12) -> false
|
||||
// isPrime(1) -> false
|
||||
```
|
||||
|
||||
@ -1,12 +0,0 @@
|
||||
### validateEmail
|
||||
|
||||
Returns `true` if the given string is a valid email, `false` otherwise.
|
||||
|
||||
Use a regular expression to check if the email is valid.
|
||||
Returns `true` if email is valid, `false` if not.
|
||||
|
||||
```js
|
||||
const validateEmail = str =>
|
||||
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(str);
|
||||
// validateEmail(mymail@gmail.com) -> true
|
||||
```
|
||||
@ -3,6 +3,7 @@
|
||||
# 30 seconds of code [](https://gitter.im/30-seconds-of-code/Lobby) [](https://travis-ci.org/Chalarangelo/30-seconds-of-code) <a data-role="dummy" href="https://insight.io/github.com/Chalarangelo/30-seconds-of-code?source=0" title="View this repo on Insight.io" target="_blank"><svg xmlns="http://www.w3.org/2000/svg" width="104" height="20"><linearGradient id="b" x2="0" y2="100%"><stop offset="0" stop-color="#bbb" stop-opacity=".1"></stop><stop offset="1" stop-opacity=".1"></stop></linearGradient><clipPath id="a"><rect width="104" height="20" rx="3" fill="#fff"></rect></clipPath><g clip-path="url(#a)"><path fill="#555" d="M0 0h61v20H0z"></path><path fill="#4c1" d="M61 0h43v20H61z"></path><path fill="url(#b)" d="M0 0h104v20H0z"></path></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="11"><text x="30.5" y="15" fill="#010101" fill-opacity=".3">insight.io</text><text x="30.5" y="14">insight.io</text><text x="81.5" y="15" fill="#010101" fill-opacity=".3">Ready</text><text x="81.5" y="14">Ready</text></g></svg></a>
|
||||
> Curated collection of useful Javascript snippets that you can understand in 30 seconds or less.
|
||||
|
||||
|
||||
- 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.
|
||||
|
||||
@ -119,7 +119,6 @@ truncateString:string
|
||||
truthCheckCollection:object
|
||||
union:array
|
||||
UUIDGenerator:utility
|
||||
validateEmail:utility
|
||||
validateNumber:utility
|
||||
without:array
|
||||
words:string
|
||||
|
||||
Reference in New Issue
Block a user