npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-diff-summarizer

v1.0.1

Published

本库提供了一系列实用的工具函数,用于处理对象和数组的比较及筛选。

Downloads

11

Readme

JavaScript 工具函数库

本库提供了一系列实用的工具函数,用于处理对象和数组的比较及筛选。

目录

简介

这是一个轻量级的 JavaScript 工具库,提供了以下功能:

  • 比较两个对象的不同字段。
  • 查找与对象键匹配的数组项。
  • 计算两个数组的差异。
  • 计算两个数组在指定键值下的对象差异。

安装

你可以通过 npm 安装此库:

npm install data-diff-summarizer

使用方法

在你的项目中引入此库:

import { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } from 'data-diff-summarizer';

或者使用 CommonJS 模块:

const { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } = require('data-diff-summarizer');

API 文档

findDifferentFields

功能: 寻找两个对象之间的不同字段。

参数:
  • obj1 (Object): 第一个对象。
  • obj2 (Object): 第二个对象。

返回值:

  • (Object): 返回一个对象,其中键表示不同字段的路径,值是一个数组,包含两个对象在该字段上的值。

示例:

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): 包含所有匹配项的数组,这些项的特定键的值在对象中作为键存在。

示例:

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,表示两个数组之间的差异。

示例

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): 返回一个数组,包含在两个数组的差集。

示例

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

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:

npm install data-diff-summarizer

Usage

Import the library in your project:

import { findDifferentFields, findMatchingKeys, arraysDifference, arrayObjectDifference } from 'data-diff-summarizer';

Or use CommonJS modules:

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:

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:

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:

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:

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.