From c489ffec4f6fb33a13f34ee6c10be1fe943c5cdb Mon Sep 17 00:00:00 2001 From: Daniel Garcia Date: Thu, 9 Sep 2021 11:34:26 +0200 Subject: [PATCH] Refactor the library completly --- src/definedObject.js | 46 ++++++++++++++++++++-------------------- src/index.js | 8 ++----- src/objects/file.js | 6 ++++-- src/types/array.js | 8 ++++--- src/types/base.js | 7 +++--- src/types/boolean.js | 2 ++ src/types/enum.js | 4 +++- src/types/number.js | 8 ++++--- src/types/string.js | 8 ++++--- src/types/typeFactory.js | 21 +++++++++--------- 10 files changed, 63 insertions(+), 55 deletions(-) diff --git a/src/definedObject.js b/src/definedObject.js index faa669a..56c6fc6 100644 --- a/src/definedObject.js +++ b/src/definedObject.js @@ -1,5 +1,7 @@ import MCF_TypeFactory from './types/typeFactory' + + export default class MCF_DefinedObject { constructor(definition) { this.definition = definition @@ -9,10 +11,6 @@ export default class MCF_DefinedObject { this.setValue(definition.values) } - getDefinition() { - return this.definition - } - getValue() { const value = {} for (const field in this.fields) { @@ -21,6 +19,23 @@ export default class MCF_DefinedObject { return value } + setValue(value) { + for (const valueKey in value) { + if (value[valueKey] === undefined) { + this.fields[valueKey].value = undefined + } else { + if (this.fields[valueKey] === undefined) { + throw new Error(`The field ${valueKey} doesn't exist in the current DefinedObject: ${JSON.stringify(Object.keys(this.fields), null, 2)}`) + } + this.fields[valueKey].setValue(value[valueKey]) + } + } + } + + getDefinition() { + return this.definition + } + validateValue(value) { for (const field in this.definition.fields) { if (value[field] === undefined || value === null) { @@ -37,27 +52,12 @@ export default class MCF_DefinedObject { } } - setValue(value) { - for (const valueKey in value) { - if (value[valueKey] === undefined) { - this.fields[valueKey].value = undefined - } else { - if (this.fields[valueKey] === undefined) { - throw new Error(`The field ${valueKey} doesn't exist in the current DefinedObject: ${JSON.stringify(Object.keys(this.fields), null, 2)}`) - } - this.fields[valueKey].setValue(value[valueKey]) - } - } - } - updateArrayField(field, arrayItem, mustBeIncluded) { const fieldArray = this.getValue()[field] || [] - if (!fieldArray.includes(arrayItem) && mustBeIncluded) { - fieldArray.push(arrayItem) - } - if (fieldArray.includes(arrayItem) && !mustBeIncluded) { - fieldArray.splice(fieldArray.indexOf(arrayItem), 1) - } + + if (!fieldArray.includes(arrayItem) && mustBeIncluded) fieldArray.push(arrayItem) + if (fieldArray.includes(arrayItem) && !mustBeIncluded) fieldArray.splice(fieldArray.indexOf(arrayItem), 1) + const newValue = {} newValue[field] = fieldArray this.setValue(newValue) diff --git a/src/index.js b/src/index.js index fbaedfd..5b7c0d5 100644 --- a/src/index.js +++ b/src/index.js @@ -1,16 +1,12 @@ -//TODO: Solucionar eslint -//TODO: Dividir correctamente la librería en clases //TODO: Hacer nuevo tipo de dato: array-enum -> array en el que los elementos solo pueden tomar determinados valores //TODO: Que no sea necesario pasar un objeto vacío ({}) para instanciar un mcfDefinedObject sin argumentos //TODO: Modificar getValue para que acepte como parámetro el nombre del campo que se quiere obtener: getvalue(fieldName) -//TODO: Añadir las funciones: createListFromDictionary, createCheckboxListFromDictionary, updateDefinedObjectArray a ¿una clase UI genérica (DefinedUI)? -//TODO: Mover la herencia de las clases derivadas a la clase padre MCF_DefinedObject -//TODO: Hacer una función que permita cambiar un atributo de un mcf_component a partir del id del componente, el nombre del atributo y el nuevo valor del atributo -//TODO: Hacer una función que permita encontrar un mcf_component a partir de su id + import MCF_DefinedObject from './definedObject' import MCF_File from './objects/file' + export { MCF_DefinedObject as DefinedObject, MCF_File as File, diff --git a/src/objects/file.js b/src/objects/file.js index 38bee3e..56ff7e1 100644 --- a/src/objects/file.js +++ b/src/objects/file.js @@ -1,6 +1,8 @@ import MCF_DefinedObject from '../definedObject' -const fileFields = { + + +const fileFieldsDefition = { name: { TYPE: 'string', required: true }, extension: { TYPE: 'string' }, mimeType: { TYPE: 'string' }, @@ -10,7 +12,7 @@ const fileFields = { export default class MCF_File extends MCF_DefinedObject { constructor(definition) { super({ - fields: { ...fileFields, ...definition.fields }, + fields: { ...fileFieldsDefition, ...definition.fields }, values: definition.values ? definition.values : definition, }) } diff --git a/src/types/array.js b/src/types/array.js index 1ff3fac..64df532 100644 --- a/src/types/array.js +++ b/src/types/array.js @@ -1,5 +1,7 @@ import MCF_BaseType from './base' + + export default class MCF_Array extends MCF_BaseType { constructor(definition) { super(definition) @@ -9,7 +11,7 @@ export default class MCF_Array extends MCF_BaseType { addValidations() { this.validations.type = function(value) { if (!Array.isArray(value)) { - throw new Error(`MCF-Objects ==> MCF_Array creation: the type of ${value} isn't "array"`) + throw new Error(`MCF-Objects ==> MCF_Array validation: the type of ${value} isn't "array"`) } } @@ -18,14 +20,14 @@ export default class MCF_Array extends MCF_BaseType { case 'maxLength': this.validations.maxLength = function(value, definition) { if (value.length > definition.maxLength) { - throw new Error(`MCF-Objects ==> MCF_Array creation: the length of the array "${value}" is higher than the maximum defined: ${definition.maxLength}`) + throw new Error(`MCF-Objects ==> MCF_Array validation: the length of the array "${value}" is higher than the maximum defined: ${definition.maxLength}`) } } break case 'minLength': this.validations.minLength = function(value, definition) { if (value.length < definition.minLength) { - throw new Error(`MCF-Objects ==> MCF_Array creation: the length of the array "${value}" is lower than the minimum defined: ${definition.minLength}`) + throw new Error(`MCF-Objects ==> MCF_Array validation: the length of the array "${value}" is lower than the minimum defined: ${definition.minLength}`) } } break diff --git a/src/types/base.js b/src/types/base.js index 1b908f6..8af0fd0 100644 --- a/src/types/base.js +++ b/src/types/base.js @@ -2,7 +2,10 @@ export default class MCF_BaseType { constructor(definition) { this.definition = definition this.validations = {} + } + getValue() { + return this.value } setValue(value) { @@ -10,10 +13,6 @@ export default class MCF_BaseType { this.value = value } - getValue() { - return this.value - } - validate(value) { for (const validation in this.validations) { this.validations[validation](value, this.definition) diff --git a/src/types/boolean.js b/src/types/boolean.js index 2f33d01..35d5bfa 100644 --- a/src/types/boolean.js +++ b/src/types/boolean.js @@ -1,5 +1,7 @@ import MCF_BaseType from './base' + + export default class MCF_Boolean extends MCF_BaseType { constructor(definition) { super(definition) diff --git a/src/types/enum.js b/src/types/enum.js index 14a8afb..8a89355 100644 --- a/src/types/enum.js +++ b/src/types/enum.js @@ -1,5 +1,7 @@ import MCF_BaseType from './base' + + export default class MCF_Enum extends MCF_BaseType { constructor(definition) { super(definition) @@ -13,7 +15,7 @@ export default class MCF_Enum extends MCF_BaseType { return isAvailable }, false) if (!valueIsAvailable) { - throw new Error(`MCF-Objects ==> MCF_Enum creation: the value "${value}" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`) + throw new Error(`MCF-Objects ==> MCF_Enum validation: the value "${value}" isn't one of the next values, included in the field definition: ${definition.availableValues.toString()}`) } } } diff --git a/src/types/number.js b/src/types/number.js index b4e89ab..c64ae87 100644 --- a/src/types/number.js +++ b/src/types/number.js @@ -1,4 +1,6 @@ import MCF_BaseType from './base' + + export default class MCF_Number extends MCF_BaseType { constructor(definition) { super(definition) @@ -8,7 +10,7 @@ export default class MCF_Number extends MCF_BaseType { addValidations() { this.validations.type = function(value) { if (typeof(value) !== 'number') { - throw new Error(`MCF-Objects ==> MCF_Number creation: the type of the value ${value} isn't "number"`) + throw new Error(`MCF-Objects ==> MCF_Number validation: the type of the value ${value} isn't "number"`) } } @@ -17,14 +19,14 @@ export default class MCF_Number extends MCF_BaseType { case 'max': this.validations.max = function(value, definition) { if (value > definition.max) { - throw new Error(`MCF-Objects ==> MCF_Number creation: the value ${value} is higher than the maximum defined: ${definition.max}`) + throw new Error(`MCF-Objects ==> MCF_Number validation: the value ${value} is higher than the maximum defined: ${definition.max}`) } } break case 'min': this.validations.min = function(value, definition) { if (value < definition.min) { - throw new Error(`MCF-Objects ==> MCF_Number creation: the value ${value} is lower than the minimum defined: ${definition.min}`) + throw new Error(`MCF-Objects ==> MCF_Number validation: the value ${value} is lower than the minimum defined: ${definition.min}`) } } break diff --git a/src/types/string.js b/src/types/string.js index 2c3f8fa..f5415bd 100644 --- a/src/types/string.js +++ b/src/types/string.js @@ -1,5 +1,7 @@ import MCF_BaseType from './base' + + export default class MCF_String extends MCF_BaseType { constructor(definition) { super(definition) @@ -9,7 +11,7 @@ export default class MCF_String extends MCF_BaseType { addValidations() { this.validations.type = function(value) { if (typeof(value) !== 'string') { - throw new Error(`MCF-Objects ==> MCF_String creation: the type of ${value} isn't "string"`) + throw new Error(`MCF-Objects ==> MCF_String validation: the type of ${value} isn't "string"`) } } @@ -18,14 +20,14 @@ export default class MCF_String extends MCF_BaseType { case 'maxLength': this.validations.maxLength = function(value, definition) { if (value.length > definition.maxLength) { - throw new Error(`MCF-Objects ==> MCF_String creation: the length of the string "${value}" is higher than the maximum defined: ${definition.maxLength}`) + throw new Error(`MCF-Objects ==> MCF_String validation: the length of the string "${value}" is higher than the maximum defined: ${definition.maxLength}`) } } break case 'minLength': this.validations.minLength = function(value, definition) { if (value.length < definition.minLength) { - throw new Error(`MCF-Objects ==> MCF_String creation: the length of the string "${value}" is lower than the minimum defined: ${definition.minLength}`) + throw new Error(`MCF-Objects ==> MCF_String validation: the length of the string "${value}" is lower than the minimum defined: ${definition.minLength}`) } } break diff --git a/src/types/typeFactory.js b/src/types/typeFactory.js index f1bdfb8..cccdc87 100644 --- a/src/types/typeFactory.js +++ b/src/types/typeFactory.js @@ -1,8 +1,9 @@ -import MCF_String from './string' -import MCF_Number from './number' -import MCF_Boolean from './boolean' import MCF_Array from './array' +import MCF_Boolean from './boolean' import MCF_Enum from './enum' +import MCF_Number from './number' +import MCF_String from './string' + export default class MCF_TypeFactory { @@ -12,11 +13,8 @@ export default class MCF_TypeFactory { const dictionary = {} for (const field in fields) { switch (fields[field]['TYPE']) { - case 'string': - dictionary[field] = new MCF_String(fields[field]) - break - case 'number': - dictionary[field] = new MCF_Number(fields[field]) + case 'array': + dictionary[field] = new MCF_Array(fields[field]) break case 'boolean': dictionary[field] = new MCF_Boolean(fields[field]) @@ -24,8 +22,11 @@ export default class MCF_TypeFactory { case 'enum': dictionary[field] = new MCF_Enum(fields[field]) break - case 'array': - dictionary[field] = new MCF_Array(fields[field]) + case 'number': + dictionary[field] = new MCF_Number(fields[field]) + break + case 'string': + dictionary[field] = new MCF_String(fields[field]) break default: throw new Error(`MCF-Objects ==> The object type ${fields[field]['TYPE']} does not exist. Check the field definition in the object definition ${objectDefinition}`)