mirror of https://github.com/status-im/js-waku.git
Decode payload to utf8 string
This commit is contained in:
parent
46c41dc50f
commit
e167f4fba4
|
@ -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;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue