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

@kitmi/jsonx

v1.3.0

Published

JSON Expression Syntax

Downloads

175

Readme

@kitmi/jsonx

JSON Expression Syntax

Installation

To install @kitmi/jsonx, run the following command:

bun install @kitmi/jsonx

Or if you're using npm:

npm install @kitmi/jsonx

Rules

  • Each jsx object can only has one $operator at a level

Usages

  • $size
Jsx.evaluate([1, 2, 3], '$size'); // 3
  • $sum
Jsx.evaluate([1, 2, 3], '$sum'); // 1+2+3 = 6
  • $type
Jsx.evaluate([1, 2, 3], '$type'); // array
  • $findIndex, $findKey
Jsx.evaluate([1, 2, 3], { $findIndex: { $eq: 2 } }); // 1
Jsx.evaluate(
    {
        key1: 10,
        key2: 20,
        key3: 30,
    },
    { $findKey: { $eq: 20 } }
); // 'key2'

// from index
Jsx.evaluate([1, 2, 2], { $findIndex: [{ $eq: 2 }, 2] }); // 2
  • $find
Jsx.evaluate([1, 3, 2], { $find: { $and: [{ $gt: 1 }, { $lt: 3 }] } }); // 2
Jsx.evaluate(
    {
        key1: 10,
        key2: 20,
        key3: 30,
    },
    { $find: { $gt: 15 } }
); // 20

// from expr as index
Jsx.evaluate(
    {
        key1: 1,
        key2: 3,
        key3: 4,

        startFrom: 2,
    },
    { $find: [{ $gt: 1 }, { $expr: '$root.startFrom' }] }
); // 4
  • $if
let obj = {
    key1: 1.11,
};

// $if: [ <condition-jsx>, <then-jsx>, <else-jsx> ]
Jsx.evaluate(obj, {
    $if: [{ $match: { key1: { $gt: 1 } } }, { $value: 'positive' }, { $value: 'non-positive' }],
}).should.be.eql('positive');

Jsx.evaluate(obj, {
    $if: [{ $match: { key1: { $gt: 2 } } }, { $value: 'positive' }, { $value: 'non-positive' }],
}).should.be.eql('non-positive');
  • $castArray

  • $add | $+, $sub | $-, $mul | $*, $div | $/, $mod | $%, $pow | $^

Jsx.evaluate(10, { $mul: { $expr: [{ $value: 10 }, { $add: 5 }] } }); // 150
Jsx.evaluate(10, { $mul: 15 }); // 150
  • $keys, $values, $pairs, $filterNull

  • $toArray

Jsx.evaluate({ key: 'value' }, '$toArray'); // [{ name: 'key', value: 'value' }]
Jsx.evaluate({ key: 'value' }, { $toArray: { myKey: '$key', myValue: '$this' } }); // [{ myKey: 'key', myValue: 'value' }]
  • $pick, $omit, $group, $sort, $reverse

  • $concat, $join, $merge

  • $filter, $remap

  • $set | $value

  • $addItem | $append

Jsx.evaluate([1, 2, 3], { $append: 4 }); // [1, 2, 3, 4]
Jsx.evaluate({ key1: 1 }, { $append: ['key2', 2] }); // { key1: 1, key2: 2 }
  • $assign
Jsx.evaluate({ key1: 1 }, { $assign: { key2: 2 } }); // { key1: 1, key2: 2 }
Jsx.evaluate(
    {
        key1: 1,
        key2: 2,
    },
    {
        $assign: {
            key2: {
                $expr: {
                    $mul: 10,
                },
            },
        },
    }
); // { key1: 1, key2: 20 }

Wrong usage

// If you want to check if key1 is greater than 2, below usage is wrong
// The first element will return { key1: false } which is treated as true, it will throw an error to avoid mistaken usage
Jsx.evaluate(
    {
        key1: 1.5,
    },
    {
        $if: [{ key1: { $match: { $gt: 2 } } }, { $value: 'positive' }, { $value: 'non-positive' }],
    }
).should.be.eql('non-positive');

// Correct usage should be
Jsx.evaluate(
    {
        key1: 1.5,
    },
    {
        $if: [{ $match: { key1: { $gt: 2 } } }, { $value: 'positive' }, { $value: 'non-positive' }],
    }
).should.be.eql('non-positive');

License

  • MIT
  • Copyright (c) 2023 KITMI PTY LTD