These are some of my syntactic sugar ideas for JavaScript.
#define add(a,b) a+b
console.log(add(4,7)); //Will output 11.
#define array [2,3]
console.log(array.pop()+array[1]); //Will output 6.| (string=>{ | |
| string=string.replace(/(\[[^\]]+\]{[^{]+})+/g,x=>"<ruby>"+x+"</ruby>"); | |
| var multiReplace=(s,o)=>{ | |
| for(var key in o){ //What if there were collisions (replacing what's already been replaced)? | |
| s=s.replace(key,o[key]); | |
| } | |
| return s; | |
| },matches=[...string.matchAll(/\[[^\]]+\]{[^{]+}/g)],iShift=0; | |
| for(var i=0;i<matches.length;i++){ | |
| var index=matches[i].index,length=matches[i][0].length, |
| // title: Perlin Noise | |
| // author: Anastasia Dunbar | |
| // script: js | |
| var width=240,height=136; | |
| function mix(a,b,t){return(t*(b-a))+a;} | |
| function mod(a,b){return((a%b)+b)%b;} | |
| function fract(x){return x-Math.floor(x);} | |
| function clamp(a,b,c){return Math.min(Math.max(a,b),c);} | |
| function dot(a,b){for(var s=0,i=0;i<a.length;i++){s+=a[i]*b[i];}return s;} | |
| function sign(x){return x>0?1:x<0?-1:0;} |
| //JavaScript uses 32-bits bitwise operands on 64-bits floating-point numbers. | |
| function readBitsLE(bytes,at,size){ | |
| let b=Math.floor(at/8),shift=Math.floor(at)%8, | |
| mask=size>=32?4294967295:(1<<size)-1; | |
| return(( | |
| (bytes[b]|bytes[b+1]<<8|bytes[b+2]<<16|bytes[b+3]<<24)>>>shift| | |
| Math.floor(bytes[b+4])*Math.pow(2,32-shift) | |
| )&mask)>>>0; | |
| } | |
| function writeBitsLE(bytes,at,size,value){ |
| script "Add grid markers" language "javascript"; | |
| var options=[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,20,24,32,64,100,128,256]; | |
| function div(a,b){var temp1=0,temp2=0;temp1=a;temp2=b;return temp1/temp2;} //Since I can't convert int to float. | |
| function addMarkers(index,asRegions,addEnd){ | |
| var start=0,end=0,count=options[index]; | |
| Editor.GetSelectionS(start,end); | |
| if(asRegions){ | |
| for(var i=0;i<count-1;i++){ | |
| EditorSample.AddRegion(IntToStr(i),start+Round(div((end-start)*i,count)),start+Round(div((end-start)*(i+1),count))-1); |
| function multiplyMatrix(a,b){ | |
| var o=[]; | |
| for(var i=0;i<a.length;i++){ | |
| o[i]=[]; | |
| for(var j=0;j<b[0].length;j++){ | |
| var s=0; | |
| for(var k=0;k<a.length;k++){s+=a[i][k]*b[k][j];} | |
| o[i][j]=s; | |
| } | |
| } |
| function clamp(x,minVal,maxVal){return Math.min(Math.max(x,minVal),maxVal);} | |
| function pointInRect(px,py,x,y,w,h){return px>=x&&py>=y&&px<x+w&&py<y+h;} | |
| function drawASCIItable(x,y,w,h,columns=8,fromChar=32,toChar=126){ | |
| stroke(255);noFill(); | |
| rect(x,y,w,h); | |
| if(user.click){user.ASCIItableClicked=pointInRect(mouseX,mouseY,x,y,w,h);} | |
| var charCount=(toChar-fromChar)+1; | |
| for(var i=1;i<columns;i++){ | |
| line(x+(i/columns)*w,y,x+(i/columns)*w,y+h); | |
| } |
| var PI=Math.PI,TAU=Math.PI*2,E=Math.E; | |
| var sin=Math.sin,cos=Math.cos,tan=Math.tan,asin=Math.asin,acos=Math.acos,atan=Math.atan; | |
| var sqrt=Math.sqrt,pow=Math.pow,log=Math.log,exp=Math.exp; | |
| function floor(x){return arguments.length==2?Math.floor(x/arguments[1])*arguments[1]:Math.floor(x);} | |
| var ceil=Math.ceil; | |
| var abs=Math.abs,sign=Math.sign; | |
| function random(...x){return 2===x.length?x[0]+Math.random()*(x[1]-x[0]):Math.random()*(x[0]||1);} | |
| function min(...x){if(x.length===1&&Array.isArray(x[0])){x=x[0];}return Math.min(...x);} | |
| function max(...x){if(x.length===1&&Array.isArray(x[0])){x=x[0];}return Math.max(...x);} | |
| function mod(a,b){return((a%b)+b)%b;} |
| javascript:(()=>{ | |
| let magic="store(code);",hn=location.hostname,d=document,qS=(s,o=document)=>o.querySelector(s), | |
| element,value,description=[new Date().getTime()]; | |
| if(/openprocessing\.org/.test(hn)){ | |
| element=codeMirror; | |
| value=element.getValue(); | |
| }else if(/(wavepot|tinyrave)\.com/.test(hn)){ | |
| element=ace.edit("editor").getSession(); | |
| value=element.getValue(); | |
| }else if(/mytextarea\.com/.test(hn)){ |
| //Note: JavaScript bitwise operators can only handle 32 bits, and 31st bit is used as a sign bit (use `x>>>0` to unsign it). | |
| //Note: `1<<x` is sometimes used like an alternative to `2**x` (or `Math.pow(2,x)`) here. | |
| //sign , (exponent bit)*eb , (mantissa bit)*mb. | |
| function customFloatSize(eb,mb){return eb+mb+1;} | |
| function maximumFloatNumber(eb,mb){let e=1<<(((1<<eb)-2)-1);return e+(e*(1-(1/(1<<mb))));} | |
| function fromCustomFloat(x,eb,mb){ | |
| let sign=(x>>>(eb+mb))&1?-1:1, | |
| maxExp=(1<<eb)-1, | |
| expBits=(x>>>mb)&maxExp, |