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

treeql

v1.0.1

Published

JSON query and mutation library. It traverses a tree structure in post-order (leaves first, root last), across objects and arrays, returning the nodes which match the partial structure passed in the query, as well as allowing you to mutate or replace matc

Downloads

50

Readme

TreeQL

JSON query and mutation library. It traverses a tree structure in post-order (leaves first, root last), across objects and arrays, returning the nodes which match the partial structure passed in the query, as well as allowing you to mutate or replace matched nodes.

Usage

npm install treeql
var treeql = require("treeql");

treeql.query(tree, query, function(node, variables) {
  // this will be called for every node matching the query
  // return replacement node [optional]
}, function(resultTree, matchesCount) {
  // resultTree: the tree obtained after applying mutations
  // matchesCount: the number of nodes that matched the query
});

Examples

Simple node selection

var data = {
  people: [{
    name: "Andrei",
    age: 28
  }, {
    name: "Homer Simpson",
    age: 60
  }, {
    name: "Corina",
    age: 28
  }]
}

// Query for people aged 28
treeql.query(data, {
  age: 28
}, function(node) {
  // this callback will be called twice:
  // 1: node = { name: "Andrei", age: 28 }
  // 2: node = { name: "Corina", age: 28 }
});

Dynamic filter

Instead of static values, you can use a function to filter based on more complex conditions.

var data = {
  people: [{
    name: "Andrei",
    age: 28
  }, {
    name: "Homer Simpson",
    age: 60
  }, {
    name: "Corina",
    age: 28
  }]
}

// Query for people aged under 50
treeql.query(data, {
  age: function(age) { return age < 50 }
}, function(node) {
  // this callback will be called twice:
  // 1: node = { name: "Andrei", age: 28 }
  // 2: node = { name: "Corina", age: 28 }
});

Variables

The library supports variable definition in the query to ensure it matches the same value across different parts of the query object.

var $ = treeql.variable;

var tree = {
  person: {
    "name": "Andrei",
    "age": 28,
    "os": "MacOS",
    "team": {
      "name": "idevelop",
      "preferred_os": "MacOS"
    }
  }
};

// Query for people using their team's preferred OS
var query = {
  "os": $("operating_system"),
  "team": {
    "preferred_os": $("operating_system")
  }
};

treeql.query(tree, query, function(node, variables) {
  // node = { "name": "Andrei", ... }
  // variables = { "operating_system": "MacOS" }
});

Tree mutation

TreeQL also supports mutating the set of matched nodes. Any mutation happens on a copy of the original structure, which remains unchanged. To obtain the resulting mutated tree, the query function takes on optional second callback parameter, completeCallback, which gets invoked at the end and receives the arguments resultTree and matchesCount.

To mutate the tree you can either change properties of a matched node inside the node callback:

treeql.query(tree, {
  "name": "Andrei"
}, function(node) {
  node.age++;
}, function(resultTree, matchesCount) {
  // resultTree contains the original tree, with all the age values incremented
});

Alternatively, if the node callback returns a value, it will replace the matched node entirely.

var data = [{
  name: "Andrei"
}, {
  differentProperty: 10
}]

// Match objects with "name" property, regardless of value
var query = {
  name: undefined
};

treeql.query(data, query, function(node) {
  return {
    name: node.name.toLowerCase()
  }
});

Here's how you can sum up a nested tree of number arrays.

var data = [1, 2, 3, [4, 5, [6]]];

var query = []; // Match any array

treeql.query(data, query, function(array) {
  var sum = 0;
  array.map(function(value) {
    sum += value;
  });

  return sum;
}, function(result) {
  // result = 21
});

You can find a couple more examples in the treeSpec.js unit test suite.

Author

Andrei Gheorghe

License

  • TreeQL is licensed under the MIT License.
  • I am providing code in this repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not from my employer (Facebook).