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

derived

v0.3.3

Published

Instant views and derived indexes for Javascript objects.

Downloads

29

Readme

Objects with derived indexes!

Build Status NPM Link

What does it do?

It creates automatic, synchronous and always-updated "views" for your data. You instantiate a main data object and define functions for creating derived indexes on it. Later you can easily access the data on the derived indexes.

It reduces bloat, verbose value getters and automatizes data organization in the context of your app. Perhaps others would call this "a database". This name would be misleading, but the power of automatic synchronous subindexes cannot be overstated.

Example

(view and fiddle with this example on Tonic)

const assert = require('chai').assert
const D = require('derived')

/* instantiate your data fundamentals -- or just begin from zero with `new D()` */
const data = new D({
  '#1': {
    name: 'shangri-la',
    zipcode: 27498,
    inhabitants: [{
      name: 'catherine',
      birthday: '11-17'
    }]
  },
  '#2': {
    name: 'el dorado',
    zipcode: 69712,
    inhabitants: [{
      name: 'pizarro',
      birthday: '04-01'
    }]
  },
  '#3': {
    name: 'atlantis',
    zipcode: 18315,
    inhabitants: [{
      name: 'plato',
      birthday: '07-18'
    }]
  },
  '#4': {
    name: 'avalon',
    zipcode: 37851
  }
})

/* reindex the data on the go, for much faster and easy access later */
data.derived('cityId', (k, v) => [v.name, k])

/* now you can access it easily, no need for costly searches on independent index management */
assert.equal(data.cityId['shangri-la'], '#1')

/* add data using super useful methods (you can't use normal methods, it's a Javascript limitation) */
let cityId = data.cityId
data.push([cityId.avalon, 'inhabitants'], {name: 'arthur', birthday: '12-30'})
data.push([cityId.avalon, 'inhabitants'], {name: 'morgana', birthday: '12-31'})

/* create multiple derived indexes */
data.derived('personZip', function (cityId, cityData) {
  if (cityData.inhabitants) {
    cityData.inhabitants.forEach(person => {
      this.emit(person.name, cityData.zipcode)
    })
  }
})

/* no more `var personZip = zipcodesIndex[personsIndex[personId]]` or other verbose tricks
   access your data directly */
let personzip = data.personZip
assert.equal(personzip['catherine'], 27498)
assert.equal(personzip['arthur'], 37851)
assert.equal(personzip['morgana'], 37851)

/* indexes can be of anything (and they don't need names): */
let birthdaysByMonth = data.derived(function (_, city) {
  if (city.inhabitants) {
    city.inhabitants.forEach(person => {
      let month = parseInt(person.birthday.split('-')[0], 10)
      this.emit(month, person.name)
    })
  }
})
assert.deepEqual(birthdaysByMonth.getAll(12), ['arthur', 'morgana'])

/* everything changes synchronously and automatically whenever you update the main data source */
data.set(`${cityId['atlantis']}.inhabitants.0.birthday`, '01-01')
assert.equal(birthdaysByMonth[1], 'plato')

Other

  • The derivation functions are inspired by CouchDB. Not equal, though, but the emit thing... well, it doesn't matter.
  • These getters and setters only work on the main data source, the data in derived indexes is read-only and can be accessed through normal Javascript syntax.
  • Other features are planned (please say what would you find more useful):
    • immutable main data source
    • immutable derived indexes
    • queryable derived indexes, returning arrays of values sorted by emitted keys
    • derived indexes based on the entire data object, instead of in each of its keys
    • derived indexes on sublevels