大家好,今天我繼續給大家分享本系列文章的第四部分,希望對你的日常工作有所幫助。
64、getColonTimeFromDate
用於判斷程序運行環境是否在瀏覽器,這有助於避免在node環境運行前端模塊時出錯。
<code>const isBrowser = () => ![typeof window, typeof document].includes('undefined');isBrowser(); // true (browser)isBrowser(); // false (Node)/<code>
65、isBrowserTabFocused
用於判斷當前頁面是否處於活動狀態(顯示狀態)。
<code>const isBrowserTabFocused = () => !document.hidden;isBrowserTabFocused(); // true/<code>
66、isLowerCase
用於判斷當前字符串是否都為小寫。
<code>const isLowerCase = str => str === str.toLowerCase();isLowerCase('abc'); // trueisLowerCase('a3@$'); // trueisLowerCase('Ab4'); // false/<code>
67、isNil
用於判斷當前變量的值是否為 null 或 undefined 類型。
<code>const isNil = val => val === undefined || val === null;isNil(null); // trueisNil(undefined); // true/<code>
68、isNull
用於判斷當前變量的值是否為 null 類型
<code>const isNull = val => val === null;isNull(null); // true/<code>
69、isNumber
用於檢查當前的值是否為數字類型
<code>function isNumber(n) { return !isNaN(parseFloat(n)) && isFinite(n);}isNumber('1'); // falseisNumber(1); // true/<code>
70、isObject
用於判斷參數的值是否是對象,這裡運用了Object 構造函數創建一個對象包裝器,如果是對象類型,將會原值返回。
<code>const isObject = obj => obj === Object(obj);isObject([1, 2, 3, 4]); // trueisObject([]); // trueisObject(['Hello!']); // trueisObject({ a: 1 }); // trueisObject({}); // trueisObject(true); // false/<code>
71、isObjectLike
用於檢查參數的值是否為null以及類型是否為對象。
<code>const isObjectLike = val => val !== null && typeof val === 'object';isObjectLike({}); // trueisObjectLike([1, 2, 3]); // trueisObjectLike(x => x); // falseisObjectLike(null); // false/<code>
72、isPlainObject
此代碼段檢查參數的值是否是由Object構造函數創建的對象。
<code>const isPlainObject = val => !!val && typeof val === 'object' && val.constructor === Object;isPlainObject({ a: 1 }); // trueisPlainObject(new Map()); // false/<code>
73、isPromiseLike
用於檢查當前的對象是否類似Promise函數。
<code>const isPromiseLike = obj => obj !== null && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'; isPromiseLike({ then: function() { return ''; }}); // trueisPromiseLike(null); // falseisPromiseLike({}); // false/<code>
74、isSameDate
用於判斷給定的兩個日期是否是同一天。
<code>const isSameDate = (dateA, dateB) => dateA.toISOString() === dateB.toISOString();isSameDate(new Date(2010, 10, 20), new Date(2010, 10, 20)); // true/<code>
75、isString
用於檢查當前的值是否為字符串類型。
<code>const isString = val => typeof val === 'string';isString('10'); // true/<code>
76、isSymbol
用於判斷參數的值是否是 Symbol 類型。
<code>const isSymbol = val => typeof val === 'symbol';isSymbol(Symbol('x')); // true/<code>
77、isUndefined
用於判斷參數的類型是否是 Undefined 類型。
<code>const isUndefined = val => val === undefined;isUndefined(undefined); // true/<code>
78、isUpperCase
用於判斷當前字符串的字母是否都為大寫。
<code>const isUpperCase = str => str === str.toUpperCase();isUpperCase('ABC'); // trueisLowerCase('A3@$'); // trueisLowerCase('aB4'); // false/<code>
79、isValidJSON
用於判斷給定的字符串是否是 JSON 字符串。
<code>const isValidJSON = str => { try { JSON.parse(str); return true; } catch (e) { return false; }};isValidJSON('{"name":"Adam","age":20}'); // trueisValidJSON('{"name":"Adam",age:"20"}'); // falseisValidJSON(null); // true/<code>
80、last
此函數功能返回數組的最後一個元素。
<code>const last = arr => arr[arr.length - 1];last([1, 2, 3]); // 3/<code>
81、matches
此函數功能用於比較兩個對象,以確定第一個對象是否包含與第二個對象相同的屬性值。
<code>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 }); // truematches({ hair: 'long', beard: true }, { age: 25, hair: 'long', beard: true }); // false/<code>
82、maxDate
此代碼段查找日期數組中最大的日期進行輸出。
<code>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); // 2018-03-11T22:00:00.000Z/<code>
83、maxN
此段代碼輸出數組中前 n 位最大的數。
<code>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]/<code>
84、minDate
此代碼段查找日期數組中最早的日期進行輸出。
<code>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); // 2016-01-08T22:00:00.000Z/<code>
小節
今天的內容就和大家分享到這裡,感謝你的閱讀,如果你喜歡我的分享,麻煩給個關注、點贊加轉發哦,你的支持,就是我分享的動力,後續會持續分享剩餘的代碼片段,歡迎持續關注。
本文原作者:Fatos Morina
來源網站:medium
注:並非直譯
相關閱讀
閱讀更多 前端達人 的文章