54 lines
1.3 KiB
Markdown
54 lines
1.3 KiB
Markdown
# Compare Three Entities
|
||
|
||
A small utility library that compares three JavaScript objects and reports the differences between them.
|
||
The comparison is deep, meaning nested objects are compared recursively. The result is an array of difference objects, each containing:
|
||
|
||
- `key`: The dot‑separated path to the differing property.
|
||
- `values`: An array of the values from the three objects in the order `[a, b, c]`.
|
||
|
||
## Installation
|
||
|
||
```bash
|
||
npm install compare-three-entities
|
||
```
|
||
|
||
## Usage
|
||
|
||
```js
|
||
const compare = require('compare-three-entities');
|
||
|
||
const a = { name: 'Alice', age: 30, address: { city: 'NY' } };
|
||
const b = { name: 'Alice', age: 31, address: { city: 'NY' } };
|
||
const c = { name: 'Alice', age: 30, address: { city: 'LA' } };
|
||
|
||
const differences = compare(a, b, c);
|
||
console.log(differences);
|
||
// [
|
||
// { key: 'age', values: [30, 31, 30] },
|
||
// { key: 'address.city', values: ['NY', 'NY', 'LA'] }
|
||
// ]
|
||
```
|
||
|
||
## API
|
||
|
||
### `compare(a, b, c)`
|
||
|
||
- **Parameters**:
|
||
- `a` – First object.
|
||
- `b` – Second object.
|
||
- `c` – Third object.
|
||
- **Returns**: `Array` – Sorted array of difference objects.
|
||
|
||
## Testing
|
||
|
||
Run the test suite with:
|
||
|
||
```bash
|
||
npm test
|
||
```
|
||
|
||
The tests cover basic equality, top‑level differences, nested differences, and missing keys.
|
||
|
||
## License
|
||
|
||
MIT |