Initial Commit ...
This commit is contained in:
@@ -0,0 +1,307 @@
|
||||
//
|
||||
//#region Imports ...
|
||||
const XFileTools = require('../tools/x-file.tools');
|
||||
const XValueTools = require('../tools/x-value.tools');
|
||||
const XBaseDescriptor = require('./x-base.descriptor');
|
||||
const XContentDescriptor = require('./x-content.descriptor');
|
||||
//#endregion
|
||||
|
||||
/**
|
||||
* check an object instance is EnumDescriptor or not ...
|
||||
*
|
||||
* @param {any} object proposed object to check ...
|
||||
* @returns
|
||||
*/
|
||||
function isDescriptor(object) {
|
||||
//
|
||||
if (!object) {
|
||||
return false;
|
||||
}
|
||||
|
||||
//
|
||||
let result = false;
|
||||
result =
|
||||
object.hasOwnProperty('raw') &&
|
||||
object.hasOwnProperty('name') &&
|
||||
object.hasOwnProperty('content') &&
|
||||
object.hasOwnProperty('isExported');
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* retrieve base or filled EnumDescriptor object instance ...
|
||||
*
|
||||
* @param {any} value a value provider object ...
|
||||
* @returns an instance of EnumDescriptor object ...
|
||||
*/
|
||||
function createDescriptor(value) {
|
||||
//
|
||||
let result = {
|
||||
raw: '',
|
||||
name: '',
|
||||
content: {
|
||||
...XContentDescriptor.createDescriptor()
|
||||
},
|
||||
isExported: false,
|
||||
}
|
||||
|
||||
//
|
||||
//#region Apply Value ...
|
||||
if (value) {
|
||||
//
|
||||
//#region raw ...
|
||||
if (XValueTools.isValidArg(value.raw)) {
|
||||
result.raw = value.raw;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region name ...
|
||||
if (XValueTools.isValidArg(value.name)) {
|
||||
result.name = value.name;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region content ...
|
||||
if (
|
||||
value.content &&
|
||||
XContentDescriptor.isDescriptor(value.content)
|
||||
) {
|
||||
result.content = {
|
||||
...value.content
|
||||
};
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region isExported ...
|
||||
if (!!value.isExported) {
|
||||
result.isExported = value.isExported;
|
||||
}
|
||||
//#endregion
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* converts an instance of Descriptor to Expression ...
|
||||
*
|
||||
* @param descriptor an instance of EnumDescriptor ...
|
||||
* @returns creator string ...
|
||||
*/
|
||||
function toExpression(descriptor = createDescriptor()) {
|
||||
//
|
||||
let result = '';
|
||||
|
||||
//
|
||||
// Validate Arg ...
|
||||
if (!isDescriptor(descriptor)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
result += (
|
||||
!!descriptor.isExported ?
|
||||
XBaseDescriptor.ExpressionKeys.Export + ' ' :
|
||||
''
|
||||
) +
|
||||
XBaseDescriptor.ExpressionKeys.Enum +
|
||||
' ' +
|
||||
descriptor.name +
|
||||
' ' +
|
||||
'{';
|
||||
|
||||
//
|
||||
let contentExpression = XContentDescriptor.toExpression(descriptor.content, ' =');
|
||||
if (XValueTools.isValidArg(contentExpression)) {
|
||||
//
|
||||
if (XValueTools.isSurroundedArray(contentExpression)) {
|
||||
contentExpression = XValueTools.clearArraySurround(contentExpression);
|
||||
} else if (XValueTools.isSurroundedObject(contentExpression)) {
|
||||
contentExpression = XValueTools.clearObjectSurround(contentExpression);
|
||||
}
|
||||
|
||||
//
|
||||
if (XValueTools.isValidArg(contentExpression)) {
|
||||
result += contentExpression;
|
||||
}
|
||||
}
|
||||
|
||||
//
|
||||
result += '}';
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* extract all exists EnumDescriptors ...
|
||||
*
|
||||
* @param {string} file a file path ...
|
||||
* @returns a collection of EnumDescriptor ...
|
||||
*/
|
||||
async function extractDescriptors(file = '') {
|
||||
//
|
||||
if (!XFileTools.isFileExists(file)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
//
|
||||
const fileContent = await XFileTools.readFile(file);
|
||||
if (!XValueTools.isValidArg(fileContent)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
//
|
||||
let result = [
|
||||
createDescriptor()
|
||||
];
|
||||
result.pop();
|
||||
|
||||
//
|
||||
result = extractDescriptorsFromContent(fileContent);
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* extract all exists EnumDescriptors ...
|
||||
*
|
||||
* @param {string} content a file content ...
|
||||
* @returns a collection of EnumDescriptor ...
|
||||
*/
|
||||
function extractDescriptorsFromContent(content = '') {
|
||||
//
|
||||
// Prepare Result ...
|
||||
let result = [
|
||||
createDescriptor()
|
||||
];
|
||||
result.pop();
|
||||
|
||||
//
|
||||
// Validate Args ...
|
||||
if (
|
||||
!XValueTools.isValidArg(content) ||
|
||||
!content.includes(XBaseDescriptor.ExpressionKeys.Enum)
|
||||
) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Extract all key Indexes ...
|
||||
const keyIndexes = XBaseDescriptor.
|
||||
extractValidIdentifierIndexes(XBaseDescriptor.ExpressionKeys.Enum, content);
|
||||
|
||||
//
|
||||
// Validate them ...
|
||||
if (!XValueTools.hasChildArray(keyIndexes)) {
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Loop through indexes ...
|
||||
for (const index of keyIndexes) {
|
||||
//
|
||||
let startIndex = index - 1;
|
||||
let endIndex = index + XBaseDescriptor.ExpressionKeys.Enum.length + 1;
|
||||
|
||||
//
|
||||
// create an empty instance of descriptor ...
|
||||
let descriptor = {
|
||||
...createDescriptor()
|
||||
}
|
||||
|
||||
//
|
||||
//#region Name extractor ...
|
||||
const nameEndIndex = content.indexOf('{', index);
|
||||
identifier = content
|
||||
.substring(index + XBaseDescriptor.ExpressionKeys.Enum.length, nameEndIndex)
|
||||
.trim();
|
||||
if (XValueTools.isValidArg(identifier)) {
|
||||
descriptor.name = identifier;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region Extract isExported ...
|
||||
const isExported = XBaseDescriptor.checkIsExported(index, content);
|
||||
descriptor.isExported = isExported;
|
||||
if (isExported) {
|
||||
startIndex = index - XBaseDescriptor.ExpressionKeys.Export.length - 1;
|
||||
}
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region Extract raw content ...
|
||||
identifier = content.substring(startIndex);
|
||||
let closedContenstIndexes = XValueTools.findClosedContent('{', '}', identifier);
|
||||
if (!XValueTools.hasChildArray(closedContenstIndexes)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
startIndex = 0;
|
||||
endIndex = closedContenstIndexes[0].end;
|
||||
if (endIndex < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
const raw = identifier.substring(startIndex, endIndex + 1);
|
||||
if (!XValueTools.isValidArg(raw)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
//
|
||||
descriptor.raw = raw;
|
||||
//#endregion
|
||||
|
||||
//
|
||||
//#region Parse Enum Content ...
|
||||
startIndex = closedContenstIndexes[0].start;
|
||||
endIndex = closedContenstIndexes[0].end + 1;
|
||||
let contentContainer = identifier.substring(startIndex, endIndex);
|
||||
contentContainer = XBaseDescriptor.clearContent(contentContainer);
|
||||
|
||||
//
|
||||
const contentType = XBaseDescriptor.getContentType(contentContainer);
|
||||
const parsedContents = XBaseDescriptor.parseContent(contentContainer);
|
||||
const contentDescriptor = XContentDescriptor.createDescriptor();
|
||||
|
||||
//
|
||||
contentDescriptor.type = contentType;
|
||||
contentDescriptor.childs = [
|
||||
...parsedContents
|
||||
];
|
||||
|
||||
//
|
||||
descriptor.content = {
|
||||
...contentDescriptor
|
||||
};
|
||||
//#endregion
|
||||
|
||||
//
|
||||
result.push(descriptor);
|
||||
}
|
||||
|
||||
//
|
||||
return result;
|
||||
}
|
||||
|
||||
//
|
||||
// Moduile Exports ...
|
||||
module.exports = {
|
||||
//
|
||||
isDescriptor,
|
||||
toExpression,
|
||||
createDescriptor,
|
||||
extractDescriptors,
|
||||
extractDescriptorsFromContent,
|
||||
}
|
||||
Reference in New Issue
Block a user