154 lines
3.5 KiB
JavaScript
154 lines
3.5 KiB
JavaScript
//
|
|
//#region Imports ...
|
|
const XValueTools = require('../tools/x-value.tools');
|
|
const XBaseDescriptor = require('./x-base.descriptor');
|
|
const XKeyValueDescriptor = require('./x-key-value.descriptor');
|
|
//#endregion
|
|
|
|
/**
|
|
* check an object is valid ContentDescriptor object ...
|
|
*
|
|
* @param {any} object proposed object to check ...
|
|
* @returns
|
|
*/
|
|
function isDescriptor(object) {
|
|
//
|
|
let result = false;
|
|
result = object &&
|
|
object.hasOwnProperty('type') &&
|
|
object.hasOwnProperty('childs') &&
|
|
Array.isArray(object.childs) &&
|
|
(
|
|
object.childs.length > 0 ?
|
|
object.childs.every(ch => XKeyValueDescriptor.isDescriptor(ch)) :
|
|
true
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* converts an instance of Descriptor to Expression ...
|
|
*
|
|
* @param descriptor an instance of ContentDescriptor ...
|
|
* @param {string} assignSymbol property assign symbol ...
|
|
* @returns creator string ...
|
|
*/
|
|
function toExpression(
|
|
descriptor = createDescriptor(),
|
|
assignSymbol = ':',
|
|
separator = ','
|
|
) {
|
|
//
|
|
let result = '';
|
|
|
|
//
|
|
// Validate Arg ...
|
|
if (!isDescriptor(descriptor)) {
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Normalize Property Assign Symbol ...
|
|
assignSymbol = !XValueTools.isValidArg(assignSymbol) ?
|
|
':' :
|
|
assignSymbol;
|
|
|
|
//
|
|
const isArrayType = descriptor.type === XBaseDescriptor.ContentTypes.Array;
|
|
const isObjectType = descriptor.type === XBaseDescriptor.ContentTypes.Object;
|
|
|
|
//
|
|
result += (
|
|
isObjectType ? '{\n' :
|
|
isArrayType ? '[\n' :
|
|
''
|
|
);
|
|
|
|
//
|
|
for (let i = 0; i < descriptor.childs.length; i++) {
|
|
//
|
|
const keyVal = descriptor.childs[i];
|
|
const keyValExpression = XKeyValueDescriptor.toExpression(keyVal, assignSymbol);
|
|
|
|
//
|
|
if (separator === ';') {
|
|
//
|
|
result += keyValExpression + (
|
|
i === descriptor.childs.length - 1 ?
|
|
separator :
|
|
`${separator}\n`
|
|
);
|
|
} else {
|
|
//
|
|
result += keyValExpression + (
|
|
i === descriptor.childs.length - 1 ?
|
|
'' :
|
|
`${separator}\n`
|
|
);
|
|
}
|
|
}
|
|
|
|
//
|
|
result += (
|
|
isObjectType ? '\n}' :
|
|
isArrayType ? '\n]' :
|
|
''
|
|
);
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* retrieve base or filled ContentDescriptor object instance ...
|
|
*
|
|
* @param {any} value value provider object instance ...
|
|
* @returns an instance of ContentDescriptor ...
|
|
*/
|
|
function createDescriptor(value) {
|
|
//
|
|
let result = {
|
|
type: XBaseDescriptor.ContentTypes.Unknown,
|
|
childs: [
|
|
XKeyValueDescriptor.createDescriptor()
|
|
]
|
|
};
|
|
result.childs.pop();
|
|
|
|
//
|
|
//#region Apply Value ...
|
|
if (value) {
|
|
//
|
|
//#region type ...
|
|
if (XBaseDescriptor.isValidContentType(value.type)) {
|
|
result.type = value.type;
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region childs ...
|
|
if (
|
|
XValueTools.hasChildArray(value.childs) &&
|
|
value.childs.every(ch => XKeyValueDescriptor.isDescriptor(ch))
|
|
) {
|
|
result.childs = [
|
|
...value.childs
|
|
];
|
|
}
|
|
//#endregion
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Module Exports ...
|
|
module.exports = {
|
|
isDescriptor,
|
|
toExpression,
|
|
createDescriptor,
|
|
} |