- Notifications
You must be signed in to change notification settings - Fork16
# Simple object hashing, serialization and comparison utils.
License
unjs/ohash
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Simple object hashing, serialization and comparison utils.
Note
You are on active v2 development branch. Checkv1 for old ohash v1 docs andrelease notes for migration.
Installohash:
# ✨ Auto-detect (npm, yarn, pnpm, bun or deno)npx nypm i ohashImport:
// ESM importimport{hash,serialize,digest,isEqual}from"ohash";import{diff}from"ohash/utils";// Dynamic importconst{ hash, serialize, digest, isEqual}=awaitimport("ohash");const{ diff}=awaitimport("ohash/utils");
Import from CDN
import{hash,serialize,digest,isEqual}from"https://esm.sh/ohash";import{diff}from"https://esm.sh/ohash/utils";// Dynamic importconst{ hash, serialize, digest, isEqual}=awaitimport("https://esm.sh/ohash");const{ diff}=awaitimport("https://esm.sh/ohash/utils");
Hashes any JS value into a string.
The input is firstserialized then it ishashed.
import{hash}from"ohash";// "g82Nh7Lh3CURFX9zCBhc5xgU0K7L0V1qkoHyRsKNqA4"console.log(hash({foo:"bar"}));
Serializes any input value into a string for hashing.
Important
serialize method uses best efforts to generate stable serialized values; however, it is not designed for security purposes. Keep in mind that there is always a chance of intentional collisions caused by user input.
import{serialize}from"ohash";// "{foo:'bar'}"console.log(serialize({foo:"bar"}));
Hashes a string using theSHA-256 algorithm and encodes it inBase64URL format.
import{digest}from"ohash";// "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk"console.log(digest("Hello World!"));
Compare two objects using=== and then fallbacks to compare based on theirserialized values.
import{isEqual}from"ohash";// trueconsole.log(isEqual({a:1,b:2},{b:2,a:1}));
Compare two objects with nestedserialization. Returns an array of changes.
The returned value is an array of diff entries with$key,$hash,$value, and$props. When logging, a string version of the changelog is displayed.
import{diff}from"ohash/utils";constcreateObject=()=>({foo:"bar",nested:{y:123,bar:{baz:"123",},},});constobj1=createObject();constobj2=createObject();obj2.nested.x=123;deleteobj2.nested.y;obj2.nested.bar.baz=123;constdiff=diff(obj1,obj2);// [-] Removed nested.y// [~] Changed nested.bar.baz from "123" to 123// [+] Added nested.xconsole.log(diff(obj1,obj2));
- Clone this repository
- EnableCorepack using
corepack enable - Install dependencies using
pnpm install - Run interactive tests using
pnpm dev
Made with 💛 Published underMIT License.
Object serialization originally based onpuleos/object-hash byScott Puleo.
sha256 implementation originally based onbrix/crypto-js.
About
# Simple object hashing, serialization and comparison utils.
Resources
License
Uh oh!
There was an error while loading.Please reload this page.