Skip to content

Commit

Permalink
add the json object. Fix #86
Browse files Browse the repository at this point in the history
  • Loading branch information
alexewerlof committed Aug 21, 2020
1 parent 56cd4c5 commit 707e46a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions playground/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ <h2>Options</h2>
<br/>
<label><input type="checkbox" id="explicit" /><code>explicit</code></label>
<br/>
<label><input type="checkbox" id="json" /><code>json</code></label>
<br/>
<label><h3><code>maxLen</code></h3><input id="maxLen" class="io entry" type="number"/></label>
<label><h3><code>maxPathCount</code></h3><input id="maxPathCount" class="io entry" type="number"/></label>
<label><h3><code>maxPathLen</code></h3><input id="maxPathLen" class="io entry" type="number"/></label>
Expand Down
2 changes: 2 additions & 0 deletions playground/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ function render() {
const options = {
validateRef: getVal(id('validateRef')),
explicit: getVal(id('explicit')),
json: getVal(id('json')),
maxLen: getVal(id('maxLen')),
maxPathCount: getVal(id('maxPathCount')),
maxPathLen: getVal(id('maxPathLen')),
Expand Down Expand Up @@ -105,6 +106,7 @@ ready(() => {

onInput(id('validateRef'), render)
onInput(id('explicit'), render)
onInput(id('json'), render)
onInput(id('maxLen'), render)
onInput(id('maxPathCount'), render)
onInput(id('maxPathLen'), render)
Expand Down
8 changes: 6 additions & 2 deletions src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ export interface StringifyOptions {
* By default it swallows those values to be compatible with Mustache.
*/
readonly explicit?: boolean
/**
* When set to a truthy value, it stringifies object values using `JSON.stringify()`
*/
readonly json?: boolean
}

/**
Expand Down Expand Up @@ -46,7 +50,7 @@ export function stringify(
throw new TypeError(`${where} got an invalid parsedTemplate: ${parsedTemplate}`)
}

const { explicit } = optObj<StringifyOptions>(where, options)
const { explicit, json } = optObj<StringifyOptions>(where, options)
const { strings, subs } = parsedTemplate
const { length } = subs

Expand All @@ -58,7 +62,7 @@ export function stringify(
const value: any = subs[i]

if (explicit || (value !== null && value !== undefined)) {
ret += value
ret += json && isObj(value) ? JSON.stringify(value) : value
}
}

Expand Down

0 comments on commit 707e46a

Please sign in to comment.