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

object-hash-strategy

v1.0.3

Published

object-hash-strategy is a lightweight npm package designed to simplify the comparison of two objects with similar structures. With this powerful tool, developers can effortlessly identify changes between objects, making data management in JavaScript proje

Downloads

12

Readme

A Complete Guide to the Hash Comparison Library

Introduction:

This library simplifies comparing hashes of complex objects, even those with nested structures. It allows you to:

  • Detect changes in specific properties.
  • Identify modifications to elements within arrays.
  • Track changes precisely.

Installation:

npm install object-hash-strategy

or

yarn add object-hash-strategy

Key Functions:

Data

First of all, data structure should be defined, This example is going to work with this data definition.

const data = {
  id: 1,
  name: "Imagine Dragons",
  genre: "Rock",
  description:
    "Imagine Dragons is an American rock band from Las Vegas, Nevada.",
  songs: [
    {
      id: 1,
      artist_id: 1,
      title: "Radioactive",
      album: "Night Visions",
      year: 2012,
      genre: "Rock",
      duration: "3:06",
      url: "https://www.youtube.com/watch?v=ktvTqknDobU"
    },
    {
      id: 2,
      artist_id: 1,
      title: "Demons",
      album: "Night Visions",
      year: 2013,
      genre: "Rock",
      duration: "3:11",
      url: "https://m.youtube.com/watch?v=m_m_m_m_m_m_m_m_m_m"
    }
  ]
};

1. Hash Creation:

  • The createHash(target, schema) function generates a hash from an object and a schema defining which properties are included.
  • The schema specifies whether a property should be hashed and can define sub-schemas for nested objects.

Example:

import { createHash, HashSchema } from "object-hash-strategy";

const schema: HashSchema<typeof newData> = {
  hash: true,
  id: "id",
  songs: {
    hash: true,
    id: "id",
    parentHash: true
  }
};

const dataObjectHash = createHash(data, schema);

console.log(dataObjectHash);
// {
//     hash: '3f7f8b0b7787fb28666da87a803878587286c531',
//     id: 1,
//     songsHash: '74e00c297d092cb744bc3c6f90c3a20840d6be19',
//     songs: [
//       { hash: '3d5d5e3db08d36a5e00623f94b8624d9e80dd2ab', id: 1 },
//       { hash: '91b74e916e3a856bc987f066c465298916ae994c', id: 2 }
//     ]
//   }

2. Hash Comparison:

  • The compareHashes(actualHash, oldHash) function compares two hashes and returns an object describing the differences.
  • This object indicates changes in properties, additions/removals in arrays, and modifications in nested objects.

Example:

const newData = { ...data };
newData.songs[1].url = "https://www.youtube.com/watch?v=mWRsgZuwf_8";
s;
const newDataObjectHash = createHash(newData, schema);

console.log(newDataObjectHash);
// {
//     hash: '3f7f8b0b7787fb28666da87a803878587286c531',
//     id: 1,
//     songsHash: '211c8c3369e0b7e823acce56261692b7828724da',
//     songs: [
//       { hash: '3d5d5e3db08d36a5e00623f94b8624d9e80dd2ab', id: 1 },
//       { hash: 'dfc19ec50d6c07acee917fa931f1ed9edacf28f7', id: 2 }
//     ]
//   }

const compareResponse = compareHashes(newHash, oldHash);

console.log(compareResponse);

// {
//     id: 1,
//     action: 'none',
//     songs: [ { id: 1, action: 'none' }, { action: 'update', id: 2 } ]
//   }

3. Extracting Data from Comparison:

  • The getDataFromCompare(data, compareResponse, schema) function retrieves specific information based on the comparison and schema.
  • This is useful for getting only the changed data or data relevant to specific use cases.

Example:

const dataResponse = getDataFromCompare(newData, compareResponse, schema);

console.dir(dataResponse, { depth: null }); 

// {
//     id: 1,
//     action: 'none',
//     songs: [
//       { id: 1, action: 'none' },
//       {
//         action: 'update',
//         id: 2,
//         data: {
//           id: 2,
//           artist_id: 1,
//           title: 'Demons',
//           album: 'Night Visions',
//           year: 2013,
//           genre: 'Rock',
//           duration: '3:11',
//           url: 'https://www.youtube.com/watch?v=mWRsgZuwf_8'
//         }
//       }
//     ]
//   }

Beyond the Basics:

  • Array Handling: The library handles array hashes, detecting changes in order, element addition/removal, and element modifications.
  • Complex Type Support: The library supports diverse data types like dates, custom objects, and functions.
  • Framework Integration: Easy integration with popular frameworks like React, Angular, and Vue.js.

Use Cases:

  • Data integrity verification
  • Configuration change detection
  • Document change tracking
  • Data synchronization between devices

Additional Resources:

  • Library repository: [Link to library repository, replace with actual URL]
  • API documentation: [Link to API documentation, replace with actual URL]
  • Usage examples: [Link to usage examples, replace with actual URL]

Contributing:

We encourage contributions to the library. You can report bugs, submit pull requests with new features or fixes, or join discussions in the repository.

We hope this guide equips you with the knowledge to utilize the library effectively in your projects!