JSFiddle - React, Tailwind, and code Playground

by joplomacedo

JavaScript

const plans = [
    {
        id: 'premium',
        name: 'Premium',
        features: {
            totalTags: Infinity,
            suggestedTags: true,
            themes: ['light', 'dark']
        }
    },

    {
        id: 'free',
        name: 'Free',
        features: {
            totalTags: 50,
            suggestedTags: false,
            themes: ['light']
        }
    }
];

const features = {
    totalTags: {
        type: 'amount',
    },
    themes: {
        type: 'options',
    },
    suggestedTags: {
        type: 'boolean'
    },
};


function testFeature({ featureId, userPlanId, test }) {
    const featureType = features[featureId].type;

    const userPlan = plans.find(({ id }) => id === userPlanId);
    const userPlanFeatureDesc = userPlan.features[featureId];

    if (featureType === 'amount') {
        return test.amount < userPlanFeatureDesc;
    }

    if (featureType === 'boolean') {
        return userPlanFeatureDesc;
    }

    if (featureType === 'options') {
        return userPlanFeatureDesc.includes(test.option);
    }
}


const canAddTag = testFeature({
  featureId: 'suggestedTags',
  userPlanId: 'premium',
  test: {
    amount: 49
  }
})

console.log('canAddTag', canAddTag)