无法弄清楚如何编码这种映射情感和颜色

can't figure out how to code this mapping emotion and color

你好我尝试将颜色和情绪映射在一起,每个post都有一个初始的情绪和颜色,但是在响应用户选择的颜色和情绪变化的同时,例如:[的初始情绪=20=]是sad和blue,用户选择happy和green,通过四个变量组合出来一个新的emotion和color的结果,我尝试通过if ?来做,但是遇到了问题,手动映射没有包括额外的可能性,所以有没有人知道如何做一个更有效的映射,我每次都可以给出不同的结果, 这是我所做的一个例子

这是主要功能

const {angry} = require('./emotions/angry/angry')


exports.ECBrige = (userEmotion, userColor,postEmotion, postColor) => {
  if(userEmotion === 'angry'){
    return angry(userEmotion, userColor,postEmotion, postColor)
  }   
}

这是愤怒的映射

const {yellow} = require('./colors/yellow');
const {lime} = require('./colors/lime');
const {green} = require('./colors/green');
const {aqua} = require('./colors/aqua');
const {blue} = require('./colors/blue');
const {pink} = require('./colors/pink');
const {red} = require('./colors/red');
const {orange} = require('./colors/orange');

exports.angry = (userEmotion, userColor,postEmotion, postColor) => {
    if(userColor === 'yellow'){
        return yellow(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'lime'){
        return lime(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'green'){
        return green(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'aqua'){
        return aqua(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'blue'){
        return blue(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'pink'){
        return pink(userEmotion, userColor,postEmotion, postColor)
    }else if (userColor === 'red'){
        return red(userEmotion, userColor,postEmotion, postColor)
    }else{
        return orange(userEmotion, userColor,postEmotion, postColor)
    }
}

它输出一些未定义的

您可以使用对象来存储颜色,然后您可以像 color[colorName] 一样访问您的颜色函数。

./colors/index.js - 导出所有颜色

const {yellow} = require('./colors/yellow');
const {lime} = require('./colors/lime');
const {green} = require('./colors/green');
const {aqua} = require('./colors/aqua');
const {blue} = require('./colors/blue');
const {pink} = require('./colors/pink');
const {red} = require('./colors/red');
const {orange} = require('./colors/orange');

export { yellow, lime, green, aqua, ... }

./mapping.js - 访问上面颜色对象的 属性。

// import your all colors
const colors = require('./colors');

exports.angry = (userEmotion, userColor,postEmotion, postColor) => {
    try {
        return colors[userColor](userEmotion, userColor,postEmotion, postColor);
    } catch (err) {
        // you may choose some color default
        return colors.orange(userEmotion, userColor,postEmotion, postColor);
    }
}