1 | "use strict";
|
2 |
|
3 | var Util = require("./boilerplate.util");
|
4 |
|
5 | module.exports = function( cls ) {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | cls.prototype.parseData = parseData;
|
14 | return cls;
|
15 | };
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 | function parseData( data ) {
|
22 | if( Array.isArray( data ) ) return parseArray.call( this, data );
|
23 | if( data && typeof data === 'object' ) return parseObject.call( this, data );
|
24 | return [JSON.stringify( data )];
|
25 | }
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | function parseArray( data ) {
|
34 | if( data.length === 0 ) return ["[]"];
|
35 | if( data.length === 1 ) {
|
36 | var parsedItem = parseData.call( this, data[0] );
|
37 | if( Array.isArray( parsedItem ) ) {
|
38 | if( parsedItem.length === 0 ) return ["[[]]"];
|
39 | if( parsedItem.length === 1 ) return ["[" + parsedItem[0] + "]"];
|
40 | return ["[", parsedItem, "]"];
|
41 | } else {
|
42 | return ["[" + parsedItem + "]"];
|
43 | }
|
44 | }
|
45 | var result = [];
|
46 | data.map( parseData.bind( this ) ).forEach(function(itm, idx, arr) {
|
47 | var isLastItem = idx === arr.length - 1;
|
48 | itm.forEach(function (elem, idx, arr) {
|
49 | if( !isLastItem || idx < arr.length - 1 ) {
|
50 | result.push( appendComma( elem ) );
|
51 | } else {
|
52 | result.push( elem );
|
53 | }
|
54 | });
|
55 | });
|
56 | return ["[", result, "]"];
|
57 | }
|
58 |
|
59 |
|
60 | function parseObject( data ) {
|
61 | if( Util.isSpecial(data) ) return parseSpecial( data[0], data );
|
62 |
|
63 | var that = this;
|
64 |
|
65 | var keys = Object.keys( data );
|
66 | if( keys.length === 0 ) return ["{}"];
|
67 | if( keys.length === 1 ) {
|
68 | var parsedItem = parseData.call( this, data[keys[0]] );
|
69 | return Util.surroundCode( parsedItem, "{" + protectKey(keys[0])+ ":", "}" );
|
70 | }
|
71 | var result = [];
|
72 | keys.forEach(function (key, k) {
|
73 | var isLast = k === keys.length - 1;
|
74 | var itm = parseData.call( that, data[key] );
|
75 | Util.surroundCode( itm, protectKey(keys[k])+ ":", isLast ? '' : ',' ).forEach(function( elem ) {
|
76 | result.push( elem );
|
77 | });
|
78 | });
|
79 | return ["{", result, "}"];
|
80 | }
|
81 |
|
82 |
|
83 | var RX_TAG = /^(H[1-9]|[A-Z]+)$/g;
|
84 | var RX_XJS = /^[a-z0-9-]+(\.[a-z0-9-]+)+$/g;
|
85 | function parseSpecial( id, data ) {
|
86 | if( RX_TAG.test( id ) ) return parseSpecialTag( id, data );
|
87 | if( RX_XJS.test( id ) ) return parseSpecialXjs( id, data );
|
88 |
|
89 | switch( id.toLowerCase() ) {
|
90 | case 'intl': return parseSpecialIntl( data );
|
91 | }
|
92 |
|
93 | Util.throwError( "Unknown special form: \"" + id + "\"!" );
|
94 | }
|
95 |
|
96 |
|
97 | function parseSpecialTag( is, data ) {
|
98 |
|
99 | }
|
100 |
|
101 |
|
102 | function parseSpecialXjs( is, data ) {
|
103 |
|
104 | }
|
105 |
|
106 |
|
107 | function parseSpecialIntl( data ) {
|
108 | var id = data[1];
|
109 | if( typeof id !== 'string' ) {
|
110 | Util.throwError(
|
111 | "In {intl <id>}, <id> must be a string! Instead, we found <id>="
|
112 | + JSON.toString( id )
|
113 | );
|
114 | }
|
115 |
|
116 | var pieces = id.split( '/' );
|
117 | if( pieces.length === 1 ) return "_(" + JSON.stringify( id ) + ")";
|
118 | if( pieces.length > 2 ) {
|
119 | Util.throwError( "In {intl <id>}, <id> can contain at most one slash!" );
|
120 | }
|
121 | return "this.$elements" + Util.att( pieces[0] ) + "._(" + JSON.stringify( pieces[1] ) + ")";
|
122 | }
|
123 |
|
124 |
|
125 | function appendComma( data ) {
|
126 | if( Array.isArray( data ) ) {
|
127 | return data;
|
128 | } else if( data === '[' || data === '{' ) {
|
129 | return data;
|
130 | } else {
|
131 | return data + ",";
|
132 | }
|
133 | }
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | function protectKey( key ) {
|
140 | if( Util.isJavascriptIdentifier( key ) ) return key;
|
141 | return JSON.stringify( "" + key );
|
142 | }
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 | function unbox( arr ) {
|
149 | if( Array.isArray( arr ) && arr.length === 1 ) {
|
150 | return unbox( arr[0] );
|
151 | } else {
|
152 | return arr;
|
153 | }
|
154 | }
|