mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
129 lines
5.3 KiB
JavaScript
129 lines
5.3 KiB
JavaScript
$(document).ready(function() {
|
|
|
|
module("Utility functions (uniqueId, template)");
|
|
|
|
test("utility: noConflict", function() {
|
|
var underscore = _.noConflict();
|
|
ok(underscore.isUndefined(_), "The '_' variable has been returned to its previous state.");
|
|
var intersection = underscore.intersect([-1, 0, 1, 2], [1, 2, 3, 4]);
|
|
equals(intersection.join(', '), '1, 2', 'but the intersection function still works');
|
|
window._ = underscore;
|
|
});
|
|
|
|
test("utility: identity", function() {
|
|
var moe = {name : 'moe'};
|
|
equals(_.identity(moe), moe, 'moe is the same as his identity');
|
|
});
|
|
|
|
test('utility: breakLoop', function() {
|
|
var result = null;
|
|
_([1,2,3,4,5,6]).each(function(num) {
|
|
result = num;
|
|
if (num == 3) _.breakLoop();
|
|
});
|
|
equals(result, 3, 'broke out of a loop');
|
|
});
|
|
|
|
test("utility: uniqueId", function() {
|
|
var ids = [], i = 0;
|
|
while(i++ < 100) ids.push(_.uniqueId());
|
|
equals(_.uniq(ids).length, ids.length, 'can generate a globally-unique stream of ids');
|
|
});
|
|
|
|
test("utility: times", function() {
|
|
var vals = [];
|
|
_.times(3, function (i) { vals.push(i); });
|
|
ok(_.isEqual(vals, [0,1,2]), "is 0 indexed");
|
|
//
|
|
vals = [];
|
|
_(3).times(function (i) { vals.push(i); });
|
|
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
|
|
});
|
|
|
|
test("utility: mixin", function() {
|
|
_.mixin({
|
|
myReverse: function(string) {
|
|
return string.split('').reverse().join('');
|
|
}
|
|
});
|
|
equals(_.myReverse('panacea'), 'aecanap', 'mixed in a function to _');
|
|
equals(_('champ').myReverse(), 'pmahc', 'mixed in a function to the OOP wrapper');
|
|
});
|
|
|
|
test("utility: template", function() {
|
|
var basicTemplate = _.template("<%= thing %> is gettin' on my noives!");
|
|
var result = basicTemplate({thing : 'This'});
|
|
equals(result, "This is gettin' on my noives!", 'can do basic attribute interpolation');
|
|
|
|
var fancyTemplate = _.template("<ul><% for (key in people) { %><li><%= people[key] %></li><% } %></ul>");
|
|
result = fancyTemplate({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
|
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
|
|
|
var namespaceCollisionTemplate = _.template("<%= pageCount %> <%= thumbnails[pageCount] %> <% _.each(thumbnails, function(p) { %><div class=\"thumbnail\"><img src=\"<%= p %>\" /></div><% }); %>");
|
|
result = namespaceCollisionTemplate({
|
|
pageCount: 3,
|
|
thumbnails: {
|
|
1: "p1-thumbnail.gif",
|
|
2: "p2-thumbnail.gif",
|
|
3: "p3-thumbnail.gif"
|
|
}
|
|
});
|
|
equals(result, "3 p3-thumbnail.gif <div class=\"thumbnail\"><img src=\"p1-thumbnail.gif\" /></div><div class=\"thumbnail\"><img src=\"p2-thumbnail.gif\" /></div><div class=\"thumbnail\"><img src=\"p3-thumbnail.gif\" /></div>");
|
|
|
|
var noInterpolateTemplate = _.template("<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
|
|
result = noInterpolateTemplate();
|
|
equals(result, "<div><p>Just some text. Hey, I know this is silly but it aids consistency.</p></div>");
|
|
|
|
var quoteTemplate = _.template("It's its, not it's");
|
|
equals(quoteTemplate({}), "It's its, not it's");
|
|
|
|
var quoteInStatementAndBody = _.template("<% if(foo == 'bar'){ %>Statement quotes and 'quotes'.<% } %>");
|
|
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
|
|
|
var withNewlinesAndTabs = _.template('This\n\t\tis: <%= x %>.\n\tok.\nend.');
|
|
equals(withNewlinesAndTabs({x: 'that'}), 'This\n\t\tis: that.\n\tok.\nend.');
|
|
|
|
_.templateSettings = {
|
|
start : '{{',
|
|
end : '}}',
|
|
interpolate : /\{\{=(.+?)\}\}/g
|
|
};
|
|
|
|
var custom = _.template("<ul>{{ for (key in people) { }}<li>{{= people[key] }}</li>{{ } }}</ul>");
|
|
result = custom({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
|
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
|
|
|
var customQuote = _.template("It's its, not it's");
|
|
equals(customQuote({}), "It's its, not it's");
|
|
|
|
var quoteInStatementAndBody = _.template("{{ if(foo == 'bar'){ }}Statement quotes and 'quotes'.{{ } }}");
|
|
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
|
|
|
_.templateSettings = {
|
|
start : '<?',
|
|
end : '?>',
|
|
interpolate : /<\?=(.+?)\?>/g
|
|
};
|
|
|
|
var customWithSpecialChars = _.template("<ul><? for (key in people) { ?><li><?= people[key] ?></li><? } ?></ul>");
|
|
result = customWithSpecialChars({people : {moe : "Moe", larry : "Larry", curly : "Curly"}});
|
|
equals(result, "<ul><li>Moe</li><li>Larry</li><li>Curly</li></ul>", 'can run arbitrary javascript in templates');
|
|
|
|
var customWithSpecialCharsQuote = _.template("It's its, not it's");
|
|
equals(customWithSpecialCharsQuote({}), "It's its, not it's");
|
|
|
|
var quoteInStatementAndBody = _.template("<? if(foo == 'bar'){ ?>Statement quotes and 'quotes'.<? } ?>");
|
|
equals(quoteInStatementAndBody({foo: "bar"}), "Statement quotes and 'quotes'.");
|
|
|
|
_.templateSettings = {
|
|
start : '{{',
|
|
end : '}}',
|
|
interpolate : /\{\{(.+?)\}\}/g
|
|
};
|
|
|
|
var mustache = _.template("Hello {{planet}}!");
|
|
equals(mustache({planet : "World"}), "Hello World!", "can mimic mustache.js");
|
|
});
|
|
|
|
});
|