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

treetorn

v0.0.2

Published

A JavaScript package that helps you enforce data strucutre consistency and write schemas by example.

Downloads

1

Readme

treetorn

A JavaScript package that helps you enforce data strucutre consistency and write "schemas" by example.

Installation

npm install treetorn --save

You may instead just want to use treetorn for testing:

npm install treetorn --save-dev

Examples

Compare two data structures that are equivalent:


import compare from 'treetorn';

let a = {
	cities: [
		{ name: 'San Francisco', nicknames: ['SF', 'the city'], id: 0 },
		{ name: 'Orlando', nicknames: ['O-town', 'The city beautiful'], id: 1 }
	],
	people: [
		{ name: 'Reid', hometown: 1, current_home: 0 },
		{ name: 'Joe', hometown: 2, current_home: 2 }
	]
}

let b = {
	cities: [
		{ name: 'Sun Valley', nicknames: ['A great place to ski'], id: 0 },
		{ name: 'San Francisco', nicknames: ['SF', 'the city'], id: 1 }
	],
	people: [ { name: 'Piper', hometown: 0, current_home: 1 } ]
}

compare(a, b);
// returns { passes: true, err: undefined }

Compare two data structures that have a small difference:

let oops = {
	cities: [
		{ name: 'San Francisco', nicknames: ['SF', 'the city'], id: 0 },
		// oops, here id is a list but should be a scalar
		{ name: 'Orlando', nicknames: ['O-town', 'The city beautiful'], id: [] }
		],
	people: [
		{ name: 'Reid', hometown: 1, current_home: 0 },
		{ name: 'Joe', hometown: 2, current_home: 2 }
	]
}

compare(b, oops);
// returns { passes: false, err: '0 is a leaf but [] is not' }

Philosophy and detail

Treetorn doesn't diff two object trees, instead it determines whether they're equivalent, with a few assumptions built in.

Objects and Maps are treated as dictionaries

object.constructor === Object // if true, object is treated as a dictionary

This includes object literals like:

let object = {whoami: 'an object but really a dictionary'};
object.constructor === Object // returns true

Map objects are treated as dictionaries:

object.constructor === Map    // if true, object is treated as a dictionary

Arrays are treated as lists

Lists do not need to have the same number of items to be equivalent. Treetorn does make sure that all objects in a list compare to be the same:

let a = [1, 2, 3];
let b = [4, 5, 6];
// compare() must be true when comparing the first item of a with the remaining items in a and
// must be true when comparing the first item in a with each item in b

If one or both lists are empty, they are equivalent.

All other objects and primitives are treated as leaf nodes.

compare() of any two leaf nodes always returns true.

Not all collection types are supported!

WeakMap, Set, and WeakSet are treated as leaf nodes. This is probably not what you want.

What's next

  • Support additional collection types.
  • When two nodes in the tree don't match, return the tree path to the mismatching nodes.