Created
June 6, 2018 07:36
-
-
Save igrek8/6dfdd9fa343a4a81361da16962e75290 to your computer and use it in GitHub Desktop.
Parse complex string redux-form
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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