import {ATTRIBUTE, VALUE} from './expression-types'
|
|
import {dashToCamelCase} from './strings'
|
|
|
|
/**
|
|
* Throw an error with a descriptive message
|
|
* @param { string } message - error message
|
|
* @returns { undefined } hoppla.. at this point the program should stop working
|
|
*/
|
|
export function panic(message) {
|
|
throw new Error(message)
|
|
}
|
|
|
|
/**
|
|
* Evaluate a list of attribute expressions
|
|
* @param {Array} attributes - attribute expressions generated by the riot compiler
|
|
* @returns {Object} key value pairs with the result of the computation
|
|
*/
|
|
export function evaluateAttributeExpressions(attributes) {
|
|
return attributes.reduce((acc, attribute) => {
|
|
const {value, type} = attribute
|
|
|
|
switch (true) {
|
|
// spread attribute
|
|
case !attribute.name && type === ATTRIBUTE:
|
|
return {
|
|
...acc,
|
|
...value
|
|
}
|
|
// value attribute
|
|
case type === VALUE:
|
|
acc.value = attribute.value
|
|
break
|
|
// normal attributes
|
|
default:
|
|
acc[dashToCamelCase(attribute.name)] = attribute.value
|
|
}
|
|
|
|
return acc
|
|
}, {})
|
|
}
|