Notes/src/services/promoted_attribute_definition_parser.ts

37 lines
1.2 KiB
TypeScript
Raw Normal View History

import { DefinitionObject } from "./promoted_attribute_definition_interface.js";
2024-02-17 01:19:49 +02:00
function parse(value: string): DefinitionObject {
2025-01-09 18:07:02 +02:00
const tokens = value.split(",").map((t) => t.trim());
2024-03-17 21:40:14 +02:00
const defObj: DefinitionObject = {};
for (const token of tokens) {
2025-01-09 18:07:02 +02:00
if (token === "promoted") {
defObj.isPromoted = true;
2025-01-09 18:07:02 +02:00
} else if (["text", "number", "boolean", "date", "datetime", "time", "url"].includes(token)) {
defObj.labelType = token;
2025-01-09 18:07:02 +02:00
} else if (["single", "multi"].includes(token)) {
defObj.multiplicity = token;
2025-01-09 18:07:02 +02:00
} else if (token.startsWith("precision")) {
const chunks = token.split("=");
defObj.numberPrecision = parseInt(chunks[1]);
2025-01-09 18:07:02 +02:00
} else if (token.startsWith("alias")) {
const chunks = token.split("=");
2023-09-22 04:58:06 -04:00
defObj.promotedAlias = chunks[1];
2025-01-09 18:07:02 +02:00
} else if (token.startsWith("inverse")) {
const chunks = token.split("=");
2025-01-09 18:07:02 +02:00
defObj.inverseRelation = chunks[1].replace(/[^\p{L}\p{N}_:]/gu, "");
} else {
console.log("Unrecognized attribute definition token:", token);
}
}
2024-03-17 21:40:14 +02:00
return defObj;
}
export default {
parse
};