impl startswith

This commit is contained in:
David Ventura
2024-05-02 22:29:02 +02:00
parent f5da7a34bd
commit 7b153d6ff8
+12
View File
@@ -59,6 +59,17 @@ DataView.prototype.writeString = function (str) {
const bytes = [...str].map((_, i) => str.charCodeAt(i));
return this.writeByteArray(bytes);
};
DataView.prototype.startsWith = function (arr) {
if (this.byteLength < arr.length) {
return false;
}
for (let i = 0; i < arr.length; i++) {
if (this.add(i).readU8() != arr[i]) {
return false;
}
}
return true;
};
declare global {
interface DataView {
@@ -77,6 +88,7 @@ declare global {
writeU32(n: number): void;
writeU64(n: bigint): void;
writeU8(n: number): void;
startsWith(arr: number[]): boolean;
}
}
export default global;