Browse Source

chore: update readme

pull/27/head
Antoine Lanoe 7 months ago
parent
commit
b84b210dfe
  1. 30
      README.md
  2. 32
      src/lib/list-diff/list-diff.test.ts

30
README.md

@ -16,9 +16,9 @@ This library compares two arrays or objects and returns a full diff of their dif
## WHY YOU SHOULD USE THIS LIBRARY ## WHY YOU SHOULD USE THIS LIBRARY
All other existing solutions return a strange diff format that often requires additional parsing. They are also limited to object comparison. Most existing solutions return a confusing diff format that often requires extra parsing. They are also limited to object comparison.
**Superdiff** gives you a complete diff for both array <u>and</u> objects in a very readable format. Last but not least, it's battle-tested, has zero dependencies, and is super fast. **Superdiff** provides a complete and readable diff for both arrays **and** objects. Plus, it's battle-tested, has zero dependencies, and is super fast.
Import. Enjoy. 👍 Import. Enjoy. 👍
@ -66,7 +66,7 @@ isObject(data)
import { getObjectDiff } from "@donedeal0/superdiff"; import { getObjectDiff } from "@donedeal0/superdiff";
``` ```
Compares two objects and return a diff for each value and their potential subvalues. Supports deeply nested objects with any kind of values. Compares two objects and returns a diff for each value and its possible subvalues. Supports deeply nested objects of any value type.
#### FORMAT #### FORMAT
@ -87,7 +87,7 @@ options?: {
- `prevData`: the original object. - `prevData`: the original object.
- `nextData`: the new object. - `nextData`: the new object.
- `options` - `options`
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order. - `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
- `showOnly`: returns only the values whose status you are interested in. It takes two parameters: - `showOnly`: returns only the values whose status you are interested in. It takes two parameters:
- `statuses`: status you want to see in the output (e.g. `["added", "equal"]`) - `statuses`: status you want to see in the output (e.g. `["added", "equal"]`)
@ -226,8 +226,8 @@ Compares two arrays and returns a diff for each entry. Supports duplicate values
- `nextList`: the new list. - `nextList`: the new list.
- `options` - `options`
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`). - `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
- `referenceProperty` will consider an object to be updated instead of added or deleted if one of its properties remains stable, such as its `id`. This option has no effect on other datatypes. - `referenceProperty` will consider an object to be `updated` rather than `added` or `deleted` if one of its properties remains stable, such as its `id`. This option has no effect on other datatypes.
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order. - `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
- `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`. - `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`.
**Output** **Output**
@ -328,18 +328,18 @@ Streams the diff of two object lists, ideal for large lists and maximum performa
- `prevList`: the original object list. - `prevList`: the original object list.
- `nextList`: the new object list. - `nextList`: the new object list.
- `referenceProperty`: a common property in all the objects of your lists (e.g. `id`). - `referenceProperty`: a property common to all objects in your lists (e.g. `id`).
- `options` - `options`
- `chunksSize` the number of object diffs returned by each streamed chunk. (e.g. `0` = 1 object diff by chunk, `10` = 10 object diffs by chunk). - `chunksSize` the number of object diffs returned by each streamed chunk. (e.g. `0` = 1 object diff per chunk, `10` = 10 object diffs per chunk).
- `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`). - `showOnly` gives you the option to return only the values whose status you are interested in (e.g. `["added", "equal"]`).
- `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`. - `considerMoveAsUpdate`: if set to `true` a `moved` value will be considered as `updated`.
**Output** **Output**
The objects diff are grouped in arrays - called `chunks` - and are consumed thanks to an event listener. You have access to 3 events: The objects diff are grouped into arrays - called `chunks` - and are consumed thanks to an event listener. You have access to 3 events:
- `data`: to be notified when a new chunk of object diffs is available. - `data`: to be notified when a new chunk of object diffs is available.
- `finish`: to be notified when the stream is complete. - `finish`: to be notified when the stream is finished.
- `error`: to be notified of an error during the stream. - `error`: to be notified if an error occurs during the stream.
```ts ```ts
interface StreamListener<T extends Record<string, unknown>> { interface StreamListener<T extends Record<string, unknown>> {
@ -442,7 +442,7 @@ diff.on("error", (err) => console.log(err))
import { isEqual } from "@donedeal0/superdiff"; import { isEqual } from "@donedeal0/superdiff";
``` ```
Checks whether two values are equal. Tests whether two values are equal.
#### FORMAT #### FORMAT
@ -455,9 +455,9 @@ options: {
ignoreArrayOrder: boolean; // false by default ignoreArrayOrder: boolean; // false by default
}, },
``` ```
- `a`: the value to compare to the value `b`. - `a`: the value to be compared to the value `b`.
- `b`: the value that will be compared to the value `a`. - `b`: the value to be compared to the value `a`.
- `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays have the same value, just not in the same order. - `ignoreArrayOrder`: if set to `true`, `["hello", "world"]` and `["world", "hello"]` will be treated as `equal`, because the two arrays contain the same values, just in a different order.
#### USAGE #### USAGE

32
src/lib/list-diff/list-diff.test.ts

@ -791,35 +791,3 @@ describe("getListDiff", () => {
}); });
}); });
}); });
describe("Performance", () => {
it("should correctly stream diff for 10.000 entries", () => {
const generateLargeList = (size: number, idPrefix: string) => {
return Array.from({ length: size }, (_, i) => ({
id: `${idPrefix}-${i}`,
value: i,
}));
};
const prevList = generateLargeList(10_000, "prev");
const nextList = [
...generateLargeList(5000, "prev"),
...generateLargeList(5000, "next"),
];
const receivedChunks = getListDiff(prevList, nextList).diff;
const deletions = receivedChunks.filter(
(diff) => diff.status === LIST_STATUS.DELETED,
);
const additions = receivedChunks.filter(
(diff) => diff.status === LIST_STATUS.ADDED,
);
const updates = receivedChunks.filter(
(diff) => diff.status === LIST_STATUS.EQUAL,
);
expect(receivedChunks.length).toBe(15_000); // 5000 deletions + 5000 equal + 5000 additions
expect(deletions.length).toBe(5000);
expect(additions.length).toBe(5000);
expect(updates.length).toBe(5000);
});
});

Loading…
Cancel
Save