Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AsyncBanana committed Oct 31, 2021
0 parents commit 84d218f
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pnpm-lock.yaml
node_modules
dist
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 AsyncBanana

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Microdiff

Microdiff is a tiny (currently <1kb), fast, zero dependency object and array comparison library. It is significantly faster than most other deep comparison libraries, and has full TypeScript support.

## Get started

First, install Microdiff

```
npm i microdiff
```

Then, simply import it and run it on two objects.

```js
import diff from "microdiff";

const obj1 = {
originalProperty: true,
};
const obj2 = {
originalProperty: true,
newProperty: "new",
};

console.log(diff(obj1, obj2));
// [{type: "CREATE", path: ["newProperty"], value: "new"}]
```

There are three different types of changes. `CREATE`, `REMOVE`, and `CHANGE`. The `path` property gives a path to the property in the new object (or the old object in the case of `REMOVE`). Each element in the array is a key to the next property a level deeper until you get to the property changed. The `value` property exists in types `CREATE` and `CHANGE`, and it contains the value of the property added/changed.
24 changes: 24 additions & 0 deletions bench.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import deepDiff from "deep-diff";
import deepObjectDiff from "deep-object-diff";
import microdiff from "./dist/index.js";
const obj = {
test: "test",
testing: true,
bananas: "awesome",
bestFruits: ["bananas", "kiwi", "blueberries"],
};
const newObj = {
test: "new test",
testing: true,
bananas: "awesome",
bestFruits: ["bananas", "kiwi", "blueberries", "blackberries"],
};
console.time("deep-diff");
deepDiff.diff(obj, newObj);
console.timeEnd("deep-diff");
console.time("deep-object-diff");
deepObjectDiff.detailedDiff(obj, newObj);
console.timeEnd("deep-object-diff");
console.time("microdiff");
microdiff(obj, newObj);
console.timeEnd("microdiff");
44 changes: 44 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
interface Difference {
type: "CREATE" | "REMOVE" | "CHANGE";
path: string[];
value?: any;
}

export default function diff(
obj: Record<string, any> | any[],
newObj: Record<string, any> | any[]
): Difference[] {
let diffs: Difference[] = [];
for (const key in obj) {
if (!(key in newObj)) {
diffs.push({
type: "REMOVE",
path: [key],
});
} else if (obj[key] && typeof obj[key] === "object") {
const nestedDiffs = diff(obj[key], newObj[key]);
diffs.push(
...nestedDiffs.map((difference) => {
difference.path.unshift(key);
return difference;
})
);
} else if (obj[key] !== newObj[key]) {
diffs.push({
path: [key],
type: "CHANGE",
value: newObj[key],
});
}
}
for (const key in newObj) {
if (!(key in obj)) {
diffs.push({
type: "CREATE",
path: [key],
value: newObj[key],
});
}
}
return diffs;
}
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "microdiff",
"version": "1.0.0",
"description": "Small, fast, zero dependency deep object and array comparison",
"main": "./dist/index.js",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"build": "tsc",
"test": "tsc && uvu tests",
"bench": "tsc && node bench.js"
},
"keywords": [
"diff",
"comparison"
],
"author": "AsyncBanana",
"license": "MIT",
"files": [
"dist"
],
"devDependencies": {
"deep-diff": "^1.0.2",
"deep-object-diff": "^1.1.0",
"typescript": "^4.4.4",
"uvu": "^0.5.2"
},
"type": "module"
}
24 changes: 24 additions & 0 deletions tests/arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { test } from "uvu";
import * as assert from "uvu/assert";
import diff from "../dist/index.js";

test("top level array & array diff", () => {
assert.equal(diff(["test", "testing"], ["test"]), [
{
type: "REMOVE",
path: ["1"],
},
]);
});

test("nested array", () => {
assert.equal(diff(["test", ["test"]], ["test", ["test", "test2"]]), [
{
type: "CREATE",
path: ["1", "1"],
value: "test2",
},
]);
});

test.run();
32 changes: 32 additions & 0 deletions tests/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { test } from "uvu";
import * as assert from "uvu/assert";
import diff from "../dist/index.js";

test("new raw value", () => {
assert.equal(diff({ test: true }, { test: true, test2: true }), [
{
type: "CREATE",
path: ["test2"],
value: true,
},
]);
});
test("change raw value", () => {
assert.equal(diff({ test: true }, { test: false }), [
{
type: "CHANGE",
path: ["test"],
value: false,
},
]);
});
test("remove raw value", () => {
assert.equal(diff({ test: true, test2: true }, { test: true }), [
{
type: "REMOVE",
path: ["test2"],
},
]);
});

test.run();
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"files": ["index.ts"],
"buildOptions": {},
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"target": "ES2020",
"moduleResolution": "node",
"module": "ES2020"
}
}

0 comments on commit 84d218f

Please sign in to comment.