Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ohash

unjs16.7mMIT2.0.5TypeScript support: included

Simple object hashing, serialization and comparison utils.

readme

#️ ohash

npm version npm downloads bundle size codecov

Simple object hashing, serialization and comparison utils.

[!NOTE] You are on active v2 development branch. Check v1 for old ohash v1 docs and release notes for migration.

Usage

Install ohash:

# ✨ Auto-detect (npm, yarn, pnpm, bun or deno)
npx nypm i ohash

Import:

// ESM import
import { hash, serialize, digest } from "ohash";
import { isEqual, diff } from "ohash/utils";

// Dynamic import
const { hash, serialize, digest } = await import("ohash");
const { isEqual, diff } = await import("ohash/utils");
<summary>Import from CDN</summary>
import { hash, serialize, digest } from "https://esm.sh/ohash";
import { isEqual, diff } from "https://esm.sh/ohash/utils";

// Dynamic import
const { hash, serialize, digest } = await import("https://esm.sh/ohash");
const { isEqual, diff } = await import("https://esm.sh/ohash/utils");

hash(input)

Hashes any JS value into a string.

The input is first serialized then it is hashed.

import { hash } from "ohash";

// "g82Nh7Lh3CURFX9zCBhc5xgU0K7L0V1qkoHyRsKNqA4"
console.log(hash({ foo: "bar" }));

serialize(input)

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" }));

digest(str)

Hashes a string using the SHA-256 algorithm and encodes it in Base64URL format.

import { digest } from "ohash";

// "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk"
console.log(digest("Hello World!"));

isEqual(obj1, obj2)

Compare two objects using === and then fallbacks to compare based on their serialized values.

import { isEqual } from "ohash/utils";

// true
console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }));

diff(obj1, obj2)

Compare two objects with nested serialization. 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";

const createObject = () => ({
  foo: "bar",
  nested: {
    y: 123,
    bar: {
      baz: "123",
    },
  },
});

const obj1 = createObject();
const obj2 = createObject();

obj2.nested.x = 123;
delete obj2.nested.y;
obj2.nested.bar.baz = 123;

const diff = diff(obj1, obj2);

// [-] Removed nested.y
// [~] Changed nested.bar.baz from "123" to 123
// [+] Added   nested.x
console.log(diff(obj1, obj2));

Contribute

  • Clone this repository
  • Enable Corepack using corepack enable
  • Install dependencies using pnpm install
  • Run interactive tests using pnpm dev

License

Made with 💛 Published under MIT License.

Object serialization originally based on puleos/object-hash by Scott Puleo.

sha256 implementation originally based on brix/crypto-js.

changelog

Changelog

All notable changes to this project will be documented in this file. See standard-version for commit guidelines.

v2.0.5

compare changes

🩹 Fixes

  • Serialization format of typed arrays (#121)

❤️ Contributors

v2.0.4

compare changes

🩹 Fixes

  • Use process.getBuiltinModule to import fast hash (#118)

📦 Build

  • Minify dist/crypto/node (76bd382)

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.3

compare changes

🔥 Performance

  • Use node crypto hash when available (#116)

🩹 Fixes

  • Workaround for stackblitz (#117)
  • Remove trailing slash from arrays (c2b8250)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.2

compare changes

📦 Build

  • Mark Serializer as pure to allow tree-shaking (83fe375)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.1

compare changes

💅 Refactors

❤️ Contributors

  • Pooya Parsa (@pi0)

v2.0.0

compare changes

🔥 Performance

  • Reduce js digest size (#109)

🩹 Fixes

  • hash, serialize: ⚠️ Always serialize string inputs (#110)

💅 Refactors

  • ⚠️ Drop murmurHash support (#104)
  • ⚠️ Rename sha256* to stringDigest (#106)
  • ⚠️ Rename objectHash to serialize (#107)
  • Rename stringDigest to digest (d4dd808)
  • ⚠️ Use standard base64url for digest (#111)
  • ⚠️ Rewrite serializer (#113)

📖 Documentation

  • Clarify what hash() does (8f7e829)

📦 Build

  • ⚠️ Esm only (#105)
  • Selectively minify js hash impl (fe3db66)
  • ⚠️ Move utils to ohash/utils (#112)

🏡 Chore

✅ Tests

⚠️ Breaking Changes

  • hash, serialize: ⚠️ Always serialize string inputs (#110)
  • ⚠️ Drop murmurHash support (#104)
  • ⚠️ Rename sha256* to stringDigest (#106)
  • ⚠️ Rename objectHash to serialize (#107)
  • ⚠️ Use standard base64url for digest (#111)
  • ⚠️ Rewrite serializer (#113)
  • ⚠️ Esm only (#105)
  • ⚠️ Move utils to ohash/utils (#112)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.4

compare changes

🩹 Fixes

  • murmurHash: Fix murmurHash3 implementation, add tests (#83)

📖 Documentation

  • Improved and finalized jsdocs in exported functions (#74)

🏡 Chore

  • Update badge (#46)
  • Update links in readme (#59)
  • Update repo (e09027f)

❤️ Contributors

v1.1.3

compare changes

🔥 Performance

  • object-hash: Avoid using array to just concatenate the string (#36)
  • object-hash: Avoid toString when we know that the value is already a string (#33)
  • object-hash: Faster isNativeFunction check (#30)
  • object-hash: Faster extract object type from toString (#31)
  • object-hash: Faster object access by avoid string concat (#32)
  • object-hash: Faster circular checking by using map (#34)
  • object-hash: Reuse default options when is not passed (#37)
  • object-hash: Avoid splice method to insert values (#35)

💅 Refactors

  • Simplify diff formatting (8e6cabc)

📖 Documentation

  • Improve jsdoc for objectHash() (#43)

🏡 Chore

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Owen Kieffer-Jones
  • Vinicius Lourenço

v1.1.2

compare changes

🩹 Fixes

  • objectHash: Serialize boolean as bool (186e719)

✅ Tests

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.1

compare changes

💅 Refactors

  • Expose diff function types (bc08321)

❤️ Contributors

  • Pooya Parsa (@pi0)

v1.1.0

compare changes

🚀 Enhancements

  • Expose sha256base64 utility (#19)
  • objectHash: Serialize objects with entries (1c8e8b9)
  • objectHash: Support serializing classes with custom toJSON() (331eceb)
  • diff utility (#28)

🩹 Fixes

  • Fix type of SHA256.prototype.toString method (#23)
  • objectHash: Serialize boolean types (7fd580f)

🏡 Chore

  • Add @vitest/coverage-c8 (#24)
  • Update all dependencies (b17fa41)
  • Lint and format with prettier (373eac4)
  • Upgrade node version for ci (9532f47)
  • Update release script (3124feb)

❤️ Contributors

  • Pooya Parsa (@pi0)
  • Nozomu Ikuta
  • Damian Głowala

1.0.0 (2022-11-14)

0.1.5 (2022-08-04)

Features

0.1.4 (2022-07-14)

Features

  • use base64 to encode sha256 hash (#13) (778413f)

0.1.3 (2022-07-14)

0.1.2 (2022-07-14)

0.1.1 (2022-07-14)

Features

Bug Fixes

  • pkg: set sideEffects field (583d85e)

0.1.1 (2021-10-18)

Bug Fixes

  • pkg: set sideEffects field (583d85e)