xonecas.io
818
Du

dustymarkdown / files

dustymarkdown / node_modules / dust / benchmark / suites / mustache_suite.js

2.7 KB
1(function(exports){
2
3var benches = {
4
5  string: {
6    source:  "Hello World!",
7    context: {}
8  },
9
10  replace: {
11    source:  "Hello {{name}}! You have {{count}} new messages.",
12    context: { name: "Mick", count: 30 }
13  },
14
15  array: {
16    source:  "{{#names}}{{name}}{{/names}}",
17    context: { names: [{name: "Moe"}, {name: "Larry"}, {name: "Curly"}, {name: "Shemp"}] }
18  },
19
20  object: {
21    source:  "{{#person}}{{name}}{{age}}{{/person}}",
22    context: { person: { name: "Larry", age: 45 } }
23  },
24
25  partial: {
26    source:   "{{#peeps}}{{>replace}}{{/peeps}}",
27    context:  { peeps: [{name: "Moe", count: 15}, {name: "Larry", count: 5}, {name: "Curly", count: 1}] },
28    partials: { replace: "Hello {{name}}! You have {{count}} new messages." }
29  },
30
31  recursion: {
32    source:   "{{name}}{{#kids}}{{>recursion}}{{/kids}}",
33    context:  {
34                name: '1',
35                kids: [
36                  {
37                    name: '1.1',
38                    kids: [
39                      {name: '1.1.1', kids: []}
40                    ]
41                  }
42                ]
43              },
44    partials: { recursion: "{{name}}{{#kids}}{{>recursion}}{{/kids}}" }
45  },
46
47  filter: {
48    source:   "{{#filter}}foo {{bar}}{{/filter}}",
49    context:  {
50                filter: function() {
51                  return function(text, render) {
52                    return render(text).toUpperCase();
53                  }
54                },
55                bar: "bar"
56              }
57  },
58
59  complex: {
60    source:  "<h1>{{header}}</h1>{{#hasItems}}<ul>{{#items}}{{#current}}" +
61             "<li><strong>{{name}}</strong></li>{{/current}}{{^current}}" +
62             "<li><a href=\"{{url}}\">{{name}}</a></li>{{/current}}"      +
63             "{{/items}}</ul>{{/hasItems}}{{^hasItems}}<p>The list is empty.</p>{{/hasItems}}",
64    context: {
65               header: function() {
66                 return "Colors";
67               },
68               items: [
69                 {name: "red", current: true, url: "#Red"},
70                 {name: "green", current: false, url: "#Green"},
71                 {name: "blue", current: false, url: "#Blue"}
72               ],
73               hasItems: function() {
74                 return this.items.length !== 0;
75               },
76               empty: function() {
77                 return this.items.length === 0;
78               }
79             }
80  }
81}
82
83exports.mustacheBench = function(suite, name, id) {
84  var bench = benches[name],
85      src = bench.source,
86      ctx = bench.context,
87      partials = bench.partials;
88
89  suite.bench(id || name, function(next) {
90    Mustache.to_html(src, ctx, partials);
91    next();
92  });
93}
94
95exports.mustacheBench.benches = benches;
96
97})(typeof exports !== "undefined" ? exports : window);