xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / dust / test / examples.js

9.8 KB
1(function(exports){
2
3exports.dustExamples = [
4  {
5    name:     "intro",
6    source:   "{#stream}{#delay}{.}{/delay}{/stream}",
7    context:  (function(){
8                var d = 1;
9                return {
10                  stream: function() {
11                    return "asynchronous templates for the browser and node.js".split('');
12                  },
13                  delay: function(chunk, context, bodies) {
14                    return chunk.map(function(chunk) {
15                      setTimeout(function() {
16                        chunk.render(bodies.block, context).end();
17                      }, d++ * 15);
18                    });
19                  }
20                }
21              })
22  },
23  {
24    name:     "plain",
25    source:   "Hello World!",
26    context:  {},
27    expected: "Hello World!"
28  },
29  {
30    name:     "replace",
31    source:   "Hello {name}! You have {count} new messages.",
32    context:  { name: "Mick", count: 30 },
33    expected: "Hello Mick! You have 30 new messages."
34  },
35  {
36    name:     "zero",
37    source:   "{#zero}{.}{/zero}",
38    context:  { zero: 0 },
39    expected: "0"
40  },
41  {
42    name:     "array",
43    source:   "{#names}{title} {name}{~n}{/names}",
44    context:  { title: "Sir", names: [ { name: "Moe" }, { name: "Larry" }, { name: "Curly" } ] },
45    expected: "Sir Moe\nSir Larry\nSir Curly\n"
46  },
47  {
48    name:     "empty_array",
49    source:   "{#names}{title} {name}{~n}{/names}",
50    context:  { title: "Sir", names: [] },
51    expected: ""
52  },
53  {
54    name:     "implicit",
55    source:   "{#names}{.}{~n}{/names}",
56    context:  { names: ["Moe", "Larry", "Curly"] },
57    expected: "Moe\nLarry\nCurly\n"
58  },
59  {
60    name:     "object",
61    source:   "{#person}{root}: {name}, {age}{/person}",
62    context:  { root: "Subject", person: { name: "Larry", age: 45 } },
63    expected: "Subject: Larry, 45"
64  },
65  {
66    name:     "rename_key",
67    source:   "{#person foo=root}{foo}: {name}, {age}{/person}",
68    context:  { root: "Subject", person: { name: "Larry", age: 45 } },
69    expected: "Subject: Larry, 45"
70  },
71  {
72    name:     "force_current",
73    source:   "{#person}{.root}: {name}, {age}{/person}",
74    context:  { root: "Subject", person: { name: "Larry", age: 45 } },
75    expected: ": Larry, 45"
76  },
77  {
78    name:     "path",
79    source:   "{foo.bar}",
80    context:  { foo: {bar: "Hello!"} },
81    expected: "Hello!"
82  },
83  {
84    name:     "escaped",
85    source:   "{safe|s}{~n}{unsafe}",
86    context:  { safe: "<script>alert('Hello!')</script>", unsafe: "<script>alert('Goodbye!')</script>" },
87    expected: "<script>alert('Hello!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
88  },
89  {
90    name:     "escape_pragma",
91    source:   "{%esc:s}\n  {unsafe}{~n}\n  {%esc:h}\n    {unsafe}\n  {/esc}\n{/esc}",
92    context:  { unsafe: "<script>alert('Goodbye!')</script>" },
93    expected: "<script>alert('Goodbye!')</script>\n&lt;script&gt;alert('Goodbye!')&lt;/script&gt;"
94  },
95  {
96    name:     "else_block",
97    source:   "{#foo}\n"         +
98              "  foo,{~s}\n"     +
99              "{:else}\n"        +
100              "  not foo,{~s}\n" +
101              "{/foo}\n"         +
102              "{#bar}\n"         +
103              "  bar!\n"         +
104              "{:else}\n"        +
105              "  not bar!\n"     +
106              "{/bar}",
107    context:  { foo: true, bar: false },
108    expected: "foo, not bar!"
109  },
110  {
111    name:     "conditional",
112    source:   "{?tags}\n"                     +
113              "  <ul>{~n}\n"                  +
114              "    {#tags}\n"                 +
115              "      {~s} <li>{.}</li>{~n}\n" +
116              "    {/tags}\n"                 +
117              "  </ul>\n"                     +
118              "{:else}\n"                     +
119              "  No Tags!\n"                  +
120              "{/tags}\n"                     +
121              "{~n}\n"                        +
122              "{^likes}\n"                    +
123              "  No Likes!\n"                 +
124              "{:else}\n"                     +
125              "  <ul>{~n}\n"                  +
126              "    {#likes}\n"                +
127              "      {~s} <li>{.}</li>{~n}\n" +
128              "    {/likes}\n"                +
129              "  </ul>\n"                     +
130              "{/likes}",
131    context:  { tags: [], likes: ["moe", "larry", "curly", "shemp"] },
132    expected: "No Tags!\n"                    +
133              "<ul>\n"                        +
134              "  <li>moe</li>\n"              +
135              "  <li>larry</li>\n"            +
136              "  <li>curly</li>\n"            +
137              "  <li>shemp</li>\n"            +
138              "</ul>"
139  },
140  {
141    name:     "sync_key",
142    source:   "Hello {type} World!",
143    context:  {
144                type: function(chunk) {
145                  return "Sync";
146                }
147              },
148    expected: "Hello Sync World!"
149  },
150  {
151    name:     "async_key",
152    source:   "Hello {type} World!",
153    context:  {
154                type: function(chunk) {
155                  return chunk.map(function(chunk) {
156                    dust.nextTick(function() {
157                      chunk.end("Async");
158                    });
159                  });
160                }
161              },
162    expected: "Hello Async World!"
163  },
164  {
165    name:     "sync_chunk",
166    source:   "Hello {type} World!",
167    context:  {
168                type: function(chunk) {
169                  return chunk.write("Chunky");
170                }
171              },
172    expected: "Hello Chunky World!"
173  },
174  {
175    name:     "async_iterator",
176    source:   "{#numbers}{#delay}{.}{/delay}{@sep}, {/sep}{/numbers}",
177    context:  {
178                numbers: [3, 2, 1],
179                delay: function(chunk, context, bodies) {
180                  return chunk.map(function(chunk) {
181                    setTimeout(function() {
182                      chunk.render(bodies.block, context).end();
183                    }, Math.ceil(Math.random()*10));
184                  });
185                }
186              },
187    expected: "3, 2, 1"
188  },
189  {
190    name:     "filter",
191    source:   "{#filter}foo {bar}{/filter}",
192    context:  {
193                filter: function(chunk, context, bodies) {
194                  return chunk.tap(function(data) {
195                    return data.toUpperCase();
196                  }).render(bodies.block, context).untap();
197                },
198
199                bar: "bar"
200              },
201    expected: "FOO BAR"
202  },
203  {
204    name:     "context",
205    source:   "{#list:projects}{name}{:else}No Projects!{/list}",
206    context:  {
207                list: function(chunk, context, bodies) {
208                  var items = context.current(),
209                      len   = items.length;
210
211                  if (len) {
212                    chunk.write("<ul>\n");
213                    for (var i=0; i<len; i++) {
214                      chunk = chunk.write("<li>")
215                        .render(bodies.block, context.push(items[i]))
216                        .write("</li>\n");
217                    }
218                    return chunk.write("</ul>");
219                  } else if (bodies['else']) {
220                    return chunk.render(bodies['else'], context);
221                  }
222                  return chunk;
223                },
224
225                projects: [
226                  {name: "Mayhem"},
227                  {name: "Flash"},
228                  {name: "Thunder"}
229                ]
230              },
231    expected: "<ul>\n"             +
232              "<li>Mayhem</li>\n"  +
233              "<li>Flash</li>\n"   +
234              "<li>Thunder</li>\n" +
235              "</ul>"
236  },
237  {
238    name:     "params",
239    source:   "{#helper foo=\"bar\"/}",
240    context:  {
241                helper: function(chunk, context, bodies, params) {
242                  return chunk.write(params.foo);
243                }
244              },
245    expected: "bar"
246  },
247  {
248    name:     "partials",
249    source:   '{>replace/} {>"plain"/} {>"{ref}"/}',
250    context:  { name: "Jim", count: 42, ref: "plain" },
251    expected: "Hello Jim! You have 42 new messages. Hello World! Hello World!"
252  },
253  {
254    name:     "partial_context",
255    source:   "{>replace:.profile/}",
256    context:  { profile: { name: "Mick", count: 30 } },
257    expected: "Hello Mick! You have 30 new messages."
258  },
259  {
260    name:     "base_template",
261    source:   "Start{~n}\n"       +
262              "{+title}\n"        +
263              "  Base Title\n"    +
264              "{/title}\n"        +
265              "{~n}\n"            +
266              "{+main}\n"         +
267              "  Base Content\n"  +
268              "{/main}\n"         +
269              "{~n}\n"            +
270              "End",
271    context:  {},
272    expected: "Start\nBase Title\nBase Content\nEnd"
273  },
274  {
275    name:     "child_template",
276    source:   "{^xhr}\n"              +
277              "  {>base_template/}\n" +
278              "{:else}\n"             +
279              "  {+main/}\n"          +
280              "{/xhr}\n"              +
281              "{<title}\n"            +
282              "  Child Title\n"       +
283              "{/title}\n"            +
284              "{<main}\n"             +
285              "  Child Content\n"     +
286              "{/main}\n",
287    context:  {xhr: false},
288    expected: "Start\nChild Title\nChild Content\nEnd"
289  },
290  {
291    name:     "recursion",
292    source:   "{name}{~n}{#kids}{>recursion:./}{/kids}",
293    context:  {
294                name: '1',
295                kids: [
296                  {
297                    name: '1.1',
298                    kids: [
299                      {name: '1.1.1'}
300                    ]
301                  }
302                ]
303              },
304    expected: "1\n1.1\n1.1.1\n"
305  },
306  {
307    name:     "comments",
308    source:   "{!\n"                      +
309              "  Multiline\n"             +
310              "  {#foo}{bar}{/foo}\n"     +
311              "!}\n"                      +
312              "{!before!}Hello{!after!}",
313    context:  {},
314    expected: "Hello"
315  }
316];
317
318})(typeof exports !== "undefined" ? exports : window);