Refactor the library completly
This commit is contained in:
parent
b095bed5ca
commit
c489ffec4f
10 changed files with 63 additions and 55 deletions
|
@ -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)
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
})
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
import MCF_BaseType from './base'
|
||||
|
||||
|
||||
|
||||
export default class MCF_Boolean extends MCF_BaseType {
|
||||
constructor(definition) {
|
||||
super(definition)
|
||||
|
|
|
@ -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()}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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}`)
|
||||
|
|
Loading…
Reference in a new issue