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

@alkemist/compare-engine

v1.3.7

Published

Comparison engine

Downloads

108

Readme

Compare Engine

Installation

  • From npm: npm install @alkemist/compare-engine
  • From yarn: yarn add @alkemist/compare-engine

Test

  • From npm: npm test
  • From yarn: yarn test

About

Compares 2 value, considering array object movement :

  • Either with a signature (identical object)
  • Or with a key defined by the method passed to the constructor
  • The search is recursive: if an object has been moved within a moved object, it will be detected as moved
  • Takes into consideration : value type/class, function name and signature (without parameter type)

Examples

With JSON

import {CompareEngine} from '@alkemist/compare-engine';

const jsonLeft = {
    objectArray: [
        {
            id: "movedIndex",
            property: "old property",
            otherObjectArray: [
                {id: "1"},
                {id: "2"},
            ],
            otherArray: [
                0, 1, 2
            ]
        },
        {
            id: "removedObject",
        },
        {
            id: "equal"
        }
    ]
};
const jsonright = {
    objectArray: [
        {
            id: "newObject",
        },
        {
            id: "movedIndex",
            property: "updated property",
            otherObjectArray: [
                {id: "2"},
                {id: "1"},
            ],
            otherArray: [
                2, 1, 3
            ]
        },
        {
            id: "equal"
        }
    ]
};

const compareEngine = new CompareEngine((paths: string[]) => {
  return "id";
});

compareEngine.updateLeft(jsonLeft);
compareEngine.updateRight(jsonRight);

compareEngine.updateCompareIndex();

compareEngine.getLeftState("") // return UPDATED CompareState
compareEngine.getLeftState("objectArray") // return UPDATED CompareState
compareEngine.getLeftState("objectArray[0]") // return UPDATED CompareState
compareEngine.getLeftState("objectArray[0].id") // return EQUAL CompareState
compareEngine.getLeftState("objectArray[0].property") // return UPDATED CompareState
compareEngine.getLeftState("objectArray[0].otherObjectArray") // return UPDATED CompareState
compareEngine.getLeftState("objectArray[0].otherObjectArray[0]") // return MOVED CompareState
compareEngine.getLeftState("objectArray[0].otherObjectArray[0].id") // return NONE CompareState
compareEngine.getLeftState("objectArray[0].otherObjectArray[1]") // return MOVED CompareState
compareEngine.getLeftState("objectArray[0].otherObjectArray[1].id") // return NONE CompareState
compareEngine.getLeftState("objectArray[0].otherArray") // return UPDATED CompareState
compareEngine.getLeftState("objectArray[0].otherArray[0]") // return REMOVED CompareState
compareEngine.getLeftState("objectArray[0].otherArray[1]") // return EQUAL CompareState
compareEngine.getLeftState("objectArray[0].otherArray[2]") // return MOVED CompareState
compareEngine.getLeftState("objectArray[1]") // return REMOVED CompareState
compareEngine.getLeftState("objectArray[1].id") // return NONE CompareState
compareEngine.getLeftState("objectArray[2]") // return EQUAL CompareState
compareEngine.getLeftState("objectArray[2].id") // return NONE CompareState

compareEngine.getRightState("") // return UPDATED CompareState
compareEngine.getRightState("objectArray") // return UPDATED CompareState
compareEngine.getRightState("objectArray[0]") // return ADDED CompareState
compareEngine.getRightState("objectArray[0].id") // return NONE CompareState
compareEngine.getRightState("objectArray[1]") // return UPDATED CompareState
compareEngine.getRightState("objectArray[1].id") // return EQUAL CompareState
compareEngine.getRightState("objectArray[1].property") // return UPDATED CompareState
compareEngine.getRightState("objectArray[1].otherObjectArray") // return UPDATED CompareState
compareEngine.getRightState("objectArray[1].otherObjectArray[0]") // return MOVED CompareState
compareEngine.getRightState("objectArray[1].otherObjectArray[0].id") // return NONE CompareState
compareEngine.getRightState("objectArray[1].otherObjectArray[1]") // return MOVED CompareState
compareEngine.getRightState("objectArray[1].otherObjectArray[1].id") // return NONE CompareState
compareEngine.getRightState("objectArray[1].otherArray") // return UPDATED CompareState
compareEngine.getRightState("objectArray[1].otherArray[0]") // return MOVED CompareState
compareEngine.getRightState("objectArray[1].otherArray[1]") // return EQUAL CompareState
compareEngine.getRightState("objectArray[1].otherArray[2]") // return ADDED CompareState
compareEngine.getRightState("objectArray[2]") // return EQUAL CompareState
compareEngine.getRightState("objectArray[2].id") // return NONE CompareState

With Objects

class Parent {
    constructor(protected property = "value") {
    }
}

class Child extends Parent {
    constructor(protected override property = "value") {
        super();
    }
}

const compareEngine = new CompareEngine((paths: string[]) => {
  return "id";
}, new Parent(), new Parent());
compareEngine.updateCompareIndex();
compareEngine.getLeftState("") // return EQUAL CompareState

const compareEngine = new CompareEngine((paths: string[]) => {
  return "id";
}, new Parent(), new Child());
compareEngine.updateCompareIndex();
compareEngine.getLeftState("") // return UPDATED CompareState

const compareEngine = new CompareEngine((paths: string[]) => {
  return "id";
}, new Child(), new Child("otherValue"));
compareEngine.updateCompareIndex();
compareEngine.getLeftState("") // return UPDATED CompareState

Exposed models, enums and utils

enum CompareStateEnum {
    NONE = "",
    ADDED = "added",
    MOVED = "moved",
    UPDATED = "updated",
    REMOVED = "removed",
    EQUAL = "equal",
}

class CompareEngine<DATA_TYPE> {
    constructor(
        protected determineArrayIndexFn?: (paths: ValueKey[]) => ValueKey
        leftValue?: DATA_TYPE = null,
        rightValue?: DATA_TYPE = null
    )

    get leftValue(): DATA_TYPE | undefined
    get rightValue(): DATA_TYPE | undefined

    getInLeft(paths: ValueKey[] | ValueKey): unknown
    getInRight(paths: ValueKey[] | ValueKey): unknown

    updateLeft(value: DATA_TYPE | undefined): void
    updateRight(value: DATA_TYPE | undefined): void

    updateInLeft(value: DATA_TYPE | undefined, paths: ValueKey[] | ValueKey): void
    updateInRight(value: DATA_TYPE | undefined, paths: ValueKey[] | ValueKey): void

    leftToRight(): void
    rightToLeft(): void

    updateCompareIndex(): void

    hasChange(): boolean

    getLeftState(paths: ValueKey[] | ValueKey): CompareState
    getRightState(paths: ValueKey[] | ValueKey): CompareState
}

class CompareState {
    value: CompareStateEnum

    isNone(): boolean
    isAdded(): boolean
    isMoved(): boolean
    isUpdated(): boolean
    isRemoved(): boolean
    isEqual(): boolean
    isChanged(): boolean

    toString(): string
}

License

Apache License, Version 2.0