angular2-pipes
Advanced tools
Comparing version 1.2.3 to 1.3.0
{ | ||
"name": "angular2-pipes", | ||
"version": "1.2.3", | ||
"version": "1.3.0", | ||
"author": "Dan Revah", | ||
@@ -5,0 +5,0 @@ "description": "Useful angular2 pipes", |
# ngx-pipes | ||
[![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![npm](https://img.shields.io/npm/dt/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) | ||
[![npm](https://img.shields.io/npm/v/ngx-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ngx-pipes) [![Travis](https://img.shields.io/travis/danrevah/ngx-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ngx-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ngx-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ngx-pipes?branch=master) [![license](https://img.shields.io/github/license/mashape/apistatus.svg?style=flat-square)](https://github.com/danrevah/ng-pipes/blob/master/LICENSE.md) | ||
@@ -581,12 +581,31 @@ > Useful pipes for Angular 2 and beyond with no external dependencies! | ||
Returns object of grouped by items by discriminator | ||
Returns object of grouped by items by discriminator, and supports nested properties. | ||
**Usage:** `array | groupBy: [string | Function]` | ||
**Usage:** `array | groupBy: [string[] | string | Function]` | ||
```typescript | ||
this.arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}]; | ||
this.arrayObject = [ | ||
{id: 1, elm: 'foo', value: 0}, | ||
{id: 2, elm: 'bar', value: 1}, | ||
{id: 3, elm: 'foo', value: 2}, | ||
{id: 4, elm: 'foo', value: 2} | ||
]; | ||
this.arrayNestedObject = [ | ||
{id: 1, prop: { deep: 'foo' }}, | ||
{id: 2, prop: { deep: 'bar' }}, | ||
{id: 3, prop: { deep: 'foo' }}, | ||
{id: 4, prop: { deep: 'bar' }} | ||
]; | ||
``` | ||
```html | ||
<p>{{ arrayObject | groupBy: 'elm' }}</p> <!-- Output: "{foo: [{elm: 'foo', value: 0}, {elm: 'foo', value: 2}], bar: [{elm: 'bar', value: 1}]}" --> | ||
<p>{{ arrayObject | groupBy: 'elm' }}</p> | ||
<!-- Output: "{foo: [{id: 1, elm: 'foo', value: 0}, {id: 3, elm: 'foo', value: 2}, {id: 4, elm: 'foo', value: 2}], bar: [{id: 2, elm: 'bar', value: 1}]}" --> | ||
<p>{{ arrayObject | groupBy: ['elm', 'value'] }}</p> | ||
<!-- Output: "{foo_0: [{elm: foo, value: 0}], bar_1: [{elm:bar,value: 1}], foo_2: [{elm:foo, value: 2}], bar_3: [{elm:bar, value: 3}]}" --> | ||
<p>{{ arrayNestedObject | groupBy: 'prop.deep' }}</p> | ||
<!-- Output:{foo: [{id: 1, prop: {deep: foo}}, {id: 3, prop: {deep: foo}}], bar: [{id: 2, prop: {deep: bar}}, {id: 4, prop: {deep: bar}}]}" --> | ||
``` | ||
@@ -593,0 +612,0 @@ |
import { PipeTransform } from '@angular/core'; | ||
export declare class GroupByPipe implements PipeTransform { | ||
transform(arr: any, ...args: any[]): any; | ||
transform(arr: any, discriminator?: any): any; | ||
private groupBy(list, discriminator); | ||
} |
@@ -16,17 +16,21 @@ "use strict"; | ||
} | ||
GroupByPipe.prototype.transform = function (arr) { | ||
var args = []; | ||
for (var _i = 1; _i < arguments.length; _i++) { | ||
args[_i - 1] = arguments[_i]; | ||
} | ||
GroupByPipe.prototype.transform = function (arr, discriminator) { | ||
if (discriminator === void 0) { discriminator = []; } | ||
if (!Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return this.groupBy(arr, args[0]); | ||
return this.groupBy(arr, discriminator); | ||
}; | ||
GroupByPipe.prototype.groupBy = function (list, discriminator) { | ||
return list.reduce(function (acc, payload) { | ||
var key = helpers_1.default.isFunction(discriminator) | ||
? discriminator(payload) | ||
: payload[discriminator]; | ||
var key; | ||
if (helpers_1.default.isFunction(discriminator)) { | ||
key = discriminator(payload); | ||
} | ||
else if (Array.isArray(discriminator)) { | ||
key = discriminator.map(function (k) { return helpers_1.default.extractDeepPropertyByMapKey(payload, k); }).join('_'); | ||
} | ||
else { | ||
key = helpers_1.default.extractDeepPropertyByMapKey(payload, discriminator); | ||
} | ||
return acc[key] = Array.isArray(acc[key]) | ||
@@ -33,0 +37,0 @@ ? acc[key].concat([payload]) |
@@ -31,2 +31,3 @@ import {GroupByPipe} from './group-by'; | ||
}); | ||
it('allow function to be used as discriminator', () => { | ||
@@ -40,2 +41,44 @@ const arrayWithDiscriminator = [{key: 'foo'}, {key: 'bar'}, {key: 'foo'}, {key: 'bar'}]; | ||
}); | ||
it('group on multiple discriminator', () => { | ||
const arrayWithDiscriminator = [ | ||
{id: 1, key: 'foo', type: 1}, | ||
{id: 2, key: 'foo', type: 2}, | ||
{id: 3, key: 'foo', type: 1}, | ||
{id: 4, key: 'foo', type: 2} | ||
]; | ||
const result = pipe.transform(arrayWithDiscriminator, ['key', 'type']); | ||
expect(result).toEqual({ | ||
foo_1: [{id: 1, key: 'foo', type: 1}, {id: 3, key: 'foo', type: 1}], | ||
foo_2: [{id: 2, key: 'foo', type: 2}, {id: 4, key: 'foo', type: 2}] | ||
}); | ||
}); | ||
it('group on deep property discriminator', () => { | ||
const arrayWithDiscriminator = [ | ||
{id: 1, prop: { deep: 'foo' }}, | ||
{id: 2, prop: { deep: 'bar' }}, | ||
{id: 3, prop: { deep: 'foo' }}, | ||
{id: 4, prop: { deep: 'bar' }} | ||
]; | ||
const result = pipe.transform(arrayWithDiscriminator, 'prop.deep'); | ||
expect(result).toEqual({ | ||
foo: [{id: 1, prop: { deep: 'foo' }}, {id: 3, prop: { deep: 'foo' }}], | ||
bar: [{id: 2, prop: { deep: 'bar' }}, {id: 4, prop: { deep: 'bar' }}] | ||
}); | ||
}); | ||
it('group on multiple deep property discriminator', () => { | ||
const arrayWithDiscriminator = [ | ||
{id: 1, prop: { deep: 'foo', type: 1 }}, | ||
{id: 2, prop: { deep: 'foo', type: 2 }}, | ||
{id: 3, prop: { deep: 'foo', type: 1 }}, | ||
{id: 4, prop: { deep: 'foo', type: 2 }} | ||
]; | ||
const result = pipe.transform(arrayWithDiscriminator, ['prop.deep', 'prop.type']); | ||
expect(result).toEqual({ | ||
foo_1: [{id: 1, prop: { deep: 'foo', type: 1 }}, {id: 3, prop: { deep: 'foo', type: 1 }}], | ||
foo_2: [{id: 2, prop: { deep: 'foo', type: 2 }}, {id: 4, prop: { deep: 'foo', type: 2 }}] | ||
}); | ||
}); | ||
}); |
@@ -7,3 +7,3 @@ import { Pipe, PipeTransform } from '@angular/core'; | ||
transform(arr: any, ...args: any[]): any { | ||
transform(arr: any, discriminator: any = []): any { | ||
if (!Array.isArray(arr)) { | ||
@@ -13,10 +13,15 @@ return arr; | ||
return this.groupBy(arr, args[0]); | ||
return this.groupBy(arr, discriminator); | ||
} | ||
private groupBy(list: any[], discriminator: Function | string) { | ||
private groupBy(list: any[], discriminator: any) { | ||
return list.reduce((acc, payload) => { | ||
const key = GeneralHelper.isFunction(discriminator) | ||
? (<Function>discriminator)(payload) | ||
: payload[<string>discriminator]; | ||
let key; | ||
if (GeneralHelper.isFunction(discriminator)) { | ||
key = (<Function>discriminator)(payload); | ||
} else if (Array.isArray(discriminator)) { | ||
key = discriminator.map(k => GeneralHelper.extractDeepPropertyByMapKey(payload, k)).join('_'); | ||
} else { | ||
key = GeneralHelper.extractDeepPropertyByMapKey(payload, <string>discriminator); | ||
} | ||
@@ -23,0 +28,0 @@ return acc[key] = Array.isArray(acc[key]) |
Sorry, the diff of this file is not supported yet
388849
5714
1050