angular2-pipes
Advanced tools
Comparing version 0.5.4 to 0.5.6
{ | ||
"name": "angular2-pipes", | ||
"version": "0.5.4", | ||
"version": "0.5.6", | ||
"author": "Dan Revah", | ||
@@ -30,3 +30,3 @@ "description": "Useful angular2 pipes", | ||
"type": "git", | ||
"url": "https://github.com/danrevah/ng2-pipes.git" | ||
"url": "https://github.com/danrevah/ng-pipes.git" | ||
}, | ||
@@ -33,0 +33,0 @@ "devDependencies": { |
@@ -1,4 +0,4 @@ | ||
# Angular 2 - Pipes [![npm](https://img.shields.io/npm/v/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![Travis](https://img.shields.io/travis/danrevah/ng2-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ng2-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ng2-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ng2-pipes?branch=master) | ||
# Angular Pipes [![npm](https://img.shields.io/npm/v/ng2-pipes.svg?style=flat-square)](https://www.npmjs.com/package/ng2-pipes) [![Travis](https://img.shields.io/travis/danrevah/ng2-pipes.svg?style=flat-square)](https://travis-ci.org/danrevah/ng2-pipes) [![Coveralls](https://img.shields.io/coveralls/danrevah/ng2-pipes.svg?style=flat-square)](https://coveralls.io/github/danrevah/ng2-pipes?branch=master) | ||
> Useful pipes for Angular2. | ||
> Useful pipes for Angular 2 | ||
@@ -557,16 +557,8 @@ ## Table of contents | ||
```javascript | ||
import {GroupByPipe} from 'ng2-pipes/src/app/pipes/array/group-by'; | ||
```typescript | ||
this.arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}]; | ||
``` | ||
@Component({ | ||
// ... | ||
providers: [GroupByPipe] | ||
}) | ||
export class AppComponent { | ||
constructor(private groupByPipe: GroupByPipe) { | ||
// .. | ||
const arrayObject = [{elm: 'foo', value: 0}, {elm: 'bar', value: 1}, {elm: 'foo', value: 2}]; | ||
const groupedObject = groupByPipe.transform(arrayObject, 'elm')); | ||
// `groupedObject` -> Contains: {foo: [{elm: 'foo', value: 0}, {elm: 'foo', value: 2}], bar: [{elm: 'bar', value: 1}]} | ||
} | ||
```html | ||
<p>{{ arrayObject | groupBy: 'elm' }}</p> <!-- Output: "{foo: [{elm: 'foo', value: 0}, {elm: 'foo', value: 2}], bar: [{elm: 'bar', value: 1}]}" --> | ||
``` | ||
@@ -573,0 +565,0 @@ |
@@ -26,4 +26,7 @@ "use strict"; | ||
var _this = this; | ||
return array.reduce(function (arr, elm) { return elm instanceof Array ? | ||
arr.concat(_this.flatten(elm)) : arr.concat(elm); }, []); | ||
return array.reduce(function (arr, elm) { | ||
return Array.isArray(elm) | ||
? arr.concat(_this.flatten(elm)) | ||
: arr.concat(elm); | ||
}, []); | ||
}; | ||
@@ -30,0 +33,0 @@ FlattenPipe = __decorate([ |
@@ -17,5 +17,8 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
private flatten(array: any[]): any[] { | ||
return array.reduce((arr: any[], elm: any) => elm instanceof Array ? | ||
arr.concat(this.flatten(elm)) : arr.concat(elm), []); | ||
return array.reduce((arr: any[], elm: any) => | ||
Array.isArray(elm) | ||
? arr.concat(this.flatten(elm)) | ||
: arr.concat(elm) | ||
, []); | ||
} | ||
} |
@@ -20,3 +20,6 @@ "use strict"; | ||
} | ||
return !Array.isArray(arr) ? arr : args.reduce(function (newArr, currArr) { | ||
if (!Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return args.reduce(function (newArr, currArr) { | ||
return newArr.filter(function (elm) { return !!~currArr.indexOf(elm); }); | ||
@@ -23,0 +26,0 @@ }, arr); |
@@ -7,6 +7,10 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
transform(arr: any, ...args: any[]): any[] { | ||
return !Array.isArray(arr) ? arr : args.reduce((newArr, currArr) => { | ||
return newArr.filter(elm => !!~currArr.indexOf(elm)) | ||
if (!Array.isArray(arr)) { | ||
return arr; | ||
} | ||
return args.reduce((newArr, currArr) => { | ||
return newArr.filter(elm => !!~currArr.indexOf(elm)); | ||
}, arr); | ||
} | ||
} |
@@ -12,2 +12,3 @@ "use strict"; | ||
var core_1 = require('@angular/core'); | ||
var helpers_1 = require('../helpers/helpers'); | ||
var ReversePipe = (function () { | ||
@@ -17,9 +18,8 @@ function ReversePipe() { | ||
ReversePipe.prototype.transform = function (value) { | ||
if (typeof value === 'string') { | ||
if (helpers_1.default.isString(value)) { | ||
return value.split('').reverse().join(''); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.reverse(); | ||
} | ||
return value; | ||
return Array.isArray(value) | ||
? value.reverse() | ||
: value; | ||
}; | ||
@@ -26,0 +26,0 @@ ReversePipe = __decorate([ |
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
@@ -8,10 +9,10 @@ @Pipe({name: 'reverse'}) | ||
{ | ||
if (typeof value === 'string') { | ||
if (GeneralHelper.isString(value)) { | ||
return value.split('').reverse().join(''); | ||
} | ||
if (Array.isArray(value)) { | ||
return value.reverse(); | ||
} | ||
return value; | ||
return Array.isArray(value) | ||
? value.reverse() | ||
: value; | ||
} | ||
} |
@@ -17,12 +17,12 @@ "use strict"; | ||
if (args === void 0) { args = []; } | ||
return (!Array.isArray(arr) || !Array.isArray(args)) | ||
? arr | ||
: args.reduce(function (newArr, currArr) { | ||
return newArr.concat(currArr.reduce(function (noDupArr, curr) { | ||
if (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) { | ||
noDupArr.push(curr); | ||
} | ||
return noDupArr; | ||
}, [])); | ||
}, arr); | ||
if (!Array.isArray(arr) || !Array.isArray(args)) { | ||
return arr; | ||
} | ||
return args.reduce(function (newArr, currArr) { | ||
return newArr.concat(currArr.reduce(function (noDupArr, curr) { | ||
return (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) | ||
? noDupArr.concat([curr]) | ||
: noDupArr; | ||
}, [])); | ||
}, arr); | ||
}; | ||
@@ -29,0 +29,0 @@ UnionPipe = __decorate([ |
@@ -7,10 +7,11 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
transform(arr: any, args: any[] = []): any[] { | ||
return (!Array.isArray(arr) || !Array.isArray(args)) | ||
? arr | ||
: args.reduce((newArr, currArr) => { | ||
if (!Array.isArray(arr) || !Array.isArray(args)) { | ||
return arr; | ||
} | ||
return args.reduce((newArr, currArr) => { | ||
return newArr.concat(currArr.reduce((noDupArr, curr) => { | ||
if (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) { | ||
noDupArr.push(curr); | ||
} | ||
return noDupArr; | ||
return (!~noDupArr.indexOf(curr) && !~newArr.indexOf(curr)) | ||
? noDupArr.concat([curr]) | ||
: noDupArr; | ||
}, [])); | ||
@@ -17,0 +18,0 @@ }, arr); |
@@ -12,2 +12,3 @@ "use strict"; | ||
var core_1 = require('@angular/core'); | ||
var helpers_1 = require('../helpers/helpers'); | ||
var IsDefinedPipe = (function () { | ||
@@ -17,3 +18,3 @@ function IsDefinedPipe() { | ||
IsDefinedPipe.prototype.transform = function (value) { | ||
return typeof value !== 'undefined'; | ||
return !helpers_1.default.isUndefined(value); | ||
}; | ||
@@ -20,0 +21,0 @@ IsDefinedPipe = __decorate([ |
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
@@ -7,4 +8,4 @@ @Pipe({name: 'isDefined'}) | ||
transform(value: any): boolean { | ||
return typeof value !== 'undefined'; | ||
return !GeneralHelper.isUndefined(value); | ||
} | ||
} |
@@ -12,2 +12,3 @@ "use strict"; | ||
var core_1 = require('@angular/core'); | ||
var helpers_1 = require('../helpers/helpers'); | ||
var IsFunctionPipe = (function () { | ||
@@ -17,3 +18,3 @@ function IsFunctionPipe() { | ||
IsFunctionPipe.prototype.transform = function (value) { | ||
return typeof value === 'function'; | ||
return helpers_1.default.isFunction(value); | ||
}; | ||
@@ -20,0 +21,0 @@ IsFunctionPipe = __decorate([ |
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
@@ -7,4 +8,4 @@ @Pipe({name: 'isFunction'}) | ||
transform(value: any): boolean { | ||
return typeof value === 'function'; | ||
return GeneralHelper.isFunction(value); | ||
} | ||
} |
@@ -12,2 +12,3 @@ "use strict"; | ||
var core_1 = require('@angular/core'); | ||
var helpers_1 = require('../helpers/helpers'); | ||
var IsObjectPipe = (function () { | ||
@@ -17,3 +18,3 @@ function IsObjectPipe() { | ||
IsObjectPipe.prototype.transform = function (value) { | ||
return value !== null && typeof value === 'object'; | ||
return helpers_1.default.isObject(value); | ||
}; | ||
@@ -20,0 +21,0 @@ IsObjectPipe = __decorate([ |
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
@@ -7,4 +8,4 @@ @Pipe({name: 'isObject'}) | ||
transform(value: any): boolean { | ||
return value !== null && typeof value === 'object'; | ||
return GeneralHelper.isObject(value); | ||
} | ||
} |
@@ -12,2 +12,3 @@ "use strict"; | ||
var core_1 = require('@angular/core'); | ||
var helpers_1 = require('../helpers/helpers'); | ||
var IsStringPipe = (function () { | ||
@@ -17,3 +18,3 @@ function IsStringPipe() { | ||
IsStringPipe.prototype.transform = function (value) { | ||
return typeof value === 'string'; | ||
return helpers_1.default.isString(value); | ||
}; | ||
@@ -20,0 +21,0 @@ IsStringPipe = __decorate([ |
import {PipeTransform, Pipe} from '@angular/core'; | ||
import GeneralHelper from '../helpers/helpers'; | ||
@@ -7,4 +8,4 @@ @Pipe({name: 'isString'}) | ||
transform(value: any): boolean { | ||
return typeof value === 'string'; | ||
return GeneralHelper.isString(value); | ||
} | ||
} |
import { PipeTransform } from '@angular/core'; | ||
export declare class BytesPipe implements PipeTransform { | ||
private dictionary; | ||
transform(value: number): string | number; | ||
} |
@@ -15,2 +15,8 @@ "use strict"; | ||
function BytesPipe() { | ||
this.dictionary = [ | ||
{ max: 1e3, type: 'B' }, | ||
{ max: 1e6, type: 'KB' }, | ||
{ max: 1e9, type: 'MB' }, | ||
{ max: 1e12, type: 'GB' } | ||
]; | ||
} | ||
@@ -21,9 +27,3 @@ BytesPipe.prototype.transform = function (value) { | ||
} | ||
var dictionary = [ | ||
{ max: 1e3, type: 'B' }, | ||
{ max: 1e6, type: 'KB' }, | ||
{ max: 1e9, type: 'MB' }, | ||
{ max: 1e12, type: 'GB' } | ||
]; | ||
var format = dictionary.find(function (d) { return value < d.max; }) || dictionary[dictionary.length - 1]; | ||
var format = this.dictionary.find(function (d) { return value < d.max; }) || this.dictionary[this.dictionary.length - 1]; | ||
var num = value / (format.max / 1e3); | ||
@@ -30,0 +30,0 @@ return num + " " + format.type; |
@@ -6,2 +6,8 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
export class BytesPipe implements PipeTransform { | ||
private dictionary: Array<{max: number, type: string}> = [ | ||
{ max: 1e3, type: 'B' }, | ||
{ max: 1e6, type: 'KB' }, | ||
{ max: 1e9, type: 'MB' }, | ||
{ max: 1e12, type: 'GB' } | ||
]; | ||
@@ -13,10 +19,3 @@ transform(value: number): string | number { | ||
const dictionary: Array<{max: number, type: string}> = [ | ||
{ max: 1e3, type: 'B' }, | ||
{ max: 1e6, type: 'KB' }, | ||
{ max: 1e9, type: 'MB' }, | ||
{ max: 1e12, type: 'GB' } | ||
]; | ||
const format = dictionary.find(d => value < d.max) || dictionary[dictionary.length - 1]; | ||
const format = this.dictionary.find(d => value < d.max) || this.dictionary[this.dictionary.length - 1]; | ||
const num = value / (format.max / 1e3); | ||
@@ -23,0 +22,0 @@ return `${num} ${format.type}`; |
@@ -17,3 +17,5 @@ "use strict"; | ||
if (power === void 0) { power = 2; } | ||
return isNaN(num) ? num : Math.pow(num, power); | ||
return !isNaN(num) | ||
? Math.pow(num, power) | ||
: num; | ||
}; | ||
@@ -20,0 +22,0 @@ PowerPipe = __decorate([ |
@@ -7,4 +7,6 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
transform(num: number, power: number = 2): number { | ||
return isNaN(num) ? num : num ** power; | ||
return !isNaN(num) | ||
? num ** power | ||
: num ; | ||
} | ||
} |
@@ -16,3 +16,5 @@ "use strict"; | ||
SqrtPipe.prototype.transform = function (num) { | ||
return isNaN(num) ? num : Math.sqrt(num); | ||
return !isNaN(num) | ||
? Math.sqrt(num) | ||
: num; | ||
}; | ||
@@ -19,0 +21,0 @@ SqrtPipe = __decorate([ |
@@ -7,4 +7,6 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
transform(num: number): number { | ||
return isNaN(num) ? num : Math.sqrt(num); | ||
return !isNaN(num) | ||
? Math.sqrt(num) | ||
: num; | ||
} | ||
} |
@@ -16,5 +16,5 @@ "use strict"; | ||
SumPipe.prototype.transform = function (arr) { | ||
return !Array.isArray(arr) | ||
? arr | ||
: arr.reduce(function (sum, curr) { return sum + curr; }, 0); | ||
return Array.isArray(arr) | ||
? arr.reduce(function (sum, curr) { return sum + curr; }, 0) | ||
: arr; | ||
}; | ||
@@ -21,0 +21,0 @@ SumPipe = __decorate([ |
@@ -7,6 +7,6 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
transform(arr: number[]): number|number[] { | ||
return !Array.isArray(arr) | ||
? arr | ||
: arr.reduce((sum, curr) => sum + curr, 0); | ||
return Array.isArray(arr) | ||
? arr.reduce((sum, curr) => sum + curr, 0) | ||
: arr; | ||
} | ||
} |
@@ -21,4 +21,3 @@ "use strict"; | ||
} | ||
return Object.keys(obj) | ||
.reduce(function (o, k) { | ||
return Object.keys(obj).reduce(function (o, k) { | ||
var key = cb ? cb(obj[k]) : obj[k]; | ||
@@ -25,0 +24,0 @@ return Array.isArray(o[key]) |
@@ -12,10 +12,9 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
return Object.keys(obj) | ||
.reduce((o, k) => { | ||
const key = cb ? cb(obj[k]) : obj[k]; | ||
return Array.isArray(o[key]) | ||
? (o[key].push(k), o) | ||
: Object.assign(o, {[key]: [k]}); | ||
return Object.keys(obj).reduce((o, k) => { | ||
const key = cb ? cb(obj[k]) : obj[k]; | ||
return Array.isArray(o[key]) | ||
? (o[key].push(k), o) | ||
: Object.assign(o, {[key]: [k]}); | ||
}, {}); | ||
} | ||
} |
@@ -19,3 +19,3 @@ "use strict"; | ||
return helpers_1.default.isString(text) | ||
? text.replace(/\{(\d+)}/g, function (match, index) { return typeof (args[index]) !== 'undefined' ? args[index] : match; }) | ||
? text.replace(/\{(\d+)}/g, function (match, index) { return !helpers_1.default.isUndefined(args[index]) ? args[index] : match; }) | ||
: text; | ||
@@ -22,0 +22,0 @@ }; |
@@ -9,5 +9,5 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
return GeneralHelper.isString(text) | ||
? text.replace(/\{(\d+)}/g, (match, index) => typeof (args[index]) !== 'undefined' ? args[index] : match) | ||
? text.replace(/\{(\d+)}/g, (match, index) => !GeneralHelper.isUndefined(args[index]) ? args[index] : match) | ||
: text; | ||
} | ||
} |
@@ -11,2 +11,3 @@ import {PipeTransform, Pipe} from '@angular/core'; | ||
} | ||
if (text.length > length) { | ||
@@ -16,2 +17,3 @@ if (wordBreak) { | ||
} | ||
if (!!~text.indexOf(' ', length)) { | ||
@@ -18,0 +20,0 @@ return text.slice(0, text.indexOf(' ', length)) + suffix; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
366151
5378
974