Skip to content

Commit ca8ecb9

Browse files
authored
Convert to monorepo and revamp build system (#350)
* Make it a monorepo and convert demos to TypeScript * Updates * Redo build system * Gut old build system and bump all dependencies * Fix package build * Cleanup and fix tests * Format * Update README * Format
1 parent 8414a55 commit ca8ecb9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+3321
-4065
lines changed

.babelrc

Lines changed: 0 additions & 6 deletions
This file was deleted.

.github/workflows/CI.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ jobs:
1818
cache: 'npm'
1919
- run: npm ci
2020
- run: npm run build
21+
working-directory: ./packages/jsondiffpatch
2122
- run: npm run test
23+
working-directory: ./packages/jsondiffpatch
2224
- run: npm run lint
25+
working-directory: ./packages/jsondiffpatch
2326
- run: npm run format-check
27+
- run: npm run start
28+
working-directory: ./demos/console-demo
29+
- run: npm run build
30+
working-directory: ./demos/html-demo
31+
- run: npm run start
32+
working-directory: ./demos/numeric-plugin-demo

.gitignore

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@ lib-cov
1212
pids
1313
logs
1414
results
15-
coverage
15+
coverage
1616
.nyc_output
17-
dist
17+
dist
1818
build
19+
lib
1920

2021
npm-debug.log
21-
.idea/
22+
.idea/

.npmignore

Whitespace-only changes.

Makefile

Lines changed: 0 additions & 11 deletions
This file was deleted.

README.md

Lines changed: 36 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ Diff & patch JavaScript objects
1515
## **[Live Demo](http://benjamine.github.io/jsondiffpatch/demo/index.html)**
1616

1717
- min+gzipped ~ 16KB
18-
- browser and server (`/dist` folder with bundles for UMD, commonjs, or ES modules)
18+
- browser and server (ESM-only)
1919
- (optionally) uses [google-diff-match-patch](http://code.google.com/p/google-diff-match-patch/) for long text diffs (diff at character level)
2020
- smart array diffing using [LCS](http://en.wikipedia.org/wiki/Longest_common_subsequence_problem), **_IMPORTANT NOTE:_** to match objects inside an array you must provide an `objectHash` function (this is how objects are matched, otherwise a dumb match by position is used). For more details, check [Array diff documentation](docs/arrays.md)
2121
- reverse a delta
@@ -31,34 +31,29 @@ Diff & patch JavaScript objects
3131

3232
## Supported platforms
3333

34-
- Any modern browser and IE8+
35-
36-
[![Testling Status](https://ci.testling.com/benjamine/jsondiffpatch.png)](https://ci.testling.com/benjamine/jsondiffpatch)
37-
38-
And you can test your current browser visiting the [test page](http://benjamine.github.io/jsondiffpatch/test/index.html).
39-
40-
- Node.js [![Build Status](https://secure.travis-ci.org/benjamine/jsondiffpatch.svg)](http://travis-ci.org/benjamine/jsondiffpatch) v8+
34+
- Any browser that supports ES6
35+
- Node.js 18, 20+
4136

4237
## Usage
4338

4439
```javascript
4540
// sample data
46-
var country = {
41+
const country = {
4742
name: 'Argentina',
4843
capital: 'Buenos Aires',
4944
independence: new Date(1816, 6, 9),
5045
unasur: true,
5146
};
5247

5348
// clone country, using dateReviver for Date objects
54-
var country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);
49+
const country2 = JSON.parse(JSON.stringify(country), jsondiffpatch.dateReviver);
5550

5651
// make some changes
5752
country2.name = 'Republica Argentina';
5853
country2.population = 41324992;
5954
delete country2.capital;
6055

61-
var delta = jsondiffpatch.diff(country, country2);
56+
const delta = jsondiffpatch.diff(country, country2);
6257

6358
assertSame(delta, {
6459
name: ['Argentina', 'Republica Argentina'], // old value, new value
@@ -70,10 +65,10 @@ assertSame(delta, {
7065
jsondiffpatch.patch(country, delta);
7166

7267
// reverse diff
73-
var reverseDelta = jsondiffpatch.reverse(delta);
68+
const reverseDelta = jsondiffpatch.reverse(delta);
7469
// also country2 can be return to original value with: jsondiffpatch.unpatch(country2, delta);
7570

76-
var delta2 = jsondiffpatch.diff(country, country2);
71+
const delta2 = jsondiffpatch.diff(country, country2);
7772
assert(delta2 === undefined);
7873
// undefined => no difference
7974
```
@@ -82,7 +77,7 @@ Array diffing:
8277

8378
```javascript
8479
// sample data
85-
var country = {
80+
const country = {
8681
name: 'Argentina',
8782
cities: [
8883
{
@@ -109,7 +104,7 @@ var country = {
109104
};
110105

111106
// clone country
112-
var country2 = JSON.parse(JSON.stringify(country));
107+
const country2 = JSON.parse(JSON.stringify(country));
113108

114109
// delete Cordoba
115110
country.cities.splice(1, 1);
@@ -120,18 +115,18 @@ country.cities.splice(4, 0, {
120115
});
121116

122117
// modify Rosario, and move it
123-
var rosario = country.cities.splice(1, 1)[0];
118+
const rosario = country.cities.splice(1, 1)[0];
124119
rosario.population += 1234;
125120
country.cities.push(rosario);
126121

127122
// create a configured instance, match objects by name
128-
var diffpatcher = jsondiffpatch.create({
123+
const diffpatcher = jsondiffpatch.create({
129124
objectHash: function (obj) {
130125
return obj.name;
131126
},
132127
});
133128

134-
var delta = diffpatcher.diff(country, country2);
129+
const delta = diffpatcher.diff(country, country2);
135130

136131
assertSame(delta, {
137132
cities: {
@@ -165,7 +160,7 @@ assertSame(delta, {
165160
});
166161
```
167162

168-
For more example cases (nested objects or arrays, long text diffs) check `test/examples/`
163+
For more example cases (nested objects or arrays, long text diffs) check `packages/jsondiffpatch/test/examples/`
169164

170165
If you want to understand deltas, see [delta format documentation](docs/deltas.md)
171166

@@ -180,20 +175,19 @@ npm install jsondiffpatch
180175
```
181176

182177
```js
183-
var jsondiffpatch = require('jsondiffpatch');
184-
var jsondiffpatchInstance = jsondiffpatch.create(options);
178+
import * as jsondiffpatch from 'jsondiffpatch';
179+
const jsondiffpatchInstance = jsondiffpatch.create(options);
185180
```
186181

187-
Some properties are available only from static main module (e.g. formatters, console), so we need to keep the reference to it if we want to use them.
188-
189182
### browser
190183

191-
In a browser, you could load directly a bundle in `/dist`, eg. `/dist/jsondiffpatch.umd.js`.
184+
In a browser, you can load a bundle using a tool like [esm.sh](https://esm.sh) or [Skypack](https://www.skypack.dev).
192185

193186
## Options
194187

195188
```javascript
196-
var jsondiffpatchInstance = require('jsondiffpatch').create({
189+
import * as jsondiffpatch from 'jsondiffpatch';
190+
const jsondiffpatchInstance = jsondiffpatch.create({
197191
// used to match objects when diffing arrays, by default only === operator is used
198192
objectHash: function (obj) {
199193
// this function is used only to when objects are not equal by ref
@@ -230,38 +224,40 @@ var jsondiffpatchInstance = require('jsondiffpatch').create({
230224
<!doctype html>
231225
<html>
232226
<head>
233-
<script
234-
type="text/javascript"
235-
src="https://cdn.jsdelivr.net/npm/jsondiffpatch/dist/jsondiffpatch.umd.min.js"
236-
></script>
237227
<link rel="stylesheet" href="./style.css" type="text/css" />
238228
<link
239229
rel="stylesheet"
240-
href="../formatters-styles/html.css"
230+
href="https://cdn.skypack.dev/jsondiffpatch/formatters/styles/html.css"
241231
type="text/css"
242232
/>
243233
<link
244234
rel="stylesheet"
245-
href="../formatters-styles/annotated.css"
235+
href="https://cdn.skypack.dev/jsondiffpatch/formatters/styles/annotated.css"
246236
type="text/css"
247237
/>
248238
</head>
249239
<body>
250240
<div id="visual"></div>
251241
<hr />
252242
<div id="annotated"></div>
253-
<script>
254-
var left = { a: 3, b: 4 };
255-
var right = { a: 5, c: 9 };
256-
var delta = jsondiffpatch.diff(left, right);
243+
<script type="module">
244+
import * as jsondiffpatch from 'https://cdn.skypack.dev/jsondiffpatch';
245+
import * as annotatedFormatter from 'https://cdn.skypack.dev/jsondiffpatch/formatters/annotated';
246+
import * as htmlFormatter from 'https://cdn.skypack.dev/jsondiffpatch/formatters/html';
247+
248+
const left = { a: 3, b: 4 };
249+
const right = { a: 5, c: 9 };
250+
const delta = jsondiffpatch.diff(left, right);
257251
258252
// beautiful html diff
259-
document.getElementById('visual').innerHTML =
260-
jsondiffpatch.formatters.html.format(delta, left);
253+
document.getElementById('visual').innerHTML = htmlFormatter.format(
254+
delta,
255+
left,
256+
);
261257
262258
// self-explained json
263259
document.getElementById('annotated').innerHTML =
264-
jsondiffpatch.formatters.annotated.format(delta, left);
260+
annotatedFormatter.format(delta, left);
265261
</script>
266262
</body>
267263
</html>
@@ -275,12 +271,12 @@ For more details check [Formatters documentation](docs/formatters.md)
275271

276272
```sh
277273
# diff two json files, colored output (using chalk lib)
278-
./node_modules/.bin/jsondiffpatch ./left.json ./right.json
274+
./node_modules/.bin/jsondiffpatch ./docs/demo/left.json ./docs/demo/right.json
279275

280276
# or install globally
281277
npm install -g jsondiffpatch
282278

283-
jsondiffpatch ./demo/left.json ./demo/right.json
279+
jsondiffpatch ./docs/demo/left.json ./docs/demo/right.json
284280
```
285281

286282
![console_demo!](docs/demo/consoledemo.png)

docs/demo/consoledemo.js renamed to demos/console-demo/demo.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,36 @@
1-
const jsondiffpatch = require('../../dist/jsondiffpatch.cjs.js');
1+
import * as jsondiffpatch from 'jsondiffpatch';
2+
import * as consoleFormatter from 'jsondiffpatch/formatters/console';
23

34
const instance = jsondiffpatch.create({
45
objectHash: function (obj) {
5-
return obj._id || obj.id || obj.name || JSON.stringify(obj);
6+
const objRecord = obj as Record<string, string>;
7+
return (
8+
objRecord._id ||
9+
objRecord.id ||
10+
objRecord.name ||
11+
JSON.stringify(objRecord)
12+
);
613
},
714
});
815

9-
const data = {
16+
interface Data {
17+
name: string;
18+
summary: string;
19+
surface?: number;
20+
timezone: number[];
21+
demographics: { population: number; largestCities: string[] };
22+
languages: string[];
23+
countries: {
24+
name: string;
25+
capital?: string;
26+
independence?: Date;
27+
unasur: boolean;
28+
population?: number;
29+
}[];
30+
spanishName?: string;
31+
}
32+
33+
const data: Data = {
1034
name: 'South America',
1135
summary:
1236
'South America (Spanish: América del Sur, Sudamérica or Suramérica;' +
@@ -158,4 +182,4 @@ data.demographics.population += 2342;
158182

159183
const right = data;
160184
const delta = instance.diff(left, right);
161-
jsondiffpatch.console.log(delta);
185+
consoleFormatter.log(delta);

demos/console-demo/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "console-demo",
3+
"type": "module",
4+
"version": "1.0.0",
5+
"scripts": {
6+
"start": "npm run build && node build/demo.js",
7+
"build": "tsc"
8+
},
9+
"dependencies": {
10+
"jsondiffpatch": "^0.5.0"
11+
},
12+
"devDependencies": {
13+
"typescript": "~5.3.2"
14+
}
15+
}

demos/console-demo/tsconfig.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es6",
4+
"lib": ["es6"],
5+
"module": "node16",
6+
"outDir": "build",
7+
"esModuleInterop": true,
8+
"forceConsistentCasingInFileNames": true,
9+
"strict": true,
10+
"skipLibCheck": false
11+
}
12+
}

0 commit comments

Comments
 (0)