In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to use JavaScript tool functions to improve development efficiency. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Utility functions:
1. Numeric operation (1) generates a specified range of random numbers (2) digit microdigit separation export const format = (n) = > {let num = n.toString (); let len = num.length; if (len 0) {/ / is not an integer multiple of 3 return num.slice (0, remainder) +','+ num.slice (remainder, len). Match (/\ d {3} / g) .join (',') + temp } else {/ / 3 is an integer multiple of return num.slice (0, len). Match (/\ d {3} / g) .join (',') + temp;}} 2. Array operation (1) Array disorder export const arrScrambling = (arr) = > {for (let I = 0; I)
< arr.length; i++) { const randomIndex = Math.round(Math.random() * (arr.length - 1 - i)) + i; [arr[i], arr[randomIndex]] = [arr[randomIndex], arr[i]]; } return arr;}(2)数组扁平化export const flatten = (arr) =>{let result = []; for (let I = 0; I
< arr.length; i++) { if(Array.isArray(arr[i])) { result = result.concat(flatten(arr[i])); } else { result.push(arr[i]); } } return result;}(3)数组中获取随机数export const sample = arr =>Arr [Math.floor (Math.random () * arr.length)]; 3. String operation (1) generates random strings export const randomString = (len) = > {let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz123456789'; let strLen = chars.length; let randomStr =''; for (let I = 0; I
< len; i++) { randomStr += chars.charAt(Math.floor(Math.random() * strLen)); } return randomStr;};(2)字符串首字母大写export const fistLetterUpper = (str) =>{return str.charAt (0). ToUpperCase () + str.slice (1);}; (3) the middle four digits of the mobile phone number become * export const telFormat = (tel) = > {tel = String (tel); return tel.substr (0Magne3) + "*" + tel.substr (7);} (4) convert hump naming to short horizontal line naming export const getKebabCase = (str) = > {return str.replace (/ [Amurz] / g, (item) = >'-'+ item.toLowerCase ()} (5) short horizontal line naming to hump naming export const getCamelCase = (str) = > {return str.replace (/-([Amurz])) / g, (I) Item) = > item.toUpperCase ()} (6) convert full-width to half-width export const toCDB = (str) = > {let result = "" For (let I = 0; I
< str.length; i++) { code = str.charCodeAt(i); if (code >= 65281 & & code {let result = ""; for (let I = 0; I
< str.length; i++) { code = str.charCodeAt(i); if (code >= 33 & & code {const fraction = ['corner', 'minute']; const digit = ['zero', 'one','II','3', 'restaurant','Wu','Lu','Qi', 'Qing', 'nine'] Const unit = [['Yuan', 'Wan', '100 million], ['', 'pick up', 'hundred', 'Qian']; n = Math.abs (n); let s =''; for (let I = 0; I
< fraction.length; i++) { s += (digit[Math.floor(n * 10 * Math.pow(10, i)) % 10] + fraction[i]).replace(/零./, ''); } s = s || '整'; n = Math.floor(n); for (let i = 0; i < unit[0].length && n >0; iTunes +) {let p =''; for (let j = 0; j
< unit[1].length && n >0; jacks +) {p = digit [n% 10] + unit [1] [j] + p; n = Math.floor (n / 10);} s = p.replace (/ (zero.) * zero $/,'). Replace (/ ^ $/, 'zero) + unit [0] [I] + s } return s.replace (/ (zero.) * zero yuan /, 'yuan'). Replace (/ (zero.) + / g, 'zero'). Replace (/ ^ integer $/, 'zero integer');}; (2) convert numbers into Chinese numerals export const intToChinese = (value) = > {const str = String (value); const len = str.length-1 Const idxs = [', 'ten', 'hundred', 'thousand', 'thousand', 'ten', 'hundred', 'thousand', 'ten', 'ten', 'hundred', 'thousand', 'hundred']; const num = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'] Return str.replace (/ ([1-9] | 0 +) / g, ($, $1, idx, full) = > {let pos = 0; if ($1 [0]! = ='0') {pos = len-idx; if (idx = = 0 & & $1 [0] = = 1 & & idxs [len-idx] = ='X') {return idxs [len-idx];} return num [$1 [0]] + idxs [len-idx] } else {let left = len-idx; let right = len-idx + $1.Ths; if (Math.floor (right / 4)-Math.floor (left / 4) > 0) {pos = left-left% 4;} if (pos) {return idxs [pos] + num [$1 [0]];} else if (idx + $1.length > = len) {return' } else {return num [$1 [0]]});} 5. Operational storage (1) Storage loalStorageexport const loalStorageSet = (key, value) = > {if (! key) return; if (typeof value! = = 'string') {value = JSON.stringify (value);} window.localStorage.setItem (key, value);}; (2) get localStorageexport const loalStorageGet = (key) = > {if (! key) return; return window.localStorage.getItem (key);} (3) delete localStorageexport const loalStorageRemove = (key) = > {if (! key) return; window.localStorage.removeItem (key);}; (4) store sessionStorageexport const sessionStorageSet = (key, value) = > {if (! key) return; if (typeof value! = = 'string') {value = JSON.stringify (value);} window.sessionStorage.setItem (key, value)}; (5) get sessionStorageexport const sessionStorageGet = (key) = > {if (! key) return Return window.sessionStorage.getItem (key)}; (6) delete sessionStorageexport const sessionStorageRemove = (key) = > {if (! key) return; window.sessionStorage.removeItem (key)}; 6. Operation cookie (1) set cookieexport const setCookie = (key, value, expire) = > {const d = new Date (); d.setDate (d.getDate () + expire); [xss_clean] = `$ {key} = ${value}; expires=$ {d.toUTCString ()} `}; (2) read cookieexport const getCookie = (key) = > {const cookieStr = unescape ([xss_clean]); const arr = cookieStr.split (';'); let cookieValue ='' For (let I = 0; I
< arr.length; i++) { const temp = arr[i].split('='); if (temp[0] === key) { cookieValue = temp[1]; break } } return cookieValue};(3)删除cookieexport const delCookie = (key) =>{[xss_clean] = `${encodeURIComponent (key)} =; expires=$ {new Date ()}`}; 7. Format verification (1) check ID card number export const checkCardNo = (value) = > {let reg = / (^\ d {15} $) | (^\ d {18} $) | (^\ d {17} (\ d | X | x) $) /; return reg.test (value);}; (2) verify whether it contains Chinese export const haveCNChars = > (value) = > {return / [\ u4e00 -\ u9fa5] / .test (value) } (3) verify whether the zip code of Chinese mainland is export const isPostCode = (value) = > {return / ^ [1-9] [0-9] {5} $/ .test (value.toString ());} (4) verify whether it is an IPv6 address
Export const isIPv6 = (str) = > {return Boolean (str.match (/: / g)? str.match (/: / g). Length {return / ^ 1 [3 return / ^ 1 [3, 4, 5, 6, 7, 8, 9] [0-9] {9} $/ .test (value.toString ());} (7) check whether it contains emoji emoticons export const isEmojiCharacter = (value) = > {value = String (value); for (let I = 0; I < value.length) If +) {const hs = value.charCodeAt (I); if (0xd800)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.