mithril-sync
v1.0.2
Published
A powerful utility to flatten, diff, rebuild, and sync deeply nested JavaScript data structures — inspired by magic, forged in code.
Maintainers
Readme
Mithril Sync 🧙🧰
A powerful utility for flattening, rebuilding, syncing, diffing, and searching deeply nested JavaScript objects. Inspired by the worlds of LOTR, Marvel, and Harry Potter.
📦 Installation
npm install mithril-sync✨ Features
- Flatten nested objects (objects, arrays, sets, maps)
- Rebuild original object from flattened structure
- Find key/value deeply with flexible filters
- Track changes (diffs)
- Live object mutation tracking
- Apply, revert or sync changes
- Use
dotPathnotation for fast access - Lightweight, dependency-free
📘 Usage
const MithrilSync = require("mithril-sync");
const obj = { user: { name: "John", age: 30 }, tags: ["hero", "wizard"] };
const tool = new MithrilSync(obj);🔍 Methods
constructor(object)
Initializes the sync tool.
const tool = new MithrilSync({ a: { b: 1 } });flatten(obj)
Static method to flatten any object.
const flat = MithrilSync.flatten({ a: { b: 1 } });rebuild(entries)
Rebuilds original object from flat structure.
const rebuilt = MithrilSync.rebuild(flat);getOriginal()
Returns original input object.
tool.getOriginal();getFlat()
Returns current flat structure (after mutations).
tool.getFlat();rebuild()
Rebuilds object from internal entries.
tool.rebuild();updateEntry(dotPath, newValue)
Updates a specific entry.
tool.updateEntry("user.name", "Jane");removeEntry(dotPath)
Removes a specific path.
tool.removeEntry("user.age");getChanges(options?)
Detects differences from original.
tool.getChanges();mergeWith(object)
Merges new object into existing one.
tool.mergeWith({ user: { email: "[email protected]" } });revertChanges(diff)
Reverts changes based on a diff array.
const diff = tool.getChanges();
tool.revertChanges(diff);watchLive(callback, interval, options?)
Watches object live and returns diffs.
tool.watchLive((changes) => console.log(changes));stopWatch()
Stops live watch.
tool.stopWatch();toTree(filterFn)
Filters entries and rebuilds into tree.
tool.toTree(entry => entry.dotPath.includes("user"));find(options)
Finds key/value with filters and options.
tool.find({ target: "name", matchKeys: true });Supports fallbackTargets:
tool.find({
target: "nickname",
fallbackTargets: ["alias", "name"]
});Static Utilities
MithrilSync.get(obj, "a.b.c");
MithrilSync.set(obj, "a.b.c", 42);
MithrilSync.watch(oldEntries, newEntries, options);
MithrilSync.applyChanges(obj, diff);
MithrilSync.fromDiff(original, diff);📜 License
MIT
🧙 Inspired by
- 🧝♂️ Lord of the Rings
- 🧙♂️ Harry Potter
- 🦸 Marvel Universe
