Skip to content

Commit b1cfd79

Browse files
committedFeb 19, 2025
chore: update readme
1 parent d4dd808 commit b1cfd79

File tree

1 file changed

+38
-60
lines changed

1 file changed

+38
-60
lines changed
 

‎README.md

+38-60
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,36 @@
1111

1212
Fast data [hashing](https://en.wikipedia.org/wiki/Hash_function) utils.
1313

14-
## Usage
14+
> [!NOTE]
15+
> You are on active v2 development branch. Check [v1](https://github.com/unjs/ohash/tree/v1) for ohash v1 docs.
1516
16-
Install `ohash`:
17+
## Usage
1718

18-
<!--automd:pm-install -->
19+
Install [`ohash`](https://www.npmjs.com/package/ohash):
1920

2021
```sh
21-
# ✨ Auto-detect
22-
npx nypm install ohash
23-
24-
# npm
25-
npm install ohash
26-
27-
# yarn
28-
yarn add ohash
29-
30-
# pnpm
31-
pnpm install ohash
32-
33-
# bun
34-
bun install ohash
35-
36-
# deno
37-
deno install ohash
22+
# ✨ Auto-detect (npm, yarn, pnpm, bun or deno)
23+
npx nypm i ohash
3824
```
3925

40-
<!--/automd -->
41-
42-
**Import:**
26+
**Then import it:**
4327

4428
```js
4529
// ESM import
4630
import { hash, serialize, isEqual, diff, digest } from "ohash";
4731

48-
// ..or dnamic import
49-
const { hash, serialize, digest } = await import("ohash");
32+
// Dynamic import
33+
const { hash } = await import("ohash");
34+
35+
// import from CDN
36+
import { hash } from "https://esm.sh/ohash";
37+
const { hash } = await import("https://esm.sh/ohash");
5038
```
5139

52-
### `hash(object, options?)`
40+
## `hash(input, options?)`
5341

5442
Hashes any JS value into a string.
5543

56-
**Usage:**
57-
5844
```js
5945
import { hash } from "ohash";
6046

@@ -64,15 +50,12 @@ console.log(hash({ foo: "bar" }));
6450

6551
**How it works:**
6652

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-
71-
### `serialize(object, options?)`
53+
- If input is not a string, it will be serialized into a string like `object:1:string:3:foo:string:3:bar,` using [`serialize()`](#serializeinput-options).
54+
- 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 using [`digest()`](#digeststr).
7255

73-
Serializes any value into a stable and safe string for hashing.
56+
## `serialize(input, options?)`
7457

75-
**Usage:**
58+
Serializes any input value into a string for usable hashing.
7659

7760
```js
7861
import { serialize } from "ohash";
@@ -81,11 +64,23 @@ import { serialize } from "ohash";
8164
console.log(serialize({ foo: "bar" }));
8265
```
8366

84-
### `isEqual(obj1, obj2, options?)`
67+
## `digest(str)`
8568

86-
Compare two objects using `==` then fallbacks to compare using `serialize`.
69+
Create a [sha256](https://en.wikipedia.org/wiki/SHA-2) digest from input (string) and returns the hash as a base64 string.
8770

88-
Usage:
71+
> [!IMPORTANT]
72+
> The `+`, `/`, and `=` characters are removed from base64 result to maximize compatibility.
73+
74+
```ts
75+
import { digest } from "ohash";
76+
77+
// "pZGm1Av0IEBKARczz7exkNYsZb8LzaMrV7J32a2fFG4"
78+
console.log(digest("Hello World"));
79+
```
80+
81+
## `isEqual(obj1, obj2, options?)`
82+
83+
Compare two objects using `===` then fallbacks to compare based on their [serialized](#serializeinput-options) values.
8984

9085
```js
9186
import { isEqual } from "ohash";
@@ -94,14 +89,12 @@ import { isEqual } from "ohash";
9489
console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }));
9590
```
9691

97-
### `diff(obj1, obj2, options?)`
92+
## `diff(obj1, obj2, options?)`
9893

99-
Compare two objects with nested hashing. Returns an array of changes.
94+
Compare two objects with nested [serialization](#serializeinput-options). Returns an array of changes.
10095

10196
Returned value is an array of diff entries with `$key`, `$hash`, `$value` and `$props`. When logging, a string version of changelog is displayed.
10297

103-
**Usage:**
104-
10598
```js
10699
import { diff } from "ohash";
107100

@@ -130,22 +123,7 @@ const diff = diff(obj1, obj2);
130123
console.log(diff(obj1, obj2));
131124
```
132125

133-
### `digest`
134-
135-
Create a [sha256](https://en.wikipedia.org/wiki/SHA-2) digest from input and returns the hash as a base64 string.
136-
137-
> [!IMPORTANT]
138-
> The `+`, `/`, and `=` characters are removed from base64 result to maximize compatibility.
139-
> This behavior differs from standard SHA-256 + Base64 encoding.
140-
141-
```ts
142-
import { digest } from "ohash";
143-
144-
// "pZGm1Av0IEBKARczz7exkNYsZb8LzaMrV7J32a2fFG4"
145-
console.log(digest("Hello World"));
146-
```
147-
148-
## 💻 Development
126+
## Contribute
149127

150128
- Clone this repository
151129
- Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
@@ -154,6 +132,6 @@ console.log(digest("Hello World"));
154132

155133
## License
156134

157-
Made with 💛
135+
Made with 💛 Published under [MIT License](./LICENSE).
158136

159-
Published under [MIT License](./LICENSE). Based on [puleos/object-hash](https://github.com/puleos/object-hash) by [Scott Puleo](https://github.com/puleos/), and [brix/crypto-js](https://github.com/brix/crypto-js).
137+
Based on [puleos/object-hash](https://github.com/puleos/object-hash) by [Scott Puleo](https://github.com/puleos/), and [brix/crypto-js](https://github.com/brix/crypto-js).

0 commit comments

Comments
 (0)