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 🙏

© 2026 – Pkg Stats / Ryan Hefner

js-memory-record

v0.0.28

Published

A simple library that handles a few records easily

Readme

MemoryRecord

npm version GitHub version

Description

A simple library that handles a few records easily.

Install

npm install js-memory-record

Simplest Example

class Gender extends MemoryRecord {
  static get define() {
    return [
      { key: "male"   },
      { key: "female" },
    ]
  }
}

gender = Gender.fetch("male")
gender.name          // => "male"
gender.code          // => 0

Gender.fetch(0).name // => "male"

More Example

Records Define

Return an array of Hash structures in define()

import MemoryRecord from 'js-memory-record'

export default class Fruit extends MemoryRecord {

  // Record definition.

  static get define() {
    return [
      { key: "apple",  name: "Poison Apple", price: 120, },
      { key: "melon",  name: "Green Melon",  price: 800, },
      { key: "peach",  name: "Pink Piece",   price: 200, },
    ]
  }

  // Define an instance method referencing an attribute.
  // Define it when accessing other than key, name and other attributes

  get full_name() {
    return `${this.name} (Now ${this.special_price} Gold)`
  }

  get special_price() {
    return Math.ceil(this.price / 2)
  }
}

fetch(key) - Key Access

Basic access by this method.

Fruit.fetch("apple").key        // => "apple"
Fruit.fetch("apple").name       // => "Poison Apple"
Fruit.fetch("apple").code       // => 0
Fruit.fetch("apple").index      // => 0

fetch(code) - Code Access

Code is something like database ID. Allocate in order from 0. Use it when you want to access by special index.

Fruit.fetch(0).name                     // => "Poison Apple"
Fruit.fetch(0) === Fruit.fetch("apple") // => true

Attributes can be referred to as properties

Fruit.fetch("apple").price      // => 120

Additional Defined Instance Methods

Define the Instance Metod freely and return the attributes in an easy-to-use form. This part is one of the merits of introducing this library.

Fruit.fetch("apple").special_price // => 60
Fruit.fetch("apple").full_name     // => "Poison Apple (Now 60 Gold)"

Unknown Key Access

There is no lookup() exception.

Fruit.lookup("grape")           // => null

In case of fetch() we will throw an exception.

Fruit.fetch("grape")            // (Throw Error)

The error message of the exception is displayed as follows.

Error: Fruit.fetch("grape") does not match anything
keys: ["apple","melon","peach"]
codes: [0,1,2]

The cause of the error is displayed in an easy-to-understand manner.

Array Access

Fruit.values                    // => [{...}, {...}, {...}]
Fruit.values.map(e => e.key)    // => ["apple", "melon", "peach"]

Other Class Methods

Fruit.keys      // => ["apple", "melon", "peach"]
Fruit.codes     // => [0, 1, 2]
Fruit.names     // => ["Poison Apple", "Green Melon", "Pink Piece"],

We do not use this class methods much. But it may be useful for debugging.

code can be explicitly specified

static get define() {
  return [
    { code: 1, key: "apple", },
    { code: 2, key: "melon", },
    { code: 4, key: "peach", },
  ]
}
Fruit.codes    // => [1, 2, 4]

This is like managing database ID yourself. We do not recommend it. It is only useful if you need consistency with old data.

How to reset the array of records later

Replace the entire record internally held. I do not recommend this method much. But it may be useful in emergency.

Fruit.memory_record_reset([
  { key: "foo" },
  { key: "bar" },
])

Fruit.fetch("foo").key    // => "foo"

Build Setup

# install dependencies
npm install

# build for production with minification
npm run build

# run unit tests
npm test