Created
December 5, 2017 16:23
-
-
Save showmeyourhits/7a7d54b1a9179a4e6875eecc122bce61 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
const isNotValidRE = /[^A-Za-z]/; | |
const matchRLE = /([A-Za-z])\1*/g; | |
function RLE(str) { | |
if (str && isNotValidRE.test(str)){ | |
throw new Error('Not valid string: ' + str); | |
} | |
return str.replace( | |
matchRLE, | |
match => match.length === 1 ? match : `${match[0]}${match.length}` | |
); | |
} | |
try { | |
RLE('AS-'); | |
} catch (e) { | |
console.log('should throw %s', e.message); | |
} | |
console.log('empty str %s', RLE('') === ''); | |
console.log('single char %s', RLE('A') === 'A'); | |
console.log('couple char %s', RLE('Aa') === 'Aa'); | |
console.log('couple of diffchar %s', RLE('AB') === 'AB'); | |
console.log('real test %s', RLE('AAAABBQWEEEE') === 'A4B2QWE4'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment