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

cuddy

v0.3.0

Published

An aggregation pipeline built in a functional programming style that lets you search, group, order and transform a collection.

Downloads

146

Readme

cuddy

cuddy is an aggregation pipeline built in a functional programming style. If you ever wanted to search through a collection using logical operators, sort, group, transform and return only the fields you care about, then cuddy is for you.

Npm version Build status Node Version License

Install

This module is distributed via npm which is bundled with node and should be installed as one of your project's dependencies:

npm install cuddy --save or yarn add cuddy

Usage

Import cuddy and create the query.

import cuddy from 'cuddy';
import { copyFrom } from 'cuddy/transform/helpers';

const query = {
  fields: ['title', 'permalink', 'runTime'],
  match: {
    in: { genre: 'Comedy', cast: 'Adam Sandler' },
    eq: { lang: 'en' }
  },
  orderBy: {
    reviews: 'asc'
  },
  groupBy: 'director',
  transform: {
    permalink: copyFrom('url')
  },
  limit: 2,
  skip: 1
};

The above query translates into:

  • I want all the items where the genre contains Comedy and cast contains Adam Sandler and the lang is en.
  • I want only 2 results and I want to skip the first item in the list.
  • I want to order the items by reviews in ascending order.
  • I want to group the items by director.
  • I want to create a new field called permalink and set its value to that of the url field.
  • From all the fields of each item, I only want the title, permalink and runTime.

Next, build the pipeline and aggregate your data.

const { aggregate } = cuddy(query, data);
const results = aggregate();

Depending on the shape of your data, the results could look like this:

{
  "Robert Smigel": [
    {
      "title": "The Week Of",
      "permalink": "https://www.imdb.com/title/tt6821012",
      "runTime": 116
    }
  ]
}

For more query examples, see the Books test cases and the associated Books collection.

If it looks like mongodb's query language, it's because lots of inspirations came from there. :)

Stages

The aggregation pipeline contains the following stages:

  • Matching. Filter out any items that do not match the criteria.
  • Sorting. Sort the items in the specified order.
  • Pagination. Limit the number of results and/or skip over the first results.
  • Grouping. Group the items by a specified field.
  • Count. Count the items by a specified field.
  • Project. Limit the fields that are returned for each item.
  • Transform. Transform existing properties and add new properties to the matched items.

Pipeline functions

Once you've built your pipeline, you can use it to aggregate your data and return results. Depending on whether you want one result, all of them or just a count of the total results, you will need to call the appropriate method:

  • aggregate() will return all the results from your query.
  • first() will return the first result in the list of results from your query.
  • last() will return the last result in the list of results from your query.
  • count() will return the total number of results from your query.
  • explain() will return an object explaining the set of steps resulted from your query. For details on the output, see the Explanation section.

Why cuddy?

noun

cud·​dy | \ ˈku̇-dē

Synonyms: donkey (British slang)

The donkey is used as a working animal and is considered the cheapest form of labor.

Contributing

  • Run tests with npm run test or yarn test.
  • Run the lint with npm run lint or yarn lint.

For details, check out the Contributing guide.

LICENSE

MIT