This commit is contained in:
Randy8080 committed 2022-10-18 11:37:51 +08:00
1 parent 7fc7945881
commit 098238ad13
286 files changed
+8596

No files matched your search

@@ -0,0 +1,25 @@
---
title: How to check if a date is a weekday in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a date is a weekday only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isWeekday = (date = new Date()) => date.getDay() % 6 !== 0;
```
Parameters `date` is a Date object.
@@ -0,0 +1,25 @@
---
title: How to check if a date is a weekend in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a date is a weekend only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isWeekend = (date = new Date()) => date.getDay() % 6 === 0;
```
Parameters `date` is a Date object.
@@ -0,0 +1,25 @@
---
title: How to check if a date is between two dates in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a date is between two dates only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isBetween = (date, min, max) => (date.getTime() >= min.getTime() && date.getTime() <= max.getTime());
```
Parameters `min`, `max` and `date` are `Date` instances.
@@ -0,0 +1,25 @@
---
title: How to check if a date is today in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a date is today only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isToday = (date) => date.toISOString().slice(0, 10) === new Date().toISOString().slice(0, 10);
```
Parameters `date` is a Date object.
@@ -0,0 +1,25 @@
---
title: How to check if a date occurs in the current year in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a date occurs in the current year only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isCurrentYear = (date) => date.getUTCFullYear() === new Date().getUTCFullYear();
```
Parameters `date` is a Date object.
@@ -0,0 +1,29 @@
---
title: How to check if a flat array has duplicate values in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a flat array has duplicate values only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const hasDuplicateValues = arr => new Set(arr).size !== arr.length;
```
### Example
```js {.wrap}
hasDuplicateValues(['h', 'e', 'l', 'l', 'o']); // true
hasDuplicateValues(['w', 'o', 'r', 'd']); // false
```
@@ -0,0 +1,22 @@
---
title: How to check if a given integer is a prime number in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a given integer is a prime number only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPrime = num => (num > 1) && Array(Math.floor(Math.sqrt(num)) - 1).fill(0).map((_, i) => i + 2).every(i => num % i !== 0);
```
@@ -0,0 +1,29 @@
---
title: How to check if a number is a power of 2 in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is a power of 2 only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPowerOfTwo = number => (number & (number - 1)) === 0;
```
### Example
```js {.wrap}
isPowerOfTwo(256); // true
isPowerOfTwo(129); // false
```
@@ -0,0 +1,47 @@
---
title: How to check if a number is even in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is even only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isEven = number => number % 2 === 0;
```
### Or
```js {.wrap}
const isEven = number => (number & 1) === 0;
```
### Or
```js {.wrap}
const isEven = number => !(number & 1);
```
### Or
```js {.wrap}
const isEven = number => Number.isInteger(number / 2);
```
### Example
```js {.wrap}
isEven(1); // false
isEven(2); // true
```
@@ -0,0 +1,31 @@
---
title: How to check if a number is in a given range in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is in a given range only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const inRange = (num, a, b, threshold = 0) => (Math.min(a, b) - threshold <= num && num <= Math.max(a, b) + threshold);
```
### Example
```js {.wrap}
inRange(10, 5, 15); // true
inRange(10, 5, 6); // false
inRange(10, 15, 5); // true
inRange(-10, -5, -15); // true
```
@@ -0,0 +1,35 @@
---
title: How to check if a number is negative in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is negative only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isNegative = number => Math.sign(number) === -1;
```
### Or
```js {.wrap}
const isNegative = number => number < 0;
```
### Example
```js {.wrap}
isNegative(-3); // true
isNegative(8); // false
```
@@ -0,0 +1,41 @@
---
title: How to check if a number is odd in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is odd only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isOdd = number => number % 2 !== 0;
```
### Or
```js {.wrap}
const isOdd = number => !!(number & 1);
```
### Or
```js {.wrap}
const isOdd = number => !Number.isInteger(number / 2);
```
### Example
```js {.wrap}
isOdd(1); // true
isOdd(2); // false
```
@@ -0,0 +1,29 @@
---
title: How to check if a number is positive in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a number is positive only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPositive = number => Math.sign(number) === 1;
```
### Example
```js {.wrap}
isPositive(3); // true
isPositive(-8); // false
```
@@ -0,0 +1,29 @@
---
title: How to check if a string contains lower case characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains lower case characters only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const containsLowerCase = str => str !== str.toUpperCase();
```
### Example
```js {.wrap}
containsLowerCase('Hello World'); // true
containsLowerCase('HELLO WORLD'); // false
```
@@ -0,0 +1,22 @@
---
title: How to check if a string contains only ascii characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains only ascii characters only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isAscii = str => /^[\x00-\x7F]+$/.test(str);
```
@@ -0,0 +1,35 @@
---
title: How to check if a string contains only digits in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains only digits only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isNumeric = str => !/[^0-9]/.test(str);
```
### Example
```js {.wrap}
isNumeric(2); // true
isNumeric('23'); // true
isNumeric('00123'); // true
isNumeric('1.23'); // false
isNumeric('-Infinity'); // false
isNumeric('Infinity'); // false
isNumeric('NaN'); // false
```
@@ -0,0 +1,32 @@
---
title: How to check if a string contains only letters and numbers in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains only letters and numbers only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isAlphanumeric = str => /^[0-9A-Z]+$/i.test(str);
```
### Example
```js {.wrap}
isAlphanumeric('helloworld'); // true
isAlphanumeric('HelloWorld'); // true
isAlphanumeric('hello world'); // false
isAlphanumeric('hello123'); // true
isAlphanumeric('hello 123'); // false
```
@@ -0,0 +1,31 @@
---
title: How to check if a string contains only letters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains only letters only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isAlpha = str => /^[A-Z]+$/i.test(str);
```
### Example
```js {.wrap}
isAlpha('helloworld'); // true
isAlpha('HelloWorld'); // true
isAlpha('hello world'); // false
isAlpha('0123456789'); // false
```
@@ -0,0 +1,29 @@
---
title: How to check if a string contains upper case characters in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains upper case characters only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const containsUpperCase = str => str !== str.toLowerCase();
```
### Example
```js {.wrap}
containsUpperCase('Hello World'); // true
containsUpperCase('hello world'); // false
```
@@ -0,0 +1,28 @@
---
title: How to check if a string contains whitespace in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string contains whitespace only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const containsWhitespace = str => str => /\s/.test(str);
```
### Example
```js {.wrap}
containsWhitespace('hello world'); // true
```
@@ -0,0 +1,31 @@
---
title: How to check if a string is a hexadecimal color in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is a hexadecimal color only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isHexColor = color => /^#([0-9A-F]{3}|[0-9A-F]{4}|[0-9A-F]{6}|[0-9A-F]{8})$/i.test(color);
```
### Example
```js {.wrap}
isHexColor('#012'); // true
isHexColor('#A1B2C3'); // true
isHexColor('012'); // false
isHexColor('#GHIJKL'); // false
```
@@ -0,0 +1,36 @@
---
title: How to check if a string is a hexadecimal number in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is a hexadecimal number only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isHexadecimal = str => /^[A-F0-9]+$/i.test(str);
```
### Or
```js {.wrap}
const isHexadecimal = str => str.split('').every(c => '0123456789ABCDEFabcdef'.indexOf(c) !== -1);
```
### Example
```js {.wrap}
isHexadecimal('123'); // true
isHexadecimal('A1B2C3'); // true
isHexadecimal('#123'); // false
```
@@ -0,0 +1,28 @@
---
title: How to check if a string is a mongodb objectid in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is a mongodb objectid only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isMongoId = str => str.length === 24 && /^[A-F0-9]+$/i.test(str);
```
### Or
```js {.wrap}
const isMongoId = str => str.length === 24 && str.split('').every(c => '0123456789ABCDEFabcdef'.indexOf(c) !== -1);
```
@@ -0,0 +1,22 @@
---
title: How to check if a string is an octal number in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is an octal number only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isOctal = str => /^(0o)?[0-7]+$/i.test(str);
```
@@ -0,0 +1,22 @@
---
title: How to check if a string is lower case in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is lower case only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isLowerCase = str => str === str.toLowerCase();
```
@@ -0,0 +1,22 @@
---
title: How to check if a string is upper case in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a string is upper case only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isUpperCase = str => str === str.toUpperCase();
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is a business identifier code in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is a business identifier code only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isBIC = value => /^[a-zA-Z]{6}[a-zA-Z0-9]{2}([a-zA-Z0-9]{3})?$/.test(value);
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is a number in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is a number only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isNumber = value => !isNaN(parseFloat(value)) && isFinite(value);
```
@@ -0,0 +1,35 @@
---
title: How to check if a value is a plain object in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is a plain object only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPlainObject = v => (!!v && typeof v === 'object' && (v.__proto__ === null || v.__proto__ === Object.prototype));
```
### Example
```js {.wrap}
isPlainObject(null); // false
isPlainObject('hello world'); // false
isPlainObject([]); // false
isPlainObject(Object.create(null)); // false
isPlainObject(function() {}); // false
isPlainObject({}); // true
isPlainObject({ a: '1', b: '2' }); // true
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is a regular expression in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is a regular expression only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isRegExp = value => Object.prototype.toString.call(value) === '[object RegExp]';
```
@@ -0,0 +1,30 @@
---
title: How to check if a value is a string in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is a string only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isString = value => Object.prototype.toString.call(value) === '[object String]';
```
### Example
```js {.wrap}
isString('hello world'); // true
isString(new String('hello world')); // true
isString(10); // false
```
@@ -0,0 +1,32 @@
---
title: How to check if a value is an object in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is an object only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isObject = v => (v !== null && typeof v === 'object');
```
### Example
```js {.wrap}
isObject(null); // false
isObject('hello world'); // false
isObject({}); // true
isObject([]); // true
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is base32 encoded in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is base32 encoded only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isBase32 = value => value.length % 8 === 0 && /^[A-Z2-7]+=*$/.test(value);
```
@@ -0,0 +1,23 @@
---
title: How to check if a value is base58 encoded in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is base58 encoded only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// It doesn't accept the I, O, l characters
const isBase58 = value => /^[A-HJ-NP-Za-km-z1-9]*$/.test(value);
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is base64 encoded in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is base64 encoded only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isBase64 = value => /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{4})$/.test(value);
```
@@ -0,0 +1,22 @@
---
title: How to check if a value is nil in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a value is nil only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isNil = (value) => value == null;
```
@@ -0,0 +1,29 @@
---
title: How to check if a year is leap year in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if a year is leap year only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isLeapYear = year => (((year % 4 === 0) && (year % 100 !== 0)) || (year % 400 === 0));
```
### Or
```js {.wrap}
// Get the number of days in February
const isLeapYear = year => new Date(year, 1, 29).getDate() === 29;
```
@@ -0,0 +1,37 @@
---
title: How to check if all array elements are equal to a given value in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if all array elements are equal to a given value only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isEqual = (arr, value) => arr.every(item => item === value);
```
### Or
```js {.wrap}
// Ends earlier for false arrays
const isEqual = (arr, value) => !arr.some(item => item !== value);
```
### Example
```js {.wrap}
isEqual(['foo', 'foo'], 'foo'); // true
isEqual(['foo', 'bar'], 'foo'); // false
isEqual(['bar', 'bar'], 'foo'); // false
```
@@ -0,0 +1,35 @@
---
title: How to check if all items in an array are equal in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if all items in an array are equal only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const areEqual = arr => arr.length > 0 && arr.every(item => item === arr[0]);
```
### Or
```js {.wrap}
const areEqual = arr => new Set(arr).size === 1;
```
### Example
```js {.wrap}
areEqual([1, 2, 3, 4]); // false
areEqual(['hello', 'hello', 'hello']); // true
```
@@ -0,0 +1,42 @@
---
title: How to check if an array contains a value matching some criterias in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an array contains a value matching some criterias only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const contains = (arr, criteria) => arr.some(v => criteria(v));
```
### Or
```js {.wrap}
const contains = (arr, criteria) => arr.some(criteria);
```
### Or
```js {.wrap}
const contains = (arr,criteria) => arr.filter(criteria).length > 0;
```
### Example
```js {.wrap}
contains([10, 20, 30], v => v > 25 ); // true
contains([10, 20, 30], v => v > 100 || v < 15 ); // true
contains([10, 20, 30], v => v > 100 ); // false
```
@@ -0,0 +1,29 @@
---
title: How to check if an array is not empty in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an array is not empty only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isNotEmpty = arr => Array.isArray(arr) && Object.keys(arr).length > 0;
```
### Example
```js {.wrap}
isNotEmpty([]); // false
isNotEmpty([1, 2, 3]); // true
```
@@ -0,0 +1,37 @@
---
title: How to check if an array is subset of other array in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an array is subset of other array only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
// Check if `b` is subset of `a`
const isSubset = (a, b) => (new Set(b)).size === (new Set(b.concat(a))).size;
```
### Or
```js {.wrap}
const isSubset = (a, b) => b.join("|").includes(a.join("|"));
```
### Example
```js {.wrap}
isSubset([1, 2], [1, 2, 3, 4]); // true
isSubset([1, 2, 5], [1, 2, 3, 4]); // false
isSubset([6], [1, 2, 3, 4]); // false
```
@@ -0,0 +1,22 @@
---
title: How to check if an object is a promise in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an object is a promise only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isPromise = obj => !!obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function';
```
@@ -0,0 +1,22 @@
---
title: How to check if an object is an array in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an object is an array only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isArray = obj => Array.isArray(obj);
```
@@ -0,0 +1,28 @@
---
title: How to check if an object is empty in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to check if an object is empty only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isEmpty = obj => Reflect.ownKeys(obj).length === 0 && obj.constructor === Object;
```
### Or for enumerable property names only
```js {.wrap}
const isEmpty = obj => JSON.stringify(obj) === '{}';
```
@@ -0,0 +1,31 @@
---
title: How to validate a gregorian date in JavaScript
tags:
- JavaScript
- ES6
- one-line
- js
- Validator
layout: note
---
In this Article we will go through how to validate a gregorian date only using single line of code in JavaScript.
This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => `Arrow Function`.
<br/>
<br/>
Let's define this short function:
```js {.wrap}
const isValidDate = (m, d, y) => 0 <= m && m <= 11 && 0 < y && y < 32768 && 0 < d && d <= (new Date(y, m, 0)).getDate();
```
Parameters `m`: the month (zero-based index).
Parameters `d`: the day.
Parameters `y`: the year.