1.0.0 ā€¢ Published 6 years ago

map-tag v1.0.0

Weekly downloads
174
License
MIT
Repository
github
Last release
6 years ago

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