mirror of
https://github.com/harttle/liquidjs.git
synced 2026-09-20 15:00:42 -07:00
feature: support toLiquid, working on #81
This commit is contained in:
@@ -25,6 +25,7 @@ Though being compatible with [Ruby Liquid](https://github.com/shopify/liquid) is
|
|||||||
* Dynamic file locating (enabled by default), which means layout/partial name can be an variable in liquidjs. See [#51](https://github.com/harttle/liquidjs/issues/51).
|
* Dynamic file locating (enabled by default), which means layout/partial name can be an variable in liquidjs. See [#51](https://github.com/harttle/liquidjs/issues/51).
|
||||||
* Truthy and Falsy. All values except `undefined`, `null`, `false` are truthy, whereas in Ruby Liquid all except `nil` and `false` are truthy. See [#26](https://github.com/harttle/liquidjs/pull/26).
|
* Truthy and Falsy. All values except `undefined`, `null`, `false` are truthy, whereas in Ruby Liquid all except `nil` and `false` are truthy. See [#26](https://github.com/harttle/liquidjs/pull/26).
|
||||||
* Number Rendering. Since JavaScript do not distinguish `float` and `integer`, we cannot either convert between them nor render regarding to their type. See [#59](https://github.com/harttle/liquidjs/issues/59).
|
* Number Rendering. Since JavaScript do not distinguish `float` and `integer`, we cannot either convert between them nor render regarding to their type. See [#59](https://github.com/harttle/liquidjs/issues/59).
|
||||||
|
* Along with [.to_liquid()](https://github.com/Shopify/liquid/wiki/Introduction-to-Drops), we provide an alias `.toLiquid()` to align with your code styles.
|
||||||
|
|
||||||
## TOC
|
## TOC
|
||||||
|
|
||||||
|
|||||||
@@ -63,7 +63,10 @@ var Scope = {
|
|||||||
} else {
|
} else {
|
||||||
if (typeof obj.to_liquid === 'function') {
|
if (typeof obj.to_liquid === 'function') {
|
||||||
obj = obj.to_liquid()
|
obj = obj.to_liquid()
|
||||||
|
} else if (typeof obj.toLiquid === 'function') {
|
||||||
|
obj = obj.toLiquid()
|
||||||
}
|
}
|
||||||
|
|
||||||
if (key === 'size' && (_.isArray(obj) || _.isString(obj))) {
|
if (key === 'size' && (_.isArray(obj) || _.isString(obj))) {
|
||||||
val = obj.length
|
val = obj.length
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -11,8 +11,12 @@ function isString (value) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function stringify (value) {
|
function stringify (value) {
|
||||||
if (value && typeof value.to_liquid === 'function') {
|
if (!isNil(value)) {
|
||||||
return stringify(value.to_liquid())
|
if (typeof value.to_liquid === 'function') {
|
||||||
|
return stringify(value.to_liquid())
|
||||||
|
} else if (typeof value.toLiquid === 'function') {
|
||||||
|
return stringify(value.toLiquid())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (isString(value)) {
|
if (isString(value)) {
|
||||||
return value
|
return value
|
||||||
|
|||||||
@@ -96,6 +96,14 @@ describe('scope', function () {
|
|||||||
expect(scope.get('foo.bar')).to.equal('BAR')
|
expect(scope.get('foo.bar')).to.equal('BAR')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should respect to toLiquid', function () {
|
||||||
|
let scope = Scope.factory({foo: {
|
||||||
|
toLiquid: () => ({bar: 'BAR'}),
|
||||||
|
bar: 'bar'
|
||||||
|
}})
|
||||||
|
expect(scope.get('foo.bar')).to.equal('BAR')
|
||||||
|
})
|
||||||
|
|
||||||
it('should access child property via dot syntax', function () {
|
it('should access child property via dot syntax', function () {
|
||||||
expect(scope.get('bar.zoo')).to.equal('coo')
|
expect(scope.get('bar.zoo')).to.equal('coo')
|
||||||
expect(scope.get('bar.arr')).to.deep.equal(['a', 'b'])
|
expect(scope.get('bar.arr')).to.deep.equal(['a', 'b'])
|
||||||
|
|||||||
@@ -34,6 +34,17 @@ describe('util/underscore', function () {
|
|||||||
expect(_.isString(123)).to.be.false
|
expect(_.isString(123)).to.be.false
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
describe('.stringify()', function () {
|
||||||
|
it('should respect to to_liquid() method', function () {
|
||||||
|
expect(_.stringify({to_liquid: () => 'foo'})).to.equal('foo')
|
||||||
|
})
|
||||||
|
it('should respect to toLiquid() method', function () {
|
||||||
|
expect(_.stringify({toLiquid: () => 'foo'})).to.equal('foo')
|
||||||
|
})
|
||||||
|
it('should recursively call toLiquid()', function () {
|
||||||
|
expect(_.stringify({toLiquid: () => ({toLiquid: () => 'foo'})})).to.equal('foo')
|
||||||
|
})
|
||||||
|
})
|
||||||
describe('.forOwn()', function () {
|
describe('.forOwn()', function () {
|
||||||
it('should iterate all properties', function () {
|
it('should iterate all properties', function () {
|
||||||
var spy = sinon.spy()
|
var spy = sinon.spy()
|
||||||
|
|||||||
Reference in New Issue
Block a user