Decode payload to utf8 string

This commit is contained in:
Franck Royer 2021-03-24 16:31:54 +11:00
parent 46c41dc50f
commit e167f4fba4
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
2 changed files with 23 additions and 0 deletions

View File

@ -14,4 +14,15 @@ describe('Waku Message', function () {
}) })
); );
}); });
it('Payload to utf-8', function () {
fc.assert(
fc.property(fc.string(), (s) => {
const msg = Message.fromUtf8String(s);
const utf8 = msg.utf8Payload();
return utf8 === s;
})
);
});
}); });

View File

@ -37,6 +37,18 @@ export class Message {
}).finish(); }).finish();
} }
utf8Payload(): string {
if (!this.payload) {
return '';
}
return Array.from(this.payload)
.map((char) => {
return String.fromCharCode(char);
})
.join('');
}
// Purely for tests purposes. // Purely for tests purposes.
// We do consider protobuf field when checking equality // We do consider protobuf field when checking equality
// As the content is held by the other fields. // As the content is held by the other fields.