Merge branch 'master' into urlJoin
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
### initializeArrayWithRange
|
||||
|
||||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with there common difference `step`.
|
||||
Initializes an array containing the numbers in the specified range where `start` and `end` are inclusive with their common difference `step`.
|
||||
|
||||
Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range.
|
||||
You can omit `start` to use a default value of `0`.
|
||||
|
||||
20
snippets/initializeArrayWithRangeRight.md
Normal file
20
snippets/initializeArrayWithRangeRight.md
Normal file
@ -0,0 +1,20 @@
|
||||
### initializeArrayWithRangeRight
|
||||
|
||||
Initializes an array containing the numbers in the specified range (in reverse) where `start` and `end` are inclusive with their common difference `step`.
|
||||
|
||||
Use `Array.from(Math.ceil((end+1-start)/step))` to create an array of the desired length(the amounts of elements is equal to `(end-start)/step` or `(end+1-start)/step` for inclusive end), `Array.map()` to fill with the desired values in a range.
|
||||
You can omit `start` to use a default value of `0`.
|
||||
You can omit `step` to use a default value of `1`.
|
||||
|
||||
```js
|
||||
const initializeArrayWithRangeRight = (end, start = 0, step = 1) =>
|
||||
Array.from({ length: Math.ceil((end + 1 - start) / step) }).map(
|
||||
(v, i, arr) => (arr.length - i - 1) * step + start
|
||||
);
|
||||
```
|
||||
|
||||
```js
|
||||
initializeArrayWithRangeRight(5); // [5,4,3,2,1,0]
|
||||
initializeArrayWithRangeRight(7, 3); // [7,6,5,4,3]
|
||||
initializeArrayWithRangeRight(9, 0, 2); // [8,6,4,2,0]
|
||||
```
|
||||
13
snippets/isArrayBuffer.md
Normal file
13
snippets/isArrayBuffer.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isArrayBuffer
|
||||
|
||||
Checks if value is classified as a ArrayBuffer object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `ArrayBuffer` object.
|
||||
|
||||
```js
|
||||
const isArrayBuffer = val => val instanceof ArrayBuffer;
|
||||
```
|
||||
|
||||
```js
|
||||
isArrayBuffer(new ArrayBuffer()); // true
|
||||
```
|
||||
13
snippets/isMap.md
Normal file
13
snippets/isMap.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isMap
|
||||
|
||||
Checks if value is classified as a Map object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `Map` object.
|
||||
|
||||
```js
|
||||
const isMap = val => val instanceof Map;
|
||||
```
|
||||
|
||||
```js
|
||||
isMap(new Map()); // true
|
||||
```
|
||||
14
snippets/isNil.md
Normal file
14
snippets/isNil.md
Normal file
@ -0,0 +1,14 @@
|
||||
### isNil
|
||||
|
||||
Returns `true` if the specified value is `null` or `undefined`, `false` otherwise.
|
||||
|
||||
Use the strict equality operator to check if the value and of `val` are equal to `null` or `undefined`.
|
||||
|
||||
```js
|
||||
const isNil = val => val === undefined || val === null;
|
||||
```
|
||||
|
||||
```js
|
||||
isNil(null); // true
|
||||
isNil(undefined); // true
|
||||
```
|
||||
13
snippets/isRegExp.md
Normal file
13
snippets/isRegExp.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isRegExp
|
||||
|
||||
Checks if value is classified as a RegExp object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `RegExp` object.
|
||||
|
||||
```js
|
||||
const isRegExp = val => val instanceof RegExp;
|
||||
```
|
||||
|
||||
```js
|
||||
isRegExp(/./g); // true
|
||||
```
|
||||
13
snippets/isSet.md
Normal file
13
snippets/isSet.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isSet
|
||||
|
||||
Checks if value is classified as a Set object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `Set` object.
|
||||
|
||||
```js
|
||||
const isSet = val => val instanceof Set;
|
||||
```
|
||||
|
||||
```js
|
||||
isSet(new Set()); // true
|
||||
```
|
||||
13
snippets/isTypedArray.md
Normal file
13
snippets/isTypedArray.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isTypedArray
|
||||
|
||||
Checks if value is classified as a TypedArray object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `TypedArray` object.
|
||||
|
||||
```js
|
||||
const isTypedArray = val => val instanceof TypedArray;
|
||||
```
|
||||
|
||||
```js
|
||||
isTypedArray(new TypedArray()); // true
|
||||
```
|
||||
13
snippets/isUndefined.md
Normal file
13
snippets/isUndefined.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isUndefined
|
||||
|
||||
Returns `true` if the specified value is `undefined`, `false` otherwise.
|
||||
|
||||
Use the strict equality operator to check if the value and of `val` are equal to `undefined`.
|
||||
|
||||
```js
|
||||
const isUndefined = val => val === undefined;
|
||||
```
|
||||
|
||||
```js
|
||||
isUndefined(undefined); // true
|
||||
```
|
||||
13
snippets/isWeakMap.md
Normal file
13
snippets/isWeakMap.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isWeakMap
|
||||
|
||||
Checks if value is classified as a WeakMap object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `WeakMap` object.
|
||||
|
||||
```js
|
||||
const isWeakMap = val => val instanceof WeakMap;
|
||||
```
|
||||
|
||||
```js
|
||||
isWeakMap(new WeakMap()); // true
|
||||
```
|
||||
13
snippets/isWeakSet.md
Normal file
13
snippets/isWeakSet.md
Normal file
@ -0,0 +1,13 @@
|
||||
### isWeakSet
|
||||
|
||||
Checks if value is classified as a WeakSet object.
|
||||
|
||||
Use the `instanceof`operator to check if the provided value is a `WeakSet` object.
|
||||
|
||||
```js
|
||||
const isWeakSet = val => val instanceof WeakSet;
|
||||
```
|
||||
|
||||
```js
|
||||
isWeakSet(new WeakSet()); // true
|
||||
```
|
||||
33
snippets/observeMutations.md
Normal file
33
snippets/observeMutations.md
Normal file
@ -0,0 +1,33 @@
|
||||
### observeMutations
|
||||
|
||||
Returns a new MutationObserver and runs the provided callback for each mutation on the specified element.
|
||||
|
||||
Use a [`MutationObserver`](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver) to observe mutations on the given element.
|
||||
Use `Array.forEach()` to run the callback for each mutation that is observed.
|
||||
Omit the third argument, `options`, to use the default [options](https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver#MutationObserverInit) (all `true`).
|
||||
|
||||
```js
|
||||
const observeMutations = (element, callback, options) => {
|
||||
const observer = new MutationObserver(mutations => mutations.forEach(m => callback(m)));
|
||||
observer.observe(
|
||||
element,
|
||||
Object.assign(
|
||||
{
|
||||
childList: true,
|
||||
attributes: true,
|
||||
attributeOldValue: true,
|
||||
characterData: true,
|
||||
characterDataOldValue: true,
|
||||
subtree: true
|
||||
},
|
||||
options
|
||||
)
|
||||
);
|
||||
return observer;
|
||||
};
|
||||
```
|
||||
|
||||
```js
|
||||
const obs = observeMutations(document, console.log); // Logs all mutations that happen on the page
|
||||
obs.disconnect(); // Disconnects the observer and stops logging mutations on the page
|
||||
```
|
||||
Reference in New Issue
Block a user