Git Product home page Git Product logo

fgroup's Introduction

fgroup

Fluent interface for grouping object arrays

Installation

npm install fgroup --save

Usage

var group = require('fgroup');

This creates and returns a function. Calling this function sets up the chaining feature and creates another function that is bound to your grouping parameters. This is the function you call on your dataset.

The following is a grouping function that is useful for the sample data included in this project.

var group = require('fgroup');

var groupByRegion = group('people')        // the name of each array of grouped objects
        .by('regionId')                    // the property by which to group
        .include('regionId', 'regionName') // what to include in each group
        .details('salesRep', 'sales')      // what to include in each of the grouped objects
        .notnull('salesRep');              // skip it if this property is null

var data = require('./sample');

var groupedData = groupByRegion(data);

The notnull parameter is useful when dealing with data retrieved from a database using on an outer join. In that case, you will typically get back data that has one parent record with the child fields set to null. You would want to group this as a parent record with an empty children array and not a parent record with a child array of length one and that one grouped object containing only null values.

The chained parameter functions can be called in any order. They can also be omitted. If include is not called, then only the grouping property is included. If details is not called, then all properties in the grouped object are included. If you forget to call the by function, then no grouping is done and you will get back your input data.

The intent is to create a grouping function once in your code and then reuse it. You could create it dynamically, but typically, when dealing with data from a database, you will know what columns you need to group for each select.

As a callback

If the created grouping function is called with another function, then it is assumed to be a callback function having the standard callback(err, data) signature. The grouping function now becomes the new callback function. Here is a SQLite example:

function getRegionalSales(db, callback) {
    db.all('SELECT * FROM [regionalSalesView]', groupByRegion(callback));
    // Assume this view is a left outer join between regions and sales staff.
}

This is a convenience feature but it does add to the overall readability of the code and makes the intent clear.


I hope you find fgroup useful. This is unlicensed software. Just do with it what you want or need.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.