成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

這些 JavaScript函數(shù)讓你的工作更加 So Easy!

開發(fā) 前端
你是否有一個(gè)DDL,它每n分鐘顯示一天的時(shí)間?用這個(gè)函數(shù)。

[[384757]]

本文已經(jīng)過原作者 YoussefZidan 授權(quán)翻譯。

randomNumber()

獲取指定區(qū)間的隨機(jī)數(shù)。

  1. ** 
  2.  * 在最小值和最大值之間生成隨機(jī)整數(shù)。 
  3.  * @param {number} min Min number 
  4.  * @param {number} max Max Number 
  5.  */ 
  6. export const randomNumber = (min = 0, max = 1000) => 
  7.   Math.ceil(min + Math.random() * (max - min)); 
  8.  
  9. // Example 
  10. console.log(randomNumber()); // 97  

capitalize()

將字符串的第一個(gè)字母變?yōu)榇髮憽?/p>

  1. /** 
  2.  * Capitalize Strings. 
  3.  * @param {string} s String that will be Capitalized 
  4.  */ 
  5. export const capitalize = (s) => { 
  6.   if (typeof s !== "string"return ""
  7.   return s.charAt(0).toUpperCase() + s.slice(1); 
  8.  
  9. // Example 
  10. console.log(capitalize("cat")); // Cat 

truncate();

這對于長字符串很有用,特別是在表內(nèi)部。

  1. /** 
  2.  * 截?cái)嘧址?... 
  3.  * @param {string} 要截?cái)嗟奈谋咀址?nbsp;
  4.  * @param {number} 截?cái)嗟拈L度 
  5.  */ 
  6. export const truncate = (text, num = 10) => { 
  7.   if (text.length > num) { 
  8.     return `${text.substring(0, num - 3)}...` 
  9.   } 
  10.   return text; 
  11.  
  12. // Example 
  13. console.log(truncate("this is some long string to be truncated"));   
  14.  
  15. // this is...  

toTop();

滾到到底部,可以通過 behavior 屬性指定滾動(dòng)速度狀態(tài)。

  1. /** 
  2.  * Scroll to top 
  3.  */ 
  4. export const toTop = () => { 
  5.   window.scroll({ top: 0, left: 0, behavior: "smooth" }); 
  6. };  

softDeepClone()

這個(gè)方法是經(jīng)常被用到的,因?yàn)橛辛怂覀兛梢陨疃瓤寺∏短讛?shù)組或?qū)ο蟆?/p>

不過,這個(gè)函數(shù)不能與new Date()、NaN、undefined、function、Number、Infinity等數(shù)據(jù)類型一起工作。

你想深度克隆上述數(shù)據(jù)類型,可以使用 lodash 中的 cloneDeep() 函數(shù)。

  1. /** 
  2.  * Deep cloning inputs 
  3.  * @param {any} input Input 
  4.  */ 
  5. export const softDeepClone= (input) => JSON.parse(JSON.stringify(input)); 

appendURLParams() & getURLParams()

快速添加和獲取查詢字符串的方法,我通常使用它們將分頁元數(shù)據(jù)存儲(chǔ)到url。

  1. /** 
  2.  * Appen query string and return the value in a query string format. 
  3.  * @param {string} key 
  4.  * @param {string} value 
  5.  */ 
  6. export const appendURLParams = (key, value) => { 
  7.   const searchParams = new URLSearchParams(window.location.search).set(key, value); 
  8.   return searchParams.toString(); 
  9. }; 
  10.  
  11. // example 
  12. console.log(appendURLParams("name""youssef")) // name=youssef 
  13.  
  14. /** 
  15.  * Get param name from URL. 
  16.  * @param {string} name 
  17.  */ 
  18. export const getURLParams = (name) => new URLSearchParams(window.location.search).get(name); 
  19.  
  20. // Example 
  21. console.log(getURLParams(id)) // 5 

getInnerHTML()

每當(dāng)服務(wù)器返回一串HTML元素時(shí),我都會(huì)使用它。

  1. /** 
  2.  * 獲取HTML字符串的內(nèi)部Text 
  3.  * @param {string} str A string of HTML 
  4.  */ 
  5. export const getInnerHTML = (str) => str.replace(/(<([^>]+)>)/gi, ""); 

scrollToHide()

上滾動(dòng)以顯示HTML元素,向下滾動(dòng)以將其隱藏。

  1. /** 
  2.  * 下滾動(dòng)時(shí)隱藏HTML元素。 
  3.  * @param {string} 元素的 id 
  4.  * @param {string} distance in px ex: "100px" 
  5.  */ 
  6. export const scrollToHide = (id, distance) => { 
  7.   let prevScrollpos = window.pageYOffset; 
  8.   window.onscroll = () => { 
  9.     const currentScrollPos = window.pageYOffset; 
  10.     if (prevScrollpos > currentScrollPos) { 
  11.       document.getElementById(id).style.transform = `translateY(${distance})`; 
  12.     } else { 
  13.       document.getElementById(id).style.transform = `translateY(-${distance})`; 
  14.     } 
  15.     prevScrollpos = currentScrollPos; 
  16.   }; 
  17. }; 

humanFileSize ()

傳入字節(jié)為單位的文件,返回我們?nèi)粘K煜さ膯挝弧?/p>

  1. /** 
  2.  * Converting Bytes to Readable Human File Sizes. 
  3.  * @param {number} bytes Bytes in Number 
  4.  */ 
  5. export const humanFileSize = (bytes) => { 
  6.   let BYTES = bytes; 
  7.   const thresh = 1024; 
  8.  
  9.   if (Math.abs(BYTES) < thresh) { 
  10.     return `${BYTES} B`; 
  11.   } 
  12.  
  13.   const units = ["kB""MB""GB""TB""PB""EB""ZB""YB"]; 
  14.  
  15.   let u = -1; 
  16.   const r = 10 ** 1; 
  17.  
  18.   do { 
  19.     BYTES /= thresh; 
  20.     u += 1; 
  21.   } while (Math.round(Math.abs(BYTES) * r) / r >= thresh && u < units.length - 1); 
  22.  
  23.   return `${BYTES.toFixed(1)} ${units[u]}`; 
  24. }; 
  25.  
  26. // Example 
  27. console.log(humanFileSize(456465465)); // 456.5 MB 

getTimes()

你是否有一個(gè)DDL,它每n分鐘顯示一天的時(shí)間?用這個(gè)函數(shù)。

  1. /** 
  2.  * Getting an Array of Times + "AM" or "PM"
  3.  * @param {number} minutesInterval 
  4.  * @param {number} startTime  
  5.  */ 
  6. export const getTimes = (minutesInterval = 15, startTime = 60) => { 
  7.   const times = []; // time array 
  8.   const x = minutesInterval; // minutes interval 
  9.   let tt = startTime; // start time 
  10.   const ap = ["AM""PM"]; // AM-PM 
  11.  
  12.   // loop to increment the time and push results in array 
  13.   for (let i = 0; tt < 24 * 60; i += 1) { 
  14.     const hh = Math.floor(tt / 60); // getting hours of day in 0-24 format 
  15.     const mm = tt % 60; // getting minutes of the hour in 0-55 format 
  16.     times[i] = `${`${hh === 12 ? 12 : hh % 12}`.slice(-2)}:${`0${mm}`.slice(-2)} ${ 
  17.       ap[Math.floor(hh / 12)] 
  18.     }`; // pushing data in array in [00:00 - 12:00 AM/PM format] 
  19.     tt += x; 
  20.   } 
  21.   return times; 
  22. }; 
  23.  
  24. // Example 
  25. console.log(getTimes()); 
  26. /* [ 
  27.     "1:00 AM"
  28.     "1:15 AM"
  29.     "1:30 AM"
  30.     "1:45 AM"
  31.     "2:00 AM"
  32.     "2:15 AM"
  33.     "2:30 AM"
  34.     // .... 
  35.     ] 
  36. */  

setLocalItem() & getLocalItem()

讓 LocalStorage 具有過期時(shí)間。

  1. /** 
  2.  * Caching values with expiry date to the LocalHost. 
  3.  * @param {string} key Local Storage Key 
  4.  * @param {any} value Local Storage Value 
  5.  * @param {number} ttl Time to live (Expiry Date in MS) 
  6.  */ 
  7. export const setLocalItem = (key, value, ttl = duration.month) => { 
  8.   const now = new Date(); 
  9.   // `item` is an object which contains the original value 
  10.   // as well as the time when it's supposed to expire 
  11.   const item = { 
  12.     value, 
  13.     expiry: now.getTime() + ttl, 
  14.   }; 
  15.   localStorage.setItem(key, JSON.stringify(item)); 
  16. }; 
  17.  
  18. /** 
  19.  * Getting values with expiry date from LocalHost that stored with `setLocalItem`. 
  20.  * @param {string} key Local Storage Key 
  21.  */ 
  22. export const getLocalItem = (key) => { 
  23.   const itemStr = localStorage.getItem(key); 
  24.   // if the item doesn't exist, return null 
  25.   if (!itemStr) { 
  26.     return null
  27.   } 
  28.   const item = JSON.parse(itemStr); 
  29.   const now = new Date(); 
  30.   // compare the expiry time of the item with the current time 
  31.   if (now.getTime() > item.expiry) { 
  32.     // If the item is expired, delete the item from storage 
  33.     // and return null 
  34.     localStorage.removeItem(key); 
  35.     return null
  36.   } 
  37.   return item.value; 
  38. };  

formatNumber()

  1. /** 
  2.  * Format numbers with separators. 
  3.  * @param {number} num 
  4.  */ 
  5. export const formatNumber = (num) => num.toLocaleString(); 
  6.  
  7. // Example 
  8. console.log(formatNumber(78899985)); // 78,899,985 

我們還可以添加其他選項(xiàng)來獲取其他數(shù)字格式,如貨幣、距離、權(quán)重等。

  1. export const toEGPCurrency = (num) => 
  2.   num.toLocaleString("ar-EG", { style: "currency", currency: "EGP" }); 
  3.  
  4. export const toUSDCurrency = (num) => 
  5.   num.toLocaleString("en-US", { style: "currency", currency: "USD" }); 
  6.  
  7. console.log(toUSDCurrency(78899985)); // $78,899,985.00 
  8. console.log(toEGPCurrency(78899985)); // ٧٨٬٨٩٩٬٩٨٥٫٠٠ ج.م. 

toFormData()

每當(dāng)我需要向服務(wù)器發(fā)送文件時(shí),我就使用這個(gè)函數(shù)。

  1. /** 
  2.  * Convert Objects to Form Data Format. 
  3.  * @param {object} obj 
  4.  */ 
  5. export const toFormData = (obj) => { 
  6.   const formBody = new FormData(); 
  7.   Object.keys(obj).forEach((key) => { 
  8.     formBody.append(key, obj[key]) 
  9.   }) 
  10.    
  11.   return formBody; 

getScreenWidth()

獲取一個(gè)表示屏幕寬度的字符串。

  1. /** 
  2.  * Detect screen width and returns a string representing the width of the screen. 
  3.  */ 
  4. export const getScreenWidth = () => { 
  5.   const screenWidth = window.screen.width; 
  6.   if (screenWidth <= 425) return "mobile"
  7.   if (screenWidth <= 768) return "tablet"
  8.   if (screenWidth <= 1024) return "laptopSm"
  9.   if (screenWidth <= 1440) return "laptopLg"
  10.   if (screenWidth <= 2560) return "HD"
  11.   return screenWidth; 
  12. };  

檢查數(shù)組中的每個(gè)元素是否存在于另一個(gè)數(shù)組中。

  1. export const containsAll = (baseArr, arr) => { 
  2.   let all = false
  3.  
  4.   for (let i = 0; i < arr.length; i += 1) { 
  5.     if (baseArr.includes(arr[i])) { 
  6.       all = true
  7.     } else { 
  8.       all = false
  9.       return all
  10.     } 
  11.   } 
  12.  
  13.   return all
  14. }; 

你還有使用其他有用的函數(shù)嗎?在評論里分享一下怎么樣?

完~,我是小智,我要去刷碗去了。

作者:YoussefZidan 譯者:前端小智 來源:dev原文:https://dev.to/youssefzidan/javascript-functions-that-will-make-your-life-much-easier-1imh

本文轉(zhuǎn)載自微信公眾號「大遷世界」,可以通過以下二維碼關(guān)注。轉(zhuǎn)載本文請聯(lián)系大遷世界公眾號。

 

責(zé)任編輯:武曉燕 來源: 大遷世界
相關(guān)推薦

2022-03-18 08:00:48

Chrome工具前端

2015-10-23 15:00:30

數(shù)據(jù)中心求職

2010-09-09 16:39:24

2025-03-11 08:30:00

Pythonretrying代碼

2020-07-06 14:18:25

Linux 系統(tǒng) 數(shù)據(jù)

2020-08-12 18:11:02

戴爾

2023-07-04 13:35:00

Monorepos工具管理

2024-08-02 10:23:20

2021-07-01 10:03:55

Distroless容器安全

2023-07-03 07:55:25

2021-09-11 22:51:38

Windows 10Windows微軟

2019-08-28 09:28:07

SSHOpenSSH運(yùn)維

2013-08-28 10:20:56

2022-09-19 15:02:24

C語言

2023-12-08 13:47:29

AI工具ChatGPT

2022-11-07 16:25:07

JavaScript技巧

2020-11-23 09:21:09

開源項(xiàng)目

2015-09-06 10:01:24

2019-07-13 15:31:10

Linux防火墻

2021-01-14 09:59:07

JS代碼編碼
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 中文字幕欧美日韩 | 精品亚洲一区二区三区四区五区高 | 日日干天天操 | 在线观看成人小视频 | wwwxxx日本在线观看 | 自拍 亚洲 欧美 老师 丝袜 | 91婷婷韩国欧美一区二区 | 国产日韩精品在线 | 午夜影视在线观看 | 国产高清视频在线观看播放 | 色资源在线观看 | 亚洲国产精品视频一区 | 日韩欧美在线播放 | 一区二区不卡 | 亚洲一区二区三区在线播放 | www.狠狠干 | 久久久www成人免费精品张筱雨 | 欧美精品首页 | 久久一区二区三区电影 | 欧美涩涩网 | 久久人爽| 亚洲一本 | 国产在线精品一区二区三区 | 欧美精品中文字幕久久二区 | jizz在线看片| 国产高清久久久 | 噜噜噜噜狠狠狠7777视频 | 日韩一区二区精品 | 国产伦精品一区二区三区精品视频 | 欧美a∨ | 美国一级黄色片 | 一区二区三区视频免费看 | 欧美黄色一区 | 91国产在线视频在线 | av在线一区二区三区 | 成人在线免费视频 | 日韩中文字幕视频在线观看 | 91在线视频免费观看 | 国产精品久久久久久久久 | 国产高潮好爽受不了了夜色 | 日韩三级在线 |