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

olap-cube

v1.0.0

Published

is an hypercube of data

Downloads

57

Readme

OLAP-cube

is an hypercube of data

Description | Installation | API | License

NPM version Build Status JavaScript Style Guide

Description

An OLAP cube is a multidimensional array of data you can explore and analyze. Here you will find an engine that could feed a graphic viewer.

Installation

Using npm

With npm do

npm install olap-cube

Using a CDN

Add this to your HTML page

<script src="https://unpkg.com/sql92-json/dist/olap-cube.min.js"></script>

API

All code in this section is run and tested in this single file. Note also that

  1. Everything is immutable, all attributes are static.
  2. Operators are chainable and they always return a brand new instance.

new Table({ dimensions, fields, points, data })

  • @param {Object} arg
  • @param {Array} arg.dimensions
  • @param {Array} arg.points
  • @param {Array} arg.fields
  • @param {Array} arg.data in the format data[pointIndex][fieldIndex]
const Table = require('olap-cube').model.Table

const table = new Table({
  dimensions: ['year', 'month'],
  fields: ['revenue'],
  points: [[2016, 'Jan']],
  data: [[100]]
})

console.log(table) // Table {
                   //   dimensions: ['year', 'month'],
                   //   fields: ['revenue']
                   // }

table.structure

Attribute structure holds necessary information to clone a table excluding its data.

Create an empty table

const emptyTable = new Table(table.structure)

table.dimensions

The (hyper)cube dimensions.

One common dimension in Business Intelligence is time: it can have different granularities, like year, month, day, etc.

console.log(table.dimensions) // [ 'year', 'month' ]

table.fields

The names of the data fields.

console.log(table.fields) // [ 'revenue' ]

table.header

Attribute header concatenates dimension names and field names.

console.log(table.header) // ['year', 'month', 'revenue']

table.addRows({ header: [key1, key2, ...], rows: [row1, row2, ...]})

Add a set of rows to the table.

  • @param {Object} data
  • @param {Array} data.header
  • @param {Array} data.rows
  • @returns {Object} table

Every row is an object which attributes are either a dimension or a field.

const table2 = emptyTable.addRows({
  header: ['year', 'month', 'revenue'],
  rows: [
    [ 2015, 'Nov', 80 ],
    [ 2015, 'Dec', 90 ],
    [ 2016, 'Jan', 100 ],
    [ 2016, 'Feb', 170 ],
    [ 2016, 'Mar', 280 ],
    [ 2017, 'Feb', 177 ],
    [ 2017, 'Apr', 410 ]
  ]
})

table.data

Attribute data holds the facts of the table.

console.log(table2.data) // [[ 80 ],
                         //  [ 90 ],
                         //  [ 100 ],
                         //  [ 170 ],
                         //  [ 280 ],
                         //  [ 177 ],
                         //  [ 410 ]]

table.rows

Attribute rows holds the dimensions and the facts of the table.

console.log(table2.rows) // [[ 2015, 'Nov', 80 ],
                         //  [ 2015, 'Dec', 90 ],
                         //  [ 2016, 'Jan', 100 ],
                         //  [ 2016, 'Feb', 170 ],
                         //  [ 2016, 'Mar', 280 ],
                         //  [ 2017, 'Feb', 177 ],
                         //  [ 2017, 'Apr', 410 ]]

table.points

The points are an ordered set of coordinates.

In this case you can see 6 points with coordinates:

  1. year
  2. month
console.log(table2.points) // [[ 2015, 'Nov' ],
                           //  [ 2015, 'Dec' ],
                           //  [ 2016, 'Jan' ],
                           //  [ 2016, 'Feb' ],
                           //  [ 2016, 'Feb' ],
                           //  [ 2017, 'Apr' ]]

table.slice(dimension, filter)

Slice operator picks a rectangular subset of a cube by choosing a single value of its dimensions.

  • @param {String} dimension
  • @param {*} filter
  • @returns {Object} table

Consider the following example, where a slice with 2016 year is created.

const table3 = table2.slice('year', 2016)

console.log(table3.points) // [[ 2016, 'Jan' ],
                           //  [ 2016, 'Feb' ],
                           //  [ 2016, 'Mar' ]]

console.log(table3.data) // [[ 100 ],
                         //  [ 170 ],
                         //  [ 280 ]]

table.dice(selector)

Dice operator picks a subcube by choosing a specific values of multiple dimensions.

  • @param {Function} selector
  • @returns {Object} table

Consider the following example, where a dice excluding one month is created.

const onlyFebruary = (point) => point[1] !== 'Feb'

const table4 = table2.dice(onlyFebruary)

console.log(table4.points) // [[ 2015, 'Nov' ],
                           //  [ 2015, 'Dec' ],
                           //  [ 2016, 'Jan' ],
                           //  [ 2016, 'Mar' ],
                           //  [ 2017, 'Apr' ]]

console.log(table4.data) // [[ 80 ],
                         //  [ 90 ],
                         //  [ 100 ],
                         //  [ 280 ],
                         //  [ 410 ]]

table.rollup(dimension, fields, aggregator, initialValue)

A roll-up involves summarizing the data along a dimension. The summarization rule might be computing totals along a hierarchy or applying a set of formulas such as "profit = sales - expenses".

  • @param {String} dimension
  • @param {Array} fields
  • @param {Function} aggregator
  • @param {*} initialValue that will be passed to Array.prototype.reduce().
  • @returns {Object} table
const table5 = new Table({
  dimensions: ['continent', 'country'],
  fields: ['numStores']
})

// NOTA BENE: Remember that tables are immuTables ☺.
const table6 = table5.addRows({
  header: [ 'continent', 'country', 'numStores' ],
  rows: [
    [ 'Europe', 'Norway', 20 ],
    [ 'Europe', 'Denmark', 48 ],
    [ 'Europe', 'Germany', 110 ],
    [ 'Europe', 'Portugal', 17 ],
    [ 'Asia', 'China', 280 ],
    [ 'Asia', 'Russia', 161 ],
    [ 'Asia', 'Thailand', 120 ]
  ]
})

// Input tables and rolled up table has only one field,
// with the same name: numStores.
// Actually the aggregator is a reducer that will receive in input an
// array of fields from the input table, and will output an array of
// fields to the rolled up table.
const summation = (sum, value) => {
  return [sum[0] + value[0]]
}

const initialValue = [0]

const table7 = table6.rollup('continent', ['numStores'], summation, initialValue)

console.log(table7.rows) // [[ 'Europe', 195 ],
                         //  [ 'Asia', 561 ]]

License

MIT