mirror of
https://github.com/harttle/liquidjs.git
synced 2026-10-03 00:55:13 -07:00
rename expression.js -> syntax.js
This commit is contained in:
@@ -6,7 +6,7 @@ const lexical = require('./src/lexical.js');
|
|||||||
const Tag = require('./src/tag.js');
|
const Tag = require('./src/tag.js');
|
||||||
const Filter = require('./src/filter.js');
|
const Filter = require('./src/filter.js');
|
||||||
const Template = require('./src/parser');
|
const Template = require('./src/parser');
|
||||||
const Expression = require('./src/expression.js');
|
const Syntax = require('./src/syntax.js');
|
||||||
const tags = require('./tags');
|
const tags = require('./tags');
|
||||||
const filters = require('./filters');
|
const filters = require('./filters');
|
||||||
const Promise = require('any-promise');
|
const Promise = require('any-promise');
|
||||||
@@ -127,9 +127,9 @@ function resolvePath(root, path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
factory.lexical = lexical;
|
factory.lexical = lexical;
|
||||||
factory.isTruthy = Expression.isTruthy;
|
factory.isTruthy = Syntax.isTruthy;
|
||||||
factory.isFalsy = Expression.isFalsy;
|
factory.isFalsy = Syntax.isFalsy;
|
||||||
factory.evalExp = Expression.evalExp;
|
factory.evalExp = Syntax.evalExp;
|
||||||
factory.evalValue = Expression.evalValue;
|
factory.evalValue = Syntax.evalValue;
|
||||||
|
|
||||||
module.exports = factory;
|
module.exports = factory;
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
const syntax = require('./syntax.js');
|
|
||||||
const lexical = require('./lexical.js');
|
|
||||||
|
|
||||||
function evalExp(exp, scope) {
|
|
||||||
if (!scope) throw new Error('unable to evalExp: scope undefined');
|
|
||||||
var operatorREs = lexical.operators,
|
|
||||||
match;
|
|
||||||
for (var i = 0; i < operatorREs.length; i++) {
|
|
||||||
var operatorRE = operatorREs[i];
|
|
||||||
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
|
|
||||||
if (match = exp.match(expRE)) {
|
|
||||||
var l = evalExp(match[1], scope);
|
|
||||||
var op = syntax.operators[match[2].trim()];
|
|
||||||
var r = evalExp(match[3], scope);
|
|
||||||
return op(l, r);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (match = exp.match(lexical.rangeLine)) {
|
|
||||||
var low = evalValue(match[1], scope),
|
|
||||||
high = evalValue(match[2], scope);
|
|
||||||
var range = [];
|
|
||||||
for (var j = low; j <= high; j++) {
|
|
||||||
range.push(j);
|
|
||||||
}
|
|
||||||
return range;
|
|
||||||
}
|
|
||||||
|
|
||||||
return evalValue(exp, scope);
|
|
||||||
}
|
|
||||||
|
|
||||||
function evalValue(str, scope) {
|
|
||||||
str = str && str.trim();
|
|
||||||
if (!str) return undefined;
|
|
||||||
|
|
||||||
if (lexical.isLiteral(str)) {
|
|
||||||
return lexical.parseLiteral(str);
|
|
||||||
}
|
|
||||||
if (lexical.isVariable(str)) {
|
|
||||||
return scope.get(str);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isTruthy(val) {
|
|
||||||
if (val instanceof Array) return !!val.length;
|
|
||||||
return !!val;
|
|
||||||
}
|
|
||||||
|
|
||||||
function isFalsy(val) {
|
|
||||||
return !isTruthy(val);
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
evalExp, evalValue, isTruthy, isFalsy
|
|
||||||
};
|
|
||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
const lexical = require('./lexical.js');
|
const lexical = require('./lexical.js');
|
||||||
const Exp = require('./expression.js');
|
const Syntax = require('./syntax.js');
|
||||||
|
|
||||||
var valueRE = new RegExp(`${lexical.value.source}`, 'g');
|
var valueRE = new RegExp(`${lexical.value.source}`, 'g');
|
||||||
|
|
||||||
@@ -8,7 +8,7 @@ module.exports = function() {
|
|||||||
|
|
||||||
var _filterInstance = {
|
var _filterInstance = {
|
||||||
render: function(output, scope) {
|
render: function(output, scope) {
|
||||||
var args = this.args.map(arg => Exp.evalValue(arg, scope));
|
var args = this.args.map(arg => Syntax.evalValue(arg, scope));
|
||||||
args.unshift(output);
|
args.unshift(output);
|
||||||
return this.filter.apply(null, args);
|
return this.filter.apply(null, args);
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
var operators = {
|
||||||
|
'==': (l, r) => l == r,
|
||||||
|
'!=': (l, r) => l != r,
|
||||||
|
'>': (l, r) => l > r,
|
||||||
|
'<': (l, r) => l < r,
|
||||||
|
'>=': (l, r) => l >= r,
|
||||||
|
'<=': (l, r) => l <= r,
|
||||||
|
'contains': (l, r) => l.indexOf(r) > -1,
|
||||||
|
'and': (l, r) => l && r,
|
||||||
|
'or': (l, r) => l || r
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = operators;
|
||||||
+2
-2
@@ -1,4 +1,4 @@
|
|||||||
const Exp = require('./expression.js');
|
const Syntax = require('./syntax.js');
|
||||||
const Promise = require('any-promise');
|
const Promise = require('any-promise');
|
||||||
|
|
||||||
var render = {
|
var render = {
|
||||||
@@ -89,7 +89,7 @@ var render = {
|
|||||||
|
|
||||||
evalOutput: function(template, scope, opts) {
|
evalOutput: function(template, scope, opts) {
|
||||||
if(!scope) throw new Error('unable to evalOutput: scope undefined');
|
if(!scope) throw new Error('unable to evalOutput: scope undefined');
|
||||||
var val = Exp.evalExp(template.initial, scope);
|
var val = Syntax.evalExp(template.initial, scope);
|
||||||
template.filters.some(filter => {
|
template.filters.some(filter => {
|
||||||
if (filter.error) {
|
if (filter.error) {
|
||||||
if (opts.strict_filters) {
|
if (opts.strict_filters) {
|
||||||
|
|||||||
+54
-12
@@ -1,13 +1,55 @@
|
|||||||
var operators = {
|
const operators = require('./operators.js');
|
||||||
'==': (l, r) => l == r,
|
const lexical = require('./lexical.js');
|
||||||
'!=': (l, r) => l != r,
|
|
||||||
'>': (l, r) => l > r,
|
|
||||||
'<': (l, r) => l < r,
|
|
||||||
'>=': (l, r) => l >= r,
|
|
||||||
'<=': (l, r) => l <= r,
|
|
||||||
'contains': (l, r) => l.indexOf(r) > -1,
|
|
||||||
'and': (l, r) => l && r,
|
|
||||||
'or': (l, r) => l || r
|
|
||||||
};
|
|
||||||
|
|
||||||
exports.operators = operators;
|
function evalExp(exp, scope) {
|
||||||
|
if (!scope) throw new Error('unable to evalExp: scope undefined');
|
||||||
|
var operatorREs = lexical.operators,
|
||||||
|
match;
|
||||||
|
for (var i = 0; i < operatorREs.length; i++) {
|
||||||
|
var operatorRE = operatorREs[i];
|
||||||
|
var expRE = new RegExp(`^(${lexical.quoteBalanced.source})(${operatorRE.source})(${lexical.quoteBalanced.source})$`);
|
||||||
|
if (match = exp.match(expRE)) {
|
||||||
|
var l = evalExp(match[1], scope);
|
||||||
|
var op = operators[match[2].trim()];
|
||||||
|
var r = evalExp(match[3], scope);
|
||||||
|
return op(l, r);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (match = exp.match(lexical.rangeLine)) {
|
||||||
|
var low = evalValue(match[1], scope),
|
||||||
|
high = evalValue(match[2], scope);
|
||||||
|
var range = [];
|
||||||
|
for (var j = low; j <= high; j++) {
|
||||||
|
range.push(j);
|
||||||
|
}
|
||||||
|
return range;
|
||||||
|
}
|
||||||
|
|
||||||
|
return evalValue(exp, scope);
|
||||||
|
}
|
||||||
|
|
||||||
|
function evalValue(str, scope) {
|
||||||
|
str = str && str.trim();
|
||||||
|
if (!str) return undefined;
|
||||||
|
|
||||||
|
if (lexical.isLiteral(str)) {
|
||||||
|
return lexical.parseLiteral(str);
|
||||||
|
}
|
||||||
|
if (lexical.isVariable(str)) {
|
||||||
|
return scope.get(str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function isTruthy(val) {
|
||||||
|
if (val instanceof Array) return !!val.length;
|
||||||
|
return !!val;
|
||||||
|
}
|
||||||
|
|
||||||
|
function isFalsy(val) {
|
||||||
|
return !isTruthy(val);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
evalExp, evalValue, isTruthy, isFalsy
|
||||||
|
};
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
const lexical = require('./lexical.js');
|
const lexical = require('./lexical.js');
|
||||||
const Promise = require('any-promise');
|
const Promise = require('any-promise');
|
||||||
const Exp = require('./expression.js');
|
const Syntax = require('./syntax.js');
|
||||||
|
|
||||||
function hash(markup, scope) {
|
function hash(markup, scope) {
|
||||||
var obj = {}, match;
|
var obj = {}, match;
|
||||||
@@ -8,7 +8,7 @@ function hash(markup, scope) {
|
|||||||
while (match = lexical.hashCapture.exec(markup)) {
|
while (match = lexical.hashCapture.exec(markup)) {
|
||||||
var k = match[1],
|
var k = match[1],
|
||||||
v = match[2];
|
v = match[2];
|
||||||
obj[k] = Exp.evalValue(v, scope);
|
obj[k] = Syntax.evalValue(v, scope);
|
||||||
}
|
}
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,10 +1,10 @@
|
|||||||
const chai = require("chai");
|
const chai = require("chai");
|
||||||
const expect = chai.expect;
|
const expect = chai.expect;
|
||||||
var expression = require('../src/expression.js');
|
var syntax = require('../src/syntax.js');
|
||||||
var Scope = require('../src/scope.js');
|
var Scope = require('../src/scope.js');
|
||||||
|
|
||||||
var evalExp = expression.evalExp;
|
var evalExp = syntax.evalExp;
|
||||||
var evalValue = expression.evalValue;
|
var evalValue = syntax.evalValue;
|
||||||
|
|
||||||
describe('expression', function() {
|
describe('expression', function() {
|
||||||
var scope;
|
var scope;
|
||||||
Reference in New Issue
Block a user