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

javascript-array-matrix

v0.0.7

Published

A JavaScript module for storing and querying multi-dimensional or n-way array data

Downloads

226

Readme

JavaScript Array Matrix

A lightweight and performant data structure for storing JavaScript objects in an n-way or n-order array matrix.

Array Matrix Demo

Table of contents

Getting Started

First, add the module to your project:

npm i -S javascript-array-matrix

Import ArrayMatrix into your project:

import ArrayMatrix from 'javascript-array-matrix';

OR

const ArrayMatrix = require('javascript-array-matrix');

Making a Matrix

To create a new array matrix, you instantiate a new ArrayMatrix object with a configuration object:

const myMatrix = new ArrayMatrix(configs);

Configuration Properties

data Property

type: {Array}

Required

This is an array of objects that will be parsed and organized into the array matrix. The objects do not have to match exactly, but each object must have the properties corresponding to the orders passed in as arguments.

orders Property

type: {Array}

Required

Each string in they array must correspond to a key or property on the objects passed in the data argument Array.

The more properties that are provided, the higher the order or rank of the matrix, and more dimensions in the array. You should provide the minimum number of orders required to map each object to a cell in the array.

Example: if you are selling a T-shirt, you would want two orders: size and color. When you know the size and color of the T-shirt, you would use those values to access the product in the Array Matrix, allowing you to get the sku, price, availability, etc.

If there are multiple T-shirts on a single page, you may want to use three orders: size, color, and manufacturer. Or maybe size, color, and style number. The important take-away is that you have the minimum number of orders/properties necessary so that each maps to at most 1 object per cell.

There will be situations where you may have 0 objects per cell, and that is okay. The Array Matrix will return null for those cells. (ex.: Blue T-shirts in Small, but not Red T-shirts in Small).

Example Matrix

const matrixData = [
  { color: 'Blue', size: 'Small', sku: '123' },
  { color: 'Blue', size: 'Medium', sku: '124' },
  { color: 'Red', size: 'Medium', sku: '125' },
  { color: 'Blue', size: 'Large', sku: '126' },
  { color: 'Red', size: 'Large', sku: '127' },
  { color: 'Green', size: 'Large', sku: '128' }
];

const matrixOrders = ['color', 'size'];

const matrix = new ArrayMatrix({
  data: matrixData,
  orders: matrixOrders
});

Querying Data

getEntry

Gets a single entry from the matrix at the provided points.

Parameters

points {Object}: an object with keys corresponding to the orders or axes of the matrix, and the value corresponding to the label or name of the point in the matrix.

Using our Example Matrix:

const chosenTShirt = matrix.getEntry({ size: 'Medium', color: 'Red' });
// chosenTshirt points to { color: 'Red', size: 'Medium', sku: '125' }

NOTE: When querying for an entry, the number of keys in the points argument object needs to equal the number of orders present in the matrix.

getDimension

Gets multiple entries from a dimension in the matrix from the provided points. The missing axis/points is the dimension that is returned.

Parameters

points {Object}: an object with keys corresponding to the orders or axes of the matrix, and the value corresponding to the label or name of the point in the matrix.

Unlike getEntry, getDimension takes a points argument Object with the number of keys one less than the number of orders present in the matrix.

Using our Example Matrix:

const allBlueShirts = matrix.getDimension({ color: 'Blue' });
// allBlueShirts is [
//    { color: 'Blue', size: 'Small', sku: '123' },
//    { color: 'Blue', size: 'Medium', sku: '124' },
//    { color: 'Blue', size: 'Large', sku: '126' }
// ];

const allSmallShirts = matrix.getDimension({ size: 'Medium' });
// allSmallShirts is [
//    { color: 'Blue', size: 'Medium', sku: '124' },
//    { color: 'Red', size: 'Medium', sku: '125' },
// ];

NOTE: Currently ArrayMatrix only supports returning 1 dimension at a time. Adding functionality to return more than one dimension is being reviewed and undergoing performance tests.

For examples on how this can scale to use 3, 4, ... n level orders, see our Demo.

Additional Methods

getAxes

NOTE: axes is the plural of axis, and is not the the tool used by lumberjacks or weapon used by Medieval Vikings.

Gets the axes and named points along those axes.

Using our Example Matrix:

const matrixAxes = matrix.getAxes();
// matrixAxes is {
//    color: ['Blue', 'Red', 'Green'],
//    size: ['Small', 'Medium', 'Large']
// };

getAxisPoints

Gets the points along an axis and returns the named points.

Parameters

axis {String}: The name of the axis whose named points you want.

Using our Example Matrix:

const axisPoints = matrix.getAxisPoints('color');
// axisPoints is ['Blue', 'Red', 'Green'];

debug

Returns all the data stored in the array matrix - matrix, props, axes.

Demo and Performance

For more advanced demonstration of different 2, 3, and 4 order array matrices, the App has examples. Running the project locally starts the demo application, where you can play with queries and see performance results as well.

Here is the Demo.

Dev Workflow

  • npm run dev starts a local development server, opens the dev page with your default browser, and watches for changes via livereload
  • npm run build compiles commonjs and ES modules and places them in the dist directory
  • npm test runs test using Jest + Enzyme.