nest-by-tuple
Groups an array/object by property values or callback with support for additional metadata
Last updated 5 years ago by rstacruz .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install nest-by-tuple 
SYNC missed versions from official npm registry.

nest-by-tuple

Groups an array/object by property values or callback and supports additional metadata

Works like nest-by, but allows you to embed more metadata. Also see group-by or lodash.groupBy.

Status

Usage

nestByTuple(object or array, property, [...properties])

property is typically a function that returns an array in the structure of:

[ indexString, metadata ]

Where indexString is a string that the group will be indexed by, and metadata will be an Object that has data about that group.

Arrays

genders = { m: 'Male', f: 'Female' }

list =
  [ { name: 'Marge', gender: 'f' },
    { name: 'Homer', gender: 'm' },
    { name: 'Bart', gender: 'm' } ]

result = nestByTuple(list,
  (person) => [ person.gender, { genderName: genders[person.gender] } ])

//  f:
//    genderName: 'Famale',
//    values: [ { name: 'Marge', gender: 'f' } ]
//  m:
//    genderName: 'Male',
//    values: [
//     { name: 'Homer', gender: 'm' },
//     { name: 'Bart', gender: 'm' } ]

Objects

list =
  { marge: { name: 'Marge', gender: 'f' },
    homer: { name: 'Homer', gender: 'm' },
    bart: { name: 'Bart', gender: 'm' } }

var result = nestBy(list,
  (person) => [ person.gender, { genderName: genders[person.gender] } ])

//  f:
//    genderName: 'Famale',
//    values: 
//      marge: { name: 'Marge', gender: 'f' }
//  m:
//    genderName: 'Male',
//    values:
//      homer: { name: 'Homer', gender: 'm' }
//      bart: { name: 'Bart', gender: 'm' }

Recursive

Yes, recursion is supported by passing in more property parameters.

Thanks

nest-by-tuple © 2015+, Rico Sta. Cruz. Released under the MIT License.
Authored and maintained by Rico Sta. Cruz with help from contributors (list).

ricostacruz.com  ·  GitHub @rstacruz  ·  Twitter @rstacruz

Current Tags

  • 1.0.0                                ...           latest (5 years ago)

1 Versions

  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (2)
Dev Dependencies (4)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |