map-tag

1.0.0 • Public • Published

map-tag

🏷 Map template literal expression interpolations with ease.

Install

npm install --save map-tag

mapTag(mapper, mapperContext?)

Returns a function that can be used for tagging template literals. The provided mapper is invoked with Array-method style arguments (expression, i, expressions) for each expression being interpolated into the template literal, where:

  • expression is the current expression
  • i is the position of expression in the list of expressions
  • expressions is every expression

The values returned from mapper will be used instead of the original expressions passed to the template literal.

usage examples

import mapTag from 'map-tag'

const uppercase = mapTag(expression =>
  String.prototype.toUpperCase.call(expression)
)

var name = 'jane'
var role = 'dentist'
uppercase`hello ${ name }, you're a great ${ role }!`
// <- 'hello JANE, you're a great DENTIST!'
import mapTag from 'map-tag'

const encodeParams = mapTag(encodeURIComponent)

var collection = 'Log'
var query = '{}'
var fields = 'level'
var page = 2

encodeParams`/database/${ collection }/${ query }/${ fields }/${ page }`
// <- '/database/Log/%7B%7D/level/2'

license

mit

Dependencies (0)

    Dev Dependencies (0)

      Package Sidebar

      Install

      npm i map-tag

      Weekly Downloads

      142

      Version

      1.0.0

      License

      MIT

      Unpacked Size

      3.7 kB

      Total Files

      5

      Last publish

      Collaborators

      • bevacqua