Skip to content

Commit 5e55e96

Browse files
Fix lint errors in hash_pandas_object: noParameterAssign, useImportType, format
- Use local variable 'h' instead of reassigning 'hash' parameter in fnvString and fnvScalar - Replace non-null assertion bytes[i]! with null-coalescing bytes[i] ?? 0 - Auto-fix: sort imports, make DataFrame import type-only, format test file Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c6462da commit 5e55e96

2 files changed

Lines changed: 31 additions & 32 deletions

File tree

src/stats/hash_pandas_object.ts

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
* @module
2929
*/
3030

31-
import type { Scalar } from "../types.ts";
32-
import { DataFrame } from "../core/frame.ts";
33-
import { Series } from "../core/series.ts";
3431
import { Dtype } from "../core/dtype.ts";
32+
import type { DataFrame } from "../core/frame.ts";
33+
import { Series } from "../core/series.ts";
34+
import type { Scalar } from "../types.ts";
3535

3636
// ─── FNV-1a 64-bit constants ──────────────────────────────────────────────────
3737

@@ -46,21 +46,22 @@ function fnvByte(hash: bigint, byte: number): bigint {
4646

4747
/** Hash an arbitrary string (UTF-8 bytes) into the FNV state. */
4848
function fnvString(hash: bigint, s: string): bigint {
49+
let h = hash;
4950
for (let i = 0; i < s.length; i++) {
50-
let code = s.charCodeAt(i);
51+
const code = s.charCodeAt(i);
5152
// Encode as UTF-8 bytes
5253
if (code < 0x80) {
53-
hash = fnvByte(hash, code);
54+
h = fnvByte(h, code);
5455
} else if (code < 0x800) {
55-
hash = fnvByte(hash, 0xc0 | (code >> 6));
56-
hash = fnvByte(hash, 0x80 | (code & 0x3f));
56+
h = fnvByte(h, 0xc0 | (code >> 6));
57+
h = fnvByte(h, 0x80 | (code & 0x3f));
5758
} else {
58-
hash = fnvByte(hash, 0xe0 | (code >> 12));
59-
hash = fnvByte(hash, 0x80 | ((code >> 6) & 0x3f));
60-
hash = fnvByte(hash, 0x80 | (code & 0x3f));
59+
h = fnvByte(h, 0xe0 | (code >> 12));
60+
h = fnvByte(h, 0x80 | ((code >> 6) & 0x3f));
61+
h = fnvByte(h, 0x80 | (code & 0x3f));
6162
}
6263
}
63-
return hash;
64+
return h;
6465
}
6566

6667
/** Hash a single scalar value into the FNV state. */
@@ -80,10 +81,11 @@ function fnvScalar(hash: bigint, val: Scalar): bigint {
8081
const buf = new ArrayBuffer(8);
8182
new DataView(buf).setFloat64(0, val, true);
8283
const bytes = new Uint8Array(buf);
84+
let h = hash;
8385
for (let i = 0; i < 8; i++) {
84-
hash = fnvByte(hash, bytes[i]!);
86+
h = fnvByte(h, bytes[i] ?? 0);
8587
}
86-
return hash;
88+
return h;
8789
}
8890
if (typeof val === "bigint") {
8991
return fnvString(hash, val.toString());
@@ -128,10 +130,7 @@ export interface HashPandasObjectOptions {
128130
* h.iat(0) !== h.iat(1); // true (with overwhelming probability)
129131
* ```
130132
*/
131-
export function hashPandasObject(
132-
obj: Series,
133-
options?: HashPandasObjectOptions,
134-
): Series<number>;
133+
export function hashPandasObject(obj: Series, options?: HashPandasObjectOptions): Series<number>;
135134

136135
/**
137136
* Return a `Series<number>` of FNV-1a 64-bit row-hashes for each row of `df`.
@@ -150,10 +149,7 @@ export function hashPandasObject(
150149
* // h.iat(0) is the hash of row 0; h.iat(1) is the hash of row 1
151150
* ```
152151
*/
153-
export function hashPandasObject(
154-
obj: DataFrame,
155-
options?: HashPandasObjectOptions,
156-
): Series<number>;
152+
export function hashPandasObject(obj: DataFrame, options?: HashPandasObjectOptions): Series<number>;
157153

158154
export function hashPandasObject(
159155
obj: Series | DataFrame,

tests/stats/hash_pandas_object.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,18 +106,21 @@ describe("hashPandasObject — Series", () => {
106106

107107
it("property: deterministic — same input same hash", () => {
108108
fc.assert(
109-
fc.property(fc.array(fc.oneof(fc.integer(), fc.string(), fc.boolean()), { maxLength: 20 }), (arr) => {
110-
const s1 = new Series({ data: arr });
111-
const s2 = new Series({ data: arr });
112-
const h1 = hashPandasObject(s1, { index: false });
113-
const h2 = hashPandasObject(s2, { index: false });
114-
for (let i = 0; i < arr.length; i++) {
115-
if (h1.iat(i) !== h2.iat(i)) {
116-
return false;
109+
fc.property(
110+
fc.array(fc.oneof(fc.integer(), fc.string(), fc.boolean()), { maxLength: 20 }),
111+
(arr) => {
112+
const s1 = new Series({ data: arr });
113+
const s2 = new Series({ data: arr });
114+
const h1 = hashPandasObject(s1, { index: false });
115+
const h2 = hashPandasObject(s2, { index: false });
116+
for (let i = 0; i < arr.length; i++) {
117+
if (h1.iat(i) !== h2.iat(i)) {
118+
return false;
119+
}
117120
}
118-
}
119-
return true;
120-
}),
121+
return true;
122+
},
123+
),
121124
);
122125
});
123126
});

0 commit comments

Comments
 (0)