Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getDocumentValue(fieldName)
{
if(this.doesDocumentValueExist(fieldName))
{
let doc = {};
Dot.copy(fieldName, fieldName, this.props.doc, doc);
doc = Dot.dot(doc);
// If it's a number
if(!isNaN(parseFloat(doc[fieldName])) && isFinite(doc[fieldName]))
{
// Return it as a String
return doc[fieldName].toString();
}
return doc[fieldName];
}
return false;
}
getDocumentValue(fieldName)
{
if(this.doesDocumentValueExist(fieldName))
{
let doc = {};
Dot.copy(fieldName, fieldName, this.props.doc, doc);
doc = Dot.dot(doc);
// If it's a number
if(!isNaN(parseFloat(doc[fieldName])) && isFinite(doc[fieldName]))
{
// Return it as a String
return doc[fieldName].toString();
}
return doc[fieldName];
}
return false;
}
doesDocumentValueExist(fieldName)
{
const doc = {};
Dot.copy(fieldName, fieldName, this.props.doc, doc);
// If we're updating an existing document and the value exists here
return this.props.type === 'update' && typeof doc !== 'undefined' && doc !== null;
}
Object.keys(this.props.schema).map(fieldName =>
{
if(typeof state[`${fieldName}_fieldValue`] !== 'undefined' &&
!(this.props.type === 'insert' && state[`${fieldName}_fieldValue`] === '') &&
this.getDocumentValue(fieldName) !== this.getStateOrDefaultSchemaValue(fieldName, null, null, state))
{
formFields[fieldName] = this.getStateOrDefaultSchemaValue(fieldName, null, null, state); // Gets the state value
if(fieldName.indexOf('.') > 0) // If this fieldName belongs to object
{
let fieldNameObj = Dot.object(Object.assign({}, {[fieldName]: formFields[fieldName]})), // Get the entire object
// schemaKey = fieldName.substr(0, fieldName.lastIndexOf('.')); // Get the parent object key
schemaKey = fieldName.substr(0, fieldName.indexOf('.')); // Get the parent object key
Dot.copy(schemaKey, schemaKey, this.props.doc, fieldNameObj); // Copy the original object
formFields = {...Dot.dot(fieldNameObj), ...formFields}; // Turn the original object into dotted object and then merge it with the new fieldName value
}
}
});
Object.keys(this.props.schema).map(fieldName =>
{
if(typeof state[`${fieldName}_fieldValue`] !== 'undefined' &&
!(this.props.type === 'insert' && state[`${fieldName}_fieldValue`] === '') &&
this.getDocumentValue(fieldName) !== this.getStateOrDefaultSchemaValue(fieldName, null, null, state))
{
formFields[fieldName] = this.getStateOrDefaultSchemaValue(fieldName, null, null, state); // Gets the state value
if(fieldName.indexOf('.') > 0) // If this fieldName belongs to object
{
let fieldNameObj = Dot.object(Object.assign({}, {[fieldName]: formFields[fieldName]})), // Get the entire object
// schemaKey = fieldName.substr(0, fieldName.lastIndexOf('.')); // Get the parent object key
schemaKey = fieldName.substr(0, fieldName.indexOf('.')); // Get the parent object key
Dot.copy(schemaKey, schemaKey, this.props.doc, fieldNameObj); // Copy the original object
formFields = {...Dot.dot(fieldNameObj), ...formFields}; // Turn the original object into dotted object and then merge it with the new fieldName value
}
}
});
doesDocumentValueExist(fieldName)
{
const doc = {};
Dot.copy(fieldName, fieldName, this.props.doc, doc);
// If we're updating an existing document and the value exists here
return this.props.type === 'update' && typeof doc !== 'undefined' && doc !== null;
}
return Object.keys(model).reduce((acc, key) => dot.copy(key, model[key], response, acc), {});
};