json-tag

1.0.0 • Public • Published

json-tag

Tagged template literal that escapes arguments as JSON values

json-tag uses JSON.stringify() to escape the template literal's arguments.

Usage:

import json from 'json-tag'
 
json`string:  ${ 'double quote "' }`
json`number:  ${ 1337 }`
json`boolean: ${ true } & ${ false }`
json`null:    ${ null }`
json`array:   ${ ['double quote "', null, 1337] }`
json`object:  ${ { doubleQuote: '"', doubleNumber: 1.337e+3 } }`

You will get the following strings:

string:  "double quote \""
number:  1337
boolean: true & false
null:    null
array:   ["double quote \"",null,1337]
object:  {"doubleQuote":"\"","doubleNumber":1337}

It works as follows, this:

json`text ${value}`

Is essentialy this:

`text ${JSON.stringify(value)}`

Package Sidebar

Install

npm i json-tag

Weekly Downloads

0

Version

1.0.0

License

MIT

Unpacked Size

2.27 kB

Total Files

4

Last publish

Collaborators

  • valentine-stone