diff --git a/README.md b/README.md index fc64e5408..b3607ac90 100644 --- a/README.md +++ b/README.md @@ -112,6 +112,7 @@ average(1, 2, 3); * [`initial`](#initial) * [`initialize2DArray`](#initialize2darray) * [`initializeArrayWithRange`](#initializearraywithrange) +* [`initializeArrayWithRangeRight`](#initializearraywithrangeright) * [`initializeArrayWithValues`](#initializearraywithvalues) * [`intersection`](#intersection) * [`isSorted`](#issorted) @@ -329,6 +330,7 @@ average(1, 2, 3); * [`isBoolean`](#isboolean) * [`isFunction`](#isfunction) * [`isMap`](#ismap) +* [`isNil`](#isnil) * [`isNull`](#isnull) * [`isNumber`](#isnumber) * [`isObject`](#isobject) @@ -339,6 +341,7 @@ average(1, 2, 3); * [`isString`](#isstring) * [`isSymbol`](#issymbol) * [`isTypedArray`](#istypedarray) +* [`isUndefined`](#isundefined) * [`isValidJSON`](#isvalidjson) * [`isWeakMap`](#isweakmap) * [`isWeakSet`](#isweakset) @@ -1024,7 +1027,7 @@ initialize2DArray(2, 2, 0); // [[0,0], [0,0]] ### 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`. @@ -1049,6 +1052,35 @@ initializeArrayWithRange(9, 0, 2); // [0,2,4,6,8]
[⬆ Back to top](#table-of-contents) +### 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 + ); +``` + +
+Examples + +```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] +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### initializeArrayWithValues Initializes and fills an array with the specified values. @@ -5277,6 +5309,29 @@ isMap(new Map()); // true
[⬆ Back to top](#table-of-contents) +### 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; +``` + +
+Examples + +```js +isNil(null); // true +isNil(undefined); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isNull Returns `true` if the specified value is `null`, `false` otherwise. @@ -5520,6 +5575,28 @@ isTypedArray(new TypedArray()); // true
[⬆ Back to top](#table-of-contents) +### 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; +``` + +
+Examples + +```js +isUndefined(undefined); // true +``` + +
+ +
[⬆ Back to top](#table-of-contents) + + ### isValidJSON Checks if the provided argument is a valid JSON. diff --git a/docs/index.html b/docs/index.html index 70ea19b9d..35d97f198 100644 --- a/docs/index.html +++ b/docs/index.html @@ -50,7 +50,7 @@ scrollToTop(); } }, false); - }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
+      }

logo 30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.

 

Adapter

call

Given a key and a set of arguments, call them when given a context. Primarily useful in composition.

Use a closure to call a stored key with stored arguments.

const call = (key, ...args) => context => context[key](...args);
 
Promise.resolve([1, 2, 3])
   .then(call('map', x => 2 * x))
   .then(console.log); //[ 2, 4, 6 ]
@@ -161,11 +161,18 @@ Object.assig
 

initialize2DArray

Initializes a 2D array of given width and height and value.

Use Array.map() to generate h rows where each is a new array of size w initialize with value. If the value is not provided, default to null.

const initialize2DArray = (w, h, val = null) =>
   Array.from({ length: h }).map(() => Array.from({ length: w }).fill(val));
 
initialize2DArray(2, 2, 0); // [[0,0], [0,0]]
-

initializeArrayWithRange

Initializes an array containing the numbers in the specified range where start and end are inclusive with there 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.

const initializeArrayWithRange = (end, start = 0, step = 1) =>
+

initializeArrayWithRange

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. You can omit step to use a default value of 1.

const initializeArrayWithRange = (end, start = 0, step = 1) =>
   Array.from({ length: Math.ceil((end + 1 - start) / step) }).map((v, i) => i * step + start);
 
initializeArrayWithRange(5); // [0,1,2,3,4,5]
 initializeArrayWithRange(7, 3); // [3,4,5,6,7]
 initializeArrayWithRange(9, 0, 2); // [0,2,4,6,8]
+

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.

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
+  );
+
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]
 

initializeArrayWithValues

Initializes and fills an array with the specified values.

Use Array(n) to create an array of the desired length, fill(v) to fill it with the desired values. You can omit val to use a default value of 0.

const initializeArrayWithValues = (n, val = 0) => Array(n).fill(val);
 
initializeArrayWithValues(5, 2); // [2,2,2,2,2]
 

intersection

Returns a list of elements that exist in both arrays.

Create a Set from b, then use Array.filter() on a to only keep values contained in b.

const intersection = (a, b) => {
@@ -1186,6 +1193,9 @@ Foo.prototypeisFunction(x => x); // true
 

isMap

Checks if value is classified as a Map object.

Use the instanceofoperator to check if the provided value is a Map object.

const isMap = val => val instanceof Map;
 
isMap(new Map()); // true
+

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.

const isNil = val => val === undefined || val === null;
+
isNil(null); // true
+isNil(undefined); // true
 

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.

const isNull = val => val === null;
 
isNull(null); // true
 

isNumber

Checks if the given argument is a number.

Use typeof to check if a value is classified as a number primitive.

const isNumber = val => typeof val === 'number';
@@ -1226,6 +1236,8 @@ Foo.prototypeShow examples
isSymbol(Symbol('x')); // true
 

isTypedArray

Checks if value is classified as a TypedArray object.

Use the instanceofoperator to check if the provided value is a TypedArray object.

const isTypedArray = val => val instanceof TypedArray;
 
isTypedArray(new TypedArray()); // true
+

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.

const isUndefined = val => val === undefined;
+
isUndefined(undefined); // true
 

isValidJSON

Checks if the provided argument is a valid JSON.

Use JSON.parse() and a try... catch block to check if the provided argument is a valid JSON.

const isValidJSON = obj => {
   try {
     JSON.parse(obj);
diff --git a/snippets/initializeArrayWithRangeRight.md b/snippets/initializeArrayWithRangeRight.md
index 0b73c0197..608352c4d 100644
--- a/snippets/initializeArrayWithRangeRight.md
+++ b/snippets/initializeArrayWithRangeRight.md
@@ -8,7 +8,9 @@ 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);
+  Array.from({ length: Math.ceil((end + 1 - start) / step) }).map(
+    (v, i, arr) => (arr.length - i - 1) * step + start
+  );
 ```
 
 ```js