Hi, today I’m going to continue the fourth part of this series of articles that I hope will help you in your daily work.

64, getColonTimeFromDate

It is used to determine whether the application is running in a browser environment, which helps to avoid errors when running front-end modules in the Node environment.

const isBrowser = () => ! [typeof window, typeof document].includes('undefined');

isBrowser(); // true (browser)
isBrowser(); // false (Node)Copy the code

65, isBrowserTabFocused

Used to determine whether the current page is active (displayed).

const isBrowserTabFocused = () => ! document.hidden; isBrowserTabFocused(); //trueCopy the code

66, isLowerCase

Used to determine whether the current string is all lowercase.

const isLowerCase = str => str === str.toLowerCase();

isLowerCase('abc'); // true
isLowerCase('a3@$'); // true
isLowerCase('Ab4'); // falseCopy the code

67, isNil

Used to determine whether the value of the current variable is null or undefined.

const isNil = val => val === undefined || val === null;

isNil(null); // true
isNil(undefined); // trueCopy the code

68, isNull

Determines whether the value of the current variable is of type NULL.

const isNull = val => val === null;

isNull(null); // trueCopy the code

69, isNumber

Used to check whether the current value is numeric.

function isNumber(n) {
    return! isNaN(parseFloat(n)) && isFinite(n); } isNumber('1'); // false
isNumber(1); // trueCopy the code

70, isObject

The Object constructor is used to create an Object wrapper that returns the value if it is an Object type.

const isObject = obj => obj === Object(obj);

isObject([1, 2, 3, 4]); // true
isObject([]); // true
isObject(['Hello! ']); // true
isObject({ a: 1 }); // true
isObject({}); // true
isObject(true); // falseCopy the code

71, isObjectLike

Used to check whether the value of the parameter is null and whether the type is an object.

const isObjectLike = val => val ! == null && typeof val ==='object';

isObjectLike({}); // true
isObjectLike([1, 2, 3]); // true
isObjectLike(x => x); // false
isObjectLike(null); // falseCopy the code

72, isPlainObject

This code snippet checks whether the value of the parameter is an Object created by the Object constructor.

const isPlainObject = val => !! val && typeof val ==='object' && val.constructor === Object;

isPlainObject({ a: 1 }); // true
isPlainObject(new Map()); // falseCopy the code

73, isPromiseLike

Use to check whether the current object resembles the Promise function.

const isPromiseLike = obj => obj ! == null && (typeof obj ==='object' || typeof obj === 'function') &&
  typeof obj.then === 'function';
  
isPromiseLike({
  then: function() {
    return ' '; }}); //true
isPromiseLike(null); // false
isPromiseLike({}); // falseCopy the code

74, isSameDate

Used to determine whether two given dates are the same day.

const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();

isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // trueCopy the code

75, isString

Used to check whether the current value is a string.

const isString = val => typeof val === 'string';

isString('10'); // trueCopy the code

76, isSymbol

Determines whether the value of the parameter is of type Symbol.

const isSymbol = val => typeof val === 'symbol';

isSymbol(Symbol('x')); // trueCopy the code

77, isUndefined

Used to determine whether the type of the parameter is Undefined.

const isUndefined = val => val === undefined;

isUndefined(undefined); // trueCopy the code

78, isUpperCase

Determines whether all letters of the current string are uppercase.

const isUpperCase = str => str === str.toUpperCase();

isUpperCase('ABC'); // true
isLowerCase('A3@$'); // true
isLowerCase('aB4'); // falseCopy the code

79, isValidJSON

Used to determine whether a given string is a JSON string.

const isValidJSON = str => {
  try {
    JSON.parse(str);
    return true;
  } catch (e) {
    return false; }}; isValidJSON('{"name":"Adam","age":20}'); // true
isValidJSON('{"name":"Adam",age:"20"}'); // false
isValidJSON(null); // trueCopy the code

80, the last

This function returns the last element of an array.

const last = arr => arr[arr.length - 1]; last([1, 2, 3]); / / 3Copy the code

81, matches

This function compares two objects to determine whether the first contains the same attributes and values as the second.

onst matches = (obj, source) =>
  Object.keys(source).every(key => obj.hasOwnProperty(key) && obj[key] === source[key]);
  
matches({ age: 25, hair: 'long', beard: true }, { hair: 'long', beard: true }); // true
matches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // falseCopy the code

82, maxDate

This code snippet looks for the largest date in the date array for output.

const maxDate = (... dates) => new Date(Math.max.apply(null, ... dates)); const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9) ]; maxDate(array); / / T22:2018-03-11 00:00. 000 zCopy the code

83, maxN

This code outputs the largest number in the first n bits of the array.

const maxN = (arr, n = 1) => [...arr].sort((a, b) => b - a).slice(0, n); maxN([1, 2, 3]); // [3] maxN([1, 2, 3], 2); / / (3, 2]Copy the code

84, minDate

This code snippet looks for the earliest date in the date array to print.

const minDate = (... dates) => new Date(Math.min.apply(null, ... dates)); const array = [ new Date(2017, 4, 13), new Date(2018, 2, 12), new Date(2016, 0, 10), new Date(2016, 0, 9) ]; minDate(array); / / T22:2016-01-08 00:00. 000 zCopy the code

section

Thank you for your reading. If you like my sharing, please pay attention, like and forward. Your support is the motivation for my sharing.

Fatos Morina

Source: Medium

Note: Not a literal translation

reading

127 common JS code snippets, each code can be read in 30 seconds.

127 common JS code snippets, each code can be understood in 30 seconds (2)

127 common JS code snippets, each code can be understood in 30 seconds (3)

More exciting content, please pay attention to the wechat “front-end talent” public number!