Useful pipes for Angular with no external dependencies
TypeSerializer - Serializer / Deserializer, designed to make prettier code while using decorators.
Segal Decorators - Bunch of highly useful decorators, helps in writing a more concise code while improving readability
Table of contents
Installation
- Use npm to install the package
$ npm install ngx-pipes --save
- You could either add into your module
imports
the NgPipesModule
in order to add all of the pipes, Or add a specific module such as NgArrayPipesModule
, NgObjectPipesModule
, NgStringPipesModule
, NgMathPipesModule
, NgDatePipesModule
or NgBooleanPipesModule
.
import {NgPipesModule} from 'ngx-pipes';
@NgModule({
imports: [
NgPipesModule
]
})
- Pipes are also injectable and can be used in Components / Services / etc..
import {ReversePipe} from 'ngx-pipes';
@Component({
providers: [ReversePipe]
})
export class AppComponent {
constructor(private reversePipe: ReversePipe) {
this.reversePipe.transform('foo');
}
}
- You can also use pipes as part of your template for ex.
<p>{{ 'foo' | reverse }}</p>
and it's also possible to stack multiple pipes
<p>{{ ' foo' | ltrim | reverse }}</p>
Date
timeAgo
Time ago pipe converts date to 'just now', 'X days ago', 'last week', 'X days ago', etc..
Usage: string | timeAgo
import * as moment from 'moment';
const now = new Date();
const lastWeek = moment().subtract(10, 'days');
<span>Updated: {{now | timeAgo}}</span>
<span>Updated: {{lastWeek | timeAgo}}</span>
String
aOrAn
Prefixes input string with "a" or "an".
Usage: string | aOrAn
<span>This is a picture of {{imageDescription | aOrAn}}</span>
repeat
Repeats a string n times
Usage: string | repeat: times: [separator|optional]
<p>{{ 'example' | repeat: 3: '@' }}</p>
scan
Scans string and replace {i}
placeholders by equivalent member of the array
Usage: 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
Usage: 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
Usage: 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>
<p>{{'shaquille o'neal' | ucwords }}</p>
trim
Strips characters from the beginning and end of a string (default character is space).
Usage: 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).
Usage: 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).
Usage: string | rtrim: [characters|optional]
<p>{{' foo ' | rtrim }}</p>
<p>{{'foobarfoo' | rtrim: 'foo' }}</p>
reverse
Reverses a string
Usage: string | reverse
<p>{{'foo bar' | reverse }}</p>
slugify
Slugify a string (lower case and add dash between words).
Usage: string | slugify
<p>{{'Foo Bar' | slugify }}</p>
<p>{{'Some Text To Slugify' | slugify }}</p>
camelize
Camelize a string replaces dashes and underscores and converts to camelCase string.
Usage: string | camelize
<p>{{'foo_bar' | camelize }}</p>
<p>{{'some_dashed-with-underscore' | camelize }}</p>
<p>{{'-dash_first-' | camelize }}</p>
latinise
Removes accents from Latin characters.
Usage: string | latinise
<p>{{'Féé' | latinise }}</p>
<p>{{'foo' | latinise }}</p>
lines
Converts a string with new lines into an array of each line.
Usage: string | lines
<p>{{'Some\nSentence with\r\nNew line' | lines }}</p>
underscore
Converts camelCase string to underscore.
Usage: string | underscore
<p>{{'angularIsAwesome' | underscore }}</p>
<p>{{'FooBar' | underscore }}</p>
test
Tests if a string matches a pattern.
Usage: string | test: {RegExp}: {Flags}
<p>{{'foo 42' | test: '[\\d]+$': 'g' }}</p>
<p>{{'42 foo' | test: '[\\d]+$': 'g' }}</p>
<p>{{'FOO' | test: '^foo': 'i' }}</p>
match
Returns array of matched elements in string.
Usage: string | match: {RegExp}: {Flags}
<p>{{'foo 42' | match: '[\\d]+$': 'g' }}</p>
<p>{{'42 foo' | match: '[\\d]+$': 'g' }}</p>
<p>{{'FOO' | match: '^foo': 'i' }}</p>
lpad
Left pad a string to a given length using a given pad character (default is a space)
Usage: string | lpad: length: [padCharacter:string|optional]
<p>{{'foo' | lpad: 5}}</p>
<p>{{String(3) | lpad: 5: '0'}}</p>
rpad
Right pad a string to a given length using a given pad character (default is a space)
Usage: string | rpad: length: [padCharacter:string|optional]
<p>{{'Foo' | rpad: 5: '#'}}</p>
makePluralString
Make a singular string plural. Optional quantity
argument specifies how many of the singular string there are.
Usage: string | makePluralString: [quantity:string|optional]
<span>{{'Painting' | makePluralString}}</span>
<span>{{'Painting' | makePluralString: 1}}</span>
<span>{{'One Painting' | makePluralString: 1}}</span>
<span>{{'Painting' | makePluralString: 4}}</span>
<span>{{'Four Painting' | makePluralString: 4}}</span>
wrap
Wrap a string between a prefix and a suffix
Usage: string | wrap: prefix: suffix
<p>{{'Foo' | wrap: 'nice prefix ': ' and awesome suffix!'}}</p>
Array
diff
Returns array of diff between arrays
Usage: 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
Usage: 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
Usage: 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
Usage: array | tail: n
this.items = [first, second, third];
<li *ngFor="let item of items | tail: 1">
intersection
Returns the intersections of an arrays
Usage: array | intersection: [ARRAY]: [ARRAY]: ... : [ARRAY]
this.items = [1, 2, 3, 4, 5];
<li *ngFor="let item of items | intersection: [1, 2, 3]: [3, 6]">
range
Returns an array with range of numbers
Usage: range: [start: number, default = '1']: [count: number]: [step: number | optional, default = '1']
this.items = this.rangePipe.transform(1, 5);
<li *ngFor="let item of items">
reverse
Reverses an array
Usage: array | reverse
this.items = [1, 2, 3];
<li *ngFor="let item of items | reverse">
truthify
Removes un-truthy values from array
Usage: array | truthify
this.items = [null, 1, false, undefined, 2, 0, 3, NaN, 4, ''];
<li *ngFor="let item of items | truthify">
union
Combine two arrays
Usage: array | union: [ARRAY]
this.items = [1, 2, 3];
<li *ngFor="let item of items | union: [[4]]">
unique
Removes duplicates from array
Usage: array | unique: 'Property (Optional)'
this.items = [1, 2, 3, 1, 2, 3];
<li *ngFor="let item of items | unique">
without
Returns array without specific elements
Usage: 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
Usage: 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
Usage: 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
Usage: 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
Usage: 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
Usage: array | sample: [amount | default = 1]
<p>{{ [1, 2, 3, 4] | sample }}</p>
<p>{{ [1, 2, 3, 4] | sample: 2 }}</p>
groupBy
Returns object of grouped by items by discriminator, and supports nested properties.
Usage: array | groupBy: [string[] | string | Function]: [delimiter: string | optional, default = '|']
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' }}
];
<p>{{ arrayObject | groupBy: 'elm' }}</p>
<p>{{ arrayObject | groupBy: ['elm', 'value'] }}</p>
<p>{{ arrayObject | groupBy: ['elm', 'value']: '_' }}</p>
<p>{{ arrayNestedObject | groupBy: 'prop.deep' }}</p>
groupByImpure
Identical to groupBy pipe, the only difference is that it's an impure pipe.
Impure pipes: https://angular.io/guide/pipes#impure-pipes
filterBy
Returns object array of grouped by items by discriminator
Usage: array | filterBy: [prop, nested.prop, ...]: search: [strict | optional]
this.users = [
{id: 1, first_name: 'John', last_name: 'Doe', work: { company: 'Foo Tech' }},
{id: 2, first_name: 'Jane', last_name: 'West', work: { company: 'AAA Solutions' }},
{id: 3, first_name: 'Bruce', last_name: 'John', work: { company: 'Bar Tech' }},
{id: 4, first_name: 'William', last_name: 'Cent', work: { company: 'Foo Tech' }, arr: [{name: 'foo'}]}
];
<p>{{ users | filterBy: ['id']: 1 }}</p>
<p>{{ users | filterBy: ['work.company']: 'Bar Tech' }}</p>
<p>{{ users | filterBy: ['arr.name']: 'foo' }}</p>
<p>{{ users | filterBy: ['first_name', 'last_name']: 'John' }}</p>
<p>{{ users | filterBy: ['first_name', 'last_name']: ['John', 'Cent'] }}</p>
filterByImpure
Identical to filterBy pipe, the only difference is that it's an impure pipe.
Impure pipes: https://angular.io/guide/pipes#impure-pipes
orderBy
Returns ordered array by configuration
Usage: array | orderBy: [prop, nested.prop, array of props, ...]
const numbers = [2, 1, 3];
const obj = [
{id: 4, name: 'Dave', amount: 2},
{id: 2, name: 'Michael', amount: 2},
{id: 3, name: 'Dan', amount: 1},
{id: 1, name: 'John', amount: 1}
];
const deepObj = [
{id: 1, name: 'John', amount: 1337, deep: {prop: 4}},
{id: 2, name: 'Michael', amount: 42, deep: {prop: 2}},
{id: 3, name: 'Dan', amount: 1, deep: {prop: 1}},
{id: 4, name: 'Dave', amount: 2, deep: {prop: 3}}
];
<p>{{ numbers | orderBy }}</p>
<p>{{ numbers | orderBy: '-' }}</p>
<p>{{ deepObj | orderBy: 'amount' }}</p>
<p>{{ deepObj | orderBy: '-amount' }}</p>
<p>{{ deepObj | orderBy: 'deep.prop' }}</p>
<p>{{ deepObj | orderBy: '-deep.prop' }}</p>
<p>{{ obj | orderBy: ['amount', 'id'] }}</p>
orderByImpure
Identical to orderBy pipe, the only difference is that it's an impure pipe.
Impure pipes: https://angular.io/guide/pipes#impure-pipes
chunk
Returns chunked array or string by size
Usage: array | size: [number | default = 1]
<p>{{ [1, 2, 3, 4, 5] | chunk: 2 }}</p>
fromPairs
Returns object of an array of key value pairs
Usage: array | fromPairs
<p>{{ [['foo', 1], ['bar', 2]] | fromPairs }}</p>
<p>{{ [['foo', [1, 2]], ['bar', [3, 4]]] | fromPairs }}</p>
Object
keys
Returns array of object keys
Usage: object | keys
<p>{{ {foo: 1, bar: 2} | keys }}</p>
values
Returns array of object values
Usage: object | values
<p>{{ {foo: 1, bar: 2} | values }}</p>
pairs
Returns array of an object key value pairs
Usage: object | pairs
<p>{{ {foo: 1, bar: 2} | pairs }}</p>
<p>{{ {foo: [1, 2], bar: [3, 4]} | pairs }}</p>
pick
Returns object with picked keys from object
Usage: object | pick: [key | string]]
<p>{{ {foo: 1, bar: 2} | pick: 'foo' }}</p>
<p>{{ {foo: 1, bar: 2} | pick: 'foo': 'bar' }}</p>
omit
Returns object after omitting keys from object (opposite of pick)
Usage: object | omit: [key | string]]
<p>{{ {foo: 1, bar: 2} | omit: 'foo' }}</p>
<p>{{ {foo: 1, bar: 2} | omit: 'foo': 'bar' }}</p>
invert
Returns object with inverted keys and values. in case of equal values, subsequent values overwrite property assignments of previous values.
Usage: object | invert
<p>{{ {foo: 1, bar: 2} | invert }}</p>
invertBy
Returns object with inverted keys and values. in case of equal values, will add to an array.
Usage: object | invertBy: [Function | optional]
this.cb = (value): string => {
return `name_${value}`;
};
<p>{{ {foo: 1, bar: 2} | invertBy }}</p>
<p>{{ {foo: 1, bar: 2} | invertBy: cb }}</p>
<p>{{ {a: 1, b: 2, c: 1, d: 2} | invertBy }}</p>
diffObj
Returns a diff object of two objects
Usage: object | diffObj: Object
<p>{{ {a: 1} | diffObj: {a: 1} }}</p>
<p>{{ {a: 1} | diffObj: {a: 2} }}</p>
<p>{{ {a: 1, b: 2} | diffObj: {a: 1, b: 1} }}</p>
<p>{{ {a: 1, b: 2, c: {d: 3} } | diffObj: {a: 1, b: 1, c: {d: 1} } }}</p>
Math
min
Returns the minimum of a given array
Usage: array | min
<p>{{ [1, 2, 3, 1, 2, 3] | min }}</p>
max
Returns the maximum of a given array
Usage: array | max
<p>{{ [1, 2, 3, 1, 2, 3] | max }}</p>
sum
Returns the sum of a given array
Usage: array | sum
<p>{{ [1, 2, 3, 4] | sum }}</p>
average
Returns the average of a given array
Usage: array | average
<p>{{ [1, 2, 3] | average }}</p>
<p>{{ [1, 2] | average }}</p>
percentage
Returns percentage between numbers
Usage: 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
Usage: number | ceil: [precision | default = 0]
<p>{{ 42.123 | ceil }}</p>
<p>{{ 42.123 | ceil: 2 }}</p>
floor
Returns floor of a number by precision
Usage: number | floor: [precision | default = 0]
<p>{{ 42.123 | floor }}</p>
<p>{{ 42.123 | floor: 2 }}</p>
round
Returns round of a number by precision
Usage: 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
Usage: number | sqrt
<p>{{ 9 | sqrt }}</p>
pow
Returns the power of a number
Usage: number | pow: [power | default = 2]
<p>{{ 3 | pow }}</p>
<p>{{ 3 | pow: 3 }}</p>
degrees
Returns the degrees of a number in radians
Usage: number | degrees
<p>{{ 3.141592653589793 | degrees }}</p>
radians
Returns the radians of a number in degrees
Usage: number | radians
<p>{{ 180 | radians }}</p>
bytes
Returns bytes with a unit symbol
Usage: number | bytes: [precision]
<p>{{ 10 | bytes }}</p>
<p>{{ 1048576 | bytes }}</p>
<p>{{ 1073741824 | bytes }}</p>
<p>{{ 1.0995116e12 | bytes }}</p>
Boolean
isNull
Usage: any | isNull
<p>{{ null | isNull }}</p>
<p>{{ 1 | isNull }}</p>
isDefined
Usage: any | isDefined
<p>{{ 1 | isDefined }}</p>
<p>{{ undefined | isDefined }}</p>
isUndefined
Usage: any | isUndefined
<p>{{ 1 | isUndefined }}</p>
<p>{{ undefined | isUndefined }}</p>
isString
Usage: any | isString
<p>{{ 1 | isString }}</p>
<p>{{ '' | isString }}</p>
isNumber
Usage: any | isNumber
this.func = () => {};
this.num = 1;
<p>{{ num | isNumber }}</p>
<p>{{ func | isNumber }}</p>
isArray
Usage: any | isArray
this.arr = [1, 2];
this.num = 1;
<p>{{ num | isArray }}</p>
<p>{{ arr | isArray }}</p>
isObject
Usage: any | isObject
this.obj = {a: 1, b: 2};
this.num = 1;
<p>{{ num | isObject }}</p>
<p>{{ obj | isObject }}</p>
isGreaterThan
Usage: number | isGreaterThan: otherNumber
<p>{{ 1 | isGreaterThan: 1 }}</p>
<p>{{ 1 | isGreaterThan: 2 }}</p>
<p>{{ 2 | isGreaterThan: 1 }}</p>
isGreaterEqualThan
Usage: number | isGreaterEqualThan: otherNumber
<p>{{ 1 | isGreaterEqualThan: 1 }}</p>
<p>{{ 1 | isGreaterEqualThan: 2 }}</p>
<p>{{ 2 | isGreaterEqualThan: 1 }}</p>
isLessThan
Usage: number | isLessThan: otherNumber
<p>{{ 1 | isLessThan: 1 }}</p>
<p>{{ 1 | isLessThan: 2 }}</p>
<p>{{ 2 | isLessThan: 1 }}</p>
isLessEqualThan
Usage: number | isLessEqualThan: otherNumber
<p>{{ 1 | isLessEqualThan: 1 }}</p>
<p>{{ 1 | isLessEqualThan: 2 }}</p>
<p>{{ 2 | isLessEqualThan: 1 }}</p>
isEqualTo
Usage: number | isEqualTo: otherNumber
<p>{{ 1 | isEqualTo: 1 }}</p>
<p>{{ 1 | isEqualTo: '1' }}</p>
<p>{{ 1 | isEqualTo: 2 }}</p>
<p>{{ 2 | isEqualTo: 1 }}</p>
isNotEqualTo
Usage: number | isNotEqualTo: otherNumber
<p>{{ 1 | isNotEqualTo: 1 }}</p>
<p>{{ 1 | isNotEqualTo: '1' }}</p>
<p>{{ 1 | isNotEqualTo: 2 }}</p>
<p>{{ 2 | isNotEqualTo: 1 }}</p>
isIdenticalTo
Usage: number | isIdenticalTo: otherNumber
<p>{{ 1 | isIdenticalTo: 1 }}</p>
<p>{{ 1 | isIdenticalTo: '1' }}</p>
<p>{{ 1 | isIdenticalTo: 2 }}</p>
<p>{{ 2 | isIdenticalTo: 1 }}</p>
isNotIdenticalTo
Usage: number | isNotIdenticalTo: otherNumber
<p>{{ 1 | isNotIdenticalTo: 1 }}</p>
<p>{{ 1 | isNotIdenticalTo: '1' }}</p>
<p>{{ 1 | isNotIdenticalTo: 2 }}</p>
<p>{{ 2 | isNotIdenticalTo: 1 }}</p>
Contributing
- Before adding any new feature or a fix make sure to open an issue first!
Make sure you have angular-cli
& karma
installed globally.
$ npm install -g angular-cli karma
Clone the project, and install dependencies.
$ git clone https://github.com/danrevah/ngx-pipes.git
$ npm install
Create a new branch
$ git checkout -b feat/someFeature
Add tests & make sure everything is running properly
$ npm test
Commit & push, and make a pull request!