shopify-liquid
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -17,8 +17,5 @@ const lexical = require('./lexical.js'); | ||
var match = lexical.filterLine.exec(str.trim()); | ||
if (!match) { | ||
throw new Error('illegal filter: ' + str); | ||
} | ||
var k = match[1], | ||
v = match[2]; | ||
if (!match) error('illegal filter: ' + str); | ||
var k = match[1], v = match[2]; | ||
return factory(k, [v]); | ||
@@ -25,0 +22,0 @@ } |
@@ -9,2 +9,3 @@ const scope = require('./scope'); | ||
const Filter = require('./filter.js'); | ||
const error = require('./error.js'); | ||
@@ -49,3 +50,4 @@ const tagsPath = path.join(__dirname, "tags"); | ||
factory.lexical = lexical; | ||
factory.error = error; | ||
module.exports = factory; |
{ | ||
"name": "shopify-liquid", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "A Shopify Liquid Implementation in Node.js", | ||
@@ -24,3 +24,4 @@ "main": "index.js", | ||
"dependencies": { | ||
"lodash": "^4.13.1" | ||
"lodash": "^4.13.1", | ||
"strftime": "^0.9.2" | ||
}, | ||
@@ -27,0 +28,0 @@ "devDependencies": { |
@@ -37,5 +37,5 @@ # shopify-liquid | ||
- [x] assign [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/assign.js) [Test][tt] | ||
- [ ] capture [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
- [ ] increment [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
- [ ] decrement [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
- [x] capture [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
- [x] increment [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
- [x] decrement [Document](https://shopify.github.io/liquid/tags/variable/) [Source](https://github.com/harttle/shopify-liquid/blob/master/tags/.js) [Test][tt] | ||
@@ -42,0 +42,0 @@ |
const lexical = require('./lexical.js'); | ||
const syntax = require('./syntax.js'); | ||
const error = require('./error.js'); | ||
@@ -20,3 +21,3 @@ module.exports = function(Filter, Tag) { | ||
default: | ||
throw new Error(`unexpected type: ${token.type}`); | ||
error(`unexpected type: ${token.type}`, token); | ||
} | ||
@@ -28,3 +29,3 @@ } | ||
function evalExp(exp, scope) { | ||
if(!scope) throw new Error('unable to evalExp: scope undefined'); | ||
if(!scope) error('unable to evalExp: scope undefined'); | ||
var operatorREs = lexical.operators; | ||
@@ -46,3 +47,3 @@ for (var i = 0; i < operatorREs.length; i++) { | ||
function evalFilter(str, scope){ | ||
if(!scope) throw new Error('unable to evalFilter: scope undefined'); | ||
if(!scope) error('unable to evalFilter: scope undefined'); | ||
var filters = str.split('|'); | ||
@@ -63,5 +64,3 @@ var val = scope.get(filters.shift()); | ||
} | ||
if (!curToken) { | ||
throw new Error(`${token.value} not closed`); | ||
} | ||
if (!curToken) error(`${token.value} not closed`); | ||
} | ||
@@ -68,0 +67,0 @@ return tag.render(subTokens, scope); |
@@ -9,3 +9,3 @@ var Liquid = require('..'); | ||
render: function(tokens, scope, token, hash) { | ||
var match = token.value.match(re); | ||
var match = token.args.match(re); | ||
var k = match[1], v = match[2]; | ||
@@ -12,0 +12,0 @@ scope.set(k, liquid.evaluate(v, scope)); |
const chai = require("chai"); | ||
const expect = chai.expect; | ||
var liquid = require('..')(); | ||
var liquid = require('..')(), | ||
ctx; | ||
var ctx = { | ||
one: 1, | ||
two: 2, | ||
leq: '<=', | ||
empty: '', | ||
foo: 'bar', | ||
arr: [-2, 'a'] | ||
}; | ||
describe('tags', function() { | ||
beforeEach(function() { | ||
ctx = { | ||
one: 1, | ||
two: 2, | ||
leq: '<=', | ||
empty: '', | ||
foo: 'bar', | ||
arr: [-2, 'a'] | ||
}; | ||
}); | ||
it('should support assign', function() { | ||
@@ -37,3 +39,3 @@ expect(liquid.render('{% assign foo="bar"%}{{foo}}', ctx)).to.equal('bar'); | ||
it('should support if', function(){ | ||
it('should support if', function() { | ||
expect(liquid.render('{% if 2==3 %}yes{%else%}no{%endif%}', ctx)).to.equal('no'); | ||
@@ -44,7 +46,23 @@ expect(liquid.render('{% if 1==2 and one<two %}a{%endif%}', ctx)).to.equal(''); | ||
it('should support unless', function(){ | ||
it('should support unless', function() { | ||
expect(liquid.render('{% unless 1 %}yes{%else%}no{%endunless%}', ctx)).to.equal('no'); | ||
expect(liquid.render('{% unless 1>2 %}yes{%endunless%}', ctx)).to.equal('yes'); | ||
}); | ||
it('should support capture', function() { | ||
expect(liquid.render('{% capture f %}{{"a" | capitalize}}{%endcapture%}{{f}}', ctx)).to.equal('A'); | ||
expect(function() { | ||
liquid.render('{% capture = %}{%endcapture%}', ctx); | ||
}).to.throw(/= not valid identifier/); | ||
}); | ||
it('should support increment', function() { | ||
expect(liquid.render('{% increment foo %}{%increment foo%}{{foo}}', ctx)).to.equal('2'); | ||
expect(liquid.render('{% increment one %}{{one}}', ctx)).to.equal('2'); | ||
}); | ||
it('should support decrement', function() { | ||
expect(liquid.render('{% decrement foo %}{%decrement foo%}{{foo}}', ctx)).to.equal('-2'); | ||
expect(liquid.render('{% decrement one %}{{one}}', ctx)).to.equal('0'); | ||
}); | ||
}); | ||
const lexical = require('./lexical.js'); | ||
const error = require('./error.js'); | ||
@@ -8,3 +9,3 @@ function parse(html){ | ||
var syntax = /({%(.*?)%})|({{(.*?)}})/g; | ||
var result, htmlFragment; | ||
var result, htmlFragment, token; | ||
var idx = 0; | ||
@@ -20,25 +21,15 @@ | ||
} | ||
var rawTag = result[1], | ||
cleanTag = result[2], | ||
rawOut = result[3], | ||
cleanOut = result[4]; | ||
if(rawTag){ | ||
var match = cleanTag.trim().match(lexical.tagLine); | ||
if (!match) throw new Error('illegal tag: ' + rawTag); | ||
var name = match[1], args = match[2]; | ||
if(result[1]){ | ||
token = factory('tag', 1, result); | ||
var match = token.value.match(lexical.tagLine); | ||
if (!match) error('illegal tag', token); | ||
tokens.push({ | ||
type: 'tag', | ||
value: cleanTag.trim(), | ||
raw: rawTag, | ||
args, name | ||
}); | ||
token.name = match[1]; | ||
token.args = match[2]; | ||
tokens.push(token); | ||
} | ||
else{ | ||
tokens.push({ | ||
type: 'output', | ||
raw: rawOut, | ||
value: cleanOut.trim() | ||
}); | ||
token = factory('output', 3, result); | ||
tokens.push(token); | ||
} | ||
@@ -56,4 +47,22 @@ idx = syntax.lastIndex; | ||
return tokens; | ||
function factory(type, offset, match){ | ||
var token = { | ||
type, | ||
raw: match[offset], | ||
value: match[offset + 1].trim(), | ||
line: crCount(match.index) + 1 | ||
}; | ||
return token; | ||
} | ||
var lastIdx = -1; | ||
var lastCount = 0; | ||
function crCount(idx){ | ||
lastCount += html.slice(lastIdx+1, idx); | ||
lastIdx = idx; | ||
return lastCount; | ||
} | ||
} | ||
exports.parse = parse; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46914
30
921
2
+ Addedstrftime@^0.9.2
+ Addedstrftime@0.9.2(transitive)