The calendar

Create an array for the past seven days. If you replace the minus sign with a plus sign in the code, you’ll get an array for the next seven days

[...Array(7).keys()].map(days => new Date(date.now () -86400000 * days));Copy the code

Generate random IDS

Create ID functionality that is often used in prototyping. But I’ve seen it used in real projects. It’s not safe

// Generate a random alphanumeric string of length 11 math.random ().toString(36).substring(2); // hg7znok52xCopy the code

Gets the query parameters of the URL

This query parameter code to get the URL is the most streamlined QAQ I have seen. foo=bar&baz=bing => {foo: bar, baz: bing}

// get the URL query parameter q={}; location.search.replace(/([^?&=]+)=([^&]+)/g,(_,k,v)=>q[k]=v); q;Copy the code

Generate random hexadecimal code (generate random colors)

Generate random hexadecimal code using JavaScript terse code

// Generate random hexadecimal code such as:'#c618b2'
The '#' + Math.floor(Math.random() * 0xffffff).toString(16).padEnd(6, '0');
Copy the code

Return a keyboard

This is a neat piece of code that’s hard to understand, but you’ll be surprised when it returns a graphical keyboard

// Return a keyboard graphic with a string (_=>[..."`1234567890-=~~QWERTYUIOP[]\\~ASDFGHJKL; '~~ZXCVBNM,./~"].map(x=>(o+=`/${b='_'.repeat(w=x\\|`,m+=y+(x+' ').slice(0,w)+y+y,n+=y+b+y+y,l+='__'+b)[73]&&(k.push(l,m,n,o),l=' ',m=n=o=y),m=n=o=y='|',p=l=k=[])&&k.join`
`)()
Copy the code

Judge variable types

function type (vars) {
	return Object.prototype.toString.call(vars).match(/\[object (.*)+\]/)[1].toLowerCase()
}
type(123) // number
type/ / array ([1, 2, 3])Copy the code

Randomly generate random numbers in any range

function getRandomIntInclusive(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    returnMath.floor(Math.random() * (max - min + 1)) + min; } getRandomIntInclusive(90, 99) //...Copy the code