Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 15, 2024
1 parent bc5b848 commit 42c1e25
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 13 deletions.
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,26 @@ Import:

```js
// ESM
import { hash, objectHash, murmurHash, sha256 } from "ohash";
import { hash, hashAsync, objectHash, murmurHash, sha256 } from "ohash";

// CommonJS
const { hash, objectHash, murmurHash, sha256 } = require("ohash");
const { hash, hashAsync, objectHash, murmurHash, sha256 } = require("ohash");
```

### `hash(object, options?)`
### `hash(object, options?)` / `hashAsync(object, options?)`

Converts object value into a string hash using `objectHash` and then applies `sha256` with Base64 encoding (trimmed by length of 10).

`hashAsync` is slightly faster as will leverage [`SubtleCrypto.digest`](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest) when available.

Usage:

```js
import { hash } from "ohash";
import { hash, hashAsync } from "ohash";

// "dZbtA7f0lK"
console.log(hash({ foo: "bar" }));
console.log(await hashAsync({ foo: "bar" }));
```

### `objectHash(object, options?)`
Expand Down
8 changes: 4 additions & 4 deletions benchmark/hash.mjs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Benchmark from "benchmark";
import { hash, asyncHash } from "ohash";
import { hash, hashAsync } from "ohash";
import largeJson from "./fixture/large.mjs";
import { generateItems } from "./_utils.mjs";

Expand All @@ -19,11 +19,11 @@ for (const [name, data] of Object.entries(dataSets)) {
hash(data);
});
suite.add(
`asyncHash(${name})`,
`hashAsync(${name})`,
(ctx) => {
asyncHash(data).then(() => ctx.resolve());
hashAsync(data).then(() => ctx.resolve());
},
{ defer: true }
{ defer: true },
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/hash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export function hash(object: any, options: HashOptions = {}): string {
return sha256base64(hashed).slice(0, 10);
}

export async function asyncHash(
export async function hashAsync(
object: any,
options: HashOptions = {},
): Promise<string> {
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export { objectHash } from "./object-hash";
export { hash, asyncHash } from "./hash";
export { hash, hashAsync } from "./hash";
export { murmurHash } from "./crypto/murmur";
export { sha256, sha256base64 } from "./crypto/sha256";
export { isEqual } from "./utils";
Expand Down
6 changes: 3 additions & 3 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
sha256,
isEqual,
diff,
asyncHash,
hashAsync,
} from "../src";
import { sha256base64 } from "../src/crypto/sha256";

Expand Down Expand Up @@ -100,8 +100,8 @@ it("hash", () => {
expect(hash({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"');
});

it("asyncHash", async () => {
expect(await asyncHash({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"');
it("hashAsync", async () => {
expect(await hashAsync({ foo: "bar" })).toMatchInlineSnapshot('"dZbtA7f0lK"');
});

describe("isEqual", () => {
Expand Down

0 comments on commit 42c1e25

Please sign in to comment.