-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: remove the
crc-32
library from dependencies (#1565)
- Loading branch information
1 parent
45f3e35
commit be96b46
Showing
5 changed files
with
42 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { crc32 } from "./crc32"; | ||
|
||
describe("crc32", () => { | ||
it("crc32 is correctly calculated from the string", () => { | ||
expect(crc32("")).toBe(0); | ||
expect(crc32("Hello Tact")).toBe(-1612685692); | ||
expect(crc32("Привет Tact")).toBe(-1470995533); | ||
expect(crc32("👋 Tact")).toBe(1855222621); | ||
expect(crc32("\u0000")).toBe(-771559539); | ||
expect(crc32("⚡")).toBe(2136484914); | ||
}); | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
function makeCRC32Table(polynomial: number): Int32Array { | ||
let c: number; | ||
const table = new Int32Array(256); | ||
for (let n = 0; n < table.length; n++) { | ||
c = n; | ||
for (let k = 0; k < 8; k++) { | ||
c = c & 1 ? (c >>> 1) ^ polynomial : c >>> 1; | ||
} | ||
table[n] = c; | ||
} | ||
return table; | ||
} | ||
|
||
// Reversed polynomial of ISO3309 CRC32 | ||
const CRC32C_TABLE = makeCRC32Table(0xedb88320); | ||
|
||
export function crc32(data: string | Uint8Array): number { | ||
if (typeof data === "string") { | ||
data = new TextEncoder().encode(data); | ||
} | ||
|
||
let crc = 0xffffffff; | ||
for (const byte of data) { | ||
crc = CRC32C_TABLE[(crc ^ byte) & 0xff]! ^ (crc >>> 8); | ||
} | ||
|
||
return crc ^ 0xffffffff; | ||
} |
This file contains 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