# diff-data-summarizer **Repository Path**: weblfg/diff-data-summarizer ## Basic Information - **Project Name**: diff-data-summarizer - **Description**: 本库提供了一系列实用的工具函数,用于处理对象和数组的比较及筛选。 - **Primary Language**: Unknown - **License**: MulanPSL-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-08-28 - **Last Updated**: 2024-08-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # JavaScript 工具函数库 本库提供了一系列实用的工具函数,用于处理对象和数组的比较及筛选。 ## 目录 - [简介](#简介) - [安装](#安装) - [使用方法](#使用方法) - [API 文档](#api-文档) - [findDifferentFields](#finddifferentfields) - [findMatchingKeys](#findmatchingkeys) - [arraysDifference](#arraysdifference) - [arrayObjectDifference](#arrayobjectdifference) ## 简介 这是一个轻量级的 JavaScript 工具库,提供了以下功能: - 比较两个对象的不同字段。 - 查找与对象键匹配的数组项。 - 计算两个数组的差异。 - 计算两个数组在指定键值下的对象差异。 ## 安装 你可以通过 npm 安装此库: ```bash npm install data-diff-summarizer ``` # 使用方法 #### 在你的项目中引入此库: ```javascript import { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } from 'data-diff-summarizer'; ``` #### 或者使用 CommonJS 模块: ```javascript const { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } = require('data-diff-summarizer'); ``` # API 文档 ### findDifferentFields #### 功能: 寻找两个对象之间的不同字段。 ##### 参数: * obj1 (Object): 第一个对象。 * obj2 (Object): 第二个对象。 #### 返回值: * (Object): 返回一个对象,其中键表示不同字段的路径,值是一个数组,包含两个对象在该字段上的值。 #### 示例: ```javascript const obj1 = { a: 1, b: { c: 3 } }; const obj2 = { a: 1, b: { c: 4 } }; console.log(findDifferentFields(obj1, obj2)); // 输出: { "b.c": [3, 4] } ``` ### findMatchingKeys #### 功能: 查找与对象键匹配的数组项。 #### 参数: * obj (Object): 被用于检查的对象,它的键与数组项的特定键的值进行比较。 * array (Array): 要遍历的数组,数组中的每一项都会被检查。 * keyToCompare (String): 数组项中用于与对象键进行比较的特定键 #### 返回值: * (Array): 包含所有匹配项的数组,这些项的特定键的值在对象中作为键存在。 #### 示例: ```javascript const obj = { x: 1, y: 2 }; const array = [{ id: 1, value: 'x' }, { id: 2, value: 'y' }]; console.log(findMatchingKeys(obj, array, 'value')); // 输出: [{ id: 1, value: 'x' }, { id: 2, value: 'y' }] ``` ### arraysDifference #### 功能: 计算两个数组的差异。 #### 参数 * arr1 (Array): 第一个数组。 * arr2 (Array): 第二个数组。 #### 返回值 * (Object): 一个对象,包含两个数组 diff1 和 diff2,表示两个数组之间的差异。 #### 示例 ```javascript const arr1 = [1, 2, 3]; const arr2 = [2, 3, 4]; console.log(arraysDifference(arr1, arr2)); // 输出: { diff1: [1], diff2: [4] } ``` ### arrayObjectDifference #### 功能: 计算两个数组在指定键值下的对象差异。 #### 参数 * arr1 (Array): 第一个数组,包含待比较的对象。 * arr2 (Array): 第二个数组,用于与第一个数组的对象进行比较。 * key (String): 对象属性的键名,用于比较对象的唯一性。 #### 返回值 * (Array): 返回一个数组,包含在两个数组的差集。 #### 示例 ```javascript const arr1 = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; const arr2 = [{ id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }]; console.log(arrayObjectDifference(arr1, arr2, 'id')); // 输出: { diff1: [{ id: 1, name: 'Alice' }], diff2: [{ id: 3, name: 'Charlie' }] } ``` # JavaScript Utility Function Library This library provides a series of practical utility functions for handling comparisons and filtering of objects and arrays. ## Table of Contents - [Introduction](#introduction) - [Installation](#installation) - [Usage](#usage) - [API Documentation](#api-documentation) - [findDifferentFields](#finddifferentfields) - [findMatchingKeys](#findmatchingkeys) - [arraysDifference](#arraysdifference) - [arrayObjectDifference](#arrayobjectdifference) ## Introduction This is a lightweight JavaScript utility library that offers the following functionalities: - Compare different fields between two objects. - Find array items that match object keys. - Calculate the difference between two arrays. - Calculate the difference of objects in two arrays based on specified key values. ## Installation You can install this library via npm: ```bash npm install data-diff-summarizer ``` ## Usage #### Import the library in your project: ```javascript import { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } from 'data-diff-summarizer'; ``` #### Or use CommonJS modules: ```javascript const { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } = require('data-diff-summarizer'); ``` ## API Documentation ### findDifferentFields #### Function: Finds different fields between two objects. ##### Parameters: - `obj1` (Object): The first object. - `obj2` (Object): The second object. #### Returns: - (Object): Returns an object where keys represent the paths of different fields, and values are arrays containing the values of those fields in both objects. #### Example: ```javascript const obj1 = { a: 1, b: { c: 3 } }; const obj2 = { a: 1, b: { c: 4 } }; console.log(findDifferentFields(obj1, obj2)); // Output: { "b.c": [3, 4] } ``` ### findMatchingKeys #### Function: Finds array items that match object keys. #### Parameters: - `obj` (Object): The object whose keys are used for comparison with specific key values in array items. - `array` (Array): The array to iterate through, where each item is checked. - `keyToCompare` (String): The specific key in array items to compare with object keys. #### Returns: - (Array): An array containing all matching items, where the specific key's value exists as a key in the object. #### Example: ```javascript const obj = { x: 1, y: 2 }; const array = [{ id: 1, value: 'x' }, { id: 2, value: 'y' }]; console.log(findMatchingKeys(obj, array, 'value')); // Output: [{ id: 1, value: 'x' }, { id: 2, value: 'y' }] ``` ### arraysDifference #### Function: Calculates the difference between two arrays. #### Parameters: - `arr1` (Array): The first array. - `arr2` (Array): The second array. #### Returns: - (Object): An object containing `diff1` and `diff2`, representing the differences between the two arrays. #### Example: ```javascript const arr1 = [1, 2, 3]; const arr2 = [2, 3, 4]; console.log(arraysDifference(arr1, arr2)); // Output: { diff1: [1], diff2: [4] } ``` ### arrayObjectDifference #### Function: Calculates the difference of objects in two arrays based on specified key values. #### Parameters: - `arr1` (Array): The first array containing objects to be compared. - `arr2` (Array): The second array of objects for comparison with the first array. - `key` (String): The key name of object properties used for uniqueness comparison. #### Returns: - (Object): Returns an object containing `diff1` and `diff2`, representing the differences between the two arrays' objects. #### Example: ```javascript const arr1 = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]; const arr2 = [{ id: 2, name: 'Bob' }, { id: 3, name: 'Charlie' }]; console.log(arrayObjectDifference(arr1, arr2, 'id')); // Output: { diff1: [{ id: 1, name: 'Alice' }], diff2: [{ id: 3, name: 'Charlie' }] } ``` --- **English Documentation** (above) and **Chinese Documentation** (below) are provided for easy switching between languages.