Skip to content

Commit

Permalink
feature: support toLiquid, working on #81
Browse files Browse the repository at this point in the history
  • Loading branch information
harttle committed Aug 13, 2018
1 parent 5b43520 commit 7a36890
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
* 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).
* 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

Expand Down
3 changes: 3 additions & 0 deletions src/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ var Scope = {
} else {
if (typeof obj.to_liquid === 'function') {
obj = obj.to_liquid()
} else if (typeof obj.toLiquid === 'function') {
obj = obj.toLiquid()
}

if (key === 'size' && (_.isArray(obj) || _.isString(obj))) {
val = obj.length
} else {
Expand Down
8 changes: 6 additions & 2 deletions src/util/underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@ function isString (value) {
}

function stringify (value) {
if (value && typeof value.to_liquid === 'function') {
return stringify(value.to_liquid())
if (!isNil(value)) {
if (typeof value.to_liquid === 'function') {
return stringify(value.to_liquid())
} else if (typeof value.toLiquid === 'function') {
return stringify(value.toLiquid())
}
}
if (isString(value)) {
return value
Expand Down
8 changes: 8 additions & 0 deletions test/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ describe('scope', function () {
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 () {
expect(scope.get('bar.zoo')).to.equal('coo')
expect(scope.get('bar.arr')).to.deep.equal(['a', 'b'])
Expand Down
11 changes: 11 additions & 0 deletions test/util/underscore.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ describe('util/underscore', function () {
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 () {
it('should iterate all properties', function () {
var spy = sinon.spy()
Expand Down

0 comments on commit 7a36890

Please sign in to comment.