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

hash-compare

v1.0.0

Published

A package for hashing and comparing objects with additional functionality for version tracking and patch generation.

Downloads

3

Readme

Hash Compare Package

A utility package for hashing objects, comparing object differences, generating patches, merging objects, and versioning.

Installation

To install the package, use npm:

npm install hash-compare

Replace hash-compare with the actual name of your package.

Functions

hashObject(obj: Record<string, any> | Record<string, any>[], hashFunction: HashFunction = simpleHash): string

Hashes an object or an array of objects using a provided hash function.

Parameters:

  • obj: The object or array of objects to hash.
  • hashFunction (optional): A function that takes a string and returns a hash. Defaults to simpleHash.

Returns:

A hash string representing the object.

Example :

import { hashObject } from 'hash-compare';

const obj = { key: 'value' };
const hash = hashObject(obj);
console.log(hash); // Output: hash string

prettyPrintDiff(diff: Record<string, { oldValue: any; newValue: any }>): string

Formats the differences between two objects into a human-readable string.

Parameters:

  • diff: An object representing the differences.

Returns:

A formatted string showing the differences.

Example:

import { prettyPrintDiff } from 'hash-compare';

const diff = { 'key': { oldValue: 'value1', newValue: 'value2' } };
const output = prettyPrintDiff(diff);
console.log(output);

generatePatch(oldObj: Record<string, any>, newObj: Record<string, any>): Record<string, any>

Generates a patch representing the changes from oldObj to newObj.

Parameters:

oldObj: The old object. newObj: The new object.

Returns:

A patch object representing the changes.

Example:

import { generatePatch } from 'hash-compare';

const oldObj = { key: 'value1' };
const newObj = { key: 'value2' };
const patch = generatePatch(oldObj, newObj);
console.log(patch); // Output: { 'key': 'value2' }

deepMerge(target: Record<string, any>, source: Record<string, any>): Record<string, any>

Deeply merges the source object into the target object.

Parameters:

target: The target object to be merged into. source: The source object to merge from.

Returns:

The merged target object.

Example:

import { deepMerge } from 'hash-compare';

const target = { key1: 'value1' };
const source = { key2: 'value2' };
const merged = deepMerge(target, source);
console.log(merged); // Output: { key1: 'value1', key2: 'value2' }

addVersion(versionedObjects: VersionedObject[], newObject: Record<string, any>): VersionedObject[]

Adds a new version of an object to an array of versioned objects.

Parameters:

versionedObjects: An array of versioned objects. newObject: The new object to add.

Returns:

An updated array with the new version added.

Example:

import { addVersion } from 'hash-compare';

const versions = [{ version: 1, data: {} }];
const newObject = { key: 'value' };
const updatedVersions = addVersion(versions, newObject);
console.log(updatedVersions);

deepCompare(obj1: Record<string, any> | Array<any>, obj2: Record<string, any> | Array<any>): Record<string, { oldValue: any; newValue: any }>

Compares two objects or arrays and returns the differences.

Parameters:

obj1: The first object or array. obj2: The second object or array.

Returns:

An object representing the differences.

Example:

import { deepCompare } from 'hash-compare';

const obj1 = { key: 'value1' };
const obj2 = { key: 'value2' };
const diff = deepCompare(obj1, obj2);
console.log(diff); // Output: { 'key': { oldValue: 'value1', newValue: 'value2' } }