118 lines
2.4 KiB
JavaScript
118 lines
2.4 KiB
JavaScript
//
|
|
//#region Imports ...
|
|
const XValueTools = require('../tools/x-value.tools');
|
|
const XBaseDescriptor = require('./x-base.descriptor');
|
|
//#endregion
|
|
|
|
/**
|
|
* check an object is valid key/value object ...
|
|
* @param {any} object proposed object to check ...
|
|
* @returns
|
|
*/
|
|
function isDescriptor(object) {
|
|
//
|
|
let result = false;
|
|
result = object &&
|
|
object.hasOwnProperty('key') &&
|
|
object.hasOwnProperty('type') &&
|
|
object.hasOwnProperty('value');
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* retrieve base or filled KeyValueProvider object instance ...
|
|
*
|
|
* @param {any} value value provider object instance ...
|
|
* @returns an instance of KeyValueDescriptor ...
|
|
*/
|
|
function createDescriptor(value) {
|
|
//
|
|
const result = {
|
|
key: '',
|
|
value: undefined,
|
|
type: XBaseDescriptor.ContentTypes.Unknown,
|
|
};
|
|
|
|
//
|
|
//#region Apply Value ...
|
|
if (value) {
|
|
//
|
|
//#region key ...
|
|
if (XValueTools.isValidArg(value.key)) {
|
|
result.key = value.key;
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region type ...
|
|
if (
|
|
XValueTools.isValidArg(value.type) &&
|
|
XBaseDescriptor.isValidContentType(value.type)
|
|
) {
|
|
result.type = value.type;
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
//#region value ...
|
|
if (value.value) {
|
|
result.value = value.value;
|
|
}
|
|
//#endregion
|
|
}
|
|
//#endregion
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* converts an instance of Descriptor to Expression ...
|
|
*
|
|
* @param descriptor an instance of KeyValueDescriptor ...
|
|
* @param {string} assignSymbol property assign symbol ...
|
|
* @returns creator string ...
|
|
*/
|
|
function toExpression(
|
|
descriptor = createDescriptor(),
|
|
assignSymbol = ''
|
|
) {
|
|
//
|
|
let result = '';
|
|
|
|
//
|
|
// Validate Args ...
|
|
if (!isDescriptor(descriptor)) {
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Normalize Assign Symbol ...
|
|
assignSymbol = !XValueTools.isValidArg(assignSymbol) ?
|
|
'' :
|
|
assignSymbol;
|
|
|
|
//
|
|
// Handle Key ...
|
|
if (descriptor.key !== XBaseDescriptor.ExpressionKeys.IGNORED_KEY) {
|
|
result += descriptor.key + assignSymbol + ' ';
|
|
}
|
|
|
|
//
|
|
// Handle Value ...
|
|
const valueExpression = XBaseDescriptor.toExpression(descriptor);
|
|
result += valueExpression;
|
|
|
|
//
|
|
return result;
|
|
}
|
|
|
|
//
|
|
// Module Exports ...
|
|
module.exports = {
|
|
isDescriptor,
|
|
toExpression,
|
|
createDescriptor,
|
|
} |