Skip to content

Commit

Permalink
Sharing string logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgan Croney committed Sep 3, 2015
1 parent 61bae0d commit 81ce2a8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 30 deletions.
13 changes: 11 additions & 2 deletions services/form-values.js
Expand Up @@ -21,6 +21,15 @@ function removeBehaviorMeta(value) {
}
}

/**
* Removes non-breaking spaces and trims the string.
* @param {string} str
* @returns {string}
*/
function cleanTextField(str) {
return str.replace(/(\u00a0| | )/g, ' ').trim();
}

/**
* get values from inputs, lists, etc
* @param {array} bindings
Expand All @@ -40,8 +49,7 @@ function getValues(bindings, data, el) {
viewData = removeBehaviorMeta(viewData);
// // if the data is a string, trim it!
if (viewData.value && _.isString(viewData.value)) {
viewData.value = viewData.value.replace(/(\u00a0| | )/g, ' '); // remove  
viewData.value = viewData.value.trim();
viewData.value = module.exports.cleanTextField(viewData.value);
}
data[name] = viewData;
}
Expand Down Expand Up @@ -72,3 +80,4 @@ function getFormValues(form) {
}

module.exports.get = getFormValues;
module.exports.cleanTextField = cleanTextField; // Used by forms.js for detecting data changes.
25 changes: 15 additions & 10 deletions services/form-values.test.js
Expand Up @@ -261,27 +261,32 @@ describe(dirname, function () {
] });
});

it('removes nonbreaking spaces from strings', function () {
it('cleans text for string values', function () {
var field = stubField('foo', true),
form = stubForm([field]);

sandbox.stub(lib, 'cleanTextField');
formCreator.getBindings.returns(stubBinding(form, { foo: {
data: { value: 'b a\u00a0r r' }
data: { value: 'a string' }
}}));
fn(form);

expect(fn(form)).to.eql({ foo: { value: 'b a r r' }});
expect(lib.cleanTextField.calledOnce).to.be.true;
});

it('trims strings', function () {
var field = stubField('foo', true),
form = stubForm([field]);
});

formCreator.getBindings.returns(stubBinding(form, { foo: {
data: { value: ' b a r ' }
}}));
describe('cleanTextField', function () {
var fn = lib[this.title];

it('removes non-breaking spaces', function () {
expect(fn('b a\u00a0r r')).to.eql('b a r r');
});

expect(fn(form)).to.eql({ foo: { value: 'b a r' }});
it('trims string', function () {
expect(fn(' b a r ')).to.eql('b a r');
});
});

});
});
23 changes: 5 additions & 18 deletions services/forms.js
Expand Up @@ -70,8 +70,7 @@ function setEditingStatus(isEditing) {
*/
function removeSpacesFromStrings(val) {
if (_.isString(val)) {
// todo: this text formatting should share a function with form-values to keep them in-sync if there are future changes.
return val.replace(/(\u00a0| | )/g, ' ').trim(); // remove   to be consistent with form-values.js
return formValues.cleanTextField(val);
} else if (_.isObject(val)) {
return removeSpacesFromData(val);
} else {
Expand All @@ -81,6 +80,7 @@ function removeSpacesFromStrings(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) {
Expand Down Expand Up @@ -121,13 +121,6 @@ function dataChanged(serverData, formData) {
var serverDataReduced = removeSpacesFromData(removeDeepMetaProperties(serverData)),
formDataReduced = removeDeepMetaProperties(formData);

console.log('currentForm.ref', currentForm.ref);
console.log('currentForm.path', currentForm.path);
console.log('serverData', serverData);
console.log('formData', formData);
console.log('serverDataReduced', serverDataReduced);
console.log('formDataReduced', formDataReduced);
console.log('is equal', _.isEqual(serverDataReduced, formDataReduced));
return !_.isEqual(serverDataReduced, formDataReduced);
}

Expand All @@ -150,24 +143,18 @@ 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,
path: path
};

console.log('data before groups', _.cloneDeep(data));

// saving the data in the normal format, prior to groups.get. Can we get rid of groups.get?

// must clone because data object is changed by groups and formCreator -- can we change this?
currentForm.data = _.cloneDeep(data); // set that data into the currentForm

// This is where the data object is getting changed, perhaps a cloneDeep would make sense here?

// then get a subset of the data, for the specific field / group
// get a subset of the data, for the specific field / group
data = groups.get(ref, data, path); // note: if path is undefined, it'll open the settings form

console.log('data after groups', data);
setEditingStatus(true); // set editing status (a class on the <body> of the page)

// determine if the form is inline, and call the relevant formCreator method
Expand Down

0 comments on commit 81ce2a8

Please sign in to comment.