1 | "use strict";
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | exports.generateCode = generateCode;
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 | exports.surroundCode = surroundCode;
|
23 |
|
24 |
|
25 |
|
26 |
|
27 | exports.throwError = throwError;
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | exports.isSpecial = isSpecial;
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 | exports.isJavascriptIdentifier = isJavascriptIdentifier;
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | exports.cap = capitalize;
|
47 |
|
48 |
|
49 |
|
50 | exports.camel = camel;
|
51 |
|
52 |
|
53 |
|
54 | exports.camelCap = camelCap;
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | exports.indentValue = indentValue;
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 | exports.att = att;
|
66 |
|
67 |
|
68 | function generateCode( lines, indentIncrement, currentIndent ) {
|
69 | if( typeof indentIncrement === 'undefined' ) indentIncrement = ' ';
|
70 | if( typeof currentIndent === 'undefined' ) currentIndent = '';
|
71 |
|
72 | return lines.map(function (line) {
|
73 | if( Array.isArray( line ) ) return generateCode( line, indentIncrement, currentIndent + indentIncrement );
|
74 | return currentIndent + line;
|
75 | }).join( "\n" );
|
76 | }
|
77 |
|
78 |
|
79 | function isSpecial( obj, expectedName ) {
|
80 | var type = typeof obj;
|
81 | if( type === 'string' || type !== 'object' || Array.isArray(obj) ) return false;
|
82 | if( !obj ) return false;
|
83 | var name = obj[0];
|
84 |
|
85 | if( typeof name !== 'string' ) return false;
|
86 | if( typeof expectedName === 'string' ) {
|
87 | return name.toLowerCase() === expectedName.toLowerCase();
|
88 | }
|
89 | return true;
|
90 | }
|
91 |
|
92 |
|
93 | function capitalize( text ) {
|
94 | if( typeof text !== 'string' ) return text;
|
95 | if( text.length === 0 ) return text;
|
96 | return text.charAt(0).toUpperCase() + text.substr( 1 ).toLowerCase();
|
97 | }
|
98 |
|
99 |
|
100 | function camel( name ) {
|
101 | return name.split('.').map(function( word, wordIdx ) {
|
102 | return word.split('-').map(function( piece, pieceIdx ) {
|
103 | if( wordIdx + pieceIdx === 0 ) return piece.toLowerCase();
|
104 | return capitalize( piece );
|
105 | }).join("");
|
106 | }).join("");
|
107 | }
|
108 |
|
109 |
|
110 | function camelCap( name ) {
|
111 | return capitalize( camel( name ) );
|
112 | }
|
113 |
|
114 |
|
115 | function indentValue( value ) {
|
116 | var type = typeof value;
|
117 | if( Array.isArray( value ) ) return indentValueArray( value );
|
118 | }
|
119 |
|
120 | function indentValueArray( arr ) {
|
121 | if( arr.length === 0 ) return "[]";
|
122 | var last = arr.length - 1;
|
123 | var out = [];
|
124 | arr.forEach(function (itm, idx) {
|
125 | if( idx === last ) {
|
126 | out.push( indentValue( itm ) );
|
127 | } else {
|
128 |
|
129 | }
|
130 | });
|
131 |
|
132 | if( arr.length === 1 ) {
|
133 | var firstItem = indentValue( arr[0] );
|
134 | if( !Array.isArray( firstItem ) ) return "[" + firstItem + "]";
|
135 | }
|
136 |
|
137 | out.push(']');
|
138 | return out;
|
139 | }
|
140 |
|
141 | function throwError( msg, ex ) {
|
142 | if( typeof ex === 'undefined' || !Array.isArray( ex.stack ) ) {
|
143 | throw { error: msg, stack: [] };
|
144 | }
|
145 | ex.stack.push( msg );
|
146 | throw ex;
|
147 | }
|
148 |
|
149 | var RX_JAVASCRIPT_IDENTIFIER = /^[$a-z_][$a-z_0-9]*$/i;
|
150 | function isJavascriptIdentifier( name ) {
|
151 | return RX_JAVASCRIPT_IDENTIFIER.test( name );
|
152 | }
|
153 |
|
154 |
|
155 | function surroundCode( code, prefix, suffix ) {
|
156 | if( !Array.isArray( code ) ) return code;
|
157 | if( code.length < 1 ) return code;
|
158 | if( typeof suffix === 'undefined' ) suffix = '';
|
159 | var first = code[0], last = code[code.length - 1];
|
160 | if( code.length === 1 ) {
|
161 | if( !Array.isArray( first ) ) code[0] = prefix + first + suffix;
|
162 | }
|
163 | else {
|
164 | if( !Array.isArray( first ) ) code[0] = prefix + first;
|
165 | if( !Array.isArray( last ) ) code[code.length - 1] = last + suffix;
|
166 | }
|
167 | return code;
|
168 | }
|
169 |
|
170 |
|
171 | var RX_NAME = /^[a-z_$][a-z_$0-9]*$/gi;
|
172 | function att( name ) {
|
173 | if( RX_NAME.test( name ) ) return "." + name;
|
174 | return "[" + JSON.stringify( name ) + "]";
|
175 | }
|