Skip to content

Instantly share code, notes, and snippets.

@wkei
Last active July 12, 2018 09:43
Show Gist options
  • Save wkei/99c72b6891c95afdb16723050858f4d4 to your computer and use it in GitHub Desktop.
Save wkei/99c72b6891c95afdb16723050858f4d4 to your computer and use it in GitHub Desktop.
JS Tips
// parse search query
const params = new URLSearchParams('?a=b');
params.get('a')
// querySelector with attribute
document.querySelector('div:not([data-role="head"])')
// online matrix
const matrix = new Array(10).fill(0).map((v, i) => (new Array(10).fill(0).map((vv, ii) => `r${i}c${ii}`)))
// querystring
const qs = {
parse (search) {
search = decodeURIComponent(search).replace(/^\?/, '')
const query = _.reduce(search.split('&'), (query, item) => {
const [key, value] = item.split('=')
query[key] = value
return query
}, {})
return query
},
stringify (query) {
let search = _.reduce(Object.keys(query), (search, key) => {
search += `${key}=${query[key]}&`
return search
}, '')
search = search.replace(/&$/, '')
return search
}
}
// random Chinese
String.fromCharCode(19968 + Math.floor(Math.random() * (40869-19968)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment