This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for ES2017 | |
{ | |
const groupArr2d = (arr2d, {swap=false} = {}) => arr2d.reduce((a,c)=>Object.assign(a,{[c[+swap]]:a[c[+swap]]?[...a[c[+swap]],c[+!swap]]:[c[+!swap]]}),{}); | |
const groupConcatArr2d = (arr2d, {swap=false, glue=","} = {}) => Object.assign({},...Object.entries(arr2d.reduce((a,c)=>Object.assign(a,{[c[+swap]]:a[c[+swap]]?[...a[c[+swap]],c[+!swap]]:[c[+!swap]]}),{})).map(e=>({[e[0]]:e[1].join(glue)}))); | |
// two-dimensional arrays can represent object. | |
// in each array, the first value can be regarded as a key, and the next value can be regarded as a value. | |
// `Object.entries(obj)` is easy to understand as an example. | |
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries#Examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// for ES2017 | |
{ | |
// "name=matsuby&age=27" => {name: "matsuby", age: "27"} | |
const parseQuery = qs => typeof qs!=="string"?{}:Object.assign({},...Object.entries([...new URLSearchParams(qs)].reduce((a,c)=>Object.assign(a,{[c[0]]:a[c[0]]?[...a[c[0]],c[1]]:[c[1]]}),{})).map(e=>({[e[0]]:e[1].join(",")}))); | |
// {name: "matsuby", age: "27"} => "name=matsuby&age=27" | |
const buildQuery = qo => new URLSearchParams(Object.entries(qo).map(kv=>kv[1].split(",").map(v=>[kv[0],v])).reduce((a,c)=>a.concat(c),[])).toString(); | |
const testParams = [ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ================================================================= | |
// https://en.wikipedia.org/wiki/Proleptic_Gregorian_calendar | |
// ================================================================= | |
// for ES2015 | |
{ | |
const isLeapYear = y => !!(+y===parseInt(y)&&!y.pop&&(y%4)^(y%100)|!(y%400)); | |
const testParams = [ | |
// expect: true (is leap) |
NewerOlder