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

tree-changes

v0.11.2

Published

Get changes between two versions of data with similar shape

Downloads

1,686,898

Readme

tree-changes

NPM version build status Quality Gate Status Coverage

Compare changes between two datasets.

Setup

npm install tree-changes

Usage

import treeChanges from 'tree-changes';

const previousData = {
  hasData: false,
  sort: {
    data: [{ type: 'asc' }, { type: 'desc' }],
    status: 'idle',
  },
};

const newData = {
  hasData: true,
  sort: {
    data: [{ type: 'desc' }, { type: 'asc' }],
    status: 'success',
  },
};

const { changed, changedFrom } = treeChanges(previousData, newData);

changed(); // true

changed('hasData'); // true
changed('hasData', true); // true
changed('hasData', true, false); // true

// support nested matches. with dot notation
changed('sort.data.0.type', 'desc'); // true

// works with array values too
changed('sort.status', ['done', 'success']); // true

// if you only need to know the previous value 
changedFrom('sort.status', 'idle'); // true

Works with arrays too.

import treeChanges from 'tree-changes';

const { changed } = treeChanges([0, { id: 2 }], [0, { id: 4 }]);

changed(); // true
changed(0); // false
changed(1); // true
changed('1.id', 4); // true

This library uses @gilbarbara/deep-equal to compare properties.

API

added(key: Key, value?: Value)
Check if something was added to the data.
Works with arrays and objects (using Object.keys).

import treeChanges from 'tree-changes';

const previousData = {
  actions: {},
  messages: [],
};

const newData = {
  actions: { complete: true },
  messages: ['New Message'],
  sudo: true,
};

const { added } = treeChanges(previousData, newData);

added(); // true
added('actions'); // true
added('messages'); // true
added('sudo'); // true

changed(key?: Key, actual?: Value, previous?: Value)
Check if the data has changed.
It also can compare to the actual value or even with the previous.

changedFrom(key: Key, previous: Value, actual?: Value)
Check if the data has changed from previous or from previous to actual.

decreased(key: Key, actual?: Value, previous?: Value)
Check if both values are numbers and the value has decreased.
It also can compare to the actual value or even with the previous.

import treeChanges from 'tree-changes';

const previousData = {
  ratio: 0.9,
  retries: 0,
};

const newData = {
  ratio: 0.5,
  retries: 1,
};

const { decreased } = treeChanges(previousData, newData);

decreased('ratio'); // true
decreased('retries'); // false

emptied(key: Key)
Check if the data was emptied. Works with arrays, objects and strings.

import treeChanges from 'tree-changes';

const previousData = {
  data: { a: 1 },
  items: [{ name: 'test' }],
  missing: 'username',
};

const newData = {
  data: {},
  items: [],
  missing: '',
};

const { emptied } = treeChanges(previousData, newData);

emptied('data'); // true
emptied('items'); // true
emptied('missing'); // true

filled(key: Key)
Check if the data was filled (from a previous empty value). Works with arrays, objects and strings.

import treeChanges from 'tree-changes';

const previousData = {
  actions: {},
  messages: [],
  username: '',
};

const newData = {
  actions: { complete: true },
  messages: ['New Message'],
  username: 'John',
};

const { filled } = treeChanges(previousData, newData);

filled('actions'); // true
filled('messages'); // true
filled('username'); // true

increased(key: Key, actual?: Value, previous?: Value)
Check if both values are numbers and the value has increased.
It also can compare to the actual value or even with the previous.

import treeChanges from 'tree-changes';

const previousData = {
  ratio: 0.9,
  retries: 0,
};

const newData = {
  ratio: 0.5,
  retries: 1,
};

const { increased } = treeChanges(previousData, newData);

increased('retries'); // true
increased('ratio'); // false

removed(key: Key, value?: Value)
Check if something was removed from the data.
Works with arrays and objects (using Object.keys).

import treeChanges from 'tree-changes';

const previousData = {
  data: { a: 1 },
  items: [{ name: 'test' }],
  switch: false,
};

const newData = {
  data: {},
  items: [],
};

const { removed } = treeChanges(previousData, newData);

removed(); // true
removed('data'); // true
removed('items'); // true
removed('switch'); // true

Types
type Key = string | number;
type ValidTypes = string | boolean | number | Record<string, any> };
type Value = ValidTypes | ValidTypes[];

License

MIT