group-array
Advanced tools
Comparing version 0.3.1 to 0.3.2
87
index.js
/*! | ||
* group-array <https://github.com/doowb/group-array> | ||
* | ||
* Copyright (c) 2015, Brian Woodward. | ||
* Licensed under the MIT License. | ||
* Copyright (c) 2015, 2017, Brian Woodward. | ||
* Released under the MIT License. | ||
*/ | ||
@@ -10,2 +10,5 @@ | ||
var union = require('union-value'); | ||
var flatten = require('arr-flatten'); | ||
var forOwn = require('for-own'); | ||
var typeOf = require('kind-of'); | ||
@@ -27,7 +30,7 @@ var get = require('get-value'); | ||
props = flatten([].slice.call(arguments, 1)); | ||
var groups = groupBy(arr, props.shift()); | ||
var args = flatten([].slice.call(arguments, 1)); | ||
var groups = groupBy(arr, args[0]); | ||
while (props.length) { | ||
subGroup(groups, props.shift()); | ||
for (var i = 1; i < args.length; i++) { | ||
toGroup(groups, args[i]); | ||
} | ||
@@ -38,6 +41,5 @@ return groups; | ||
function groupBy(arr, prop, key) { | ||
var len = arr.length, i = -1; | ||
var groups = {}; | ||
while (++i < len) { | ||
for (var i = 0; i < arr.length; i++) { | ||
var obj = arr[i]; | ||
@@ -54,11 +56,15 @@ var val; | ||
if (typeof val === 'string' || typeof val === 'number') { | ||
groups[val] = groups[val] || []; | ||
groups[val].push(obj); | ||
} else if (typeOf(val) === 'object') { | ||
groupObject(groups, obj, val); | ||
} else if (Array.isArray(val)) { | ||
groupArray(groups, obj, val); | ||
} else if (typeOf(val) === 'function') { | ||
throw new Error('group-array expects group keys to be strings, objects or undefined: ' + key); | ||
switch (typeOf(val)) { | ||
case 'undefined': | ||
break; | ||
case 'string': | ||
case 'number': | ||
union(groups, String(val), obj); | ||
break; | ||
case 'object': | ||
case 'array': | ||
eachValue(groups, obj, val); | ||
break; | ||
case 'function': | ||
throw new Error('invalid argument type: ' + key); | ||
} | ||
@@ -69,29 +75,22 @@ } | ||
function groupObject(groups, obj, val) { | ||
for (var k in val) { | ||
if (val.hasOwnProperty(k)) { | ||
groups[k] = groups[k] || []; | ||
groups[k].push(obj); | ||
} | ||
function eachValue(groups, obj, val) { | ||
if (Array.isArray(val)) { | ||
val.forEach(function(key) { | ||
union(groups, key, obj); | ||
}); | ||
} else { | ||
forOwn(val, function(v, key) { | ||
union(groups, key, obj); | ||
}); | ||
} | ||
} | ||
function groupArray(groups, obj, val) { | ||
val.forEach(function (item) { | ||
groups[item] = groups[item] || []; | ||
groups[item].push(obj); | ||
function toGroup(groups, prop) { | ||
forOwn(groups, function(val, key) { | ||
if (!Array.isArray(val)) { | ||
groups[key] = toGroup(val, prop, key); | ||
} else { | ||
groups[key] = groupBy(val, prop, key); | ||
} | ||
}); | ||
} | ||
function subGroup(groups, prop) { | ||
for (var key in groups) { | ||
if (groups.hasOwnProperty(key)) { | ||
var val = groups[key]; | ||
if (!Array.isArray(val)) { | ||
groups[key] = subGroup(val, prop); | ||
} else { | ||
groups[key] = groupBy(val, prop, key); | ||
} | ||
} | ||
} | ||
return groups; | ||
@@ -101,10 +100,2 @@ } | ||
/** | ||
* Flatten the given array. | ||
*/ | ||
function flatten(arr) { | ||
return [].concat.apply([], arr); | ||
} | ||
/** | ||
* Expose `groupArray` | ||
@@ -111,0 +102,0 @@ */ |
{ | ||
"name": "group-array", | ||
"description": "Group array of objects into lists.", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"homepage": "https://github.com/doowb/group-array", | ||
"author": "Brian Woodward (https://github.com/doowb)", | ||
"contributors": [ | ||
"Brian Woodward <brian.woodward@gmail.com> (https://github.com/doowb)", | ||
"Chris Kirk (http://www.chrispkirk.com)", | ||
"Jon Schlinkert <jon.schlinkert@sellside.com> (http://twitter.com/jonschlinkert)" | ||
], | ||
"repository": "doowb/group-array", | ||
@@ -22,12 +27,13 @@ "bugs": { | ||
}, | ||
"devDependencies": { | ||
"dependencies": { | ||
"arr-flatten": "^1.0.1", | ||
"for-own": "^0.1.4", | ||
"gulp-format-md": "^0.1.9", | ||
"mocha": "^2.5.3", | ||
"should": "^9.0.2", | ||
"get-value": "^2.0.6", | ||
"kind-of": "^3.1.0", | ||
"union-value": "^0.2.3" | ||
}, | ||
"dependencies": { | ||
"get-value": "^2.0.6", | ||
"kind-of": "^3.0.3" | ||
"devDependencies": { | ||
"gulp-format-md": "^0.1.11", | ||
"mocha": "^3.2.0", | ||
"should": "^11.2.0" | ||
}, | ||
@@ -55,16 +61,16 @@ "keywords": [ | ||
"list": [ | ||
"arr-flatten", | ||
"get-value", | ||
"group-object", | ||
"get-value", | ||
"union-value", | ||
"arr-reduce" | ||
"union-value" | ||
] | ||
}, | ||
"reflinks": [ | ||
"verb", | ||
"verb-generate-readme" | ||
], | ||
"lint": { | ||
"reflinks": true | ||
}, | ||
"reflinks": [ | ||
"verb-generate-readme", | ||
"verb" | ||
] | ||
} | ||
} | ||
} |
211
README.md
@@ -1,4 +0,4 @@ | ||
# group-array [![NPM version](https://img.shields.io/npm/v/group-array.svg?style=flat)](https://www.npmjs.com/package/group-array) [![NPM downloads](https://img.shields.io/npm/dm/group-array.svg?style=flat)](https://npmjs.org/package/group-array) [![Build Status](https://img.shields.io/travis/doowb/group-array.svg?style=flat)](https://travis-ci.org/doowb/group-array) | ||
# group-array [![NPM version](https://img.shields.io/npm/v/group-array.svg?style=flat)](https://www.npmjs.com/package/group-array) [![NPM monthly downloads](https://img.shields.io/npm/dm/group-array.svg?style=flat)](https://npmjs.org/package/group-array) [![NPM total downloads](https://img.shields.io/npm/dt/group-array.svg?style=flat)](https://npmjs.org/package/group-array) [![Linux Build Status](https://img.shields.io/travis/doowb/group-array.svg?style=flat&label=Travis)](https://travis-ci.org/doowb/group-array) | ||
Group array of objects into lists. | ||
> Group array of objects into lists. | ||
@@ -11,8 +11,2 @@ ## Table of Contents | ||
- [About](#about) | ||
* [Related projects](#related-projects) | ||
* [Contributing](#contributing) | ||
* [Building docs](#building-docs) | ||
* [Running tests](#running-tests) | ||
* [Author](#author) | ||
* [License](#license) | ||
@@ -75,26 +69,26 @@ _(TOC generated by [verb](https://github.com/verbose/verb) using [markdown-toc](https://github.com/jonschlinkert/markdown-toc))_ | ||
var arr = [ | ||
{ data: { year: '2014', tag: 'one', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'feb', day: '12'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'one', month: 'feb', day: '12'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'two', month: 'jan', day: '14'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'two', month: 'jan', day: '14'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'two', month: 'jan', day: '16'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'two', month: 'jan', day: '16'}, content: '...'}, | ||
{ data: { year: '2014', tag: 'two', month: 'feb', day: '18'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'two', month: 'feb', day: '18'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'two', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'two', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'feb', day: '01'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'feb', day: '01'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'feb', day: '02'}, content: '...'}, | ||
{ data: { year: '2015', tag: 'three', month: 'feb', day: '02'}, content: '...'} | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16'}, content: '...'}, | ||
{ data: { year: '2016', tag: 'two', month: 'feb', day: '18'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'two', month: 'feb', day: '18'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02'}, content: '...'}, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02'}, content: '...'} | ||
] | ||
@@ -112,53 +106,76 @@ ``` | ||
```js | ||
{ | ||
'2014': | ||
{ one: | ||
{ jan: | ||
{ '01': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ], | ||
'02': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ] }, | ||
feb: | ||
{ '10': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ], | ||
'12': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ] } }, | ||
two: | ||
{ jan: | ||
{ '14': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ], | ||
'16': | ||
[ { data: { year: '2014', ... }, | ||
{ data: { year: '2014', ... } ] }, | ||
feb: | ||
{ '18': | ||
[ { data: { year: '2014', ... } ] } } }, | ||
'2015': | ||
{ two: | ||
{ feb: | ||
{ '10': | ||
[ { data: { year: '2015', ... }, | ||
{ data: { year: '2015', ... } ], | ||
'18': | ||
[ { data: { year: '2015', ... } ] } }, | ||
three: | ||
{ jan: | ||
{ '01': | ||
[ { data: { year: '2015', ... }, | ||
{ data: { year: '2015', ... } ], | ||
'02': | ||
[ { data: { year: '2015', ... }, | ||
{ data: { year: '2015', ... } ] }, | ||
feb: | ||
{ '01': | ||
[ { data: { year: '2015', ... }, | ||
{ data: { year: '2015', ... } ], | ||
'02': | ||
[ { data: { year: '2015', ... }, | ||
{ data: { year: '2015', ... } ] } } }}; | ||
{ '2016': | ||
{ one: | ||
{ jan: | ||
{ '01': | ||
[ { data: { year: '2016', tag: 'one', month: 'jan', day: '01' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '01' }, | ||
content: '...' } ], | ||
'02': | ||
[ { data: { year: '2016', tag: 'one', month: 'jan', day: '02' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'one', month: 'jan', day: '02' }, | ||
content: '...' } ] }, | ||
feb: | ||
{ '10': | ||
[ { data: { year: '2016', tag: 'one', month: 'feb', day: '10' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '10' }, | ||
content: '...' } ], | ||
'12': | ||
[ { data: { year: '2016', tag: 'one', month: 'feb', day: '12' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'one', month: 'feb', day: '12' }, | ||
content: '...' } ] } }, | ||
two: | ||
{ jan: | ||
{ '14': | ||
[ { data: { year: '2016', tag: 'two', month: 'jan', day: '14' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '14' }, | ||
content: '...' } ], | ||
'16': | ||
[ { data: { year: '2016', tag: 'two', month: 'jan', day: '16' }, | ||
content: '...' }, | ||
{ data: { year: '2016', tag: 'two', month: 'jan', day: '16' }, | ||
content: '...' } ] }, | ||
feb: | ||
{ '18': | ||
[ { data: { year: '2016', tag: 'two', month: 'feb', day: '18' }, | ||
content: '...' } ] } } }, | ||
'2017': | ||
{ two: | ||
{ feb: | ||
{ '10': | ||
[ { data: { year: '2017', tag: 'two', month: 'feb', day: '10' }, | ||
content: '...' }, | ||
{ data: { year: '2017', tag: 'two', month: 'feb', day: '10' }, | ||
content: '...' } ], | ||
'18': | ||
[ { data: { year: '2017', tag: 'two', month: 'feb', day: '18' }, | ||
content: '...' } ] } }, | ||
three: | ||
{ jan: | ||
{ '01': | ||
[ { data: { year: '2017', tag: 'three', month: 'jan', day: '01' }, | ||
content: '...' }, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '01' }, | ||
content: '...' } ], | ||
'02': | ||
[ { data: { year: '2017', tag: 'three', month: 'jan', day: '02' }, | ||
content: '...' }, | ||
{ data: { year: '2017', tag: 'three', month: 'jan', day: '02' }, | ||
content: '...' } ] }, | ||
feb: | ||
{ '01': | ||
[ { data: { year: '2017', tag: 'three', month: 'feb', day: '01' }, | ||
content: '...' }, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '01' }, | ||
content: '...' } ], | ||
'02': | ||
[ { data: { year: '2017', tag: 'three', month: 'feb', day: '02' }, | ||
content: '...' }, | ||
{ data: { year: '2017', tag: 'three', month: 'feb', day: '02' }, | ||
content: '...' } ] } } } } | ||
``` | ||
@@ -170,3 +187,3 @@ | ||
* [arr-reduce](https://www.npmjs.com/package/arr-reduce): Fast array reduce that also loops over sparse elements. | [homepage](https://github.com/jonschlinkert/arr-reduce "Fast array reduce that also loops over sparse elements.") | ||
* [arr-flatten](https://www.npmjs.com/package/arr-flatten): Recursively flatten an array or arrays. This is the fastest implementation of array flatten. | [homepage](https://github.com/jonschlinkert/arr-flatten "Recursively flatten an array or arrays. This is the fastest implementation of array flatten.") | ||
* [get-value](https://www.npmjs.com/package/get-value): Use property paths (`a.b.c`) to get a nested value from an object. | [homepage](https://github.com/jonschlinkert/get-value "Use property paths (`a.b.c`) to get a nested value from an object.") | ||
@@ -180,10 +197,18 @@ * [group-object](https://www.npmjs.com/package/group-object): Group object keys and values into lists. | [homepage](https://github.com/doowb/group-object "Group object keys and values into lists.") | ||
### Contributors | ||
| **Commits** | **Contributor** | | ||
| --- | --- | | ||
| 23 | [doowb](https://github.com/doowb) | | ||
| 6 | [jonschlinkert](https://github.com/jonschlinkert) | | ||
| 1 | [cperryk](https://github.com/cperryk) | | ||
### Building docs | ||
_(This document was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme) (a [verb](https://github.com/verbose/verb) generator), please don't edit the readme directly. Any changes to the readme must be made in [.verb.md](.verb.md).)_ | ||
_(This project's readme.md is generated by [verb](https://github.com/verbose/verb-generate-readme), please don't edit the readme directly. Any changes to the readme must be made in the [.verb.md](.verb.md) readme template.)_ | ||
To generate the readme and API documentation with [verb](https://github.com/verbose/verb): | ||
To generate the readme, run the following command: | ||
```sh | ||
$ npm install -g verb verb-generate-readme && verb | ||
$ npm install -g verbose/verb#dev verb-generate-readme && verb | ||
``` | ||
@@ -193,6 +218,6 @@ | ||
Install dev dependencies: | ||
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command: | ||
```sh | ||
$ npm install -d && npm test | ||
$ npm install && npm test | ||
``` | ||
@@ -205,11 +230,11 @@ | ||
* [github/doowb](https://github.com/doowb) | ||
* [twitter/doowb](http://twitter.com/doowb) | ||
* [twitter/doowb](https://twitter.com/doowb) | ||
### License | ||
Copyright © 2016, [Brian Woodward](https://github.com/doowb). | ||
Released under the [MIT license](https://github.com/doowb/group-array/blob/master/LICENSE). | ||
Copyright © 2017, [Brian Woodward](https://github.com/doowb). | ||
Released under the [MIT License](LICENSE). | ||
*** | ||
_This file was generated by [verb](https://github.com/verbose/verb), v0.9.0, on July 15, 2016._ | ||
_This file was generated by [verb-generate-readme](https://github.com/verbose/verb-generate-readme), v0.4.2, on February 24, 2017._ |
Sorry, the diff of this file is not supported yet
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
13972
3
233
5
83
1
+ Addedarr-flatten@^1.0.1
+ Addedfor-own@^0.1.4
+ Addedunion-value@^0.2.3
+ Addedarr-flatten@1.1.0(transitive)
+ Addedarr-union@3.1.0(transitive)
+ Addedextend-shallow@2.0.1(transitive)
+ Addedfor-in@1.0.2(transitive)
+ Addedfor-own@0.1.5(transitive)
+ Addedis-extendable@0.1.1(transitive)
+ Addedis-plain-object@2.0.4(transitive)
+ Addedisobject@3.0.1(transitive)
+ Addedset-value@0.4.3(transitive)
+ Addedto-object-path@0.3.0(transitive)
+ Addedunion-value@0.2.4(transitive)
Updatedkind-of@^3.1.0