UNPKG

5.03 kBJavaScriptView Raw
1"use strict";
2
3/**
4 * @param {array} lines - Array of lines of code.
5 * @param {string=" "} indentIncrement - Indentation to add for every new bloc.
6 * @param {string=""} currentIndent - Starting indentation.
7 * @example
8 * generate(["a", ["b", "c"], "d"], 2) ===
9 * "a" + "\n" +
10 * " b" + "\n" +
11 * " c" + "\n" +
12 * "d"
13 */
14exports.generateCode = generateCode;
15/**
16 * If `code` is an array, prepend `prefix` before the first item (if it is a string) and append
17 * `suffix` after last item (if it is a string).
18 * @param {array} code
19 * @param {string} prefix
20 * @param {string=''} suffix
21 */
22exports.surroundCode = surroundCode;
23/**
24 * @param {string} msg - Error message.
25 * @param {object=undefined} ex - Exception to bubble with.
26 */
27exports.throwError = throwError;
28/**
29 * Determine if an object is special or not. An object is special as soon as it owns the "0"
30 * attribute.
31 * @param {object} obj - Object to test.
32 * @param {string=undefined} expectedName - If defined, test the value of `obj[0]`. If it is
33 * different, return false.
34 */
35exports.isSpecial = isSpecial;
36/**
37 * Determnine if `name` is a valid Javascript identifier.
38 * @param {string} name
39 * @return {boolean}
40 */
41exports.isJavascriptIdentifier = isJavascriptIdentifier;
42/**
43 * @param {string} text - The text you want to capitalize.
44 * @return {string} the `text` with the first letter uppercased.
45 */
46exports.cap = capitalize;
47/**
48 * @return the CamelCase version of the input string.
49 */
50exports.camel = camel;
51/**
52 * @return the capitalized CamelCase version of the input string.
53 */
54exports.camelCap = camelCap;
55/**
56 * Transform a Javascript value into an array of strings/arrays. This is made for code génération.
57 * @return {array} Lines to be used with `generateCode`.
58 */
59exports.indentValue = indentValue;
60/**
61 * @param {string} attribName - Name of the atribute you want access.
62 * @return {string} Util.att("foobar") === ".foobar"
63 * Util.att("foo-bar") === '["foo-bar"]'
64 */
65exports.att = att;
66
67
68function 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
79function 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
93function 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
100function 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
110function camelCap( name ) {
111 return capitalize( camel( name ) );
112}
113
114
115function indentValue( value ) {
116 var type = typeof value;
117 if( Array.isArray( value ) ) return indentValueArray( value );
118}
119
120function 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
141function 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
149var RX_JAVASCRIPT_IDENTIFIER = /^[$a-z_][$a-z_0-9]*$/i;
150function isJavascriptIdentifier( name ) {
151 return RX_JAVASCRIPT_IDENTIFIER.test( name );
152}
153
154
155function 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
171var RX_NAME = /^[a-z_$][a-z_$0-9]*$/gi;
172function att( name ) {
173 if( RX_NAME.test( name ) ) return "." + name;
174 return "[" + JSON.stringify( name ) + "]";
175}