UNPKG

3.9 kBJavaScriptView Raw
1"use strict";
2
3var Util = require("./boilerplate.util");
4
5module.exports = function( cls ) {
6 /**
7 * @member parseData
8 * @param {amy} data
9 * @example
10 * x.parseData("Hello") === ['"Hello"']
11 * x.parseData([27, "Hello"]) === ["[", ["27,", '"Hello"'], "]"]
12 */
13 cls.prototype.parseData = parseData;
14 return cls;
15};
16
17
18/**
19 * @return {array}
20 */
21function 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 * @example
29 * [] -> ["[]"]
30 * [27] -> ["[27]"]
31 * [18, 27] -> ["[", ["18", "27" ], "]"]
32 */
33function 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
60function 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
83var RX_TAG = /^(H[1-9]|[A-Z]+)$/g;
84var RX_XJS = /^[a-z0-9-]+(\.[a-z0-9-]+)+$/g;
85function 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
97function parseSpecialTag( is, data ) {
98
99}
100
101
102function parseSpecialXjs( is, data ) {
103
104}
105
106
107function 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
125function 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 * Ensure `key` is a valid Javascript identifier by quoting it if necessary.
138 */
139function protectKey( key ) {
140 if( Util.isJavascriptIdentifier( key ) ) return key;
141 return JSON.stringify( "" + key );
142}
143
144/**
145 * If `arr` is an array of length 1, return `unbox( arr[0] )`.
146 * Otherwise, return `arr`.
147 */
148function unbox( arr ) {
149 if( Array.isArray( arr ) && arr.length === 1 ) {
150 return unbox( arr[0] );
151 } else {
152 return arr;
153 }
154}