From 81ce2a84bba24a6c69c74a91ff78fd25796a7654 Mon Sep 17 00:00:00 2001 From: Morgan Croney Date: Thu, 3 Sep 2015 14:42:39 -0400 Subject: [PATCH] Sharing string logic. --- services/form-values.js | 13 +++++++++++-- services/form-values.test.js | 25 +++++++++++++++---------- services/forms.js | 23 +++++------------------ 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/services/form-values.js b/services/form-values.js index fe4498637..17d981b05 100644 --- a/services/form-values.js +++ b/services/form-values.js @@ -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 @@ -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; } @@ -72,3 +80,4 @@ function getFormValues(form) { } module.exports.get = getFormValues; +module.exports.cleanTextField = cleanTextField; // Used by forms.js for detecting data changes. diff --git a/services/form-values.test.js b/services/form-values.test.js index 92592cddd..70d67f460 100644 --- a/services/form-values.test.js +++ b/services/form-values.test.js @@ -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'); }); }); + }); }); diff --git a/services/forms.js b/services/forms.js index 696e1b4c5..54b94237c 100644 --- a/services/forms.js +++ b/services/forms.js @@ -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 { @@ -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) { @@ -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); } @@ -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 of the page) // determine if the form is inline, and call the relevant formCreator method