fix: include line number in hashline computation
This commit is contained in:
parent
df1a0a59d9
commit
5ae9de0e8e
@ -2,19 +2,31 @@ import { describe, it, expect } from "bun:test"
|
|||||||
import { computeLineHash, formatHashLine, formatHashLines } from "./hash-computation"
|
import { computeLineHash, formatHashLine, formatHashLines } from "./hash-computation"
|
||||||
|
|
||||||
describe("computeLineHash", () => {
|
describe("computeLineHash", () => {
|
||||||
it("returns deterministic 2-char CID hash", () => {
|
it("returns deterministic 2-char CID hash per line", () => {
|
||||||
//#given
|
//#given
|
||||||
const content = "function hello() {"
|
const content = "function hello() {"
|
||||||
|
|
||||||
//#when
|
//#when
|
||||||
const hash1 = computeLineHash(1, content)
|
const hash1 = computeLineHash(1, content)
|
||||||
const hash2 = computeLineHash(999, content)
|
const hash2 = computeLineHash(1, content)
|
||||||
|
|
||||||
//#then
|
//#then
|
||||||
expect(hash1).toBe(hash2)
|
expect(hash1).toBe(hash2)
|
||||||
expect(hash1).toMatch(/^[ZPMQVRWSNKTXJBYH]{2}$/)
|
expect(hash1).toMatch(/^[ZPMQVRWSNKTXJBYH]{2}$/)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it("produces different hashes for same content on different lines", () => {
|
||||||
|
//#given
|
||||||
|
const content = "function hello() {"
|
||||||
|
|
||||||
|
//#when
|
||||||
|
const hash1 = computeLineHash(1, content)
|
||||||
|
const hash2 = computeLineHash(2, content)
|
||||||
|
|
||||||
|
//#then
|
||||||
|
expect(hash1).not.toBe(hash2)
|
||||||
|
})
|
||||||
|
|
||||||
it("ignores whitespace differences", () => {
|
it("ignores whitespace differences", () => {
|
||||||
//#given
|
//#given
|
||||||
const content1 = "function hello() {"
|
const content1 = "function hello() {"
|
||||||
|
|||||||
@ -1,9 +1,9 @@
|
|||||||
import { HASHLINE_DICT } from "./constants"
|
import { HASHLINE_DICT } from "./constants"
|
||||||
|
|
||||||
export function computeLineHash(lineNumber: number, content: string): string {
|
export function computeLineHash(lineNumber: number, content: string): string {
|
||||||
void lineNumber
|
|
||||||
const stripped = content.replace(/\s+/g, "")
|
const stripped = content.replace(/\s+/g, "")
|
||||||
const hash = Bun.hash.xxHash32(stripped)
|
const hashInput = `${lineNumber}:${stripped}`
|
||||||
|
const hash = Bun.hash.xxHash32(hashInput)
|
||||||
const index = hash % 256
|
const index = hash % 256
|
||||||
return HASHLINE_DICT[index]
|
return HASHLINE_DICT[index]
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user