@supercat1337/meta-tree
v1.0.7
Published
A JavaScript library for creating, managing, and serializing hierarchical data structures with fields, sections, and records.
Downloads
124
Readme
Structured Data Tree Library
A JavaScript library for creating, managing, and serializing hierarchical data structures with fields, sections, and records.
Installation
npm install @supercat1337/meta-treeUsage
Importing the Library
import {
Field,
Record,
Section,
Tree,
treeFromString,
} from "@supercat1337/meta-tree";Core Classes
1. Field
Represents a single data field with attributes and metadata.
const field = new Field(
"username", // name
false, // isOptional
"anonymous", // defaultValue
"The user display name" // description
);
// Manage attributes
field.setAttribute("maxLength", "32");
field.setAttribute("validation", "alphanumeric");
console.log(field.stringify());2. Section
A container for related fields.
const section = new Section("userDetails");
section.addField(new Field("email", false, null, "User email address"));
section.addField(new Field("age", true, null, "User age"));
console.log(section.getFields());3. Record
Represents a complete record with multiple sections.
const userRecord = new Record(
"User", // entityName
"Profile", // propertyName
"Update", // verb
"User profile information" // description
);
// Add fields to main section
userRecord.addField(new Field("username"));
// Create and add to custom section
const addressSection = userRecord.addSection("address");
addressSection.addField(new Field("street"));
addressSection.addField(new Field("city"));
console.log(userRecord.stringify());4. Tree
A collection of records that can be serialized/deserialized.
const tree = new Tree();
// Add records
tree.addRecord("Product", "Price", "Update");
tree.addRecord("Order", null, "Create");
// Convert to string
const treeString = tree.stringify();
// Parse from string
const parsedTree = treeFromString(treeString);API Reference
Field
constructor(name: string, isOptional?: boolean, defaultValue?: string | null, description?: string | null)- Properties:
name: stringattributes: Map<string, string|null>isOptional: booleandefaultValue: string|nulldescription: string|null
- Methods:
hasAttribute(name: string): booleangetAttribute(name: string): string|nulldeleteAttribute(name: string): voidsetAttribute(name: string, value?: string|null): voidstringify(): stringtoJSON(): object
Record
constructor(entityName: string, propertyName: string | null, verb: string | null, description?: string | null)- Properties:
entityName: stringpropertyName: string|nullverb: string|nullsections: Map<string, Section>mainSection: Sectiondescription: string|null
- Methods:
getFullName(): stringaddSection(name: string): SectiongetSection(name: string): Section|nulldeleteSection(name: string): voidhasSection(name: string): booleansetSection(section: Section): voidgetSections(): Section[]addField(field: Field, sectionName?: string): voidgetField(name: string, sectionName?: string): Field|nullhasField(name: string, sectionName?: string): booleansetField(field: Field, sectionName?: string): voiddeleteField(name: string, sectionName?: string): voidgetFields(sectionName?: string): Field[]|nullstringify(): stringtoJSON(): object
Section
constructor(name: string)- Properties:
name: stringfields: Map<string, Field>
- Methods:
addField(field: Field): voidhasField(name: string): booleangetField(name: string): Field|nullsetField(field: Field): voiddeleteField(name: string): voidgetFields(): Field[]stringify(padding?: string): stringtoJSON(): object
Tree
- Properties:
records: Map<string, Record>
- Methods:
addRecord(entityName: string, propertyName?: string | null, verb?: string | null, description?: string | null): RecordhasRecord(recordFullName: string): booleangetRecord(recordFullName: string): Record|nulldeleteRecord(recordFullName: string): voidgetRecords(): Record[]setRecord(record: Record): voidgetRecordNames(): string[]stringify(): stringtoJSON(): object
Utility Functions
treeFromString(treeString: string): Tree- Parses a string representation into a Tree object
Serialization Format
The library supports string serialization with a custom format:
Entity[.Property][.Verb] [// description]
Field [attribute] [attribute=value] ... [// description] // required
Field? [attribute] [attribute=value] ... ... [// description] // optional
[Field="defaultValue"] [attribute] [attribute=value] ... ... [// description] // optional with default value
...
@SectionName // use section name as the section header
Field [attribute] [attribute=value] ... ... [// description] // required
Field? [attribute] [attribute=value] ... ... [// description] // optional
\[Field="defaultValue"\] [attribute] [attribute=value] ... ... [// description] // optional with default valueExample:
user.profile.update
username maxLength="32" // Sample description of the field
password minLength="8"
@returns
result boolean // boolean is attribute
user.profile.create
username maxLength="32" // The display name
password minLength="8"
@returns
userId // no attrs hereLicense
MIT
