|
113 | 113 | * @private static |
114 | 114 | * @property proxy will match the token-identifying wrapper (e.g. the "{{ info }}" of <span>{{ info }}</span>). |
115 | 115 | * @property notation will match any string that appears to be dot syntax (e.g. "{{ someObject.someProperty }}"). |
| 116 | + * @property address will match object address of dot or bracket syntax (e.g. foo.bar and foo[bar] and foo[bar][too]). |
116 | 117 | * @property expression will match any prefix to the token value (e.g. "{{ parseDateToTimeValue:someDateString }}"). |
117 | 118 | * @property expressionsplit separates transforms applied progressively (e.g. "{{ thenDoThis:doThis:someValue }}"). |
118 | 119 | * @property orsplit separates tokens progressively considered for value (e.g. "{{ perhaps|maybe|probably|definitely }}"). |
|
121 | 122 | var PATTERN = { |
122 | 123 | proxy: /{{\s*\b(.*)\s* }}/, |
123 | 124 | notation: /[\.\[\]]+/, |
| 125 | + address: /(\w+(\.\w+)+)|(\w+(\[\w+\])+)/, |
124 | 126 | expression: /^(.*)\:(?=[^:]*$)(.*)/, |
125 | 127 | expressionsplit: /\:/, |
126 | 128 | orsplit: /\|/, |
|
137 | 139 | * @private extendable |
138 | 140 | * "{{ join:value }} inserts a comma-delineated string concatenating an assumed array of values. |
139 | 141 | * "{{ toLocaleString:value }}" inserts commas into large numbers (e.g. 1234567.890 becomes 1,234,567.89). |
140 | | - * "{{ parseDateToTimeValue:value }}" inserts the time value parsed from a standard date string. |
| 142 | + * "{{ parseDateToTimeValue:value }}" inserts the time value parsed from an assumed standard date string. |
141 | 143 | * "{{ toMebibytes:value }}" converts bytes to mebibytes. (e.g. 1048576 becomes "1.00"). |
142 | 144 | * "{{ exists:value }}" inserts "true" or "false" if value exists (e.g. as a CSS class name to denote visibility). |
143 | | - * "{{ absent:value }}" inserts "true" or "false" if value does not exist (e.g. as a CSS class name to denote visibility). |
144 | | - * "{{ combineString:(value1, value2,...) }}" for combining strings and attributes to one string value. |
145 | | - * Input param can either be string literal or property name of the data model. |
| 145 | + * "{{ absent:value }}" inserts "true" or "false" if value does not exist (the obverse of exists). |
| 146 | + * "{{ combineString:('abc', token1, '123', token2...) }}" combines arbitrary strings and/or token values. |
146 | 147 | */ |
147 | 148 | var TRANSFORMER = { |
148 | 149 | join: function (value, index) { |
|
166 | 167 | }, |
167 | 168 | combineString: function (values, index) { |
168 | 169 | var result = ""; |
169 | | - |
170 | 170 | if (values) { |
171 | 171 | values.forEach(function (value) { |
172 | 172 | result += value; |
173 | 173 | }) |
174 | 174 | } |
175 | | - |
176 | 175 | return result; |
177 | 176 | } |
178 | 177 | }; |
|
241 | 240 |
|
242 | 241 | var parseDotNotation = function (obj, notation) { |
243 | 242 | /* TODO: return [obj].concat(notation.split(".")).reduce(function(parent, child){ return !!parent && child in parent ? parent[child] : null; }); */ |
244 | | - var address = notation.split(PATTERN.notation); |
| 243 | + var address = notation.split(/\.|\[|\]\[|\]/).filter(function(item){ return item.length > 0; }); // (PATTERN.notation); |
245 | 244 | while (address.length > 0 && !!obj) { |
246 | 245 | var index, property = address.shift(); |
247 | 246 | if (Array.isArray(obj)) { |
|
345 | 344 | return alt in datum; |
346 | 345 | })[0]; |
347 | 346 | } |
348 | | - if (notation) { |
| 347 | + if (/\(/.test(proxy[0])) { |
| 348 | + // this is a parenthetical, e/g combineString:(foo,bar) |
| 349 | + value = []; |
| 350 | + var params = proxy.replace(/[\(\)]/g, ""); |
| 351 | + params = params.split(","); |
| 352 | + params.forEach(function (param) { |
| 353 | + param = param.trim(); |
| 354 | + if (PATTERN.address.test(param)) { |
| 355 | + value.push(parseDotNotation(datum, param)); |
| 356 | + } else if (datum.hasOwnProperty(param)) { |
| 357 | + value.push(datum[param]); |
| 358 | + } else { |
| 359 | + value.push(param.replace(/[\'\"]/g, "")); |
| 360 | + } |
| 361 | + }); |
| 362 | + } else if (notation) { |
349 | 363 | value = parseDotNotation(datum, proxy); |
350 | 364 | } else { |
351 | 365 | value |
|
396 | 410 | return alt in datum; |
397 | 411 | })[0]; |
398 | 412 | } |
399 | | - if (notation) { |
400 | | - value = parseDotNotation(datum, proxy); |
401 | | - } else if (!!proxy && proxy[0] == '(') { |
| 413 | + if (/\(/.test(proxy[0])) { |
| 414 | + // this is a parenthetical, e/g combineString:(foo,bar) |
402 | 415 | value = []; |
403 | | - //remove brackets like ( and ) |
404 | 416 | var params = proxy.replace(/[\(\)]/g, ""); |
405 | 417 | params = params.split(","); |
406 | 418 | params.forEach(function (param) { |
407 | 419 | param = param.trim(); |
408 | | - value.push(datum.hasOwnProperty(param) ? datum[param] : param.replace(/[\'\"]/g, "")); |
| 420 | + if (PATTERN.address.test(param)) { |
| 421 | + value.push(parseDotNotation(datum, param)); |
| 422 | + } else if (datum.hasOwnProperty(param)) { |
| 423 | + value.push(datum[param]); |
| 424 | + } else { |
| 425 | + value.push(param.replace(/[\'\"]/g, "")); |
| 426 | + } |
409 | 427 | }); |
410 | | - |
| 428 | + } else if (notation) { |
| 429 | + value = parseDotNotation(datum, proxy); |
411 | 430 | } else { |
412 | 431 | value |
413 | 432 | = proxy === TOKEN.index ? i |
|
0 commit comments