Skip to content

Commit

Permalink
refactor, clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
jonschlinkert committed Jan 8, 2017
1 parent 11b10da commit 56dcec5
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 63 deletions.
52 changes: 20 additions & 32 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,35 @@ var isNumber = require('is-number');
var merge = require('mixin-deep');

module.exports = function repeat(n, options) {
/* jshint validthis: true */
var hasNumber = isNumber(n);
var context = {};
var self = this;
var isNum = isNumber(n);

if (!hasNumber) {
if (!isNum) {
options = n;
n = 0;
}

if (self && self.context) {
merge(self, self.context);
}

if (options.hasOwnProperty('hash')) {
merge(options, options.hash);
}

if (options.count) {
n = options.count;
hasNumber = true;
}
options = options || {};
var opts = merge({count: n}, options, options.hash);
var ctx = this.context
? merge({}, this.context, opts)
: merge({}, this, opts);

merge(options, self, context);
if (hasNumber) {
return block(options);
} else {
return options.inverse(options);
if (opts.count) {
return block(ctx);
}

function block(opts) {
opts = opts || {};
var str = '';
return options.inverse(ctx);
};

var hash = opts.hash || {};
hash.start = hash.start || 0;
function block(options) {
var max = options.count;
var str = '';

for (var i = hash.start; i < n + hash.start; i++) {
hash.index = i;
var start = options.start || 0;

str += opts.fn(opts, {data: hash});
}
return str;
for (var i = start; i < (max + start); i++) {
var data = merge({index: i}, options);
str += options.fn(options, {data: data});
}
return str;
}
62 changes: 31 additions & 31 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,76 +7,76 @@

'use strict';

/* deps:mocha */
require('mocha');
var assert = require('assert');
var should = require('should');
var Templates = require('templates');
var helper = require('./');
var Template = require('template');
var handlebars;
var template;
var app;

describe('handlebars', function () {
beforeEach(function () {
describe('handlebars', function() {
beforeEach(function() {
handlebars = require('handlebars');

handlebars.registerHelper('repeat', helper);
handlebars.registerPartial('button', '<button>{{text}}</button>');
handlebars.registerPartial('outter', '<button>{{> inner }}</button>');
handlebars.registerPartial('inner', '<button>{{zzz}}</button>');
});

it('should repeat a block n times:', function () {
it('should repeat a block n times:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat 2}}{{> button }}\n{{/repeat}}')(ctx);
actual.should.eql('<button>foo</button>\n<button>foo</button>\n');
assert.equal(actual, '<button>foo</button>\n<button>foo</button>\n');
});

it('should output the inverse when no number is specified:', function () {
it('should output the inverse when no number is specified:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
actual.should.eql('Nothing :(');
assert.equal(actual, 'Nothing :(');
});

it('should allow the count to be specified on the hash:', function () {
it('should allow the count to be specified on the hash:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat count=2}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
actual.should.eql('<button>foo</button>\n<button>foo</button>\n');
assert.equal(actual, '<button>foo</button>\n<button>foo</button>\n');
});

it('should expose hash variables as private variables:', function () {
it('should expose hash variables as private variables:', function() {
var ctx = {text: 'foo'};
var a = handlebars.compile('{{#repeat count=2}}{{@count}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
a.should.eql('2<button>foo</button>\n2<button>foo</button>\n');
assert.equal(a, '2<button>foo</button>\n2<button>foo</button>\n');

var b = handlebars.compile('{{#repeat count=2}}{{@index}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
b.should.eql('0<button>foo</button>\n1<button>foo</button>\n');
assert.equal(b, '0<button>foo</button>\n1<button>foo</button>\n');
});

it('should start the index with the given number:', function () {
it('should start the index with the given number:', function() {
var ctx = {text: 'foo'};
var actual = handlebars.compile('{{#repeat count=2 start=17}}{{@index}}{{> button }}\n{{else}}Nothing :({{/repeat}}')(ctx);
actual.should.eql('17<button>foo</button>\n18<button>foo</button>\n');
assert.equal(actual, '17<button>foo</button>\n18<button>foo</button>\n');
});
});

describe('Template', function () {
beforeEach(function () {
template = new Template();
describe('Templates', function() {
beforeEach(function() {
app = new Templates();
handlebars = require('engine-handlebars');
app.create('partial', {viewType: ['partial']});
app.create('page');

template.engine('hbs', handlebars);
template.helper('repeat', helper);
app.engine('hbs', handlebars);
app.helper('repeat', helper);

template.partial('button.hbs', '<button>{{text}}</button>');
template.page('fixture.hbs', '{{#repeat 2}}{{> button }}\n{{/repeat}}');
template.data({text: 'foo'})
app.partial('button.hbs', '<button>{{text}}</button>');
app.page('fixture.hbs', '{{#repeat 2}}{{> button }}\n{{/repeat}}');
app.data({text: 'foo'});
});

it('should work with Template:', function (done) {
template.render('fixture.hbs', function (err, content) {
if (err) console.log(err)
content.should.eql('<button>foo</button>\n<button>foo</button>\n');
it('should work with Templates:', function(cb) {
app.render('fixture.hbs', function(err, view) {
if (err) return cb(err);
assert.equal(view.contents.toString(), '<button>foo</button>\n<button>foo</button>\n');
cb();
});
done();
});
});

0 comments on commit 56dcec5

Please sign in to comment.