@memory-bank/mcp
Version:
Memory-enabled Co-Pilot (MCP) server for managing project documentation and context
179 lines • 6.32 kB
JavaScript
/**
* Section class represents a section of a template with localized titles and content
*/
export class Section {
_id;
_titleMap;
_contentMap;
_isOptional;
/**
* Constructor with validation
*
* @param id Section identifier
* @param titleMap Map of language codes to localized titles
* @param contentMap Optional map of language codes to localized content
* @param isOptional Whether this section is optional
* @throws Error if id is empty or titleMap is empty
*/
constructor(id, titleMap, contentMap = {}, isOptional = false) {
if (!id || id.trim() === '') {
throw new Error('Section ID cannot be empty');
}
if (!titleMap || Object.keys(titleMap).length === 0) {
throw new Error('Section must have at least one title translation');
}
this._id = id.trim();
this._titleMap = { ...titleMap };
this._contentMap = { ...contentMap };
this._isOptional = isOptional;
}
/**
* Gets the section identifier
*/
get id() {
return this._id;
}
/**
* Gets the map of language codes to titles
*/
get titleMap() {
return { ...this._titleMap };
}
/**
* Gets the map of language codes to content
*/
get contentMap() {
return { ...this._contentMap };
}
/**
* Gets whether this section is optional
*/
get isOptional() {
return this._isOptional;
}
/**
* Creates a Section instance, validating inputs
*
* @param id Section identifier
* @param titleMap Map of language codes to localized titles
* @param contentMap Optional map of language codes to localized content
* @param isOptional Whether this section is optional
* @returns Section instance
* @throws Error if id is empty or titleMap is empty
*/
static create(id, titleMap, contentMap = {}, isOptional = false) {
return new Section(id, titleMap, contentMap, isOptional);
}
/**
* Gets the localized title for a specific language
*
* @param language Language to get title for
* @returns Localized title string
*/
getTitle(language) {
// Try to get title for the requested language
if (this._titleMap[language.code]) {
// Return title for the specified language, fallback to English, then empty string
return this._titleMap[language.code] ?? this._titleMap['en'] ?? '';
}
// Fall back to English if available
if (this._titleMap.en) {
return this._titleMap.en;
}
// Fall back to first available language
const firstKey = Object.keys(this._titleMap)[0];
// Fallback logic for getTitle without language (already handles fallback)
return this._titleMap[firstKey] ?? ''; // Ensure empty string fallback
}
/**
* Gets the localized content for a specific language
*
* @param language Language to get content for
* @returns Localized content string or empty string if not available
*/
getContent(language) {
// Try to get content for the requested language
if (this._contentMap[language.code]) {
// Return content for the specified language, fallback to English, then empty string
return this._contentMap[language.code] ?? this._contentMap['en'] ?? '';
}
// If no content for requested language, try English
if (this._contentMap.en) {
return this._contentMap.en;
}
// If no English content, try first available
const contentKeys = Object.keys(this._contentMap);
if (contentKeys.length > 0) {
const firstKey = contentKeys[0];
// Fallback logic for getContent without language (already handles fallback)
return this._contentMap[firstKey] ?? ''; // Ensure empty string fallback
}
// No content available
return '';
}
/**
* Creates a new Section with updated content for a language
*
* @param languageCode Language code to update content for
* @param content New content text
* @returns New Section instance with updated content
*/
withContent(languageCode, content) {
// Create new content map with updated value
const newContentMap = {
...this._contentMap,
[languageCode]: content,
};
return new Section(this._id, this._titleMap, newContentMap, this._isOptional);
}
/**
* Creates a new Section with updated title for a language
*
* @param languageCode Language code to update title for
* @param title New title text
* @returns New Section instance with updated title
*/
withTitle(languageCode, title) {
// Create new title map with updated value
const newTitleMap = {
...this._titleMap,
[languageCode]: title,
};
return new Section(this._id, newTitleMap, this._contentMap, this._isOptional);
}
/**
* Compare equality with another Section object
*
* @param other Another Section object to compare
* @returns true if equal, false otherwise
*/
equals(other) {
if (this._id !== other.id || this._isOptional !== other.isOptional) {
return false;
}
// Compare title maps
const thisTitleKeys = Object.keys(this._titleMap);
const otherTitleKeys = Object.keys(other.titleMap);
if (thisTitleKeys.length !== otherTitleKeys.length) {
return false;
}
for (const key of thisTitleKeys) {
if (this._titleMap[key] !== other.titleMap[key]) {
return false;
}
}
// Compare content maps
const thisContentKeys = Object.keys(this._contentMap);
const otherContentKeys = Object.keys(other.contentMap);
if (thisContentKeys.length !== otherContentKeys.length) {
return false;
}
for (const key of thisContentKeys) {
if (this._contentMap[key] !== other.contentMap[key]) {
return false;
}
}
return true;
}
}
//# sourceMappingURL=Section.js.map