Skip to content

Instantly share code, notes, and snippets.

@igrek8
Created June 6, 2018 07:36
Show Gist options
  • Save igrek8/6dfdd9fa343a4a81361da16962e75290 to your computer and use it in GitHub Desktop.
Save igrek8/6dfdd9fa343a4a81361da16962e75290 to your computer and use it in GitHub Desktop.
Parse complex string redux-form
const escapeRegex = /([^\\])\./g;
export const normalizeToken = index => (str, prev) => {
if (
typeof str === 'string' ||
typeof str === 'number' ||
typeof str === 'boolean') {
const groups = prev.toString()
.replace(escapeRegex, '$1¬')
.split('¬');
const escapedString = str
.toString()
.replace(/^\./, '\\.')
.replace(escapeRegex, '$1\\.');
groups[index] = escapedString;
return groups.join('.');
}
return str || prev;
};
export const getToken = index => (str) => {
if (
typeof str === 'string' ||
typeof str === 'number' ||
typeof str === 'boolean') {
const match = str.toString()
.replace(escapeRegex, '$1¬')
.split('¬');
return match[index] || '';
}
return str;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment