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

structdiff

v0.1.8

Published

testing object structure difference.

Downloads

28

Readme

Detect only differences in the structure of objects. This is useful for validation.

GitHub package.json version npm version npm type definitions GitHub 7thCode node.js.yml GitHub last commit

README DEMO in detail


Motivation

Is it the same shape as record m and record n in MongoDB ?

Features

It is determined whether the two objects have the same shape, ignoring the difference in primitive values. Such processing is effective as dynamic validation.

Installation

npm install structfiff

No modules depend on it.

Usage

How to use

example

const object1 = {
	children: {
		john: {
			schooling: true, hobby: [{name: "Cycling"}, {name: "Dance", type: "HipHop"}],
			pet: [{type: "dog", name: "Max"}], age: 12
		},
		tom: {
			schooling: false, hobby: [{name: "Squash"}], pet: [{type: "cat", name: "Chloe"}], age: 5
		}
	}
};

const object2 = {
	children: {
		tom: {
			pet: [{type: "cat", name: "Chloe"}], age: 5, schooling: false, hobby: [{name: "Squash"}]
		},
		john: {
			hobby: [{name: "Cycling"}, {name: "Dance", type: "HipHop"}],
			pet: [{name: "Max",type: "dog"}], age: 12, schooling: true
		}
	}
};

CommonJS

const structdiff: any = require("structdiff");

const cjs_detector = new structdiff.StructDiff();

let result:boolean = cjs_detector.isSame(object1, object2, [type]);

ESModule

import {StructDiff} from "structdiff";

const es_detector = new StructDiff();

let result:boolean = es_detector.isSame(object1, object2, [type]);

| params | meaning | |---------|------------------------| | object1 | targets for comparison | | object2 | targets for comparison | | type | type of comparison |

type of comparison

| value | meaning | |-------|-----------------------------------------------------------------------| | 0 | default. Detects differences in structure and value "types". | | 1 | Detects differences in structure and values. | | 2 | Only structural differences are detected. |

  • Prefer evaluation in handlers over type parameters.

With Handler

CommonJS

const structdiff: any = require("structdiff");

class CJSHandler extends structdiff.DetectHandler {

    constructor() {
        super()
    }

    public compare(s: any, d: any): boolean {
        return ((typeof s) === (typeof d));
    }
}

const cjs_detector = new structdiff.StructDiff(new CJSHandler());
let result:boolean = cjs_detector.isSame(object1, object2);

ESModule

import {DetectHandler, StructDiff} from "structdiff";

class ESHandler extends DetectHandler {

    constructor() {
        super()
    }

    public compare(s: any, d: any): boolean {
        return ((typeof s) === (typeof d));
    }

}

const es_detector = new StructDiff(new ESHandler());
let result:boolean = es_detector.isSame(object1, object2);
comp_type: ignored.

Default

Detects differences in structure and value "types".
console.log(detector.isSame({a: 1, b: 1}, {a: 1}))
> false

console.log(detector.isSame({a: 1}, {a: 1, b: 1}))
> false
console.log(detector.isSame({a: 1}, {a: 2}, 0))
> true

console.log(detector.isSame({a: 1}, {b: 2}, 0))
> false

Strict Mode

Detects differences in structure and values.
console.log(detector.isSame({a: 1}, {a: 1}, 1))
> true

console.log(detector.isSame({a: 1}, {a: 2}, 1))
> false

Loose Mode

Only structural differences are detected.
console.log(detector.isSame({a: 1}, {a: "2"}, 2))
> true

console.log(detector.isSame({a: 1}, {b: "2"}, 2))
> false

Array order

The difference in the arrangement order is the difference in the structure.
console.log(detector.isSame([{a: 1}, {b: 1}], [{a: 1}, {b: 1}]))
> true

console.log(detector.isSame([{a: 1}, {b: 1}], [{b: 1}, {a: 1}]))
> false

Part of the object

Of course, some of the objects can also be compared.
console.log(detector.isSame(_origin.children.john, copy.children.john))
> true

console.log(detector.isSame(_origin.children.john, _origin.children.tom))
> false

Exceptionally

Exceptionally, [] and {} are the same, as are 0 and NaN.
console.log(detector.isSame([], {}))
> true

console.log(detector.isSame(0, NaN))
> true

Note

See demo.md for unclear cases.

Author

[email protected]

License

"structdiff" is under MIT license.