Voici la fonction testée :
const delimitedBinary = /^(?:[01]{8} ){3,}$/gm;
const nonDelimitedBinary = /^(?:[01]{8}){3,}$/gm;
const byteRegex = /[01]{8}/gm;
function decode(string) {
string = string.trim();
let bytes;
if (delimitedBinary.test(string + ' ')) {
bytes = (string + ' ').match(byteRegex);
} else if(nonDelimitedBinary.test(string)) {
bytes = string.match(byteRegex);
}
if (bytes) {
return decodeBytes(bytes);
}
return '';
}
function decodeBytes(bytes) {
return utf.getStringFromBytes(bytes.map(byte => parseInt(byte, 2)));
}
J'ai quelques tests dans test/tests.js
. En voici un extrait :
test('Decodes binary on separate line', t => {
t.is(app.decode('text \n01110000 01100001 01110011 01110011'), 'pass');
});
test('Decodes emojis', t => {
t.is(app.decode('11110000 10011111 10001110 10001001'), '