|
| 1 | +# deep-diff-ts |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/deep-diff-ts) |
| 4 | +[](https://www.npmjs.com/package/deep-diff-ts) |
| 5 | +[](https://github.com/ofershap/deep-diff-ts/blob/main/LICENSE) |
| 6 | +[](https://github.com/ofershap/deep-diff-ts/actions/workflows/ci.yml) |
| 7 | + |
| 8 | +Fast deep object diff with full TypeScript types. Zero dependencies. ~1.4KB. |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +## Install |
| 13 | + |
| 14 | +```bash |
| 15 | +npm install deep-diff-ts |
| 16 | +``` |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +```typescript |
| 21 | +import { diff } from "deep-diff-ts"; |
| 22 | + |
| 23 | +const oldObj = { |
| 24 | + name: "Alice", |
| 25 | + age: 30, |
| 26 | + tags: ["admin"], |
| 27 | + config: { theme: "dark" }, |
| 28 | +}; |
| 29 | + |
| 30 | +const newObj = { |
| 31 | + name: "Alice", |
| 32 | + age: 31, |
| 33 | + tags: ["admin", "editor"], |
| 34 | + config: { theme: "light" }, |
| 35 | +}; |
| 36 | + |
| 37 | +const changes = diff(oldObj, newObj); |
| 38 | +// [ |
| 39 | +// { type: "UPDATE", path: ["age"], oldValue: 30, value: 31 }, |
| 40 | +// { type: "CREATE", path: ["tags", 1], value: "editor" }, |
| 41 | +// { type: "UPDATE", path: ["config", "theme"], oldValue: "dark", value: "light" } |
| 42 | +// ] |
| 43 | +``` |
| 44 | + |
| 45 | +### Apply Diffs |
| 46 | + |
| 47 | +```typescript |
| 48 | +import { diff, applyDiff } from "deep-diff-ts"; |
| 49 | + |
| 50 | +const changes = diff(oldObj, newObj); |
| 51 | +const result = applyDiff(oldObj, changes); |
| 52 | +// result deeply equals newObj |
| 53 | +// oldObj is not mutated |
| 54 | +``` |
| 55 | + |
| 56 | +## Diff Types |
| 57 | + |
| 58 | +Each difference has a `type` and `path`: |
| 59 | + |
| 60 | +| Type | Fields | Description | |
| 61 | +| -------- | --------------------------- | ---------------------- | |
| 62 | +| `CREATE` | `path`, `value` | Property was added | |
| 63 | +| `UPDATE` | `path`, `oldValue`, `value` | Property value changed | |
| 64 | +| `DELETE` | `path`, `oldValue` | Property was removed | |
| 65 | + |
| 66 | +`path` is an array of keys/indices: `["users", 0, "name"]` |
| 67 | + |
| 68 | +## Supported Types |
| 69 | + |
| 70 | +- Objects (deep recursive) |
| 71 | +- Arrays (element-by-element) |
| 72 | +- Dates (compared by time value) |
| 73 | +- RegExps (compared by string representation) |
| 74 | +- Primitives (string, number, boolean, null, undefined) |
| 75 | +- NaN (correctly handled via `Object.is`) |
| 76 | + |
| 77 | +## API |
| 78 | + |
| 79 | +### `diff(oldObj, newObj)` |
| 80 | + |
| 81 | +Returns `Difference[]` describing all changes from `oldObj` to `newObj`. |
| 82 | + |
| 83 | +### `applyDiff(target, diffs)` |
| 84 | + |
| 85 | +Returns a new object with all diffs applied. Does not mutate the original. |
| 86 | + |
| 87 | +## Other Projects |
| 88 | + |
| 89 | +- [ts-result](https://github.com/ofershap/ts-result) — Rust-style Result<T, E> for TypeScript |
| 90 | +- [ts-nano-event](https://github.com/ofershap/ts-nano-event) — Typed event emitter in <200 bytes |
| 91 | +- [hover-effects](https://github.com/ofershap/hover-effects) — Canvas-based hover effects for images |
| 92 | + |
| 93 | +## License |
| 94 | + |
| 95 | +MIT |
0 commit comments