Merge branch 'patch-1' of https://github.com/kriadmin/30-seconds-of-code
This commit is contained in:
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
|
||||
```
|
||||
Reference in New Issue
Block a user