pricing4react
Version:
A library of components that ease the integration of feature toggling driven by pricing plans into your React application's UI.
191 lines (190 loc) • 6.44 kB
JavaScript
;
exports.__esModule = true;
exports.rawPlansToPlans = exports.rawFeatureAttributesToAttributes = exports.buildRawPricingContext = exports.parseExpression = exports.parseAttributeExpressionToUserAttributes = exports.computeType = exports.computeEvaluation = void 0;
function computeEvaluation(leftOperand, operator, rightOperand) {
switch (operator) {
case "":
return "";
case "<":
case "<=":
case "==":
case ">=":
case ">":
case "!=":
case "||":
case "&&":
return "".concat(leftOperand, " ").concat(operator, " ").concat(rightOperand);
case "None":
return leftOperand;
}
}
exports.computeEvaluation = computeEvaluation;
function computeType(value) {
switch (typeof value) {
case "string":
return "TEXT";
case "number":
return "NUMERIC";
case "boolean":
return "CONDITION";
default:
return "TEXT";
}
}
exports.computeType = computeType;
function parseAttributeExpressionToUserAttributes(attributes) {
return attributes.map(function (attribute) {
var _a;
var expression = parseExpression(attribute.expression);
return { type: attribute.type, id: (_a = expression.userContext) !== null && _a !== void 0 ? _a : "" };
});
}
exports.parseAttributeExpressionToUserAttributes = parseAttributeExpressionToUserAttributes;
function parseExpression(expression) {
var tokens = expression
.trim()
.split(" ")
.map(function (token) { return parseToken(token); });
var res = { operator: "", planContext: "" };
tokens.map(function (token) {
switch (token.type) {
case "PlanContext": {
res.planContext = token.value;
break;
}
case "UserContext": {
res.userContext = token.value;
break;
}
case "CustomValue": {
res.customValue = token.value;
break;
}
case "Operator":
res.operator = token.value;
case "Noop":
case "Unknown":
break;
}
});
return res;
}
exports.parseExpression = parseExpression;
function parseToken(token) {
var userContextRegex = /userContext\['(\w+)'\]/gm;
var planContextRegex = /planContext\['(\w+)'\]/gm;
var userContextMatch = Array.from(token.matchAll(userContextRegex));
var planContextMatch = Array.from(token.matchAll(planContextRegex));
if (token === "<") {
return { type: "Operator", value: "<" };
}
else if (token === "<=") {
return { type: "Operator", value: "<=" };
}
else if (token === "==") {
return { type: "Operator", value: "==" };
}
else if (token === ">=") {
return { type: "Operator", value: ">=" };
}
else if (token === ">") {
return { type: "Operator", value: ">" };
}
else if (token === "!=") {
return { type: "Operator", value: "!=" };
}
else if (token === "&&") {
return { type: "Operator", value: "&&" };
}
else if (token === "||") {
return { type: "Operator", value: "||" };
}
else if (token === "None") {
return { type: "Operator", value: "None" };
}
else if (token === "") {
return { type: "Noop", value: "" };
}
else if (userContextMatch.length === 0 && planContextMatch.length > 0) {
return { type: "PlanContext", value: planContextMatch[0][1] };
}
else if (userContextMatch.length > 0 && planContextMatch.length === 0) {
return { type: "UserContext", value: userContextMatch[0][1] };
}
else if (userContextMatch.length === 0 && planContextMatch.length === 0) {
return { type: "CustomValue", value: token };
}
else {
return { type: "Unknown", value: "" };
}
}
function buildRawPricingContext(attributes, plans) {
return {
features: attributesToRawAttributes(attributes),
plans: plansToRawPlans(plans)
};
}
exports.buildRawPricingContext = buildRawPricingContext;
function attributesToRawAttributes(attributes) {
return Object.fromEntries(attributes.map(function (attribute) {
var rawAttributes = {
description: attribute.description,
expression: attribute.expression,
type: attribute.type,
defaultValue: attribute.defaultValue
};
return [attribute.id, rawAttributes];
}));
}
function plansToRawPlans(plans) {
return Object.fromEntries(plans.map(function (plan) {
var rawPlan = {
description: plan.description,
price: plan.price,
currency: plan.currency,
features: featuresToRawFeatures(plan.features)
};
return [plan.name, rawPlan];
}));
}
function featuresToRawFeatures(features) {
return Object.fromEntries(features.map(function (feature) { return [feature.name, { value: feature.value }]; }));
}
function rawFeatureAttributesToAttributes(rawFeatureAttributes) {
return Object.entries(rawFeatureAttributes).map(function (_a) {
var attributeName = _a[0], attributes = _a[1];
var attribute = {
id: attributeName,
description: attributes.description,
expression: attributes.expression,
type: attributes.type,
defaultValue: attributes.defaultValue
};
return attribute;
});
}
exports.rawFeatureAttributesToAttributes = rawFeatureAttributesToAttributes;
function rawPlansToPlans(rawPlans) {
return Object.entries(rawPlans).map(function (_a) {
var planName = _a[0], attributes = _a[1];
var plan = {
name: planName,
description: attributes.description,
price: attributes.price,
currency: attributes.currency,
features: rawFeaturesToFeatures(attributes.features)
};
return plan;
});
}
exports.rawPlansToPlans = rawPlansToPlans;
function rawFeaturesToFeatures(features) {
return Object.entries(features).map(function (_a) {
var featureName = _a[0], attributes = _a[1];
return ({
name: featureName,
type: computeType(attributes.value),
value: attributes.value
});
});
}