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

jora

v1.0.0-beta.13

Published

JavaScript object query engine

Downloads

474,364

Readme

Jora

NPM version Build Status Coverage Status Twitter

JavaScript object query language, and a library to process and perform Jora queries on data.

STATUS: Jora is stable, but syntax may change in next releases. Still very much work in progress (ideas and thoughts).

Features:

  • Tolerant to data stucture queries (e.g. just returns nothing for paths that not reachable)
  • Compact syntax for common tasks
  • Aggregate values across arrays and eliminate duplicates by default
  • Stat collecting mode (powers suggestions)
  • Tolerant parsing mode (useful to provide suggestions for query in an editor)
  • Extensible DSL on query build by custom method list

Related projects:

Table of content:

Query syntax overview

Jora is a query language designed for JSON-like data structures. It extends JSON5 and shares many similarities with JavaScript.

See Docs & playground.

Comments

// single-line comment
/* multi-line
comment */

Expressions

Jora expressions are the building blocks of Jora queries. Expressions can include comments, literals, operators, functions, and variables.

Literals

Jora supports literals, which include:

  • Numbers: 42, -3.14, 6.022e23
  • Strings: "hello", 'world', `template${yes}`, "\u{1F600}"
  • Booleans: true, false
  • Regular expressions: /regexp/flags
  • Object literals: { hello: 'world' } (see Object literals)
  • Array literals: [1, 2, 3] (see Array literals)
  • Functions: => … (see Functions)
  • Keywords: NaN, Infinity, null and undefined

See Literals

Operators

Jora supports most JavaScript operators, including:

  • Arithmetic: +, -, *, /, %
  • Comparison: =, !=, <, <=, >, >=, ~=
  • Logical: and, or, not (alias no), ??, is, in, not in, has, has no
  • Ternary: ?:
  • Grouing: ( )
  • Pipeline: |

See Operators

Dot, bracket and slice notations

Jora provides notations for accessing properties and elements: dot, bracket and slice notations. Dot notation is similar to JavaScript's property access notation, using a period followed by the property name (e.g., $.propertyName). Bracket notation encloses the property name or index within square brackets (e.g., $['propertyName'] or $[0]), it's also possible to use functions to choose. Slice notation provides a concise syntax to slice elements with optional step (array[5:10:2] selects each odd element from 5th to 10th indecies).

Methods and functions

Jora provides a rich set of built-in methods for manipulating data, such as map(), filter(), group(), sort(), reduce(), and many others. You can also define custom functions using the => arrow function syntax, and use them as a method.

Mapping and filtering

Jora has a concise syntax for mapping and filtering. The map(fn) method is equivalent to .(fn()), while the filter(fn) method is equivalent to .[fn()].

Variables

Variables in Jora are helpful for storing intermediate results or simplifying complex expressions. To define a variable, use the $variableName: expression; syntax.

See Variables

NPM package

Install & import

Install with npm:

npm install jora

Basic usage:

// ESM
import jora from 'jora';

// CommonJS
const jora = require('jora');

Bundles are available for use in a browser:

  • dist/jora.js – minified IIFE with jora as global
<script src="node_modules/jora/dist/jora.js"></script>
<script>
  jora('query')(data, context);
</script>
  • dist/jora.esm.js – minified ES module
<script type="module">
  import jora from 'node_modules/jora/dist/jora.esm.js'
  // ...
</script>

By default (for short path) a ESM version is exposing. For IIFE version a full path to a bundle should be specified. One of CDN services like unpkg or jsDelivr can be used:

  • jsDeliver

    <!-- ESM -->
    <script type="module">
    import jora from 'https://cdn.jsdelivr.net/npm/jora';
    </script>
    <!-- IIFE with an export `jora` to global -->
    <script src="https://cdn.jsdelivr.net/npm/jora/dist/jora.js"></script>
  • unpkg

    <!-- ESM -->
    <script type="module">
    import jora from 'https://unpkg.com/jora';
    </script>
    <!-- IIFE with an export `jora` to global -->
    <script src="https://unpkg.com/jora/dist/jora.js"></script>

API

import jora from 'jora';

// create a query
const query = jora('foo.bar');

// perform a query
const result = query(data, context);

See the details in Jora library API

Quick demo

Get npm dependency paths (as a tree) that have packages with more than one version:

import jora from 'jora';
import { exec } from 'child_process';

function printTree() {
    // see implementation in examples/npm-ls.js
}

exec('npm ls --all --json', (error, stdout) => {
    if (error) {
        return;
    }

    const npmTree = JSON.parse(stdout);
    const depsPathsToMultipleVersionPackages = jora(`
        $normalizedDeps: => dependencies.entries().({ name: key, ...value });
        $multiVersionPackages:
            ..$normalizedDeps()
            .group(=>name, =>version)
            .({ name: key, versions: value.sort() })
            .[versions.size() > 1];

        $pathToMultiVersionPackages: => .($name; {
            name,
            version,
            otherVersions: $multiVersionPackages[=>name=$name].versions - version,
            dependencies: $normalizedDeps()
                .$pathToMultiVersionPackages()
                .[name in $multiVersionPackages.name or dependencies]
        });

        $pathToMultiVersionPackages()
    `)(npmTree);

    printTree(depsPathsToMultipleVersionPackages);
});

Example of output:

[email protected]
├─ [email protected]
│  ├─ [email protected]
│  │  └─ [email protected] [more versions: 8.1.1]
│  ├─ [email protected]
│  │  └─ [email protected] [more versions: 3.0.4]
│  ├─ [email protected]
│  │  └─ [email protected]
│  │     └─ [email protected] [more versions: 5.2.1]
│  ├─ [email protected] [more versions: 20.2.4]
│  └─ [email protected]
│     └─ [email protected] [more versions: 20.2.4]
├─ [email protected]
│  ├─ @eslint/[email protected]
│  │  ├─ [email protected] [more versions: 5.2.0]
│  │  └─ [email protected] [more versions: 3.0.4]
...

See more examples in Complex Jora query examples