generator-begcode
Version:
Spring Boot + Angular/React/Vue in one handy generator
89 lines (88 loc) • 2.54 kB
JavaScript
import { aigcTypeExists } from '../basic-types/aigc-types.js';
import { aigcTypes } from '../basic-types/index.js';
export default class JDLAigcs {
aigcs;
constructor() {
this.aigcs = {
Method: new Map(),
Api: new Map(),
Feature: new Map(),
};
}
add(aigc) {
if (!aigc) {
throw new Error('A aigc must be passed so as to be added.');
}
this.aigcs[aigc.type].set(aigc.getId(), aigc);
}
getMethod(aigcId) {
return this.get(aigcTypes.METHOD, aigcId);
}
getApi(aigcId) {
return this.get(aigcTypes.API, aigcId);
}
getFeature(aigcId) {
return this.get(aigcTypes.FEATURE, aigcId);
}
get(type, aigcId) {
if (!aigcTypeExists(type)) {
throw new Error(`A valid aigc type must be passed so as to retrieve the aigc, got '${type}'.`);
}
if (!aigcId) {
throw new Error('A aigc id must be passed so as to retrieve the aigc.');
}
return this.aigcs[type].get(aigcId);
}
methodQuantity() {
return this.aigcs.Method.size;
}
apiQuantity() {
return this.aigcs.Api.size;
}
featureQuantity() {
return this.aigcs.Feature.size;
}
size() {
return this.methodQuantity() + this.apiQuantity() + this.featureQuantity();
}
forEach(passedFunction) {
if (!passedFunction) {
return;
}
this.toArray().forEach(aigc => {
passedFunction(aigc);
});
}
toArray() {
const aigcs = [];
Object.keys(this.aigcs).forEach(type => {
this.aigcs[type].forEach(aigc => {
aigcs.push(aigc);
});
});
return aigcs;
}
toString() {
if (this.size() === 0) {
return '';
}
let string = '';
Object.keys(this.aigcs).forEach(type => {
if (this.aigcs[type].size !== 0) {
const result = aigcTypeToString(this.aigcs[type], type);
string += `${result}\n`;
}
});
return string.slice(0, string.length - 1);
}
}
function aigcTypeToString(aigcs, type) {
let aigc = `aigc ${type} {\n`;
aigcs.forEach(internalAigc => {
let lines = internalAigc.toString().split('\n');
lines = lines.slice(1, lines.length - 1);
aigc += `${lines.join('\n')}\n`;
});
aigc = `${aigc.slice(0, aigc.length - 1)}\n}`;
return aigc;
}