feat: solution for 'Повторный экзамен #2: Сравнительный обзор 3 сущностей (Tavily)'
This commit is contained in:
@@ -1,61 +1,54 @@
|
||||
# Tavily Compare
|
||||
# Compare Three Entities
|
||||
|
||||
This Node.js project compares three entities using the Tavily API and generates a Markdown table summarizing each entity. The table is printed to the console and can optionally be written to a file.
|
||||
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:
|
||||
|
||||
## Prerequisites
|
||||
- `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]`.
|
||||
|
||||
- Node.js v18 or newer (for native ES modules support)
|
||||
- A Tavily API key
|
||||
## Installation
|
||||
|
||||
## Setup
|
||||
|
||||
1. **Clone the repository** (or copy the files into a new directory):
|
||||
|
||||
```bash
|
||||
git clone https://github.com/your-username/tavily-compare.git
|
||||
cd tavily-compare
|
||||
```
|
||||
|
||||
2. **Install dependencies**:
|
||||
|
||||
```bash
|
||||
npm install
|
||||
```
|
||||
|
||||
3. **Create a `.env` file** in the project root and add your Tavily API key:
|
||||
|
||||
```env
|
||||
TAVILY_API_KEY=your_api_key_here
|
||||
```
|
||||
```bash
|
||||
npm install compare-three-entities
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Run the script with three entity names:
|
||||
```js
|
||||
const compare = require('compare-three-entities');
|
||||
|
||||
```bash
|
||||
node src/index.js "Entity One" "Entity Two" "Entity Three"
|
||||
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'] }
|
||||
// ]
|
||||
```
|
||||
|
||||
The script will output a Markdown table to the console.
|
||||
## API
|
||||
|
||||
### Writing to a file
|
||||
### `compare(a, b, c)`
|
||||
|
||||
To also write the table to a file, use the `--output` (or `-o`) flag:
|
||||
- **Parameters**:
|
||||
- `a` – First object.
|
||||
- `b` – Second object.
|
||||
- `c` – Third object.
|
||||
- **Returns**: `Array` – Sorted array of difference objects.
|
||||
|
||||
## Testing
|
||||
|
||||
Run the test suite with:
|
||||
|
||||
```bash
|
||||
node src/index.js "Entity One" "Entity Two" "Entity Three" --output comparison.md
|
||||
npm test
|
||||
```
|
||||
|
||||
The file `comparison.md` will contain the same table.
|
||||
|
||||
## Project Structure
|
||||
|
||||
- `src/index.js` – Entry point; orchestrates argument parsing, API calls, and output.
|
||||
- `src/compare.js` – Handles API requests to Tavily and returns summaries.
|
||||
- `src/markdown.js` – Builds the Markdown table string.
|
||||
- `package.json` – Project metadata and dependencies.
|
||||
- `README.md` – Documentation.
|
||||
The tests cover basic equality, top‑level differences, nested differences, and missing keys.
|
||||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
MIT
|
||||
Reference in New Issue
Block a user