handlebars-helper-repeat
Advanced tools
Comparing version 1.0.0 to 2.0.0
70
index.js
/*! | ||
* handlebars-helper-repeat <https://github.com/helpers/handlebars-helper-repeat> | ||
* | ||
* Copyright (c) 2014-2015, Jon Schlinkert. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2014-2018, Jon Schlinkert. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -11,36 +11,60 @@ | ||
var isNumber = require('is-number'); | ||
var merge = require('mixin-deep'); | ||
module.exports = function repeat(n, options) { | ||
var isNum = isNumber(n); | ||
module.exports = function() { | ||
var args = arguments; | ||
if (!isNum) { | ||
options = n; | ||
n = 0; | ||
if (args.length > 2) { | ||
throw new Error(`Expected 0, 1 or 2 arguments, but got ${args.length}`); | ||
} | ||
options = options || {}; | ||
var opts = merge({count: n}, options, options.hash); | ||
var ctx = this.context | ||
? merge({}, this.context, opts) | ||
: merge({}, this, opts); | ||
var options = args[args.length - 1]; | ||
var hash = options.hash || {}; | ||
var count = hash.count || args[0] || 0; | ||
var start = hash.start || 0; | ||
var step = hash.step || 1; | ||
var data = { count, start, step }; | ||
if (opts.count) { | ||
return block(ctx); | ||
if (typeof args[0] === 'string' && !isNumber(args[0]) && args[0] !== '') { | ||
return repeat(data, args[0]); | ||
} | ||
return options.inverse(ctx); | ||
if (data.count > 0) { | ||
return repeatBlock(data, this, options); | ||
} | ||
return options.inverse(this); | ||
}; | ||
function block(options) { | ||
var max = options.count; | ||
function repeat({ count, start, step }, thisArg) { | ||
var max = count * step + start; | ||
var index = start; | ||
var str = ''; | ||
var start = options.start || 0; | ||
for (var i = start; i < (max + start); i++) { | ||
var data = merge({index: i}, options); | ||
str += options.fn(options, {data: data}); | ||
while (index < max) { | ||
str += thisArg; | ||
index += step; | ||
} | ||
return str; | ||
} | ||
function repeatBlock({ count, start, step }, thisArg, options) { | ||
var max = count * step + start; | ||
var index = start; | ||
var str = ''; | ||
do { | ||
var data = { | ||
index, | ||
count, | ||
start, | ||
step, | ||
first: index === start, | ||
last: index >= max - step | ||
}; | ||
var blockParams = [index, data]; | ||
str += options.fn(thisArg, { data, blockParams }); | ||
index += data.step; | ||
} while (index < max); | ||
return str; | ||
} |
{ | ||
"name": "handlebars-helper-repeat", | ||
"description": "Handlebars block helper for repeating whatever is inside the block _n_ times.", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"homepage": "https://github.com/helpers/handlebars-helper-repeat", | ||
@@ -19,16 +19,19 @@ "author": "Jon Schlinkert (https://github.com/jonschlinkert)", | ||
}, | ||
"scripts": { | ||
"test": "mocha" | ||
}, | ||
"dependencies": { | ||
"is-number": "^3.0.0", | ||
"mixin-deep": "^1.1.3" | ||
"is-number": "^5.0.0" | ||
}, | ||
"devDependencies": { | ||
"engine-handlebars": "^0.8.0", | ||
"gulp-format-md": "^0.1.11", | ||
"handlebars": "^4.0.6", | ||
"mocha": "^3.2.0", | ||
"templates": "^0.25.3" | ||
"engine-handlebars": "^0.8.2", | ||
"gulp-format-md": "^1.0.0", | ||
"handlebars": "^4.0.11", | ||
"handlebars-helpers": "^0.10.0", | ||
"mocha": "^3.5.3", | ||
"templates": "^1.2.9" | ||
}, | ||
"keywords": [ | ||
"assemble", | ||
"assemblehelper", | ||
"compile", | ||
"handlebars", | ||
@@ -40,2 +43,3 @@ "handlebars helper", | ||
"repeat", | ||
"repeat-string", | ||
"template", | ||
@@ -56,9 +60,6 @@ "templates" | ||
"handlebars-helpers", | ||
"repeat-string", | ||
"template-helpers" | ||
] | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"verb-generate-readme" | ||
], | ||
"lint": { | ||
@@ -65,0 +66,0 @@ "reflinks": true |
@@ -1,5 +0,7 @@ | ||
# handlebars-helper-repeat [![NPM version](https://img.shields.io/npm/v/handlebars-helper-repeat.svg?style=flat)](https://www.npmjs.com/package/handlebars-helper-repeat) [![NPM monthly downloads](https://img.shields.io/npm/dm/handlebars-helper-repeat.svg?style=flat)](https://npmjs.org/package/handlebars-helper-repeat) [![NPM total downloads](https://img.shields.io/npm/dt/handlebars-helper-repeat.svg?style=flat)](https://npmjs.org/package/handlebars-helper-repeat) [![Linux Build Status](https://img.shields.io/travis/helpers/handlebars-helper-repeat.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/handlebars-helper-repeat) | ||
# handlebars-helper-repeat [![NPM version](https://img.shields.io/npm/v/handlebars-helper-repeat.svg?style=flat)](https://www.npmjs.com/package/handlebars-helper-repeat) [![NPM monthly downloads](https://img.shields.io/npm/dm/handlebars-helper-repeat.svg?style=flat)](https://npmjs.org/package/handlebars-helper-repeat) [![NPM total downloads](https://img.shields.io/npm/dt/handlebars-helper-repeat.svg?style=flat)](https://npmjs.org/package/handlebars-helper-repeat) [![Linux Build Status](https://img.shields.io/travis/helpers/handlebars-helper-repeat.svg?style=flat&label=Travis)](https://travis-ci.org/helpers/handlebars-helper-repeat) | ||
> Handlebars block helper for repeating whatever is inside the block _n_ times. | ||
Please consider following this project's author, [Jon Schlinkert](https://github.com/jonschlinkert), and consider starring the project to show your :heart: and support. | ||
## Install | ||
@@ -18,3 +20,4 @@ | ||
```js | ||
var helper = require('handlebars-helper-repeat'); | ||
const repeat = require('handlebars-helper-repeat'); | ||
handlebars.registerHelper('repeat', repeat); | ||
``` | ||
@@ -25,6 +28,6 @@ | ||
```js | ||
var handlebars = require('handlebars'); | ||
const handlebars = require('handlebars'); | ||
// 2. register the helper, name it whatever you want | ||
handlebars.registerHelper('repeat', helper); | ||
handlebars.registerHelper('repeat', require('handlebars-helper-repeat')); | ||
@@ -35,3 +38,5 @@ // 3. register some partials | ||
// 4. use in templates | ||
handlebars.compile('{{repeat 2}}{{> button }}{{/repeat}}')({text: 'Click me!'}); | ||
const fn = handlebars.compile('{{#repeat 2}}{{> button }}{{/repeat}}'); | ||
console.log(fn({text: 'Click me!'})); | ||
//=> '<button>Click me!</button><button>Click me!</button>' | ||
@@ -71,6 +76,4 @@ ``` | ||
```handlebars | ||
{{#repeat '2'}} | ||
<div id="{{@index}}"> | ||
{{> button }} | ||
</div> | ||
{{#repeat 2}} | ||
<div id="{{@index}}"> {{> button }} </div> | ||
{{/repeat}} | ||
@@ -82,8 +85,4 @@ ``` | ||
```html | ||
<div id="0"> | ||
<button>Click me</button> | ||
</div> | ||
<div id="1"> | ||
<button>Click me</button> | ||
</div> | ||
<div id="0"> <button>Click me</button> </div> | ||
<div id="1"> <button>Click me</button> </div> | ||
``` | ||
@@ -93,29 +92,41 @@ | ||
### Related projects | ||
<details> | ||
<summary><strong>Contributing</strong></summary> | ||
* [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/assemble/handlebars-helpers) | [homepage](https://github.com/assemble/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.") | ||
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.") | ||
### Contributing | ||
Pull requests and stars are always welcome. For bugs and feature requests, [please create an issue](../../issues/new). | ||
### Building docs | ||
</details> | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
<details> | ||
<summary><strong>Running Tests</strong></summary> | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install && npm test | ||
``` | ||
### Running tests | ||
</details> | ||
Install dev dependencies: | ||
<details> | ||
<summary><strong>Building docs</strong></summary> | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -d && npm test | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
</details> | ||
### Related projects | ||
You might also be interested in these projects: | ||
* [handlebars-helpers](https://www.npmjs.com/package/handlebars-helpers): More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate… [more](https://github.com/helpers/handlebars-helpers) | [homepage](https://github.com/helpers/handlebars-helpers "More than 130 Handlebars helpers in ~20 categories. Helpers can be used with Assemble, Generate, Verb, Ghost, gulp-handlebars, grunt-handlebars, consolidate, or any node.js/Handlebars project.") | ||
* [repeat-string](https://www.npmjs.com/package/repeat-string): Repeat the given string n times. Fastest implementation for repeating a string. | [homepage](https://github.com/jonschlinkert/repeat-string "Repeat the given string n times. Fastest implementation for repeating a string.") | ||
* [template-helpers](https://www.npmjs.com/package/template-helpers): Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or… [more](https://github.com/jonschlinkert/template-helpers) | [homepage](https://github.com/jonschlinkert/template-helpers "Generic JavaScript helpers that can be used with any template engine. Handlebars, Lo-Dash, Underscore, or any engine that supports helper functions.") | ||
### Author | ||
@@ -125,12 +136,13 @@ | ||
* [github/jonschlinkert](https://github.com/jonschlinkert) | ||
* [twitter/jonschlinkert](http://twitter.com/jonschlinkert) | ||
* [LinkedIn Profile](https://linkedin.com/in/jonschlinkert) | ||
* [GitHub Profile](https://github.com/jonschlinkert) | ||
* [Twitter Profile](https://twitter.com/jonschlinkert) | ||
### License | ||
Copyright © 2017, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT license](LICENSE). | ||
Copyright © 2018, [Jon Schlinkert](https://github.com/jonschlinkert). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.3.0, on January 08, 2017._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.6.0, on March 23, 2018._ |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9145
1
56
141
6
+ Addedis-number@5.0.0(transitive)
- Removedmixin-deep@^1.1.3
- Removedfor-in@1.0.2(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-extendable@1.0.1(transitive)
- Removedis-number@3.0.0(transitive)
- Removedis-plain-object@2.0.4(transitive)
- Removedisobject@3.0.1(transitive)
- Removedkind-of@3.2.2(transitive)
- Removedmixin-deep@1.3.2(transitive)
Updatedis-number@^5.0.0