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

kinda-equal

v0.0.8

Published

Equality checking for messy models

Downloads

2

Readme

kinda-equal

Equality checking for messy models

Build Status

TL;DR


import kindaEqual from 'kinda-equal';

// Default with default filters applied;
const defaultEqual = kindaEqual();

// No filters; standard deep equality object checker;
const noFilerEqual = kindaEqual({filters: []})

// Filters default with custom;
const customFilterEqual = kindaEqual({filters: [
        kindaEqual.ignoreEmptyArray,
        kindaEqual.ignoreEmptyNullUndefined,
        kindaEqual.ignoreEmptyObject,
        (value, key, index) => key.startsWith('$')
]})


let result1 = defaultEqual(obj1, obj2);
let result2 = noFilerEqual(obj1, obj2);
let result3 = customFilterEqual(obj1, obj2);

About

Allows deep equality checks between 2 objects ignoring insignificant properties.

Properties are ignored for comparison if they match filter functions. 3 filter functions are applied by default (but can be overridden)

  • ignoreEmptyNullUndefined
  • ignoreEmptyArray
  • ignoreEmptyObject

Collectively They remove all values that are null, undefined,'', [], {}

The filters are applied in a depth first scan so:


{
    id: 99,
    items: [{name: {log: [], prive: null}, {name: undefined}]
}

is equivelent to :

{
    id: 99
}

filters are simple functions returning true if the property must be ignored.

They recieve 4 parameters value, key, index, path

| Param | Type | | | ------------- |-------------|------------- | | value | * | | | key | string | | | index | number | Optional if value is Array | | path | string | String path from root to the current property e.g 'person.contact.phone[1]' |

Example filters


function compareFirstPersonFilter(value, key, index) {
    if (index !== undefined && key === 'people') {
        return index > 0;
    }
}

function ignoreAngular(value, key) {
    return key.startsWith('$');
}

which filters to apply is set in the config object, if supplied they override the default filters.

If you would like to apply any of the defaults with your filter you need to include them.

{
    filters: []
}

Installing

Install with: npm install kinda-equal --save

3 modules builds are provided

  • index (commonjs)
  • kinda-equal.es (es6)
  • kinda-equal.umd (umd)

Usage

Comparing objects with the default ingore filters


const pure = {
    person: {
        first: 'Bob',
        phone: ['555-666', '444-333'],
    },
    transactions: [
        { id: 1, stamp: new Date(2017, 01, 01), log: {} }
    ]
};

const dirty = {
    person: {
        first: 'Bob',
        surname: undefined,
        phone: ['555-666', '444-333', null],
        address: {
            street: null,
            number: null
        }
    },
    transactions: [
        { id: 1, stamp: new Date(2017, 01, 01), log: {} },
        { log: {} }
    ]
};

const result = kindaEqual()(pure, dirty);
assert.equal(true, result);

Adding a custom ignore function including the default ignore functions


const pure = {
    person: {
        first: 'Bob',
        phone: ['555-666', '444-333'],
    },
    transactions: [
        { id: 1, stamp: new Date(2017, 01, 01), log: {} }
    ]
};

// use the default ignore functions and all properties starting with $
const dirty = {
    person: {
        first: 'Bob',
        $idKey: 88,
        surname: undefined,
        phone: ['555-666', '444-333', null],
        address: {
            street: null,
            number: null
        }
    },
    transactions: [
        { id: 1, stamp: new Date(2017, 01, 01), log: {}, $someOtherId: 99 },
        { log: {} }
    ]
};

const config = {
    filters: [
        kindaEqual.ignoreEmptyArray,
        kindaEqual.ignoreEmptyNullUndefined,
        kindaEqual.ignoreEmptyObject,
        (value, key, index) => key.startsWith('$')
    ]
};

const customEqual = kindaEqual(config);
const result = customEqual(pure, dirty);
assert.equal(true, result);

Adding a custom ignore function which uses path


// ignore all but the first (index 0) phone number
const bob_record_1 = {
    person: {
        first: 'Bob',
        contact: {
            phone: ['555-666', '444-333']
        }
    }
};

const bob_record_2 = {
    person: {
        first: 'Bob',
        contact: {
            phone: ['555-666', '699-333']
        }
    }
};

const config = {
    filters: [
        kindaEqual.ignoreEmptyArray,
        kindaEqual.ignoreEmptyNullUndefined,
        kindaEqual.ignoreEmptyObject,
        (value, key, index, path) => path.startsWith('person.contact.phone') && index > 0
    ]
};

const customEqual = kindaEqual(config);
const result = customEqual(bob_record_1, bob_record_2);
assert.equal(true, result);

Dependencies

Developer instructions

npm run test
npm run build

License

MIT