Skip to content

Commit

Permalink
Tweaks for clarity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Croney committed Sep 3, 2015
1 parent 81ce2a8 commit ec4de9a
Showing 1 changed file with 11 additions and 19 deletions.
30 changes: 11 additions & 19 deletions services/forms.js
Expand Up @@ -64,27 +64,20 @@ function setEditingStatus(isEditing) {
}
}

/**
* If the val is a string, then remove white spaces.
* @param {*} val
*/
function removeSpacesFromStrings(val) {
if (_.isString(val)) {
return formValues.cleanTextField(val);
} else if (_.isObject(val)) {
return removeSpacesFromData(val);
} else {
return val;
}
}

/**
* Recursively remove white spaces from all string values in the object.
* This is necessary because form-values.js removes white spaces as well.
* @param {{}} data
*/
function removeSpacesFromData(data) {
return _.mapValues(data, removeSpacesFromStrings);
function cleanDeepStringValues(data) {
return _.mapValues(data, function (val) {
if (_.isString(val)) {
return formValues.cleanTextField(val);
} else if (_.isObject(val)) {
return cleanDeepStringValues(val);
} else {
return val;
}
});
}

/**
Expand Down Expand Up @@ -118,7 +111,7 @@ function removeDeepMetaProperties(data) {
* @returns {boolean}
*/
function dataChanged(serverData, formData) {
var serverDataReduced = removeSpacesFromData(removeDeepMetaProperties(serverData)),
var serverDataReduced = cleanDeepStringValues(removeDeepMetaProperties(serverData)), // necessary because form-values.js cleans strings as well.
formDataReduced = removeDeepMetaProperties(formData);

return !_.isEqual(serverDataReduced, formDataReduced);
Expand All @@ -143,7 +136,6 @@ function open(ref, el, path, e) {

// grab the (possibly cached) data and create the form
return edit.getData(ref).then(function (data) {

// set current form data
currentForm = {
ref: ref,
Expand Down

0 comments on commit ec4de9a

Please sign in to comment.