@mapbox/expression-jamsession
Advanced tools
Comparing version 0.3.0 to 0.3.1
# Changelog | ||
## 0.3.1 | ||
- Fix handling of the `literal` expression, whose argument can be an array whose items are primitives and arrays of primitives. | ||
## 0.3.0 | ||
@@ -4,0 +8,0 @@ |
@@ -7,3 +7,3 @@ 'use strict'; | ||
function handleSyntaxErrors(error) { | ||
function handleSyntaxErrors(error, input) { | ||
var newError = new Error('Syntax error'); | ||
@@ -22,2 +22,6 @@ newError.type = 'SyntaxError'; | ||
if (/literal\(\s*{/.test(input)) { | ||
newError.description = 'Only array arguments are supported for the literal expression'; | ||
} | ||
return newError; | ||
@@ -30,2 +34,14 @@ } | ||
function handleLiteralArgument(arg) { | ||
switch (arg.type) { | ||
case 'ArrayExpression': | ||
return arg.elements.map(function (item) { | ||
if (item.type === 'Literal') return item.value; | ||
return handleLiteralArgument(item); | ||
}); | ||
default: | ||
throw new Error('Invalid syntax'); | ||
} | ||
} | ||
function astToExpression(input) { | ||
@@ -62,5 +78,9 @@ if (input.value !== undefined) return input.value; | ||
input.arguments.forEach(function (i) { | ||
expressionArguments.push(astToExpression(i)); | ||
}); | ||
if (expressionOperator === 'literal') { | ||
expressionArguments = expressionArguments.concat(input.arguments.map(handleLiteralArgument)); | ||
} else { | ||
input.arguments.forEach(function (i) { | ||
expressionArguments.push(astToExpression(i)); | ||
}); | ||
} | ||
} | ||
@@ -94,3 +114,3 @@ | ||
} catch (syntaxError) { | ||
throw handleSyntaxErrors(syntaxError); | ||
throw handleSyntaxErrors(syntaxError, input); | ||
} | ||
@@ -102,5 +122,13 @@ | ||
function stringifyLiteralArray(arr) { | ||
var items = arr.map(function (item) { | ||
if (Array.isArray(item)) return stringifyLiteralArray(item); | ||
return JSON.stringify(item); | ||
}); | ||
return '[' + items.join(', ') + ']'; | ||
} | ||
function expressionToFormula(expression) { | ||
if (!Array.isArray(expression)) { | ||
throw new Error('input must be an array'); | ||
throw new Error('Input must be an array'); | ||
} | ||
@@ -110,2 +138,10 @@ | ||
if (operator === 'literal') { | ||
var arg = expression[1]; | ||
if (!Array.isArray(arg)) { | ||
throw new Error('Only array arguments are supported for the literal expression'); | ||
} | ||
return operator + '(' + stringifyLiteralArray(arg) + ')'; | ||
} | ||
var args = expression.slice(1).map(function (arg) { | ||
@@ -112,0 +148,0 @@ if (typeof arg === 'string') { |
{ | ||
"name": "@mapbox/expression-jamsession", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Write Mapbox GL expressions in a more familiar, handwritable, spreadsheet-like, programming-like syntax.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -65,1 +65,4 @@ # @mapbox/expression-jamsession | ||
- You can use this library to create expressions that are syntactically acceptable but invalid as Mapbox GL expressions, e.g. `notARealExpression(true)` outputs `["notARealExpression", true]`. | ||
- You cannot use JSON object literal arguments to [the `literal` expression](https://www.mapbox.com/mapbox-gl-js/style-spec/#expressions-types-literal). | ||
Even though this is allowed in the spec, objects are not supported by jsep and the use case for this type of expression is enough of an edge case that it doesn't seem worth serious work. | ||
If you disagree, please consider filing an issue and/or PR. |
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
10693
143
68