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

wequ

v0.0.12

Published

A logical-operator-style array-searching utility

Downloads

3

Readme

wequ

npm version

A logical-operator-style array-searching utility.

Short for "We Query". Pronounced like "we koo".

features

  • It's stupid-fast - it turns a nice query object into a function that performs series of logical comparisons (the fastest way to match a query)
  • It's stupid-simple - I mean I think it's easier to follow than regular filter functions, especially if they are complicated
  • it's tiny - 641B gzipped, for web-build, and under 600B for the 2 different node-side versions (commonjs and mjs.) That is nutso. I got dog-pictures bigger than that (the one on this page is almost 100X bigger.)
  • pre-compute queries-functions for slight speedup (especially if you run it in multiple places)
  • It has a describe function that outputs the string of an ES6 function, so you can see how it works, more easily
  • use it in an easy-to-install CLI util (no nodejs needed) that is streaming (any length is cool in a unix-pipe!)
  • use it direclty in the browser, if you want
  • use it in nodejs

library

The basic usage of wequ goes like this:


// this is a query
const query = {
  published: true,

  and: {
    category: 2
  },

  nand: {
    preview: false
  },

  or: {
    id: [0, 1, 2, 3]
  },

  nor: {
    title: ['Bad', 'No'],
    stupid: true
  }
}

// this will describe the query as an ES6 function, so you can troubleshoot or just read it in another form
console.log(describe(query))

// pre-compute the query-function for use in other things
const q = wequ(query)

// honestly, this is pretty similar to describe(), but with a more laid-back ES5 chillwave type of vibe:
console.log(q.toString())

// get all the items that match the query
const subset = bigArrayOfObjects.filter(q)

// get the first item that matches the query
const single = bigArrayOfObjects.find(q)

// get an array of true/false for every record
const report = bigArrayOfObjects.map(q)

// get a true/false if every item matches your query
const report = bigArrayOfObjects.every(q)

// get a true/false if any of your items match your query
const report = bigArrayOfObjects.some(q)

Generally, I find it most useful to a.filter(q) for "get all matches" and a.find(q) for "get first match"

Any fields other than and|or|nor|nand in the top-level are merged into and. This allows for quick and queries for a few fields, which is my most common use-case.

It might take a second to get used to it, but each keyword is about the relationship the fields have to each other, like "if it's this or that or that" or "if this field is this and this other field is this". nand and nor are the same but "not matches" so nor is "this is not this or this other thing is not this" nand is "this is not this and this other thing is not this".

Using describe you will discover the function the query above generates is like this:

r => (
     ( r["category"] === 2 && r["published"] === true )
  && ( r["id"] === 0 || r["id"] === 1 || r["id"] === 2 || r["id"] === 3 )
  && ( r["title"] !== "Bad" || r["title"] !== "No" || r["stupid"] !== true )
  && ( r["preview"] !== false )
)

This is maybe a bit harder to read & maintain.

cli

I love jq, but the syntax can be a little complicated to get what you need, especially with escaping and stuff. I made the wequ CLI to address this, so I don't need to lookup syntax, I can just do what I do all over:

cat file.json | wequ '{ title: "COOL" }'

or

wequ '{ title: "COOL" }' < file.json

The syntax is regular javascript, in a string. stdin data should be a JSON array of objects, so you might still need jq to turn it into that.

If you do want to get super-jiggy with it, but just like js syntax better than jq:

wequ '{ [ (new Date()).getMonth() + 1 ]: "COOL" }' < file.json

to find an object with the current month-number as a key, set to "COOL".

or the other way:

wequ '{ month: (new Date()).getMonth() + 1 }' < file.json

to find records with month set to the current month-number.

You can require any installed npm-module (in node_modules in current directory) and do whatever nutso thing you want, in there.

Remember it's building a query-object, so dynamic field-names need to be in square-brackets

While we are talking about cool javascript CLI tools for finding what you need, I recommend flat. It's extremely useful for flattening complex objects (in a way that can be unflattened easily) and search for stuff. It pairs very nicely with wequ:

cat foo.json | flat | wequ '{ "deep.nested.object.is.no.problem.now": true }'

installation

Install the CLI tool a few ways:

  • Grab a ready-built CLI tool from the releases. No node or other dev-tools needed.
  • Install globally with npm i -g wequ and it will be in your path
  • Use npx for 0-install: npx wequ

library

You can get the wequ and describe functions, from above, in a few ways, depending on how you are doing things.

browser script tag

Kick it oldschool, for a quick demo:

<script src="https://unpkg.com/wequ"></script>

This adds window.wequ, which has wequ() and describe() in it.

browser modules

You can use es6 modules in modern browsers.

<script type="module">
import { wequ, describe } from 'https://unpkg.com/wequ@latest/dist/wequ.module.js'
</script>

Or with sick new import-maps:

<script type="importmap">
{
  "imports": {
    "wequ": "https://unpkg.com/wequ@latest/dist/wequ.module.js"
  }
}
</script>
<script type="module">
import { wequ, describe } from 'wequ'
</script>

These are pretty new features, but if you want them in your old-browser-supporting-projects try a polyfill. It doesn't add too much overhead, and the syntax is the wave of the future.

node

Install it in your project:

npm i wequ

It works with older commonjs style:

const { wequ, describe } = require('wequ')

or ES6 module (type: module or some sort of builder):

import { wequ, describe } from 'wequ'

deno

import { wequ, describe } from 'https://unpkg.com/wequ@latest/dist/wequ.module.js'

development

This is mostly just notes for myself.

To release, just tag with a version and push, and CI will do the rest.

npm version patch && git push --mirror