README
ngx-pipes
Useful pipes for Angular with no external dependencies
Table of contents
Installation
- Use npm to install the package
$ npm install ngx-pipes --save
- You could either add into your module
importstheNgPipesModulein order to add all of the pipes, Or add a specific module such asNgArrayPipesModule,NgObjectPipesModule,NgStringPipesModule,NgMathPipesModuleorNgBooleanPipesModule.
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/src/app/pipes/array/reverse';
@Component({
// ..
providers: [ReversePipe]
})
export class AppComponent {
constructor(private reversePipe: ReversePipe) {
this.reversePipe.transform('foo'); // Returns: "oof"
}
// ..
}
String
repeat
Repeats a string n times
Usage: string | repeat: times: [separator|optional]
<p>{{ 'example' | repeat: 3: '@' }}</p> <!-- Output: "example@example@example" -->
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> <!-- Output: "Hey foo, bar" -->
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> <!-- Output: "Hey..." -->
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> <!-- Output: "foo bar" -->
<p>{{'<a href="">foo</a> <p class="foo">bar</p>' | stripTags: 'p'}}</p> <!-- Output: foo <p class="foo">bar</p> -->
ucfirst
Uppercase first letter of first word
<p>{{'foo bar' | ucfirst }}</p> <!-- Output: "Foo bar" -->
ucwords
Uppercase first letter every word
<p>{{'foo bar' | ucwords }}</p> <!-- Output: "Foo Bar" -->
trim
Strips characters from the beginning and end of a string (default character is space).
Usage: string | trim: [characters|optional]
<p>{{' foo ' | trim }}</p> <!-- Output: "foo" -->
<p>{{'foobarfoo' | trim: 'foo' }}</p> <!-- Output: "bar" -->
ltrim
Strips characters from the beginning of a string (default character is space).
Usage: string | ltrim: [characters|optional]
<p>{{' foo ' | ltrim }}</p> <!-- Output: "foo " -->
<p>{{'foobarfoo' | ltrim: 'foo' }}</p> <!-- Output: "barfoo" -->
rtrim
Strips characters from the end of a string (default character is space).
Usage: string | rtrim: [characters|optional]
<p>{{' foo ' | rtrim }}</p> <!-- Output: " foo" -->
<p>{{'foobarfoo' | rtrim: 'foo' }}</p> <!-- Output: "foobar" -->
reverse
Reverses a string
Usage: string | reverse
<p>{{'foo bar' | reverse }}</p> <!-- Output: "rab oof" -->
slugify
Slugify a string (lower case and add dash between words).
Usage: string | slugify
<p>{{'Foo Bar' | slugify }}</p> <!-- Output: "foo-bar" -->
<p>{{'Some Text To Slugify' | slugify }}</p> <!-- Output: "some-text-to-slugify" -->
camelize
Camelize a string replaces dashes and underscores and converts to camelCase string.
Usage: string | camelize
<p>{{'foo_bar' | camelize }}</p> <!-- Output: "fooBar" -->
<p>{{'some_dashed-with-underscore' | camelize }}</p> <!-- Output: "someDashedWithUnderscore" -->
<p>{{'-dash_first-' | camelize }}</p> <!-- Output: "dashFirst" -->
latinise
Removes accents from Latin characters.
Usage: string | latinise
<p>{{'Féé' | latinise }}</p> <!-- Output: "Fee" -->
<p>{{'foo' | latinise }}</p> <!-- Output: "foo" -->
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> <!-- Output: "['Some', 'Sentence with', 'New line']" -->
underscore
Converts camelCase string to underscore.
Usage: string | underscore
<p>{{'angularIsAwesome' | underscore }}</p> <!-- Output: "angular_is_awesome" -->
<p>{{'FooBar' | underscore }}</p> <!-- Output: "foo_bar" -->
test
Tests if a string matches a pattern.
Usage: string | test: {RegExp}: {Flags}
<p>{{'foo 42' | test: '[\\d]+