@nestia/sdk
Version:
Nestia SDK and Swagger generator
48 lines • 2.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.LiteralFactory = void 0;
const factory_1 = require("@ttsc/factory");
const ExpressionFactory_1 = require("./ExpressionFactory");
const IdentifierFactory_1 = require("./IdentifierFactory");
const PASSTHROUGH_KINDS = new Set([
"ArrowFunction",
"CallExpression",
"Identifier",
]);
const isNode = (value) => typeof value === "object" &&
value !== null &&
typeof value.kind === "string";
/**
* Recursive value-to-AST-literal builder. Hands back already-AST inputs
* unchanged (so callers can mix factory output with raw JS values inside the
* same object/array), and emits the appropriate literal node otherwise.
*/
var LiteralFactory;
(function (LiteralFactory) {
LiteralFactory.write = (input) => {
if (input === null)
return factory_1.factory.createNull();
if (isNode(input) && PASSTHROUGH_KINDS.has(input.kind))
return input;
if (Array.isArray(input))
return writeArray(input);
if (typeof input === "object")
return writeObject(input);
if (typeof input === "boolean")
return input ? factory_1.factory.createTrue() : factory_1.factory.createFalse();
if (typeof input === "number")
return ExpressionFactory_1.ExpressionFactory.number(input);
if (typeof input === "string")
return factory_1.factory.createStringLiteral(input);
if (typeof input === "bigint")
return factory_1.factory.createStringLiteral(input.toString());
if (typeof input === "function")
return factory_1.factory.createIdentifier("undefined");
throw new TypeError("LiteralFactory.write: unsupported input type.");
};
const writeObject = (obj) => factory_1.factory.createObjectLiteralExpression(Object.entries(obj)
.filter(([, value]) => value !== undefined)
.map(([key, value]) => factory_1.factory.createPropertyAssignment(IdentifierFactory_1.IdentifierFactory.identifier(key), LiteralFactory.write(value))), true);
const writeArray = (array) => factory_1.factory.createArrayLiteralExpression(array.map(LiteralFactory.write), true);
})(LiteralFactory || (exports.LiteralFactory = LiteralFactory = {}));
//# sourceMappingURL=LiteralFactory.js.map