Preface: summary of dry goods in the process of work, will continue to update oh…

  1. random()
/* * @name: random * @description: Produces a random integer that contains a lower bound, Includes upper limit * @param {Number} lower lower limit * @param {Number} upper upper limit * @return {Number} returns a random integer between lower limit and upper limit */ export function random(lower, upper) { return Math.floor(Math.random() * (upper - lower+1)) + lower; }Copy the code
  1. randomColor()
/* * @name: @return {String} returns the String content of the RGB value of the color, such as: RGB (201, 57, 96) */ export function randomColor() { Var r = random(0, 256), g = random(0, 256), b = random(0, 256); / / the result of the connection string var result = "RGB (" + r +", "+ g +", "a + b +") "; // return result; }Copy the code
  1. getFormatCode()
/* * @name: getFormatCode * @description: HTML code with newline and space format according to Value * @param: strValue {String} The Value to be converted * @return: @example getFormatCode(" test \r\n\s test ") => "test <br/> test" */ export function getFormatCode(strValue) { return strValue.replace(/\r\n/g, '<br/>').replace(/\n/g, '<br/>').replace(/\s/g, '&nbsp; ')}Copy the code
  1. removal()
/* * @name: removal * @description: @param: arr {Array} @id: @id {Array} @id: @id {Array} @id: @id; {Array} to heavy code * @ example: after removal ([{id: 1}, {id: 2}, {id: 1}], "id") = > [{id: 1}, {id: 2}] */ export function removal(arr, key) { let hash = {}; arr = arr.reduce((preVal, curVal) => { hash[curVal[key]] ? '' : hash[curVal[key]] = true && preVal.push(curVal); return preVal }, []) }Copy the code
  1. methodGetByteLen()
/* * @name: methodGetByteLen * @description: methodGetByteLen * @param: @return: {String} @example: Removal (' hahaha hahaha ', 2) => 'hahaha... ' */ export function methodGetByteLen(str, len) { let status = true if (! str) return ['-', status] let templen = 0 for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) > 255) { templen += 2 } else { templen++ } if (templen >= len) { status = false return [str.substring(0, i) + '...', status] } } return [str, status] }Copy the code
  1. getFloatStr()
/* * @name: getFloatStr * @description: auto complete with 2 decimal digits, convert the incoming data to a string, and clear the string of non-numeric and. @param: num {String,Number} The value to be converted * @return: {String} The converted Number * @example: GetFloatStr ("123.1") => 123.10 */ export function getFloatStr(value) {if (isNaN(value)) {return value} else {var value = Math.round(parseFloat(value) * 100) / 100 var xsd = value.toString().split('.') if (xsd.length == 1) { value = value.toString() + '.00' return value.toString() } if (xsd.length > 1) { if (xsd[1].length < 2) { value = value.toString() + '0' } return value.toString() } } }Copy the code
  1. milliFormat()
/* * @name: milliFormat * @description: saves 2 decimal places and converts the incoming data to a string, and clears the string of non-numeric and. @param: {Number,String} The value to be converted * @return: {String} The converted Number * @example: Function milliFormat(s) {s = String(s) s = s.place (/^(\d*)$/, '$1.') s = (s + '00').replace(/(\d*\.\d\d)\d*/, '$1') s = s.replace('.', ',') var re = /(\d)(\d{3},)/ while (re.test(s)) { s = s.replace(re, '$1,$2') } s = s.replace(/,(\d\d)$/, '.$1') return s.replace(/^\./, '0.') }Copy the code
  1. commafyback()
/* * @name: commafyback * @description: 1000 character * @param: STR {String} the value to convert * @return: {String} the converted number * @example: Commafyback ("102,123.18") => commafyback(STR) {var x = String(STR).split(',') return x.join('') }Copy the code
  1. timestampToTime()
/* * @name: timestampToTime * @description: * @param: timestamp {Number} timestamp * flag {Boolean} Default value true, timestamp is 10 bits true, timestamp is 13 bits flase * @return: {String} * @example:  timestampToTime(1403058804) => 2014-06-18 10:33:24 */ export function timestampToTime(timestamp, Flag =true) {// If the timestamp is 10 bits *1000, let date = flag? new Date(timestamp * 1000) : new Date(timestamp); Y = date.getFullYear() + '-'; M = (date.getMonth()+1 < 10 ? '0'+(date.getMonth()+1) : date.getMonth()+1) + '-'; D = date.getDate() + ' '; h = date.getHours() + ':'; m = date.getMinutes() + ':'; s = date.getSeconds(); return Y+M+D+h+m+s; }Copy the code

10. Regular phone number

export const validatePhone = (rule, value, callback) => { if (value) { if (! / ^ [1], [[3] [0-9]) | ([4] [5-9]) | ([5] [0, 3, 5-9]) | ([6] [5, 6]) | ([7] [0 to 8]) | ([8] [0-9]) | ([9],8,9 [1])) [0-9] {8} $/. The test (value)) { Return callback(new Error(' Please input the correct phone number ')); } else { callback(); }} else {callback(new Error(' please input phone number ')); }}Copy the code

11. Count the number of days between two dates

/* * @name: dateDiff * @description: Calculate the number of days between two dates * @param: EDate {String} End time sDate {String} Start time * splitStr {String} default value '-', as in (2010-10-10: '-') {Number} Number of days * @example:  dateDiff('2020-08-03', '2020-08-01', '-') => 2 */ export const dateDiff = (eDate, sDate, splitStr="-") => { let aDate, oDate1, oDate2, iDays; aDate = eDate.split(splitStr); oDate1 = new Date(aDate[1] + splitStr + aDate[2] + splitStr + aDate[0]); // Convert to 08-03-2020 format aDate = sdate.split (splitStr); oDate2 = new Date(aDate[1] + splitStr + aDate[2] + splitStr + aDate[0]); iDays = parseInt((oDate1 - oDate2) / 1000 / 60 / 60 / 24); // Convert milliseconds to days return iDays; }Copy the code

12. Gets the previous days of the current date

/* * @name: getOtherDay * @description: * @param: num {String} days * @return: {Number} days * @example:  getOtherDay('1') => 2020-08-02 */ export const getOtherDay = (num) => { let date1 = new Date(), time1=date1.getFullYear()+"-"+(date1.getMonth()+1)+"-"+date1.getDate(); Time1 let date2 = new Date(date1); date2.setDate(date1.getDate()+num); let time2 = date2.getFullYear()+"-"+(date2.getMonth()+1)+"-"+date2.getDate(); return time2 }Copy the code

Conclusion: If you have any common functions that you recommend, feel free to add them in the comments and include them in this article. Of course, you can also directly use loDash these more popular functional libraries, here only for learning reference use.