@mapbox/expression-jamsession
Advanced tools
Comparing version 0.2.0 to 0.3.0
# Changelog | ||
## 0.3.0 | ||
- Fix handling of `^` (exponentiation) and `%` (remainder) expression operators. | ||
- Fix handling of empty input to `formulaToExpression`. It now returns `undefined`. | ||
- Fix handling of hyphenated expression operators. | ||
## 0.2.0 | ||
@@ -4,0 +10,0 @@ |
@@ -24,2 +24,6 @@ 'use strict'; | ||
// GL expressions use ^ for exponentiation, while JS uses **. | ||
// 15 is precedence of ** operator in JS. | ||
jsep.addBinaryOp('^', 15); | ||
function astToExpression(input) { | ||
@@ -32,7 +36,4 @@ if (input.value !== undefined) return input.value; | ||
if (input.operator) { | ||
expressionOperator = input.operator === '&' ? 'concat' : input.operator; | ||
} | ||
if (input.type === 'UnaryExpression') { | ||
expressionOperator = input.operator; | ||
expressionArguments.push(astToExpression(input.argument)); | ||
@@ -42,2 +43,4 @@ } | ||
if (input.type === 'BinaryExpression') { | ||
expressionOperator = input.operator === '&' ? 'concat' : input.operator; | ||
// Collapse concat arguments, in case the & operator was used to join | ||
@@ -64,2 +67,8 @@ // more than two successive strings. | ||
// Change undescores in expression operators to hyphens, reversing the | ||
// transformation below. | ||
if (/[a-z]+_[a-z]+/.test(expressionOperator)) { | ||
expressionOperator = expressionOperator.replace(/([a-z]+)_([a-z]+)/, '$1-$2'); | ||
} | ||
return [expressionOperator].concat(expressionArguments); | ||
@@ -69,2 +78,14 @@ } | ||
function formulaToExpression(input) { | ||
if (input === '' || input === undefined) { | ||
return; | ||
} | ||
if (typeof input !== 'string') { | ||
throw new Error('input must be a string'); | ||
} | ||
// Change hyphens in expression operators to underscores. This allows JS | ||
// parsing to work, but then needs to be reversed above. | ||
input = input.replace(/([a-z]+)-([a-z]+)\(/, '$1_$2('); | ||
var ast = void 0; | ||
@@ -82,2 +103,6 @@ try { | ||
function expressionToFormula(expression) { | ||
if (!Array.isArray(expression)) { | ||
throw new Error('input must be an array'); | ||
} | ||
var operator = expression[0]; | ||
@@ -94,3 +119,5 @@ | ||
var argFormula = expressionToFormula(arg); | ||
if (/^[+-]$/.test(argOperator) && /^[*/]$/.test(operator)) { | ||
if ( | ||
// Use parentheses to deal with operator precedence. | ||
/^[+-]$/.test(argOperator) && /^[*/%]$/.test(operator) || operator === '^') { | ||
return '(' + argFormula + ')'; | ||
@@ -101,3 +128,7 @@ } | ||
if (operator === '+' || operator === '*' || operator === '-' || operator === '/') { | ||
if (operator === '^') { | ||
return '' + args.join(operator); | ||
} | ||
if (operator === '+' || operator === '*' || operator === '-' || operator === '/' || operator === '%') { | ||
return '' + args.join(' ' + operator + ' '); | ||
@@ -104,0 +135,0 @@ } |
{ | ||
"name": "@mapbox/expression-jamsession", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Write Mapbox GL expressions in a more familiar, handwritable, spreadsheet-like, programming-like syntax.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
9049
111