1. Determine whether it is a Node.js environment
function isNode(){
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
}
Run in the browser, as shown above
Run in Node.js, as shown above
2. Parameter summation
Calculate the sum in the physical and chemical form of the function, and do it in one line of reduce
const sum = (...args) => args.reduce((a,b)=>a+b)
3.ES6 Set array deduplication
const uniqueArr = (arr) => [...new Set(arr)]
4. Randomly obtain a hexadecimal color
function getRandomColor(){
return `#${
Math.floor(Math.random()*0xffffff).toString(16)}`
}
5. Move the last item of the array to the first
const setLastToFirst = (arr) => arr.unshift(arr.pop())
6. Hide the middle four digits of the mobile phone number through regular expressions
function hidePhone(tel){
return tel.replace(/(\d{3})\d{4}(\d{4})/,'$1****$2')
}
7. Get a Boolean value randomly
The range of Math,random() is 0-0.99, and 0.5 is used to have a 50% probability in the middle.
function getRomBool(){
return 0.5 > Math.random()
}
8. Extract the year, month, time, etc. in the time in one step
在这里插入代码片