Created
January 30, 2017 01:14
-
-
Save railsstudent/f1f30c54a1d3fd391641c7a042c7eba7 to your computer and use it in GitHub Desktop.
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
function decode(str) { | |
var result = []; | |
let idx = 0; | |
let charCount = 0; | |
let strCharCount = ''; | |
while (idx < str.length) { | |
if (str[idx] === '\\') { | |
// parse digits | |
strCharCount = ''; | |
idx += 1; | |
while (idx < str.length && (str[idx] >= '0' && str[idx] <= '9')) { | |
strCharCount += str[idx++]; | |
} | |
charCount = parseInt(strCharCount); | |
let endIdx = idx + charCount; | |
let strGroup = (endIdx <= str.length) ? str.substring(idx, endIdx) : str.substring(idx); | |
result.push(strGroup); | |
idx = endIdx; | |
} else { | |
result.push(str[idx++]); | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment