Skip to content

Commit f565ea5

Browse files
fix: date objs compare & types
1 parent 07c6c60 commit f565ea5

File tree

11 files changed

+4585
-91
lines changed

11 files changed

+4585
-91
lines changed

.changeset/breezy-buses-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@opentf/obj-diff": minor
3+
---
4+
5+
Added diff function to deep diff two objects.

.changeset/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"access": "public",
88
"baseBranch": "main",
99
"updateInternalDependencies": "patch",
10-
"ignore": ["docs"],
10+
"ignore": [],
1111
"privatePackages": false,
1212
"bumpVersionsWithWorkspaceProtocolOnly": true
1313
}

.github/workflows/publish.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
name: Publish
2+
on:
3+
push:
4+
branches:
5+
- main
6+
7+
jobs:
8+
publish:
9+
runs-on: ubuntu-latest
10+
11+
permissions:
12+
contents: read
13+
id-token: write
14+
15+
steps:
16+
- uses: actions/checkout@v4
17+
18+
- name: Publish package
19+
run: cd packages/obj-diff && npx jsr publish

README.md

Lines changed: 226 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,227 @@
1+
<img align="left" src="https://open-tech-foundation.pages.dev/img/Logo.svg" width="50" height="50">
2+
3+
&nbsp;[OPEN TECH FOUNDATION](https://open-tech-foundation.pages.dev/)
4+
5+
<div align="center">
6+
17
# obj-diff
2-
The Fast,Accurate,JavaScript Object Diffing Library.
8+
9+
</div>
10+
11+
> The Fast, Accurate, JavaScript Objects Diffing Library.
12+
13+
## Features
14+
15+
- Deep Objects Diffing
16+
17+
- Supports More Object Types, eg: `Map`, `Set`
18+
19+
- TypeScript Support
20+
21+
## Supported Types
22+
23+
- Primitives
24+
- Undefined
25+
- Null
26+
- Number
27+
- String
28+
- Boolean
29+
- BigInt
30+
31+
- Objects
32+
- Plain Objects, eg: `{}`
33+
- Array
34+
- Date
35+
- Map
36+
- Set
37+
38+
## Installation
39+
40+
Install it using your favourite package manager.
41+
42+
```sh
43+
npm install @opentf/obj-diff
44+
```
45+
46+
```sh
47+
yarn add @opentf/obj-diff
48+
```
49+
50+
```sh
51+
pnpm add @opentf/obj-diff
52+
```
53+
54+
```sh
55+
bun add @opentf/obj-diff
56+
```
57+
58+
```sh
59+
deno add @opentf/obj-diff
60+
```
61+
62+
## Usage
63+
64+
```js
65+
import { diff } from '@opentf/obj-diff';
66+
67+
diff(obj1: object, obj2: object): Array<DiffResult>
68+
```
69+
70+
```ts
71+
type DiffResult = {
72+
t: 0 | 1 | 2; // The type of diff, 0 - Deleted, 1 - Created, 2 - Updated
73+
p: Array<string | number>; // The object path
74+
v?: unknown; // The current value
75+
};
76+
```
77+
78+
## Examples
79+
80+
1. Diff two simple objects.
81+
82+
```js
83+
const a = {a: 1, b: 2};
84+
const b = {a: 2, c: 3};
85+
86+
diff(a, b);
87+
/*
88+
[
89+
{
90+
t: 2,
91+
p: ["a"],
92+
v: 2,
93+
},
94+
{
95+
t: 0,
96+
p: ["b"],
97+
},
98+
{
99+
t: 1,
100+
p: ["c"],
101+
v: 5,
102+
},
103+
]
104+
*/
105+
```
106+
107+
2. Diff two arrays.
108+
109+
```js
110+
const a = [1, 2, 3, 4, 5];
111+
const b = [1, 3, 5];
112+
113+
diff(a, b);
114+
/*
115+
[
116+
{
117+
t: 2,
118+
p: [1],
119+
v: 3,
120+
},
121+
{
122+
t: 2,
123+
p: [2],
124+
v: 5,
125+
},
126+
{
127+
t: 0,
128+
p: [3],
129+
},
130+
{
131+
t: 0,
132+
p: [4],
133+
},
134+
]
135+
*/
136+
```
137+
138+
3. Deep diff two objects.
139+
140+
```js
141+
const a = {
142+
foo: {
143+
bar: {
144+
a: ['a', 'b'],
145+
b: 2,
146+
c: ['x', 'y'],
147+
e: 100
148+
}
149+
},
150+
buzz: 'world'
151+
};
152+
153+
const b = {
154+
foo: {
155+
bar: {
156+
a: ['a'],
157+
b: 2,
158+
c: ['x', 'y', 'z'],
159+
d: 'Hello, world!'
160+
}
161+
},
162+
buzz: 'fizz'
163+
};
164+
165+
diff(a, b);
166+
/*
167+
[
168+
{
169+
t: 0,
170+
p: ["foo", "bar", "a", 1],
171+
},
172+
{
173+
t: 1,
174+
p: ["foo", "bar", "c", 2],
175+
v: "z",
176+
},
177+
{
178+
t: 0,
179+
p: ["foo", "bar", "e"],
180+
},
181+
{
182+
t: 1,
183+
p: ["foo", "bar", "d"],
184+
v: "Hello, world!",
185+
},
186+
{
187+
t: 2,
188+
p: ["buzz"],
189+
v: "fizz",
190+
},
191+
]
192+
*/
193+
```
194+
195+
## Benchmark
196+
197+
```diff
198+
┌───┬──────────────────┬─────────┬───────────────────┬────────┬─────────┐
199+
│ │ Task Name │ ops/sec │ Average Time (ns) │ Margin │ Samples │
200+
├───┼──────────────────┼─────────┼───────────────────┼────────┼─────────┤
201+
+ 0 │ diff │ 252,694 │ 3957.346814404028 │ ±1.60% │ 25270 │
202+
│ 1 │ microdiff │ 218,441 │ 4577.892286564301 │ ±0.92% │ 21845 │
203+
│ 2 │ deep-object-diff │ 121,385 │ 8238.188318642591 │ ±1.66% │ 12139 │
204+
│ 3 │ just-diff │ 105,292 │ 9497.35384615396 │ ±1.66% │ 10530 │
205+
│ 4 │ deep-diff │ 160,802 │ 6218.820533549017 │ ±1.59% │ 16081 │
206+
└───┴──────────────────┴─────────┴───────────────────┴────────┴─────────┘
207+
```
208+
209+
210+
### Running benchmarks
211+
212+
```sh
213+
$ bun run build
214+
$ bun benchmark.js
215+
```
216+
217+
## Articles
218+
219+
Please read our important articles:
220+
221+
- [Introducing Our New JavaScript Standard Library](https://ganapathy.hashnode.dev/introducing-our-new-javascript-standard-library)
222+
223+
- [You Don’t Need JavaScript Native Methods](https://ganapathy.hashnode.dev/you-dont-need-javascript-native-methods)
224+
225+
## License
226+
227+
Copyright (c) [Thanga Ganapathy](https://github.com/Thanga-Ganapathy) ([MIT License](./LICENSE)).

benchmark.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Bench, hrtimeNow } from "tinybench";
22
import mdiff from "microdiff";
3-
import { diff } from "./packages/obj-diff/src";
4-
import { diff as deepObjDiff } from "deep-object-diff";
3+
import { diff } from "./packages/obj-diff/dist/index";
4+
import { detailedDiff } from "deep-object-diff";
55
import { diff as justDiff } from "just-diff";
66
import deepDiff from "deep-diff";
77

@@ -66,7 +66,7 @@ bench
6666
mdiff(obj1, obj2);
6767
})
6868
.add("deep-object-diff", () => {
69-
deepObjDiff(obj1, obj2);
69+
detailedDiff(obj1, obj2);
7070
})
7171
.add("just-diff", () => {
7272
justDiff(obj1, obj2);

package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
{
22
"private": true,
33
"scripts": {
4-
"test": "echo \"Error: no test specified\" && exit 1"
4+
"build": "pnpm --filter=@opentf/obj-diff run build",
5+
"test": "pnpm --filter=@opentf/obj-diff run test",
6+
"ci": "pnpm --filter=@opentf/obj-diff run ci",
7+
"publish-packages": "pnpm run build && pnpm run test && changeset version && changeset publish"
58
},
69
"devDependencies": {
10+
"@changesets/cli": "^2.27.1",
711
"deep-diff": "^1.0.2",
812
"deep-object-diff": "^1.1.9",
913
"just-diff": "^6.0.2",

packages/obj-diff/__tests__/diff.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ describe("diff", () => {
125125
});
126126

127127
test("objects", () => {
128+
expect(diff({ a: undefined }, { a: undefined })).toEqual([]);
129+
expect(diff({ a: undefined }, {})).toEqual([{ t: 0, p: ["a"] }]);
128130
expect(diff({ a: 1 }, { a: 1 })).toEqual([]);
129131
expect(diff({ a: 1 }, { a: 2 })).toEqual([{ t: 2, p: ["a"], v: 2 }]);
130132
expect(diff({ a: 1 }, { a: 1, b: 2 })).toEqual([{ t: 1, p: ["b"], v: 2 }]);

packages/obj-diff/eslint.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import globals from "globals";
2+
import pluginJs from "@eslint/js";
3+
import tseslint from "typescript-eslint";
4+
5+
6+
export default [
7+
{languageOptions: { globals: {...globals.browser, ...globals.node} }},
8+
pluginJs.configs.recommended,
9+
...tseslint.configs.recommended,
10+
];

packages/obj-diff/package.json

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@opentf/obj-diff",
3-
"description": "The Fast,Accurate,JavaScript Object Diffing Library.",
3+
"description": "The Fast,Accurate,JavaScript Objects Diffing Library.",
44
"version": "0.0.0",
55
"author": {
66
"name": "Thanga Ganapathy",
@@ -32,8 +32,7 @@
3232
"build": "tsup",
3333
"test": "jest",
3434
"lint": "eslint src/** --fix",
35-
"check-types": "tsc --noEmit",
36-
"ci": "pnpm run build && pnpm run test && pnpm run lint && pnpm run check-types"
35+
"ci": "pnpm run build && pnpm run test && pnpm run lint"
3736
},
3837
"files": [
3938
"dist"
@@ -43,8 +42,14 @@
4342
"provenance": true
4443
},
4544
"devDependencies": {
45+
"@eslint/js": "^9.1.1",
46+
"@swc/jest": "^0.2.36",
4647
"@types/jest": "^29.5.12",
48+
"eslint": "^9.1.1",
49+
"globals": "^15.0.0",
50+
"jest": "^29.7.0",
4751
"tsup": "^8.0.2",
48-
"typescript": "^5.4.5"
52+
"typescript": "^5.4.5",
53+
"typescript-eslint": "^7.7.1"
4954
}
5055
}

0 commit comments

Comments
 (0)