Skip to content

Commit 8f7e829

Browse files
committedFeb 19, 2025·
docs: clarify what hash() does
1 parent 46221d7 commit 8f7e829

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed
 

‎README.md

+10-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ Fast data [hashing](https://en.wikipedia.org/wiki/Hash_function) utils.
1313

1414
## Usage
1515

16-
Install package:
16+
Install `ohash`:
1717

1818
<!--automd:pm-install -->
1919

@@ -39,19 +39,19 @@ deno install ohash
3939

4040
<!--/automd -->
4141

42-
Import:
42+
**Import:**
4343

4444
```js
4545
// ESM import
46-
import { hash, objectHash, stringDigest } from "ohash";
46+
import { hash, objectHash, isEqual, diff, stringDigest } from "ohash";
4747

4848
// ..or dnamic import
4949
const { hash, objectHash, stringDigest } = await import("ohash");
5050
```
5151

5252
### `hash(object, options?)`
5353

54-
Serializes any value into a string hash using `objectHash` and then hashes result sha256+base64 (using `stringDigest`) trimmed by length of `10`.
54+
Hashes any JS value into a string.
5555

5656
**Usage:**
5757

@@ -62,6 +62,12 @@ import { hash } from "ohash";
6262
console.log(hash({ foo: "bar" }));
6363
```
6464

65+
**How it works:**
66+
67+
- Input will be serialized into a string like `"object:1:string:3:foo:string:3:bar,"`
68+
- Then it is hashed using [SHA-256](https://en.wikipedia.org/wiki/SHA-2) algorithm and encoded as a [base64](https://en.wikipedia.org/wiki/Base64) string
69+
- `+`, `/` and `=` characters will be removed and string trimmed to `10` chars
70+
6571
### `objectHash(object, options?)`
6672

6773
Serializes any value into a stable and safe string for hashing.

‎src/hash/hash.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@ import { objectHash, type HashOptions } from "./object-hash";
22
import { stringDigest } from "ohash/crypto";
33

44
/**
5-
* Hash any JS value into a string.
5+
* Hashes any JS value into a string.
6+
*
7+
* **How it works:**
8+
* - Input will be serialized into a string like `"object:1:string:3:foo:string:3:bar,"`
9+
* - Then it is hashed using [SHA-256](https://en.wikipedia.org/wiki/SHA-2) algorithm and encoded as a [base64](https://en.wikipedia.org/wiki/Base64) string
10+
* - `+`, `/` and `=` characters will be removed and string trimmed to `10` chars
611
*
712
* @param {object} object value to hash
813
* @param {HashOptions} options hashing options. See {@link HashOptions}.
914
* @return {string} hash value
10-
* @api public
1115
*/
1216
export function hash(object: any, options: HashOptions = {}): string {
1317
const hashed =

0 commit comments

Comments
 (0)
Please sign in to comment.