1+ /* 
2+  * Copyright (c) 2014 MKLab. All rights reserved. 
3+  * 
4+  * Permission is hereby granted, free of charge, to any person obtaining a 
5+  * copy of this software and associated documentation files (the "Software"), 
6+  * to deal in the Software without restriction, including without limitation 
7+  * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
8+  * and/or sell copies of the Software, and to permit persons to whom the 
9+  * Software is furnished to do so, subject to the following conditions: 
10+  * 
11+  * The above copyright notice and this permission notice shall be included in 
12+  * all copies or substantial portions of the Software. 
13+  * 
14+  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
15+  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
16+  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
17+  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
18+  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
19+  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
20+  * DEALINGS IN THE SOFTWARE. 
21+  * 
22+  */ 
23+ class  CodeWriter  { 
24+     /** 
25+      * CodeWriter 
26+      * @constructor  
27+      */ 
28+     constructor ( indentString )  { 
29+ 
30+         /** @member  {Array.<string>} lines */ 
31+         this . lines  =  [ ] ; 
32+ 
33+         /** @member  {string} indentString */ 
34+         this . indentString  =  ( indentString  ? indentString  : "    " ) ;  // default 4 spaces 
35+ 
36+         /** @member  {Array.<string>} indentations */ 
37+         this . indentations  =  [ ] ; 
38+     } 
39+ 
40+     /** 
41+      * Indent 
42+      */ 
43+     indent ( )  { 
44+         this . indentations . push ( this . indentString ) ; 
45+     } ; 
46+ 
47+     /** 
48+      * Outdent 
49+      */ 
50+     outdent ( )  { 
51+         this . indentations . splice ( this . indentations . length  -  1 ,  1 ) ; 
52+     } ; 
53+ 
54+     /** 
55+      * Write a line 
56+      * @param  {string } line 
57+      */ 
58+     writeLine ( line )  { 
59+         if  ( line )  { 
60+             this . lines . push ( this . indentations . join ( "" )  +  line ) ; 
61+         }  else  { 
62+             this . lines . push ( "" ) ; 
63+         } 
64+     } ; 
65+ 
66+     /** 
67+      * Return as all string data 
68+      * @return  {string } 
69+      */ 
70+     getData ( )  { 
71+         return  this . lines . join ( "\n" ) ; 
72+     } ; 
73+ } 
74+ 
75+ module . exports  =  CodeWriter ; 
0 commit comments