oso-cloud
Version:
Oso Cloud Node.js Client SDK
105 lines • 3.91 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.paramToFact = paramToFact;
exports.mapParamsToFacts = mapParamsToFacts;
exports.paramToConcreteFact = paramToConcreteFact;
exports.mapParamsToConcreteFacts = mapParamsToConcreteFacts;
exports.factToParam = factToParam;
exports.mapFactsToParams = mapFactsToParams;
exports.toValue = toValue;
exports.toValuePattern = toValuePattern;
function paramToFact(fact) {
if (!Array.isArray(fact)) {
throw new TypeError("Fact must be an array, got " + typeof fact);
}
const [predicate, ...args] = fact;
return { predicate, args: args.map(toValuePattern) };
}
function mapParamsToFacts(params) {
if (!params)
return [];
return params.map(paramToFact);
}
function paramToConcreteFact([predicate, ...args]) {
return { predicate, args: args.map(toValue) };
}
function mapParamsToConcreteFacts(params) {
if (!params)
return [];
return params.map(paramToConcreteFact);
}
function factToParam({ predicate, args, }) {
return [predicate, ...args];
}
function mapFactsToParams(facts) {
if (!facts)
return [];
return facts.map(factToParam);
}
function toValue(instance) {
if (typeof instance === "string") {
if (instance === "") {
throw new TypeError(`Oso: Instance cannot be an empty string. For wildcards, use the empty object ({}) or null.`);
}
return { type: "String", id: instance };
}
if (typeof instance === "boolean") {
return { type: "Boolean", id: instance.toString() };
}
if (typeof instance === "number") {
if (instance > Number.MAX_SAFE_INTEGER ||
instance < Number.MIN_SAFE_INTEGER) {
throw new Error(`Oso: integer identifier is outside the range of safe integers, making it unsuitable for use as an identifier`);
}
if (!Number.isInteger(instance)) {
throw new Error(`Oso: non-integer numeric values may not be used as identifiers`);
}
return { type: "Integer", id: instance.toString() };
}
if (typeof instance === "bigint") {
return { type: "Integer", id: instance.toString() };
}
return { type: instance.type, id: instance.id };
}
function toValuePattern(instance) {
if (instance === null) {
return { type: null, id: null };
}
if (typeof instance === "string") {
if (instance === "") {
throw new TypeError(`Oso: Instance cannot be an empty string. For wildcards, use the empty object ({}) or null.`);
}
return { type: "String", id: instance };
}
if (typeof instance === "boolean") {
return { type: "Boolean", id: instance.toString() };
}
if (typeof instance === "number") {
if (instance > Number.MAX_SAFE_INTEGER ||
instance < Number.MIN_SAFE_INTEGER) {
throw new Error(`Oso: integer identifier is outside the range of safe integers, making it unsuitable for use as an identifier`);
}
if (!Number.isInteger(instance)) {
throw new Error(`Oso: non-integer numeric values may not be used as identifiers`);
}
return { type: "Integer", id: instance.toString() };
}
if (typeof instance === "bigint") {
return { type: "Integer", id: instance.toString() };
}
if (instance.id === undefined || instance.id === null) {
if (instance.type === undefined || instance.type === null) {
return { type: null, id: null };
}
else {
return { type: instance.type, id: null };
}
}
else {
if (instance.type === undefined || instance.type === null) {
throw new TypeError(`Oso: Instances with an ID must also have a type: ${instance}`);
}
return { type: instance.type, id: instance.id };
}
}
//# sourceMappingURL=helpers.js.map