0.5.2 • Published 11 years ago
lohand v0.5.2
Lohand
A library for wrapping many Lodash functions into Handlebars.
Installation
npm install lohandBasic Setup
var handlebars = require('handlebars');
require('lohand')
.registerAll(handlebars);Individual Helpers
var lohand = require('lohand');
lohand.helpers; // a dictionary of helpers { name: fn() {} }Testing
npm testHelpers
Iterators
every
{{#every 3 [1, 2, 3]}}
<!-- index % 3 === 0 -->
{{else}}
<!-- otherwise -->
{{/every}}iter
{{#iter items}}
{{i}} // index
{{iPlus1}} // index + 1
{{/iter}}Collection
at
{{at [1, 2, 3] 1}}
→ 2pluck
{{at {foo: 'bar'} 'foo'}}
→ barsample
{{#each (sample [1, 2, 3], 2)}}
{{this}};
{{/each}}
1;3;shuffle
{{#each (shuffle [1, 2, 3])}}
{{this}};
{{/each}}
3;1;2;size
{{size [1, 2, 3]}}
→ 3Comparisons
endsWith
{{#endsWith 'abc' 'c'}}
<!-- string starts with 'c' -->
{{else}}
<!-- string does not start with 'c' -->
{{/endsWith}}eq
{{#eq val1 val2}}
<!-- === -->
{{else}}
<!-- !== -->
{{/eq}}greaterThan
{{#greaterThan left right}}
<!-- left > right -->
{{else}}
<!-- left <= right -->
{{/greaterThan}}lessThan
{{#lessThan left right}}
<!-- left < right -->
{{else}}
<!-- left >= right -->
{{/lessThan}}startsWith
{{#startsWith 'abc' 'a'}}
// if 'abc' starts with 'a'
{{else}}
// if 'abc' does not start with 'a'
{{/startsWith}}Strings
camelCase
Utilizes Lodash’s camelCase.
{{camelCase 'Foo Bar'}}
→ fooBarcapitalize
{{capitalize 'foo bar'}}
→ Foo Bardeburr
{{deburr 'déjà vu'}}
→ deja vuencodeURIComponent
{{encodeURIComponent 'Foo Bar'}}
→ Foo%20BarkebabCase
{{kebabCase 'Foo Bar'}} // 'foo-bar'markdown
Utilizes Showdown to transform text into Markdown.
{{{markdown '# Foo'}}}
→ <h1>Foo</h1>encodeURIComponent
{{encodeURIComponent 'Foo Bar'}}
→ Foo%20BarkebabCase
{{kebabCase 'Foo Bar'}} // 'foo-bar'markdown
{{{markdown '# Foo'}}}pad
{{pad 'abc' 8}} // ' abc '
{{pad 'abc' 8 '_-'}} // _-abc_-_padLeft
{{padLeft 'abc' 6}} // ' abc'
{{padLeft 'abc' 6 '_-'}} // _-_abcpadRight
{{padRight 'abc' 6}} // 'abc '
{{padRight 'abc' 6 '_-'}} // abc_-_parseInt
{{parseInt '08'}} // 8possessive
{{possessive 'Susan'}} // Susan’s
{{possessive 'Chris'}} // Chris’repeat
{{repeat '*' 3}} // ***snakeCase
{{snakeCase 'Foo Bar'}} // foo_barstartCase
A naive version of “Title Case” which simply upper cases the first letter of each word.
{{startCase '--foo-bar'}}
<!-- Foo Bar -->trim
{{trim ' abc '}}
<!-- abc -->
{{trim '-_-abc-_-' '_-'}}
<!-- abc -->trimLeft
{{trimLeft ' abc '}}
<!-- 'abc ' -->
{{trimLeft '-_-abc-_-' '_-'}}
<!-- abc-_- -->trimRight
{{trimRight ' abc '}}
<!-- ' abc' -->
{{trimRight '-_-abc-_-' '_-'}}
<!-- -_-abc -->trunc
{{trunc 'hi-diddly-ho there, neighborino'}}
<!-- hi-diddly-ho there, neighbo... -->
{{trunc 'hi-diddly-ho there, neighborino' 24}}
<!-- hi-diddly-ho there, n... -->unescape
{{unescape 'fred, barney, & pebbles'}}
<!-- fred, barney, & pebbles -->words
{{#each (words 'foo bar')}}
<div>{{this}}</div>
{{/each}}
<div>foo</div>
<div>bar</div>union
{{union [1, 2, 3] [4, 5]}}
// → [1, 2, 3, 4, 5]