This bitty bit of code takes JavaScript and makes it smaller. It could do the wrong thing if it misparsed a division operator as a regular expression. JavaScript::Squish was misparsing a regexp so I wrote this instead.

I sorted list rules to check for syntax by frequency within my code. Here's my table:

NameCount
WhiteSpace3621
*3281
"..."231
// ...166
/* ... */80
'...'60
/.../5
division1
#!/usr/bin/perl $/ = undef; print smoosh( scalar <> ); use constant DEBUG => !!$ENV{DEBUG}; use constant { PRE => ( DEBUG ? '<' : '' ), POST => ( DEBUG ? '>' : '' ), }; my ( $UnicodeEscapeSequence, $IdentifierStart, $WhiteSpace, $LineTermi +nator ); BEGIN { $UnicodeEscapeSequence = '\u[\da-fA-F]{4}'; $IdentifierStart = '$_\p{Lu}\p{Ll}\p{Lt}\p{Lm}\p{Lo}\p{Nl}\p{Mn}\p{Mc}\p{Nd}\p{ +Pc}'; $WhiteSpace = '\t\x0b\f \xa0\p{Zs}'; $LineTerminator = '\r\n\x{2028}\x{2029}'; } sub smoosh { local $_ = shift @_; my $out = ''; my %stat if DEBUG; while ( not m{\G\z}gc ) { if (0) { } # whitespace elsif (m{\G[$WhiteSpace]+}gco) { DEBUG and ++$stat{WhiteSpace}; my $pos = pos; # Insert whitespace only if I'm joining things that could # be identifiers. if (( $out =~ /[$IdentifierStart]\z/o or $out =~ /$UnicodeEscapeSequence\z/o ) and ( m{\G[$IdentifierStart]}gco or m{\G$UnicodeEscapeSequence}gco ) ) { $out .= ' '; } pos() = $pos; } # everything else elsif (m{\G[^'"/$LineTerminator$WhiteSpace]+}gco) { DEBUG and ++$stat{'*'}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # line endings. Must be separated from other whitespace # because somtimes there are implicit semicolons here. elsif (m{\G[$LineTerminator]+}gco) { DEBUG and ++$stat{LineTerminator}; if ( $out !~ /[$LineTerminator]\z/o ) { $out .= "\n"; } } # String literals elsif (m{\G"(?:[^\\"$LineTerminator]*|\\.)*"}sgco) { DEBUG and ++$stat{'"StringLiteral"'}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } elsif (m{\G'(?:[^\\'$LineTerminator]*|\\.)*'}sgco) { DEBUG and ++$stat{q['StringLiteral']}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # Comments elsif (m{\G//[^$LineTerminator]*[$LineTerminator]}gco) { DEBUG and ++$stat{SingleLineComment}; } elsif (m{\G/\*(?:[^*\\]*|\\.)*\*/}sgc) { DEBUG and ++$stat{MultiLineComment}; } # Regexps elsif (m{\G/(?:[^/\\$LineTerminator]*|\\.)+/}gco) { DEBUG and ++$stat{RegularExpressionLiteral}; $out .= PRE . substr( $_, $-[0], $+[0] - $-[0] ) . POST; } # Division elsif (m{\G/(?![/*])}gc) { DEBUG and ++$stat{Division}; $out .= PRE . '/' . POST; } else { my $pos = pos; my $line = 1 + tr/\x0a\x0d\x2028\x2029//; die 'Error parsing JavaScript: "' . substr( $_, pos(), 20 ) . q[" at line $line.\n]; } } if (DEBUG) { print "STATS:\n"; for ( sort { $stat{$b} <=> $stat{$a} } keys %stat ) { print " $_: $stat{$_}\n"; } } return $out; }

In reply to JavaScript Smoosher by diotalevi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.