|
| 1 | +# @sandboxed/diff |
| 2 | + |
| 3 | +A **zero dependency, high-performance, security-conscious** JavaScript diffing library for comparing complex data structures with ease. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- ⚡️ **Zero dependencies** – lightweight and no external libraries required |
| 8 | +- 📝 Detects **additions, deletions, and modifications** |
| 9 | +- 💡 Supports **Primitives, Objects, Arrays, Maps, and Sets** |
| 10 | +- 🔄 **Handles circular references** safely |
| 11 | +- 🛠️ **Highly configurable** to fit different use cases |
| 12 | +- 🚨 **Built with security in mind** to prevent prototype pollution and other risks |
| 13 | +- 💻 Works in both **Node.js and browser environments** |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +``` |
| 18 | +npm install @sandboxed/diff |
| 19 | +
|
| 20 | +yarn add @sandboxed/diff |
| 21 | +``` |
| 22 | + |
| 23 | +### Supports `esm` and `cjs` |
| 24 | + |
| 25 | +Works with both ESM (`import`) and CJS (`require`). Use the syntax that matches your environment: |
| 26 | + |
| 27 | +```javascript |
| 28 | +// ESM |
| 29 | +import diff, { ChangeType } from '@sandboxed/diff'; |
| 30 | + |
| 31 | +// CJS option 1 |
| 32 | +const diff = require('@sandboxed/diff').default; |
| 33 | +const { ChangeType } = require('@sandboxed/diff'); |
| 34 | + |
| 35 | +// CJS option 2 |
| 36 | +const { default: diff, ChangeType } = require('@sandboxed/diff'); |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +#### `diff(lhs: any, rhs: any, config?: DiffConfig): Diff` |
| 42 | + |
| 43 | +```javascript |
| 44 | +import diff, { ChangeType } from '@sandboxed/diff'; |
| 45 | + |
| 46 | +const a = { name: "Alice", age: 25 }; |
| 47 | +const b = { name: "Alice", age: 26, city: "New York" }; |
| 48 | + |
| 49 | +const result = diff(a, b); |
| 50 | + |
| 51 | +console.log(result); |
| 52 | +console.log(result.toDiffString()); |
| 53 | +console.log(result.equal); // false |
| 54 | +``` |
| 55 | + |
| 56 | +**Output**: |
| 57 | + |
| 58 | +```javascript |
| 59 | +[ |
| 60 | + { type: 'noop', str: '{', depth: 0, path: [] }, |
| 61 | + { |
| 62 | + type: 'noop', |
| 63 | + str: '"name": "Alice",', |
| 64 | + depth: 1, |
| 65 | + path: [ 'name', { deleted: false, value: 'Alice' } ] |
| 66 | + }, |
| 67 | + { |
| 68 | + type: 'remove', |
| 69 | + str: '"age": 25,', |
| 70 | + depth: 1, |
| 71 | + path: [ 'age', { deleted: true, value: 25 } ] |
| 72 | + }, |
| 73 | + { |
| 74 | + type: 'update', |
| 75 | + str: '"age": 26,', |
| 76 | + depth: 1, |
| 77 | + path: [ 'age', { deleted: false, value: 26 } ] |
| 78 | + }, |
| 79 | + { |
| 80 | + type: 'add', |
| 81 | + str: '"city": "New York",', |
| 82 | + depth: 1, |
| 83 | + path: [ 'city', { deleted: false, value: 'New York' } ] |
| 84 | + }, |
| 85 | + { type: 'noop', str: '}', depth: 0, path: [] } |
| 86 | +] |
| 87 | + |
| 88 | +// --- |
| 89 | + |
| 90 | +{ |
| 91 | + "name": "Alice", |
| 92 | +- "age": 25, |
| 93 | +! "age": 26, |
| 94 | ++ "city": "New York", |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +## Config |
| 99 | + |
| 100 | +| option | Description | |
| 101 | +|-|-| |
| 102 | +|[config.include](/docs/config.md#include-changetype--changetype)| Include only these change types from the diff result. Can be combined with `exclude`. | |
| 103 | +|[config.exclude](/docs/config.md#exclude-changetype--changetype)| Excludes the change types from the diff result. Can be combined with `include`. | |
| 104 | +|[config.strict](/docs/config.md#strict-boolean)| Performs loose type check if disabled. | |
| 105 | +|[config.showUpdatedOnly](/docs/config.md#showupdatedonly-boolean)| `@sandboxed/diff` creates a `ChangeType.REMOVE` entry for every `ChangeType.UPDATE`. This flags prevents this behavior. | |
| 106 | +|[config.pathHints](/docs/config.md#pathhints-pathints)| Hashmap of `map` and `set` path hints. These strings will be used in the `path` array to provide a hit about the object's type. | |
| 107 | +|[config.redactKeys](/docs/config.md#redactkeys-arraystring)| List of keys that should be redacted from the output. Works with `string` based keys and serialized `Symbol`. | |
| 108 | +|[config.maxDepth](/docs/config.md#maxdepth-number)| Max depth that the diffing function can traverse. | |
| 109 | +|[config.maxKeys](/docs/config.md#maxkeys-number)| Max keys the diffing function can traverse. | |
| 110 | +|[config.timeout](/docs/config.md#timeout-number)| Milliseconds before throwing a timeout error. | |
| 111 | + |
| 112 | +## Utils |
| 113 | + |
| 114 | +| util | Description | |
| 115 | +|-|-| |
| 116 | +|[toDiffString](/docs/utils.md#diff-string-output)| Generates the diff string representation of the diff result. | |
| 117 | +|[equal](/docs/utils.md#equality-detection)| Determines whether the inputs are structurally equal based on the diff result. | |
| 118 | + |
| 119 | +## Motivation |
| 120 | + |
| 121 | +Many diffing libraries are optimized for either structured output or human-readable text, but rarely both. `@sandboxed/diff` is designed to provide a structured diff result along with a utility to generate a string representation, making it easy to use in both programmatic logic and UI rendering. |
| 122 | + |
| 123 | +Trade-off: It may be **slower than other libraries**, but if you prioritize structured diffs with a built-in string representation, `@sandboxed/diff` is a great fit. |
| 124 | + |
| 125 | +## LICENSE |
| 126 | + |
| 127 | +[MIT](LICENSE) |
0 commit comments