reflection logic
[max/min, padStart/padEnd, some/every, forEach, map, sort, reduce, flatMap, flatten]で構成。define reflection.
reflection of limit[max/min](data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const define=(o,p,value)=>Reflect.defineProperty(o.prototype,p,{value})
define(Number,`limit`,function(b,v){
return b===v<this?v:this
})
console.table([
{this:0,b:false,v:1}
,{this:1,b:false,v:1}
,{this:2,b:false,v:1}
,{this:0,b:true,v:1}
,{this:1,b:true,v:1}
,{this:2,b:true,v:1}
].map(v=>({
default:`${v.b?`Math.min(${v.this},${v.v})`:`Math.max(${v.this},${v.v})`}=${v.b?Math.min(v.this,v.v):Math.max(v.this,v.v)}`
,this:`${`(${v.this}).limit(${v.b},${v.v})`}=${v.this.limit(v.b,v.v)}`
})))
}</script></head></html>
reflection of pad[padStart/padEnd](data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const define=(o,p,value)=>Reflect.defineProperty(o.prototype,p,{value})
,map=function(b,v,c){
let l,vs
try{
if(undefined===v.length){
l=v
throw Error()
}
l=v.length
if(`string`===typeof v)throw Error()
vs=new v.constructor(l)
}catch{
vs=Array(l)
}
return(b?async()=>{
for(let i=0;i<l;i++)vs[i]=await c(v[i],i,v)
return vs
}:()=>{
for(let i=0;i<l;i++)vs[i]=c(v[i],i,v)
return vs
})()
}
define(String,`pad`,function(b,v){
return(c=>map(false,this.length,c))((v=>{
if(b)return(_,i)=>v[i]||this[i]
const addend=v.length-this.length
return(_,i)=>v[i+addend]||this[i]
})(v.toString())).join(``)
})
console.table([
{this:`00`,b:false,v:1}
,{this:`00`,b:false,v:12}
,{this:`00`,b:false,v:123}
,{this:`00`,b:true,v:1}
,{this:`00`,b:true,v:12}
,{this:`00`,b:true,v:123}
].map(v=>({
default:`${v.b?`"${v.v}".padEnd(2,0)`:`"${v.v}".padStart(2,0)`}=${v.b?v.v.toString().padEnd(2,0):v.v.toString().padStart(2,0)}`
,this:`${`"${v.this}".pad(${v.b},${v.v})`}=${v.this.pad(v.b,v.v)}`
})))
}</script></head></html>
reflection of all[some/every](data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const all=function(b,v,c){
return(b>>>1?async l=>{
b=!(b&0b01)
for(let i=0;i<l;i++)if(b!==!await c(v[i],i,v))return b
return !b
}:l=>{
b=!(b&0b01)
for(let i=0;i<l;i++)if(b!==!c(v[i],i,v))return b
return !b
})(undefined===v.length?v:v.length)
}
console.table([
{this:[0,1,2],b:false,c:()=>false}
,{this:[0,1,2],b:false,c:v=>1===v}
,{this:[0,1,2],b:false,c:()=>true}
,{this:[0,1,2],b:true,c:()=>false}
,{this:[0,1,2],b:true,c:v=>1===v}
,{this:[0,1,2],b:true,c:()=>true}
].map(v=>({
default:`${v.b?`[${v.this}].every(${v.c})`:`[${v.this}].some(${v.c})`}=${v.b?v.this.every(v.c):v.this.some(v.c)}`
,this:`${`all(${v.b},[${v.this}],${v.c})`}=${all(v.b,v.this,v.c)}`
})))
}</script></head></html>
reflection of forEach(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const forEach=function(b,v,c){
return(b?async l=>{
for(let i=0;i<l;i++)await c(v[i],i,v)
}:l=>{
for(let i=0;i<l;i++)c(v[i],i,v)
})(undefined===v.length?v:v.length)
}
}</script></head></html>
reflection of map(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const map=function(b,v,c){
let l,vs
try{
if(undefined===v.length){
l=v
throw Error()
}
l=v.length
if(`string`===typeof v)throw Error()
vs=new v.constructor(l)
}catch{
vs=Array(l)
}
return(b?async()=>{
for(let i=0;i<l;i++)vs[i]=await c(v[i],i,v)
return vs
}:()=>{
for(let i=0;i<l;i++)vs[i]=c(v[i],i,v)
return vs
})()
}
}</script></head></html>
reflection of sort(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const sort=function(b,v,c){
if(undefined===v.length)throw Error()
let l,vs
try{
l=v.length
if(`string`===typeof v)throw Error()
vs=new v.constructor(l)
}catch{
vs=Array(l)
}
return(b?async()=>{
for(let y=0,x;(x=y)<l;vs[x]=v[y++])while(0<x&&0>await c(v[y],vs[x-1]))vs[x]=vs[--x]
return vs
}:()=>{
for(let y=0,x;(x=y)<l;vs[x]=v[y++])while(0<x&&0>c(v[y],vs[x-1]))vs[x]=vs[--x]
return vs
})()
}
}</script></head></html>
reflection of reduce(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const reduce=function(b,v,c,a){
return(b?async l=>{
for(let i=0;i<l;i++)a=await c(a,v[i],i,v)
return a
}:l=>{
for(let i=0;i<l;i++)a=c(a,v[i],i,v)
return a
})(undefined===v.length?v:v.length)
}
}</script></head></html>
reflection of flatMap(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const flatMap=function(b,v,c){
return(b?async l=>{
const vs=Array(l)
for(let i=0;i<l;i++)vs.length+=Array.isArray(vs[i]=await c(v[i],i,v))?vs[i].length:1
let i=(vs.length-=l)-1
for(let y=l;y--;)if(Array.isArray(vs[y]))for(let x=vs[y].length;x--;)vs[i--]=vs[y][x]
else vs[i--]=vs[y]
return vs
}:l=>{
const vs=Array(l)
for(let i=0;i<l;i++)vs.length+=Array.isArray(vs[i]=c(v[i],i,v))?vs[i].length:1
let i=(vs.length-=l)-1
for(let y=l;y--;)if(Array.isArray(vs[y]))for(let x=vs[y].length;x--;)vs[i--]=vs[y][x]
else vs[i--]=vs[y]
return vs
})(undefined===v.length?v:v.length)
}
}</script></head></html>
reflection of flatten(data uri)
code
<!doctype html><head><style>
body{background-color:#ffffffff;color:#000000ff}
@media(prefers-color-scheme:dark){body{background-color:#000000ff;color:#ffffffff}}
</style>
<script>{
const define=(o,p,value)=>Reflect.defineProperty(o.prototype,p,{value})
define(Array,`flatten`,function(){
const l=this.length
for(let i=0;i<l;i++)this.length+=Array.isArray(this[i])?this[i].length:1
let i=(this.length-=l)-1
for(let y=l;y--;)if(Array.isArray(this[y]))for(let x=this[y].length;x--;)this[i--]=this[y][x]
else this[i--]=this[y]
return this
})
}</script></head></html>