提交学习笔记专用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.9 KiB

  1. # #️ ohash
  2. <!-- automd:badges bundlephobia codecov -->
  3. [![npm version](https://img.shields.io/npm/v/ohash)](https://npmjs.com/package/ohash)
  4. [![npm downloads](https://img.shields.io/npm/dm/ohash)](https://npm.chart.dev/ohash)
  5. [![bundle size](https://img.shields.io/bundlephobia/minzip/ohash)](https://bundlephobia.com/package/ohash)
  6. [![codecov](https://img.shields.io/codecov/c/gh/unjs/ohash)](https://codecov.io/gh/unjs/ohash)
  7. <!-- /automd -->
  8. Simple object hashing, serialization and comparison utils.
  9. > [!NOTE]
  10. > You are on active v2 development branch. Check [v1](https://github.com/unjs/ohash/tree/v1) for old ohash v1 docs and [release notes](https://github.com/unjs/ohash/releases/tag/v2.0.1) for migration.
  11. ## Usage
  12. Install [`ohash`](https://www.npmjs.com/package/ohash):
  13. ```sh
  14. # ✨ Auto-detect (npm, yarn, pnpm, bun or deno)
  15. npx nypm i ohash
  16. ```
  17. **Import:**
  18. ```js
  19. // ESM import
  20. import { hash, serialize, digest, isEqual } from "ohash";
  21. import { diff } from "ohash/utils";
  22. // Dynamic import
  23. const { hash, serialize, digest, isEqual } = await import("ohash");
  24. const { diff } = await import("ohash/utils");
  25. ```
  26. <details>
  27. <summary>Import from CDN</summary>
  28. ```js
  29. import { hash, serialize, digest, isEqual } from "https://esm.sh/ohash";
  30. import { diff } from "https://esm.sh/ohash/utils";
  31. // Dynamic import
  32. const { hash, serialize, digest, isEqual } = await import(
  33. "https://esm.sh/ohash"
  34. );
  35. const { diff } = await import("https://esm.sh/ohash/utils");
  36. ```
  37. </details>
  38. ## `hash(input)`
  39. Hashes any JS value into a string.
  40. The input is first [serialized](#serializeinput) then it is [hashed](#digeststr).
  41. ```js
  42. import { hash } from "ohash";
  43. // "g82Nh7Lh3CURFX9zCBhc5xgU0K7L0V1qkoHyRsKNqA4"
  44. console.log(hash({ foo: "bar" }));
  45. ```
  46. ## `serialize(input)`
  47. Serializes any input value into a string for hashing.
  48. > [!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.
  49. ```js
  50. import { serialize } from "ohash";
  51. // "{foo:'bar'}"
  52. console.log(serialize({ foo: "bar" }));
  53. ```
  54. ## `digest(str)`
  55. Hashes a string using the [SHA-256](https://en.wikipedia.org/wiki/SHA-2) algorithm and encodes it in [Base64URL](https://base64.guru/standards/base64url) format.
  56. ```ts
  57. import { digest } from "ohash";
  58. // "f4OxZX_x_FO5LcGBSKHWXfwtSx-j1ncoSt3SABJtkGk"
  59. console.log(digest("Hello World!"));
  60. ```
  61. ## `isEqual(obj1, obj2)`
  62. Compare two objects using `===` and then fallbacks to compare based on their [serialized](#serializeinput) values.
  63. ```js
  64. import { isEqual } from "ohash";
  65. // true
  66. console.log(isEqual({ a: 1, b: 2 }, { b: 2, a: 1 }));
  67. ```
  68. ## `diff(obj1, obj2)`
  69. Compare two objects with nested [serialization](#serializeinput-options). Returns an array of changes.
  70. 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.
  71. ```js
  72. import { diff } from "ohash/utils";
  73. const createObject = () => ({
  74. foo: "bar",
  75. nested: {
  76. y: 123,
  77. bar: {
  78. baz: "123",
  79. },
  80. },
  81. });
  82. const obj1 = createObject();
  83. const obj2 = createObject();
  84. obj2.nested.x = 123;
  85. delete obj2.nested.y;
  86. obj2.nested.bar.baz = 123;
  87. const diff = diff(obj1, obj2);
  88. // [-] Removed nested.y
  89. // [~] Changed nested.bar.baz from "123" to 123
  90. // [+] Added nested.x
  91. console.log(diff(obj1, obj2));
  92. ```
  93. ## Contribute
  94. - Clone this repository
  95. - Enable [Corepack](https://github.com/nodejs/corepack) using `corepack enable`
  96. - Install dependencies using `pnpm install`
  97. - Run interactive tests using `pnpm dev`
  98. ## License
  99. Made with 💛 Published under [MIT License](./LICENSE).
  100. Object serialization originally based on [puleos/object-hash](https://github.com/puleos/object-hash) by [Scott Puleo](https://github.com/puleos/).
  101. sha256 implementation originally based on [brix/crypto-js](https://github.com/brix/crypto-js).