You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

54 lines
1.5 KiB

4 years ago
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)
}
/**
* Returns the memoized (cached) function.
* // borrowed from https://www.30secondsofcode.org/js/s/memoize
* @param {Function} fn - function to memoize
* @returns {Function} memoize function
*/
export function memoize(fn) {
const cache = new Map()
const cached = val => {
return cache.has(val) ? cache.get(val) : cache.set(val, fn.call(this, val)) && cache.get(val)
}
cached.cache = cache
return cached
}
/**
* 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
}, {})
}