|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.cleancode.format.JSArchive.JSCompactor
public class JSArchive.JSCompactor
Compacts a series of text strings,
discarding any unnecessary whitespace as well as all comments.
Typically, you pass each line of a file to the
trimLine
method, generating a revised, compacted line.
However, you may also pass an entire file stored as a single string.
Whatever you pass in, the result will come back as a single line.
So if you pass individual lines, they will retain their "line-nature".
If you pass a block of lines, it becomes a single line.
Note that all lines are terminated with a single newline character regardless of the system on which you are executing. This delimiter works fine in all browsers, and is obviously more compact than the two-character Windows line ending.
Constructor Summary | |
---|---|
JSArchive.JSCompactor()
Creates a JSCompactor, the workhorse compaction engine. |
Method Summary | |
---|---|
String |
trimLine(List<String> lines)
Compacts a list of strings, removing any unnecessary whitespace and comments. |
String |
trimLine(String s)
Compacts a String removing any unnecessary whitespace and comments. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public JSArchive.JSCompactor()
Method Detail |
---|
public String trimLine(String s)
Compacts a String removing any unnecessary whitespace and comments. This method is designed to be called repeatedly for each line of input within a single file. It retains state across calls so that block comments (/* ... ) may span multiple lines.
The input string is tokenized by examining the following delimiters: whitespace, single quote ('), double quote ("), block comment start (/*), and inline comment (//). trimLine handles any mixture of these as JavaScript semantics require, i.e. putting an inline comment (//...) inside a block comment will be ignored, so that even if the ending block comment marker is on the same line, it will still be seen. Also handles escaped quotes inside quotes (i.e. \' inside 'string' or \" inside "string").
The only known anomaly is a minor issue with regular expressions. Just as an apostrophe (') is a string delimiter, JavaScript uses the virgule (/) as a regular expression delimiter. Inside a regular expression, one should be able to use a single or double quote character uninhibited, but JSCompactor will see these as starting a string. Simply escape these characters inside the regular expression to avoid this issue. That is, instead of something like /ab*c'd/ use /ab*c\'d/.
In the event of a parsing error, this is marked with a block comment (/* ...) at the end of the string.
s
- input string
trimLine(List)
public String trimLine(List<String> lines)
lines
- list of input strings to optimize on one line
trimLine(String)
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
CleanCode Java Libraries | Copyright © 2001-2012 Michael Sorens - Revised 2012.12.10 |