Siorki / RegPack

Self-contained packer for size-constrained JS code
Other
299 stars 14 forks source link

Unpacking routine using Array.reduce() on dictionary #98

Open Siorki opened 4 years ago

Siorki commented 4 years ago

Idea taken from code golf entry by Arnauld https://codegolf.stackexchange.com/questions/206619/print-a-tongue-twister

Full entry :

_=>`246 / I'm5ure/he56/ the4ore,
The/5eash/ s/ells`.split`/`.reduce((s,p,i)=>s.split(i).join(p),`S0by35h652 are46,1.
So if50on3n1524ore5h6.`)

Details :

// The first string is a dictionary that is immediately turned into an array
`246 / I'm5ure/he56/ the4ore,
The/5eash/ s/ells`.split`/`

// The second string, used as initial value for reduce()
`S0by35h652 are46,1.So if50on3n1524ore5h6.`

The technique is similar to the one from crusher/regpack, with split(a).join(b) being called once for each token. Catch : tokens are numbers, starting from 0, automatically assigned to variable i which means the implementation is limited to the range 0-9.

Can we elaborate on this to use a full token range and shorten the unpacking routine ?