UNPKG

hypertune

Version:

[Hypertune](https://www.hypertune.com/) is the most flexible platform for feature flags, A/B testing, analytics and app configuration. Built with full end-to-end type-safety, Git-style version control and local, synchronous, in-memory flag evaluation. Opt

129 lines 4.34 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.getAnonymousExpression = getAnonymousExpression; exports.getNoOpExpression = getNoOpExpression; exports.getBooleanIfExpression = getBooleanIfExpression; exports.getBooleanExpression = getBooleanExpression; exports.getIntExpression = getIntExpression; exports.getFloatExpression = getFloatExpression; exports.getStringExpression = getStringExpression; exports.getAnonymousListExpression = getAnonymousListExpression; exports.getAnonymousObjectExpression = getAnonymousObjectExpression; const uniqueId_1 = __importDefault(require("./uniqueId")); // When modifying expression constructors, ensure that they properly thread // through optional fields like isTransient and don't accidentally drop them function getAnonymousExpression(value, isTransient) { if (typeof value === "boolean") { return getBooleanExpression(value, isTransient); } if (typeof value === "number") { if (Number.isInteger(value)) { return getIntExpression(value, isTransient); } return getFloatExpression(value, isTransient); } if (typeof value === "string") { return getStringExpression(value, isTransient); } if (Array.isArray(value)) { return getAnonymousListExpression(value, isTransient); } if (typeof value === "object") { return getAnonymousObjectExpression(value, isTransient); } const neverValue = value; throw new Error(`Unexpected value: ${neverValue}`); } function getNoOpExpression(isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "NoOpExpression", valueType: { type: "VoidValueType" }, }; return expression; } function getBooleanIfExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "SwitchExpression", valueType: { type: "BooleanValueType" }, control: getBooleanExpression(true), cases: [], default: getBooleanExpression(value, isTransient), }; return expression; } function getBooleanExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "BooleanExpression", valueType: { type: "BooleanValueType" }, value, }; return expression; } function getIntExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "IntExpression", valueType: { type: "IntValueType" }, value: Math.round(value), }; return expression; } function getFloatExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "FloatExpression", valueType: { type: "FloatValueType" }, value, }; return expression; } function getStringExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "StringExpression", valueType: { type: "StringValueType" }, value, }; return expression; } function getAnonymousListExpression(value, isTransient) { const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "ListExpression", valueType: { type: "ListValueType", itemValueType: { type: "VoidValueType" }, }, // Dummy items: value.map((item) => getAnonymousExpression(item, isTransient)), }; return expression; } function getAnonymousObjectExpression(value, isTransient) { const objectTypeName = ""; // Dummy const expression = { id: (0, uniqueId_1.default)(), isTransient, type: "ObjectExpression", valueType: { type: "ObjectValueType", objectTypeName }, objectTypeName, fields: Object.fromEntries(Object.entries(value).map(([fieldName, fieldValue]) => [ fieldName, getAnonymousExpression(fieldValue, isTransient), ])), }; return expression; } //# sourceMappingURL=expression.js.map