Angular2 Pipes
![npm version](https://badge.fury.io/js/ng2-pipes.svg)
Useful pipes for Angular2.
Table of contents
Installation
- Use npm to install the package
$ npm install ng2-pipes --save
- Add into your module
imports
the NgPipesModule
import {NgPipesModule} from 'ng2-pipes';
@NgModule({
imports: [
NgPipesModule
]
})
String
repeat
Repeats a string n times
API: string | repeat: times: [separator|optional]
<p>{{ 'example' | repeat: 3: '@' }}</p>
scan
Scans string and replace {i}
placeholders by equivalent member of the array
API: string | scan: [ARRAY]
<p>{{'Hey {0}, {1}' | scan: ['foo', 'bar']}}</p>
shorten
Shortening a string by length and providing a custom string to denote an omission
API: string | shorten: length: [suffix|optional]: [wordBreak boolean|optional]
<p>{{'Hey foo bar' | shorten: 3: '...'}}</p>
stripTags
Strips a HTML tags from string and providing which tags should not be removed
API: string | stripTags: [ARRAY]
<p>{{'<a href="">foo</a> <p class="foo">bar</p>' | stripTags }}</p>
<p>{{'<a href="">foo</a> <p class="foo">bar</p>' | stripTags: 'p'}}</p>
ucfirst
Uppercase first letter of first word
<p>{{'foo bar' | ucfirst }}</p>
ucwords
Uppercase first letter every word
<p>{{'foo bar' | ucwords }}</p>
trim
Strips characters from the beginning and end of a string (default character is space).
API: string | trim: [characters|optional]
<p>{{' foo ' | trim }}</p>
<p>{{'foobarfoo' | trim: 'foo' }}</p>
ltrim
Strips characters from the beginning of a string (default character is space).
API: string | ltrim: [characters|optional]
<p>{{' foo ' | ltrim }}</p>
<p>{{'foobarfoo' | ltrim: 'foo' }}</p>
rtrim
Strips characters from the end of a string (default character is space).
API: string | rtrim: [characters|optional]
<p>{{' foo ' | rtrim }}</p>
<p>{{'foobarfoo' | rtrim: 'foo' }}</p>
reverse
Reverses a string
API: string | reverse
<p>{{'foo bar' | reverse }}</p>
Array
diff
Returns array of diff between arrays
API: array | diff: [ARRAY]: [ARRAY]: ... : [ARRAY]
this.items = [1, 2, 3, 4];
<li *ngFor="let item of items | diff: [1, 2]">
flatten
Flattens nested array, passing shallow will mean it will only be flattened a single level
API: array | flatten: [shallow|optional]
this.items = [1,2,3,[4,5,6,[7,8,9],[10,11,12,13,[14],[15],[16, [17]]]]];
<li *ngFor="let item of items | flatten">
initial
Slicing off the end of the array by n elements
API: array | initial: n
this.items = [first, second, third];
<li *ngFor="let item of items | initial: 1">
tail
Slicing off the start of the array by n elements
API: array | tail: n
this.items = [first, second, third];
<li *ngFor="let item of items | tail: 1">
intersection
Returns the intersections of an arrays
API: array | intersection: [ARRAY]: [ARRAY]: ... : [ARRAY]
this.items = [1, 2, 3, 4, 5];
<li *ngFor="let item of items | intersection: [1, 2]: [3, 6]">
reverse
Reverses an array
API: array | reverse
this.items = [1, 2, 3];
<li *ngFor="let item of items | reverse">
truthify
Removes un-truthy values from array
API: array | truthify
this.items = [null, 1, false, undefined, 2, 0, 3, NaN, 4, ''];
<li *ngFor="let item of items | truthify">
union
Removes un-truthy values from array
API: array | union: [ARRAY]
this.items = [1, 2, 3];
<li *ngFor="let item of items | union: [[4]]">
unique
Removes duplicates from array
API: array | unique
this.items = [1, 2, 3, 1, 2, 3];
<li *ngFor="let item of items | unique">
without
Returns array without specific elements
API: array | without: [ARRAY]
this.items = [1, 2, 3, 1, 2, 3];
<li *ngFor="let item of items | without: [1,3]">
pluck
Returns array of properties values
API: array | pluck: propertyName
this.items = [
{
a: 1,
b: {
c: 4
}
},
{
a: 2,
b: {
c: 5
}
},
{
a: 3,
b: {
c: 6
}
},
];
<li *ngFor="let item of items | pluck: 'a'">
<li *ngFor="let item of items | pluck: 'b.c'">
shuffle
Returns randomly shuffled array
API: array | shuffle
this.items = [1, 2, 3, 4, 5, 6];
<li *ngFor="let item of items | shuffle">
every
Returns true if every elements of the array fits the predicate otherwise false
API: array | every: predicate
this.itemsOne = [1, 1, 1];
this.itemsTwo = [1, 1, 2];
this.itemsThree = [2, 2, 2];
this.predicate = (value: any, index: number, array: any[]): boolean => {
return value === 1;
};
<p>{{ itemsOne | every: predicate }}</p>
<p>{{ itemsTwo | every: predicate }}</p>
<p>{{ itemsThree | every: predicate }}</p>
some
Returns true if some elements of the array fits the predicate otherwise false
API: array | some: predicate
this.itemsOne = [1, 1, 1];
this.itemsTwo = [1, 1, 2];
this.itemsThree = [2, 2, 2];
this.predicate = (value: any, index: number, array: any[]): boolean => {
return value === 1;
};
<p>{{ itemsOne | some: predicate }}</p>
<p>{{ itemsTwo | some: predicate }}</p>
<p>{{ itemsThree | some: predicate }}</p>
sample
Returns sample items randomly from array
API: array | sample: [amount | default = 1]
<p>{{ [1, 2, 3, 4] | sample }}</p>
<p>{{ [1, 2, 3, 4] | sample: 2 }}</p>
Math
min
Returns the minimum of a given array
API: array | min
<p>{{ [1, 2, 3, 1, 2, 3] | min }}</p>
max
Returns the maximum of a given array
API: array | max
<p>{{ [1, 2, 3, 1, 2, 3] | max }}</p>
sum
Returns the sum of a given array
API: array | sum
<p>{{ [1, 2, 3, 4] | sum }}</p>
percent
Returns percent between numbers
API: number | percentage: [total | default = 100]: [floor | default = false]
<p>{{ 5 | percentage }}</p>
<p>{{ 5 | percentage: 160 }}</p>
<p>{{ 5 | percentage: 160: true }}</p>
ceil
Returns ceil of a number by precision
API: number | ceil: [precision | default = 0]
<p>{{ 42.123 | ceil }}</p>
<p>{{ 42.123 | ceil: 2 }}</p>
floor
Returns floor of a number by precision
API: number | floor: [precision | default = 0]
<p>{{ 42.123 | floor }}</p>
<p>{{ 42.123 | floor: 2 }}</p>
round
Returns round of a number by precision
API: number | round: [precision | default = 0]
<p>{{ 42.4 | round }}</p>
<p>{{ 42.5 | round }}</p>
<p>{{ 42.123 | round: 2 }}</p>
sqrt
Returns the square root of a number
API: number | sqrt
<p>{{ 9 | sqrt }}</p>
pow
Returns the power of a number
API: number | pow: [power | default = 2]
<p>{{ 3 | pow }}</p>
<p>{{ 3 | pow: 3 }}</p>
degrees
Returns the degrees of a number in radians
API: number | degrees
<p>{{ 3.141592653589793 | degrees }}</p>
radians
Returns the radians of a number in degrees
API: number | radians
<p>{{ 180 | radians }}</p>
bytes
Returns bytes with a unit symbol
API: number | bytes
<p>{{ 10 | bytes }}</p>
<p>{{ 1000 | bytes }}</p>
<p>{{ 1000000 | bytes }}</p>
<p>{{ 1000000000 | bytes }}</p>
Boolean
isNull
API: any | isNull
<p>{{ null | isNull }}</p>
<p>{{ 1 | isNull }}</p>
isDefined
API: any | isDefined
<p>{{ 1 | isDefined }}</p>
<p>{{ undefined | isDefined }}</p>
isUndefined
API: any | isUndefined
<p>{{ 1 | isUndefined }}</p>
<p>{{ undefined | isUndefined }}</p>
isString
API: any | isString
<p>{{ 1 | isString }}</p>
<p>{{ '' | isString }}</p>
isNumber
API: any | isNumber
this.func = () => {};
this.num = 1;
<p>{{ num | isNumber }}</p>
<p>{{ func | isNumber }}</p>
isArray
API: any | isArray
this.arr = [1, 2];
this.num = 1;
<p>{{ num | isArray }}</p>
<p>{{ arr | isArray }}</p>
isObject
API: any | isObject
this.obj = {a: 1, b: 2};
this.num = 1;
<p>{{ num | isObject }}</p>
<p>{{ obj | isObject }}</p>
isGreaterThan
API: number | isGreaterThan: otherNumber
<p>{{ 1 | isGreaterThan: 1 }}</p>
<p>{{ 1 | isGreaterThan: 2 }}</p>
<p>{{ 2 | isGreaterThan: 1 }}</p>
isGreaterEqualThan
API: number | isGreaterEqualThan: otherNumber
<p>{{ 1 | isGreaterEqualThan: 1 }}</p>
<p>{{ 1 | isGreaterEqualThan: 2 }}</p>
<p>{{ 2 | isGreaterEqualThan: 1 }}</p>
isLessThan
API: number | isLessThan: otherNumber
<p>{{ 1 | isLessThan: 1 }}</p>
<p>{{ 1 | isLessThan: 2 }}</p>
<p>{{ 2 | isLessThan: 1 }}</p>
isLessEqualThan
API: number | isLessEqualThan: otherNumber
<p>{{ 1 | isLessEqualThan: 1 }}</p>
<p>{{ 1 | isLessEqualThan: 2 }}</p>
<p>{{ 2 | isLessEqualThan: 1 }}</p>
isEqualTo
API: number | isEqualTo: otherNumber
<p>{{ 1 | isEqualTo: 1 }}</p>
<p>{{ 1 | isEqualTo: '1' }}</p>
<p>{{ 1 | isEqualTo: 2 }}</p>
<p>{{ 2 | isEqualTo: 1 }}</p>
isNotEqualTo
API: number | isNotEqualTo: otherNumber
<p>{{ 1 | isNotEqualTo: 1 }}</p>
<p>{{ 1 | isNotEqualTo: '1' }}</p>
<p>{{ 1 | isNotEqualTo: 2 }}</p>
<p>{{ 2 | isNotEqualTo: 1 }}</p>
isIdenticalTo
API: number | isIdenticalTo: otherNumber
<p>{{ 1 | isIdenticalTo: 1 }}</p>
<p>{{ 1 | isIdenticalTo: '1' }}</p>
<p>{{ 1 | isIdenticalTo: 2 }}</p>
<p>{{ 2 | isIdenticalTo: 1 }}</p>
isNotIdenticalTo
API: number | isNotIdenticalTo: otherNumber
<p>{{ 1 | isNotIdenticalTo: 1 }}</p>
<p>{{ 1 | isNotIdenticalTo: '1' }}</p>
<p>{{ 1 | isNotIdenticalTo: 2 }}</p>
<p>{{ 2 | isNotIdenticalTo: 1 }}</p>