minify icon indicating copy to clipboard operation
minify copied to clipboard

More low-hanging transformations

Open jvoisin opened this issue 11 months ago • 9 comments

Maths

  • [x] Number(s) → ~+[s]~ +s
  • [ ] n=n-1n--
  • [ ] n=n+1n++
  • [ ] all the n = n ° mn°=m family, with ° being +, -, /, * and %
  • [x] ~~ParseFloat(n)+n~~
  • [ ] x!=yx^y if x and y are integers
  • [ ] -10~9, -100~99, … and so on :D
  • [x] N00…0 with N an integer → Ne00…0
  • [ ] a>=ba>b-1 if -10 < b < 10
  • [ ] a<=ba<b-1 if -10 < b < 10
  • [x] Math.abs(a)a>0?a:-a if a is one char.
  • [ ] Math.sqrt(a)a**.5

Arrays

  • [ ] a.concat()[...a]
  • [ ] x[x.length-N]x.at(N)

Random

  • [ ] Math.random()<0.5new Date&1, as Date returns a number of ms, 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(;;)

jvoisin avatar Jan 20 '25 20:01 jvoisin

Thanks Voisin. What's the difference between +[s] and +s?

tdewolff avatar Jan 20 '25 20:01 tdewolff

Oops, it's a typo, +s should be enough for Number

jvoisin avatar Jan 20 '25 20:01 jvoisin

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.

tdewolff avatar Jan 20 '25 21:01 tdewolff

> parseFloat("aaa")
NaN
> +"aaa"
NaN 

looks identical do me.

jvoisin avatar Jan 21 '25 11:01 jvoisin

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: @.***>

tdewolff avatar Jan 21 '25 12:01 tdewolff

> +"5a"
NaN
> parseInt("5a")
5
> parseFloat("5a")
5 

Damn :<

jvoisin avatar Jan 21 '25 16:01 jvoisin

I'm not sure n=n-1n-- 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.

torstenvl avatar Sep 26 '25 13:09 torstenvl

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++n and n=n-1--n
  • [x] all the n = n + mn+=m for 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?)

tdewolff avatar Sep 29 '25 19:09 tdewolff

I've implemented the n=n+1 => ++n, the n=n+a => n+=a, and Math.sqrt(a) => a**.5 transformations!

tdewolff avatar Sep 29 '25 20:09 tdewolff