mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
fix string type checking and circular reference, working on #81
This commit is contained in:
+8
-8
@@ -1,6 +1,7 @@
|
|||||||
const Syntax = require('./syntax.js')
|
const Syntax = require('./syntax.js')
|
||||||
const mapSeries = require('./util/promise.js').mapSeries
|
const mapSeries = require('./util/promise.js').mapSeries
|
||||||
const RenderBreakError = require('./util/error.js').RenderBreakError
|
const RenderBreakError = require('./util/error.js').RenderBreakError
|
||||||
|
const _ = require('./util/underscore.js')
|
||||||
const RenderError = require('./util/error.js').RenderError
|
const RenderError = require('./util/error.js').RenderError
|
||||||
const assert = require('./util/assert.js')
|
const assert = require('./util/assert.js')
|
||||||
|
|
||||||
@@ -27,9 +28,7 @@ var render = {
|
|||||||
return this.renderTag(template, scope)
|
return this.renderTag(template, scope)
|
||||||
.then(partial => partial === undefined ? '' : partial)
|
.then(partial => partial === undefined ? '' : partial)
|
||||||
} else if (template.type === 'value') {
|
} else if (template.type === 'value') {
|
||||||
return Promise.resolve()
|
return this.renderValue(template, scope)
|
||||||
.then(() => this.evalValue(template, scope))
|
|
||||||
.then(partial => partial === undefined ? '' : stringify(partial))
|
|
||||||
} else { // template.type === 'html'
|
} else { // template.type === 'html'
|
||||||
return Promise.resolve(template.value)
|
return Promise.resolve(template.value)
|
||||||
}
|
}
|
||||||
@@ -46,6 +45,12 @@ var render = {
|
|||||||
return template.render(scope)
|
return template.render(scope)
|
||||||
},
|
},
|
||||||
|
|
||||||
|
renderValue: function (template, scope) {
|
||||||
|
return Promise.resolve()
|
||||||
|
.then(() => this.evalValue(template, scope))
|
||||||
|
.then(partial => partial === undefined ? '' : _.stringify(partial))
|
||||||
|
},
|
||||||
|
|
||||||
evalValue: function (template, scope) {
|
evalValue: function (template, scope) {
|
||||||
assert(scope, 'unable to evalValue: scope undefined')
|
assert(scope, 'unable to evalValue: scope undefined')
|
||||||
return template.filters.reduce(
|
return template.filters.reduce(
|
||||||
@@ -59,9 +64,4 @@ function factory () {
|
|||||||
return instance
|
return instance
|
||||||
}
|
}
|
||||||
|
|
||||||
function stringify (val) {
|
|
||||||
if (typeof val === 'string') return val
|
|
||||||
return JSON.stringify(val)
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = factory
|
module.exports = factory
|
||||||
|
|||||||
+22
-1
@@ -6,7 +6,27 @@ const toStr = Object.prototype.toString
|
|||||||
* @return {Boolean} Returns true if value is a string, else false.
|
* @return {Boolean} Returns true if value is a string, else false.
|
||||||
*/
|
*/
|
||||||
function isString (value) {
|
function isString (value) {
|
||||||
return value instanceof String || typeof value === 'string'
|
return toStr.call(value) === '[object String]'
|
||||||
|
}
|
||||||
|
|
||||||
|
function stringify (value) {
|
||||||
|
if (isString(value)) {
|
||||||
|
return value
|
||||||
|
}
|
||||||
|
if (value && typeof value.to_liquid === 'function') {
|
||||||
|
return value.to_liquid()
|
||||||
|
}
|
||||||
|
|
||||||
|
let cache = []
|
||||||
|
return JSON.stringify(value, (key, value) => {
|
||||||
|
if (isObject(value)) {
|
||||||
|
if (cache.indexOf(value) !== -1) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cache.push(value)
|
||||||
|
}
|
||||||
|
return value
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function isNil (value) {
|
function isNil (value) {
|
||||||
@@ -126,3 +146,4 @@ exports.last = last
|
|||||||
exports.forOwn = forOwn
|
exports.forOwn = forOwn
|
||||||
exports.assign = assign
|
exports.assign = assign
|
||||||
exports.uniq = uniq
|
exports.uniq = uniq
|
||||||
|
exports.stringify = stringify
|
||||||
|
|||||||
+41
-2
@@ -17,11 +17,17 @@ describe('render', function () {
|
|||||||
var scope, render
|
var scope, render
|
||||||
|
|
||||||
beforeEach(function () {
|
beforeEach(function () {
|
||||||
scope = Scope.factory({
|
var ctx = {
|
||||||
foo: {
|
foo: {
|
||||||
bar: ['a', 2]
|
bar: ['a', 2]
|
||||||
|
},
|
||||||
|
bar: {
|
||||||
|
to_liquid: x => 'custom'
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
ctx.self = ctx
|
||||||
|
|
||||||
|
scope = Scope.factory(ctx)
|
||||||
filter.clear()
|
filter.clear()
|
||||||
tag.clear()
|
tag.clear()
|
||||||
render = Render()
|
render = Render()
|
||||||
@@ -39,6 +45,34 @@ describe('render', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe('.renderValue()', function () {
|
||||||
|
it('should respect to .to_liquid() method', function () {
|
||||||
|
var tpl = Template.parseValue('bar')
|
||||||
|
return expect(render.renderValue(tpl, scope)).to.eventually.equal('custom')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should stringify objects', function () {
|
||||||
|
let scope = Scope.factory({
|
||||||
|
foo: { obj: { arr: ['a', 2] } }
|
||||||
|
})
|
||||||
|
let tpl = Template.parseValue('foo')
|
||||||
|
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"obj":{"arr":["a",2]}}')
|
||||||
|
})
|
||||||
|
it('should skip circular property', function () {
|
||||||
|
let ctx = { foo: { num: 2 }, bar: 'bar' }
|
||||||
|
ctx.foo.circular = ctx
|
||||||
|
|
||||||
|
let scope = Scope.factory(ctx)
|
||||||
|
let tpl = Template.parseValue('foo')
|
||||||
|
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"num":2,"circular":{"bar":"bar"}}')
|
||||||
|
})
|
||||||
|
it('should skip function property', function () {
|
||||||
|
let scope = Scope.factory({obj: {foo: 'foo', bar: x => x}})
|
||||||
|
let tpl = Template.parseValue('obj')
|
||||||
|
return expect(render.renderValue(tpl, scope)).to.eventually.equal('{"foo":"foo"}')
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
it('should eval filter with correct arguments', function () {
|
it('should eval filter with correct arguments', function () {
|
||||||
var date = sinon.stub().returns('y')
|
var date = sinon.stub().returns('y')
|
||||||
var time = sinon.spy()
|
var time = sinon.spy()
|
||||||
@@ -62,5 +96,10 @@ describe('render', function () {
|
|||||||
var tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
|
var tpl = Template.parseValue('foo.bar[0] | date: "b" | time:2')
|
||||||
expect(render.evalValue(tpl, scope)).to.equal('ab6')
|
expect(render.evalValue(tpl, scope)).to.equal('ab6')
|
||||||
})
|
})
|
||||||
|
it('should reserve type', function () {
|
||||||
|
filter.register('arr', () => [1])
|
||||||
|
var tpl = Template.parseValue('"x" | arr')
|
||||||
|
expect(render.evalValue(tpl, scope)).to.deep.equal([1])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user