map-tag
v1.0.0
Published
🏷 Map template literal expression interpolations with ease
Downloads
482
Readme
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 expressioni
is the position ofexpression
in the list ofexpressions
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