Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 30, 2017 01:14
Show Gist options
  • Save railsstudent/f1f30c54a1d3fd391641c7a042c7eba7 to your computer and use it in GitHub Desktop.
Save railsstudent/f1f30c54a1d3fd391641c7a042c7eba7 to your computer and use it in GitHub Desktop.
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