Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/constants/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/util/handleChangeText.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {isEmpty, replace} from 'lodash';
import EMOJI_FILTER_REGEXP from '../constants';

/**
* Clean questions's value by removing emojis like expressions.
* @param {String} value The new answer to be handled.
* @param {Boolean} allowEmojis Should emojis be allowed or not.
*/
const sanitize = (value, allowEmojis) => (allowEmojis ? value : replace(value, new RegExp(EMOJI_FILTER_REGEXP), ''));

/**
* Invoke question's RegExp to format the value.
Expand All @@ -12,11 +20,12 @@ const formatValue = (value, regex) => (regex ? replace(value, new RegExp(regex),
* @param {Object} question Question's data.
* @param {String} question.name The name of question field.
* @param {RegExp|String} question.regex The RegExp for matching text with a pattern.
* @param {Boolean} question.allowEmojis Should emojis be allowed or not.
* @param {String} value The new answer to be handled.
* @param {Function} onChange Handle when the answer has changed.
*/
const handleChangeText = ({name, regex}, value, onChange) => onChange({
[name]: !isEmpty(value) ? formatValue(value, regex) : undefined
const handleChangeText = ({name, regex, allowEmojis = false}, value, onChange) => onChange({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahora que lo veo mejor, hace lo mismo que el formatValue() 👀.

Se puede simplificar más si concantenás las regex en una sola: https://stackoverflow.com/a/185529. Lo interesante ahora va a ser que el test siga dando verde cuando modifiques la implementación, prescindiendo del sanitize().

[name]: !isEmpty(value) ? formatValue(sanitize(value, allowEmojis), regex) : undefined
});

export default handleChangeText;
28 changes: 28 additions & 0 deletions test/util/handleChangeText.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ describe('handleChangeText', () => {
should(result.answer).be.undefined();
});
});

context('with an answer including emojis', () => {
it('should sanitize it by default', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excelente!

let result = '';
handleChangeText(
{
name: 'answer'
},
'answer with \ud83c\udff4',
value => result = value
);
result.answer.should.be.String();
result.answer.should.be.equal('answer with ');
});
it('should not sanitize it if allowEmojis is set to true', () => {
let result = '';
handleChangeText(
{
name: 'answer',
allowEmojis: true
},
'answer with \ud83c\udff4',
value => result = value
);
result.answer.should.be.String();
result.answer.should.be.equal('answer with \ud83c\udff4');
});
});
});