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.

39 lines
1.0 KiB

4 years ago
  1. import {ATTRIBUTE, VALUE} from './expression-types'
  2. import {dashToCamelCase} from './strings'
  3. /**
  4. * Throw an error with a descriptive message
  5. * @param { string } message - error message
  6. * @returns { undefined } hoppla.. at this point the program should stop working
  7. */
  8. export function panic(message) {
  9. throw new Error(message)
  10. }
  11. /**
  12. * Evaluate a list of attribute expressions
  13. * @param {Array} attributes - attribute expressions generated by the riot compiler
  14. * @returns {Object} key value pairs with the result of the computation
  15. */
  16. export function evaluateAttributeExpressions(attributes) {
  17. return attributes.reduce((acc, attribute) => {
  18. const {value, type} = attribute
  19. switch (true) {
  20. // spread attribute
  21. case !attribute.name && type === ATTRIBUTE:
  22. return {
  23. ...acc,
  24. ...value
  25. }
  26. // value attribute
  27. case type === VALUE:
  28. acc.value = attribute.value
  29. break
  30. // normal attributes
  31. default:
  32. acc[dashToCamelCase(attribute.name)] = attribute.value
  33. }
  34. return acc
  35. }, {})
  36. }