1.6.0 • Published 2 years ago

handlebars-ramda-helpers v1.6.0

Weekly downloads
-
License
-
Repository
-
Last release
2 years ago

handlebars-ramda-helpers

version downloads license dependencies coveralls

Provide powerful data processing capabilities for handlebars, by adding the ramda function as helpers.

Usage

Adding r__ before the ramda function name is the handlebars helpers name. Let see an example:

import * as Handlebars from 'handlebars'
import * as HandlebarsRamdaHelpers from 'handlebars-ramda-helpers'

// register helpers before compile
HandlebarsRamdaHelpers.register(Handlebars)


const view = {
  books: [
    { type: 'computer', name: 'C Primer Plus', price: 10 },
    { type: 'computer', name: 'An Introduction to Programming in Go', price: 20 },
    { type: 'computer', name: 'Javascript the definitive guide', price: 100 },
    { type: 'comic', name: 'One Piece', price: 100 },
  ]
}

const template = `
I have a lot of books:

{{~r__define 'divide' (r__compose (r__join '')  (r__repeat '-'))}}

{{r__padRight 40 ' ' 'bookName'}} | bookPrice
{{divide 40}} | {{divide 10}}
{{#each books}}
{{r__padRight 40 ' ' name}} | {{r__padRight 10 ' ' price}}
{{/each}}

{{r__define 'priceList' (r__pluck 'price' books)}}
The total value of these books is approx {{r__sum priceList}}.

I have {{r__count (r__propEq 'type' 'comic') books}} comic books and {{r__count (r__propEq 'type' 'computer') books}} computer books.
`


Handlebars.compile(template)(view)

The Output:

I have a lot of books:

bookName                                 | bookPrice
---------------------------------------- | ----------
C Primer Plus                            | 10
An Introduction to Programming in Go     | 20
Javascript the definitive guide          | 100
One Piece                                | 100


The total value of these books is approx 230.

I have 1 comic books and 3 computer books.

For more functions, please see Ramda Doc.

Extension

In order to facilitate the use of ramda in handlebars, some extension functions have been added.

Helpers

namedescription
r__defineDefine a variable on context `{{r__define "variableName" "variableValue"}}
r__ArrayCreate an array {{r__Array "0" "1" "2"}}
r__ObjectCreate an object {{r__Object "key1"="value1" "key2"="value2"}}
r__isObjectR.is(Object)
r__isNumberR.is(Number)
r__isStringR.is(String)
r__isArrayR.is(Array)
r__isFunctionR.is(Function)
r__isBooleanR.is(Boolean)
r__isTrueR.identical(R.T())
r__isFalseR.identical(R.F())
r__allIsR.useWith(R.all, R.is, R.identity)
r__allIsObjectR.all(R.is(Object))
r__allIsNumberR.all(R.is(Number))
r__allIsStringR.all(R.is(String))
r__allIsArrayR.all(R.is(Array))
r__allIsFunctionR.all(R.is(Function))
r__allIsBooleanR.all(R.is(Boolean))
r__allIsTrueR.all(R.identical(R.T()))
r__allIsFalseR.all(R.identical(R.F()))
r__padLeftR.curry((fillString, maxLength, str) => String(str).padStart(maxLength, fillString))
r__padRightR.curry((fillString, maxLength, str) => String(str).padEnd(maxLength, fillString))

If a function was defined by {{r__define "fn" (r__equals "example")}}, the fn should be used like an helper, rather than a variable.

Error: {{r__all fn (r__Array "example" "example")}}

Correct:{{r__all (fn) (r__Array "example" "example")}}

Block Helpers

namedescription
r__defineDefine a variable on context {{#r__define "variableName"}}"variableValue"{{/r__define}}
r__toUpperR.toUpper
r__toLowerR.toLower
r__trimR.trim

All block helpers can use as non-block helper

Contributing & Development

If there is any doubt, it is very welcome to discuss the issue together. Please read Contributor Covenant Code of Conduct and CONTRIBUTING.