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

arrayql-js

v1.1.3

Published

Map and filter your array of objects

Downloads

18

Readme

📦🔍 A "GraphQL" for arrays of objects

Installation

npm i arrayql-js

yarn add arrayql-js

Get started

arrayQL(array, queryOptions)

array: T[]

An array of objects.

queryOptions: QueryOptions<T>

keys: BooleanKeys<T> - selects which keys will be in the output. They must be boolean, it doesn't matter if they're true or false.

where?: (object: T) => boolean: - (optional) filter callback that recieves each object and returns a boolean expression.

Considering GraphQL queries features, arrayQL is recursive, which means that you can have objects and arrays nested and it'll work for them as well. In below example, friends is also an array of objects. Attention: array of primitive values, like keywords which is an array of strings, must be selected as boolean, not as QueryOptions.

import { arrayQL } from "arrayql-js";

const normalizedUsers = arrayQL(users, {
  keys: {
    name: true,
    id: true,
    keywords: true,
    address: {
      city: true,
      country: true,
    },
    friends: {
      where: ({ name }) => name.includes("Smith"),
      keys: { id: true },
    },
  },
  where: ({ age }) => age > 23,
});

Don't worry, arrayQL has a nice type definition, it will infer the shape of your object, then your editor will do the rest.

With ES6

You can obtain the same result with usual .map() and .filter(). But it is a bit more complex, especially when things get nested 😅.

const withES6 = users
  .filter(({ age }) => age > 23)
  .map(({ name, id, keywords, address: { city, country }, friends }) => ({
    name,
    id,
    keywords,
    address: { city, country },
    friends: friends
      .filter(({ name }) => name.includes("Smith"))
      .map(({ id }) => ({ id })),
  }));

Now you know how to start, just go and query your arrays 😃.

Contribute

You are able to share your ideas or propose changes/features. Some problems are open to get a resolution:

  • [ ] Infer the types of the returned array, if possible, of course.
  • [ ] Check input errors (invalid object | array structure).
  • [ ] 🤔 Build a mutation option. Am I too crazy? I mean, who's gonna use it, anyway?

Let's say you want to help, one thing is important: the root index.d.ts file has to be manually copied to lib/ folder after TS to JS compilation with tsc. 🤷‍♂️ Let's change this as well, I'm not any tsconfig.json master. But before doing this, just yarn the project dependencies and don't forget to test your implementations 😎.