Skip to content

Commit cf0584c

Browse files
ptrdopsylwester
authored andcommitted
Abstracts combineString transformer
1 parent 669d9cb commit cf0584c

3 files changed

Lines changed: 49 additions & 19 deletions

File tree

README.md

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,24 @@ By default, the value inserted for a token will be applied as the nodeValue of t
208208

209209
| Modifer | Usage | Description |
210210
|----------|-----------|---------|
211-
| html | {{ html:value }} | Insert value with innerHTML rather than nodeValue (e.g. escaped content, unicodes, html entities). |
212-
| concat | {{ concat:value }} | Concatenates value in-context to any adjacent content within the targeted attribute or node. |
213-
| forin | {{ forin:value }} | Iterates over every property found in the assumed object (see [object](/example/simple-javascript/object.html) example). |
214-
| boolean | {{ boolean:value }} | Inserts boolean per resolved truthiness of value (e.g. *checked="false"*, *disabled="true"*). |
211+
| html | `{{ html:token }}` | Insert value with innerHTML rather than nodeValue (e.g. escaped content, unicodes, html entities). |
212+
| concat | `{{ concat:token }}` | Concatenates value in-context to any adjacent content within the targeted attribute or node. |
213+
| forin | `{{ forin:token }}` | Iterates over every property found in the assumed object (see [object](/example/simple-javascript/object.html) example). |
214+
| boolean | `{{ boolean:token }}` | Inserts boolean per resolved truthiness of value (e.g. *checked="false"*, *disabled="true"*, *class="true"*). |
215215

216216
***
217217
### Transformers
218+
Transformers allow for modification of a value prior to insertion into the targeted location. This allows for performing logic on the data without embedding logic into the markup. The transformer function receive two arguments: the value and the index of the current iteration.
219+
220+
| Transformer | Usage | Description |
221+
|----------|-----------|---------|
222+
| join | `{{ join:token }}` | Inserts comma-delineated string concatenating an assumed array of values. |
223+
| toLocaleString | `{{ toLocaleString:token }}` | Inserts commas into large numbers (e.g. 1234567.890 becomes 1,234,567.89). |
224+
| parseDateToTimeValue | `{{ parseDateToTimeValue:token }}` | Inserts a time value parsed from an assumed standard date string. |
225+
| toMebibytes | `{{ toMebibytes:token }}` | Converts bytes to mebibytes. (e.g. 1048576 becomes "1.00"). |
226+
| exists | `{{ exists:token }}` | Inserts `true` or `false` if value exists (e.g. as a CSS class name to denote visibility). |
227+
| absent | `{{ absent:token }}` | Inserts `true` or `false` if value does not exist (the obverse of `exists`). |
228+
| combineString | `{{ combineString:('foo',token1,'bar',token2,…) }}` | Combines arbitrary strings and/or token values (e.g. to construct an URL). |
218229

219230
***
220231
### Public API

example/simple-javascript/transformer.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<article>
4343
<section>
4444
<blockquote itemscope itemid="{{ id }}" itemtype="https://schema.org/UserTweets" hidden>
45-
<h5 itemprop="screen_name">@{{ concat:user.screen_name }} tweeted:</h5>
45+
<h5 itemprop="screen_name">{{ concat:combineString:("@", user.screen_name, " tweeted from ", user.location) }}:</h5>
4646
<p itemprop="text">{{ text }}</p>
4747
<time itemprop="created_at" datetime="{{ parseDateToTimeValue:created_at }}">{{ formatDate:created_at }}</time>
4848
</blockquote>

lib/microdata-template.js

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@
113113
* @private static
114114
* @property proxy will match the token-identifying wrapper (e.g. the "{{ info }}" of <span>{{ info }}</span>).
115115
* @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]).
116117
* @property expression will match any prefix to the token value (e.g. "{{ parseDateToTimeValue:someDateString }}").
117118
* @property expressionsplit separates transforms applied progressively (e.g. "{{ thenDoThis:doThis:someValue }}").
118119
* @property orsplit separates tokens progressively considered for value (e.g. "{{ perhaps|maybe|probably|definitely }}").
@@ -121,6 +122,7 @@
121122
var PATTERN = {
122123
proxy: /{{\s*\b(.*)\s* }}/,
123124
notation: /[\.\[\]]+/,
125+
address: /(\w+(\.\w+)+)|(\w+(\[\w+\])+)/,
124126
expression: /^(.*)\:(?=[^:]*$)(.*)/,
125127
expressionsplit: /\:/,
126128
orsplit: /\|/,
@@ -137,12 +139,11 @@
137139
* @private extendable
138140
* "{{ join:value }} inserts a comma-delineated string concatenating an assumed array of values.
139141
* "{{ 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.
141143
* "{{ toMebibytes:value }}" converts bytes to mebibytes. (e.g. 1048576 becomes "1.00").
142144
* "{{ 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.
146147
*/
147148
var TRANSFORMER = {
148149
join: function (value, index) {
@@ -166,13 +167,11 @@
166167
},
167168
combineString: function (values, index) {
168169
var result = "";
169-
170170
if (values) {
171171
values.forEach(function (value) {
172172
result += value;
173173
})
174174
}
175-
176175
return result;
177176
}
178177
};
@@ -241,7 +240,7 @@
241240

242241
var parseDotNotation = function (obj, notation) {
243242
/* 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);
245244
while (address.length > 0 && !!obj) {
246245
var index, property = address.shift();
247246
if (Array.isArray(obj)) {
@@ -345,7 +344,22 @@
345344
return alt in datum;
346345
})[0];
347346
}
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) {
349363
value = parseDotNotation(datum, proxy);
350364
} else {
351365
value
@@ -396,18 +410,23 @@
396410
return alt in datum;
397411
})[0];
398412
}
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)
402415
value = [];
403-
//remove brackets like ( and )
404416
var params = proxy.replace(/[\(\)]/g, "");
405417
params = params.split(",");
406418
params.forEach(function (param) {
407419
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+
}
409427
});
410-
428+
} else if (notation) {
429+
value = parseDotNotation(datum, proxy);
411430
} else {
412431
value
413432
= proxy === TOKEN.index ? i

0 commit comments

Comments
 (0)