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

list-query

v0.1.0

Published

SQL-like querying on arrays

Downloads

1

Readme

List Query

Lightweight, dependency free node module for SQL Like querying on arrays of objects.

list query logo

Usage

Require the list-query npm package like so:

const select = require('list-query');

The module returns a function that when chained with the available methods will read in a SQL-like syntax.

For example:

const select = require('list-query');

const recentlyLoggedInAdmins = select('*')
  .from(users)
  .where('isAccountLocked')
  .is(false)
  .and('roleList')
  .contains('admin')
  .and('lastLogin')
  .isGreaterThan(1600473600)
  .and()
  .isLessThanOrEqualTo(1600473900)
  .run();

Query Set Up

Queries must be chained with the following structure:

  • Start with the select() function
  • Include a .from() method
  • .where() or .and() prefixing chained query methods
  • End with .run()
Example:
const select = require('list-query');

const recentlyLoggedInAdmins = select('*') // SELECT STATEMENT
  .from(users) // FROM METHOD INCLUDING ARRAY

  // QUERY METHODS
  .where('isAccountLocked')
  .is(false)

  .and('roleList')
  .contains('admin')

  .and('lastLogin')
  .isGreaterThan(1600473600)
  .and()
  .isLessThanOrEqualTo(1600473900)

  // ENDS WITH '.run()' METHOD
  .run();

Select

The select() function controls the format to return the query results and can take in the following types:

  • " * " - Return entire object
Example:
const select = require('list-query');

const locations = [
  { city: 'Dallas', state: 'Texas', zip: 75001 },
  { city: 'San Diego', state: 'California', zip: 22434 },
  { city: 'Seattle', state: 'Washington', zip: 98101 },
  { city: 'Houston', state: 'Texas', zip: 77001 },
];

const texasCities = select('*').from(locations).where('state').is('Texas').run();

// Expected: [{ city: 'Dallas', state: 'Texas', zip: 75001 }, { city: 'Houston', state: 'Texas', zip: 77001 }]
  • {String of Object Key} - Return a list of only the specified key's value .
Example:
const select = require('list-query');

const locations = [
  { city: 'Dallas', state: 'Texas', zip: 75001 },
  { city: 'San Diego', state: 'California', zip: 22434 },
  { city: 'Seattle', state: 'Washington', zip: 98101 },
  { city: 'Houston', state: 'Texas', zip: 77001 },
];
const texasCities = select('city').from(locations).where('state').is('Texas').run();

// Expected: ['Dallas', 'Houston']
  • {Array of Object Keys} - Returns an object containing only the specified keys
Example
const select = require('list-query');

const locations = [
  { city: 'Dallas', state: 'Texas', zip: 75001 },
  { city: 'San Diego', state: 'California', zip: 22434 },
  { city: 'Seattle', state: 'Washington', zip: 98101 },
  { city: 'Houston', state: 'Texas', zip: 77001 },
];
const texasCities = select(['city', 'zip']).from(locations).where('state').is('Texas').run();

// Expected: [ { city: 'Dallas', zip: 75001 }, { city: 'Houston', zip: 77001 }]

From

The .from() method provides the array source for the query (passed in as an argument). It must be included in the query chain.

Querying

Key Setters

.where() and .and() are key setter methods. These methods set the active key to examine for the next query method. All query methods should have a key setter called directly before it in the query chain.

While both methods are extremely similiar, for syntactic sugar .where() should be called before the first query method & .and() should be called before all following query methods.

Both functions take in a string that represents the object key to focus on for the next query. Optionally, if no value is passed to .and(), the focused key doesnt change.

Example
const select = require('list-query');

const recentlyLoggedInAdmins = select('*')
  .from(users)

  // where "isAccountLocked" is false
  .where('isAccountLocked') // Active key set to "isAccountLocked"
  .is(false) // Active key (isAccountLocked) is false

  // lastLogin must be > 1600473600 & <= 1600473900
  .and('lastLogin') // Active key set to "lastLogin"
  .isGreaterThan(1600473600) // Active key (lastLogin) > 1600473600
  .and() // Since no argument past, active key does not change
  .isLessThanOrEqualTo(1600473900) // Active key (lastLogin) <= 1600473900
  .run();

Query Methods

  • .is(val) - Any - Compared Value must equal provided value
    • Alias - equals(val)
  • .isNot(val) - Any - Compared Value must NOT equal provided value
    • Alias - doesNotEqual(val)
  • .startsWith(str) - String - Compared Value (as string) must start with provided value
  • .endsWith(str) - String - Compared Value (as string) must end with provided value
  • .isGreaterThan(n) - Number - Compared Value must be greater than provided value
  • .isLessThan(n) - Number - Compared Value must be less than provided value
  • .isGreaterThanOrEqualTo(n) - Number - Compared Value must be greater than or equal to provided value
  • .isLessThanOrEqualTo(n) - Number - Compared Value must be less than or equal to provided value
  • .within(arr) - Array - Compared value must match a value in the provided array
    • Alias - in(arr)
  • .contains(val) - Any - Compared Value (must be an array) must contain provided value

Limit

Optionally, you can limit the max number of query results with the .limit() method.

Example
const select = require('list-query');

const locations = [
  { city: 'Dallas', state: 'Texas', zip: 75001 },
  { city: 'San Diego', state: 'California', zip: 22434 },
  { city: 'Seattle', state: 'Washington', zip: 98101 },
  { city: 'Houston', state: 'Texas', zip: 77001 },
];
const cities = select('city').from(locations).where('state').isNot('California').limit(2).run();

// Expected: ['Dallas', 'Seattle']

Example Queries

const select = require('list-query');

const users = [
  {
    id: 123,
    username: 'johnsmith11',
    firstName: 'John',
    lastName: 'Smith',
    age: 25,
    isAccountLocked: false,
    lastLogin: 1600473700,
    roleList: ['user', 'moderator', 'admin'],
  },
  {
    id: 456,
    username: 'foobarz',
    firstName: 'Jane',
    lastName: 'Doe',
    age: 24,
    isAccountLocked: false,
    lastLogin: 1600473700,
    roleList: ['user', 'moderator'],
  },
  {
    id: 903,
    username: 'joejoe',
    firstName: 'Joe',
    lastName: 'Bar',
    age: 25,
    isAccountLocked: false,
    lastLogin: 1600473900,
    roleList: ['user', 'moderator', 'admin'],
  },
  {
    id: 211,
    username: 'amanda92',
    firstName: 'Amanda',
    lastName: 'Baz',
    age: 24,
    isAccountLocked: true,
    lastLogin: 1600472500,
    roleList: ['user'],
  },
  {
    id: 115,
    username: 'billybills',
    firstName: 'Bill',
    lastName: 'Baz',
    age: 23,
    isAccountLocked: true,
    lastLogin: 1600473650,
    roleList: ['user', 'moderator', 'admin'],
  },
];

// Passing "*" into "select" will return entire object
const recentlyLoggedInAdmins = select('*')
  .from(users) // From the users array
  .where('isAccountLocked')
  .is(false)
  .and('roleList')
  .contains('admin')
  .and('lastLogin')
  .isGreaterThan(1600473600)
  .and()
  .isLessThanOrEqualTo(1600473900)
  .run();

// EXPECTED:
//
// [
//   {
//     id: 123,
//     firstName: 'John',
//     lastName: 'Smith',
//     age: 25,
//     isAccountLocked: false,
//     lastLogin: 1600473700,
//     roleList: [ 'user', 'moderator', 'admin' ]
//   },
//   {
//     id: 903,
//     firstName: 'Joe',
//     lastName: 'Bar',
//     age: 25,
//     isAccountLocked: false,
//     lastLogin: 1600473900,
//     roleList: [ 'user', 'moderator', 'admin' ]
//   }
// ]

// Passing an array of strings into "select" will return an object containing the specified keys
const lockedAccounts = select('username').from(users).where('isAccountLocked').is(true).run();

// EXPECTED:
//
// [ 'amanda92', 'billybills' ]

// Passing a single string into "select" will return only the specified key's value
const jNamesExcludingJohn = select(['firstName', 'lastName'])
  .from(users)
  .where('firstName')
  .startsWith('J')
  .and()
  .isNot('John')
  .run();

// EXPECTED:
//
// [
//   { firstName: 'Jane', lastName: 'Doe' },
//   { firstName: 'Joe', lastName: 'Bar' }
// ]