More low-hanging transformations
Maths
- [x]
Number(s)→ ~+[s]~+s - [ ]
n=n-1→n-- - [ ]
n=n+1→n++ - [ ] all the
n = n ° m→n°=mfamily, with°being+,-,/,*and% - [x] ~~
ParseFloat(n)→+n~~ - [ ]
x!=y→x^yifxandyare integers - [ ]
-10→~9,-100→~99, … and so on :D - [x]
N00…0withNan integer →Ne00…0 - [ ]
a>=b→a>b-1if-10 < b < 10 - [ ]
a<=b→a<b-1if-10 < b < 10 - [x]
Math.abs(a)→a>0?a:-aifais one char. - [ ]
Math.sqrt(a)→a**.5
Arrays
- [ ]
a.concat()→[...a] - [ ]
x[x.length-N]→x.at(N)
Random
- [ ]
Math.random()<0.5→new Date&1, asDatereturns a number ofms, albeit it might be problematic if called in a super-tight loop. - [ ]
Math.floor(Math.random()*N))→new Date%N
Misc
- [x]
while(1)→for(;;)
Thanks Voisin. What's the difference between +[s] and +s?
Oops, it's a typo, +s should be enough for Number
The Random ones are not feasible. Math.random is not the same as new Date, the former is probably a much more secure random source, while the latter is a poor man's random source. I believe we already do the while(1) => for(;;) one.
The parseInt and parseFloat conversions are actually not such a good idea. Try inputting something invalid and the output is different between the two.
> parseFloat("aaa")
NaN
> +"aaa"
NaN
looks identical do me.
Try "5a" for example, or try parseInt("5.5") != +"5.5"
On Tue, Jan 21, 2025, 12:02 Julien Voisin @.***> wrote:
parseFloat("aaa") NaN +"aaa" NaN
looks identical do me.
— Reply to this email directly, view it on GitHub https://github.com/tdewolff/minify/issues/794#issuecomment-2604405397, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABKOGHVJWUTU2PXMRO76XOT2LYSK3AVCNFSM6AAAAABVRBRLX6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDMMBUGQYDKMZZG4 . You are receiving this because you commented.Message ID: @.***>
> +"5a"
NaN
> parseInt("5a")
5
> parseFloat("5a")
5
Damn :<
I'm not sure n=n-1 → n-- is good either. What if I have x = y = n = n-1?
It's possible that --n works—I think it should, but I haven't tested it.
Yes, many of the conversions are incorrect, but converting (n = n + 1) => (++n) should be fine (not n++ as you correctly mention).
The ones that are valid and still missing are:
- [x]
n=n+1→++nandn=n-1→--n - [x] all the
n = n + m→n+=mfor all operators (+,-,/,*,%,**,<<,>>,>>>,&,^,|,&&,||,??) - [ ]
-10→~9,-100→~99, … and so on :D - [x]
Math.sqrt(a)→a**.5 - [ ]
x[x.length-N]→x.at(N)(not sure if this is valid for all types that permit the index operator?)
I've implemented the n=n+1 => ++n, the n=n+a => n+=a, and Math.sqrt(a) => a**.5 transformations!