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

aggregationjs

v1.0.8

Published

Inspired by mongodb aggregation framework, aggregationjs its lightweight and easy tool to manipulate data.

Downloads

4

Readme

Aggregation JS

Inspired by mongodb aggregation framework, aggregationjs its a lightweight and easy tool to manipulate data.

Installation

npm install aggregationjs

Pipelines operators

Usage

const { aggregate } = require("aggregationjs");
const res = aggregate(data, pipeline);

// ES6
import { aggregate } from "aggregationjs";

$match

Filters the documents to pass only the documents that match the specified condition(s) to the next pipeline stage.

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [{ $match: { age: 35 } }]);

(res) => [
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
];

$groupBy

Groups the elements of the calling array according to the string values returned by a provided testing function. The returned object has separate properties for each group, containing arrays with the elements in the group.

const data = [
 { id: 1, name: "Jane", age: 20 },
 { id: 2, name: "Bill", age: 35 },
 { id: 3, name: "joe", age: 35 },
 { id: 4, name: "Dean", age: 40 },
]

const res = aggregate(data, [{ $groupBy: { age: 1 } }]);

res => {
  '20': [ { id: 1, name: 'Jane', age: 20 } ],
  '35': [
    { id: 2, name: 'Bill', age: 35 },
    { id: 3, name: 'joe', age: 35 }
  ],
  '40': [ { id: 4, name: 'Dean', age: 40 } ]
}

$sort

Sorts all input documents and returns them to the pipeline in sorted order.

const data = [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "joe", age: 35 },
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
];

const res = aggregate(data, [{ $sort: { age: 1 } }]);

(res) => [
  { id: 1, name: "Jane", age: 20 },
  { id: 3, name: "joe", age: 35 },
  { id: 2, name: "Bill", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

$limit

Limits the number of documents passed to the next stage in the pipeline.

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [{ $limit: { $count: 1 } }]);

(res) => [{ id: 1, name: "Jane", age: 20 }];

$skip

Skips over the specified number of documents that pass into the stage and passes the remaining documents to the next stage in the pipeline.

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [{ $skip: { $count: 1 } }]);

(res) => [
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

$format

Passes along the documents with the requested fields to the next stage in the pipeline

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [{ $format: { age: 1, name: 1 } }]);

(res) => [
  { age: 20, name: "Jane" },
  { age: 35, name: "Bill" },
  { age: 35, name: "Jane" },
  { age: 40, name: "Dean" },
];

$unwind

Deconstructs an array field from the input documents to output a document for each element. Each output document is the input document with the value of the array field replaced by the element.

const actors = [
  {
    id: 1,
    name: "John",
    age: 30,
    movies: [
      {
        id: 1,
        name: "The Shawshank Redemption",
        year: 1994,
      },
      {
        id: 2,
        name: "The Godfather",
        year: 1972,
      },
      {
        id: 3,
        name: "The Godfather: Part II",
        year: 1974,
      },
    ],
  },
];

const res = aggregate(data, [{ $unwind: { $path: "movies" } }]);

(res) => [
  {
    id: 1,
    name: "John",
    age: 30,
    movies: { id: 1, name: "The Shawshank Redemption", year: 1994 },
  },
  {
    id: 1,
    name: "John",
    age: 30,
    movies: { id: 2, name: "The Godfather", year: 1972 },
  },
  {
    id: 1,
    name: "John",
    age: 30,
    movies: { id: 3, name: "The Godfather: Part II", year: 1974 },
  },
];

$remove

Remove all element from an document that match the expression.

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [{ $remove: { id: 1 } }]);

(res) => [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "Jane", age: 35 },
  { id: 2, name: "Bill", age: 35 },
];

$math

$sum

Calculates and returns the collective sum of numeric values.

const users = [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "Jane", age: 35 },
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
];

const res = aggregate(users, [
  {
    $math: {
      $sum: {
        age: 1,
      },
    },
  },
]);

(res) => 130;

$avg

Returns the average value of the numeric values.

const users = [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "Jane", age: 35 },
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
];

const res = aggregate(users, [
  {
    $math: {
      $avg: {
        age: 1,
      },
    },
  },
]);

(res) => 35.5;

$min

Returns the minimum value.

const users = [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "Jane", age: 35 },
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
];

const res = aggregate(users, [
  {
    $math: {
      $min: {
        age: 1,
      },
    },
  },
]);

(res) => 20;

$max

Returns the maximum value.

const users = [
  { id: 4, name: "Dean", age: 40 },
  { id: 3, name: "Jane", age: 35 },
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
];

const res = aggregate(users, [
  {
    $math: {
      $avg: {
        age: 1,
      },
    },
  },
]);

(res) => 40;

Multiple pipeline

Pipelines can support multiple operators

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  { $match: { age: 35 } },
  {
    $format: {
      name: 1,
    },
  },
]);

(res) => [{ name: "Bill" }, { name: "joe" }];

Conditional operators

$eq

Returns all documents equals to

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $eq: {
        age: 35,
      },
    },
  },
]);

(res) => [
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
];

$ne

Returns all documents no equals to

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $ne: {
        age: 35,
      },
    },
  },
]);

(res) => [
  { id: 1, name: "Jane", age: 20 },
  { id: 4, name: "Dean", age: 40 },
];

$gt

Returns all documents greater than

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $gt: {
        age: 20,
      },
    },
  },
]);

(res) => [
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

$gte

Returns all documents greater than or equal to

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $gte: {
        age: 35,
      },
    },
  },
]);

(res) => [
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

$lt

Returns all documents ​​less than

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $lt: {
        age: 35,
      },
    },
  },
]);

(res) => [{ id: 1, name: "Jane", age: 20 }];

$lte

Returns all documents ​​less than or equal to

const data = [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
  { id: 4, name: "Dean", age: 40 },
];

const res = aggregate(data, [
  {
    $match: {
      $lte: {
        age: 35,
      },
    },
  },
]);

(res) => [
  { id: 1, name: "Jane", age: 20 },
  { id: 2, name: "Bill", age: 35 },
  { id: 3, name: "joe", age: 35 },
];

License

MIT

Support

For support, questions or recommendations | [email protected]