Registry / database / rus-diff

rus-diff

JSON →
library1.1.0jsnpmunverified

A MongoDB-compatible JSON diff library that computes differences between two JSON objects and produces delta objects using MongoDB operators like $set, $unset, $rename, and $inc. Version 1.1.0 currently stable; low release cadence (last update 2015). Key differentiators: output directly usable with MongoDB collection.update(), supports incremental diffs via $inc option, and provides apply() to apply diffs without manual traversal. Suitable for syncing JSON documents or generating MongoDB update commands.

npm install rus-diff
INSTALL
IMPORT
SIG · RUS-DIFF
R
rus-diff
databasejavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

diff
import { diff } from 'rus-diff'
const diff = require('rus-diff').diff
ESM import works since package is dual. CommonJS require also works.
apply
import { apply } from 'rus-diff'
Named import for applying diff to an object.
clone
import { clone } from 'rus-diff'
import clone from 'rus-diff'
clone is a named export, not default.
resolve
import { resolve } from 'rus-diff'
const resolve = require('rus-diff').resolve
resolve is a named export, not a default function.

Shows diffing two objects and applying the delta with clone to avoid mutation.

import { diff, apply, clone } from 'rus-diff'; const a = { a: 1, b: 'x', c: { d: 2 } }; const b = { a: 2, b: 'x', c: { d: 2 }, e: 3 }; const delta = diff(a, b); console.log(delta); // { $set: { a: 2, e: 3 }, $unset: { b: true } } // Apply delta (mutates a unless cloned) const copy = clone(a); apply(copy, delta); console.log(copy); // { a: 2, b: 'x', c: { d: 2 }, e: 3 }
Debug
Known issues
gotchaapply() mutates the target object in place. Always use clone(a) first if you want to preserve the original.
fix
Use apply(clone(a), delta) to avoid mutation.
affects: >=0.0.0
deprecatedOptions.inc (boolean) is deprecated and may be removed in future versions. Use explicit $inc handling instead.
fix
Manually compute numeric differences or use diff with { inc: true } but be aware it is not recommended for production.
affects: >=1.0.0
gotchaThe diff function does not detect array element moves; it treats arrays as atomic values. Use with caution for array-heavy documents.
fix
For arrays, consider manual comparison or a specialized diff library.
affects: >=0.0.0
breakingIn version 1.1.0, the default export was removed. Import named exports only.
fix
Use named imports like import { diff } from 'rus-diff' instead of default import.
affects: >=1.1.0
Errors
Common errors & fixes
TypeError: rus_diff_1.default is not a function
Using default import (import rusDiff from 'rus-diff') but package does not have default export in v1.1.0.
fix
Use named import: import { diff } from 'rus-diff'
Cannot find module 'babel/polyfill'
Old documentation suggests require('babel/polyfill') which is not needed in modern environments.
fix
Remove the require('babel/polyfill') line and use native ES6 features or add @babel/polyfill as a separate dependency.
Delta contains $inc but was not set in options
Passing { inc: true } to diff() is required to generate $inc operations for numeric changes.
fix
Call diff(a, b, [], { inc: true }) to enable $inc.
Invalid update: $rename: { ... } is not allowed with other operators in same object
MongoDB does not allow $rename combined with $set, $unset in the same update object.
fix
Apply $rename separately or use apply() which handles it correctly.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
13 hits · last 30 days
node
10
Meta
1
Resources
rus-diff — npm install rus-diff · libregistry