Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
/ohashPublic

# Simple object hashing, serialization and comparison utils.

License

NotificationsYou must be signed in to change notification settings

unjs/ohash

Repository files navigation

npm versionnpm downloadsbundle sizecodecov

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.

Usage

Installohash:

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

Import:

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

hash(input)

Hashes any JS value into a string.

The input is firstserialized then it ishashed.

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 theSHA-256 algorithm and encodes it inBase64URL 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 theirserialized values.

import{isEqual}from"ohash";// trueconsole.log(isEqual({a:1,b:2},{b:2,a:1}));

diff(obj1, obj2)

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

Contribute

  • Clone this repository
  • EnableCorepack usingcorepack enable
  • Install dependencies usingpnpm install
  • Run interactive tests usingpnpm dev

License

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

Stars

Watchers

Forks


[8]ページ先頭

©2009-2025 Movatter.jp