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

object-list

v1.4.0

Published

Treat arrays of objects like a db you can query.

Downloads

12

Readme

object-list Build Status

Treat arrays of objects like a db you can query. A single object from an object-list is called a record.

A common API for object collections

You may be scratching your head right now and wondering how this is different from Underscore, Lodash, or Rx Observables. Astute observations. The implementation will likely lean heavily on both Lodash and Rx Observables.

The difference is that this is intended to provide a universal facade for many types of object collections. It is an interface contract that will hopefully support a number of modular collection adapters. Read on for more details.

Status

Developer preview.

API Design Goals

Only a few of these design goals have been met in the current implementation, so read this section like everything is prefixed with "eventually..." See future.

  • A common API for object collections (e.g. arrays of objects). Adapters for:
    • Array
    • Immutable.js List
    • Rx Observable
    • Siren-Resource API
    • Mongo client
    • Redis client
  • object-lists are immutable. Instead of mutating the source data, a new object-list will be returned.
  • Completely modular. Enable require() at the function level similar to require('lodash/object/assign'), etc... Compatibility transforms for things that aren't required for hard dependencies should also be separate modules. Minimize browserify bundle weight.
  • Node or Browser.
  • Provide ES6 Array.prototype compatible API. Almost anything that takes an array as input should be able to take an object-list, as well, provided the API does not rely on array mutation.
  • Use ES6 and make compiled ES5 version available on npm.
  • Compatible with infinite streams and generators.
  • Should be capable of sync or async. Add .subscribe() method for async results.

Getting Started

Install:

$ npm install object-list

Use:

var list = require('object-list');

var records = [{a: 'a'}, {b: 'b'},{c: 'c'}];

list(records).getByKey('a'); // -> 'a'

.getByKey()

Take a key name and return the first value found. Returns a single value, not an array.

Usage

list(records).getByKey(key) -> val

var
records = [
  {a: 'a'},
  {b: 'b'},
  {c: 'c'}
],
expected = 'a',
copy = slice.call(records),

val = list(records).getByKey('a');

assert.equal(val, expected,
'should return the first value it finds.');

assert.equal(Array.isArray(val), false,
'should only return a single result, not an array.');

assert.deepEqual(records, copy,
'should not alter original list.');

.whitelist()

Take a list of keys and return a filtered list of records which contain those keys. Exclude any records that don't contain any of the whitelisted keys.

Usage

list(records).whitelist(whitelist) -> records

var
records = [
  {a: 'a'},
  {b: 'b'},
  {c: 'c'},
  {d: 'd', a: 'a'}
],
copy = slice.call(records),

whitelisted = list(records).whitelist(['a', 'c']),

expected = [
  {a: 'a'},
  {c: 'c'},
  {d: 'd', a: 'a'}
],

missing = list(expected).getByKey('b');

assert.deepEqual(whitelisted, expected,
'should contain all the expected keys.');

assert.strictEqual(missing, undefined,
'should not contain excluded keys');

assert.deepEqual(records, copy,
'should not alter original list.');

.concat()

Mix a list into a single record.

Usage

list(records).concat() -> record

var
records = [
  {a: 'a'},
  {b: 'b'},
  {c: 'c'},
  {c: 'override'}
],
copy = slice.call(records),

record = list(records).concat(),

expected = {
  a: 'a',
  b: 'b',
  c: 'override'
};

assert.deepEqual(record, expected,
'should combine records similar to Object.assign()');

assert.deepEqual(records, copy,
'should not alter original list.');

.where()

Select all records which match the given predicate pair.

Usage

list(records).where({key: value}) -> records

// Find all orders by customer email
var records = [
    {
      "id": "ci6r6aliv00007poxc2zgnjvf",
      "date": "2014-12-30 05:29:28",
      "billingEmail": "[email protected]",
      "firstName": "Dennis",
      "lastName": "Chambers",
      "lineItems": {
        "name": "Zildjian K Custom Organic Ride - 21\"",
        "sku": "h617xrh",
        "quantity": "1",
        "total": "379.95"
      }
    },
    {
      "id": "ci6r6aliv00007poxc2zgnjvf",
      "date": "2014-12-30 05:29:28",
      "billingEmail": "[email protected]",
      "firstName": "Carlos",
      "lastName": "Santana",
      "lineItems": {
        "name": "Gibson Memphis 1963 ES-335 TD - '60s Cherry 2014",
        "sku": "sc37x3m",
        "quantity": "1",
        "total": "3999.00"
      }
    },
    {
      "id": "ci6r6aliv00007poxc2zgnjvf",
      "date": "2014-12-30 05:29:28",
      "billingEmail": "[email protected]",
      "firstName": "Dennis",
      "lastName": "Chambers",
      "lineItems": {
        "name": "DW Collector's Series Metal Snare - 6.5\"x14\" Titanium 1mm",
        "sku": "sc37x3m",
        "quantity": "1",
        "total": "379.95"
      }
    }
  ],
  copy = slice.call(records),

  result = list(records).where({
    billingEmail: '[email protected]'
  }),

  expected = [
    {
      "id": "ci6r6aliv00007poxc2zgnjvf",
      "date": "2014-12-30 05:29:28",
      "billingEmail": "[email protected]",
      "firstName": "Dennis",
      "lastName": "Chambers",
      "lineItems": {
        "name": "Zildjian K Custom Organic Ride - 21\"",
        "sku": "h617xrh",
        "quantity": "1",
        "total": "379.95"
      }
    },
    {
      "id": "ci6r6aliv00007poxc2zgnjvf",
      "date": "2014-12-30 05:29:28",
      "billingEmail": "[email protected]",
      "firstName": "Dennis",
      "lastName": "Chambers",
      "lineItems": {
        "name": "DW Collector's Series Metal Snare - 6.5\"x14\" Titanium 1mm",
        "sku": "sc37x3m",
        "quantity": "1",
        "total": "379.95"
      }
    }
  ];


assert.deepEqual(result, expected,
  'should contain all the expected records.');

assert.equal(result.length, 2,
  'should not contain excluded records.');

assert.deepEqual(records, copy,
  'should not alter original list.');

Courses

This was written for the "Learn JavaScript with Eric Elliott" course series.

Students will get a series of short videos, lots of interactive lessons explaining concepts in-depth, the ability to help and learn from each other, and a lot more.

Here's a sneak peek at our members-only site:

Course homepage

Student profile

Learn JavaScript