A library of pipes for Angular apps.
Use npm to install @nglrx/pipes.
npm i @nglrx/pipes
Import module NglrxPipesModule
to your module for using all pipes.
import { NglrxPipesModule } from '@nglrx/pipes';
@NgModule({
//...
imports: [
NglrxPipesModule
]
})
export class YourModule { }
Alternatively, you can use pipes from specific module(s) like NglrxNumberPipesModule
or NglrxStringPipesModule
.
Pipes can be used in your component's template
{{ 'This-is-a-string' | length }}
<!-- Returns 16 -->
They can also be chained
{{ ' Another-string ' | trim | length }}
<!-- Returns 14 -->
Or they can be used within components or services by calling the transform
method
import { LengthPipe } from '@nglrx/pipes';
@Component({
providers: [ LengthPipe ]
})
export class YourComponent {
constructor(private lengthPipe: LengthPipe) {
this.lengthPipe.transform('Yet-another-string'); // Returns 18
}
}
A collection of pipes exported by NglrxStringPipesModule
.
Converts a string to camel case and strips hyphens, underscores and whitespaces.
Usage: string | camelCase
{{ 'Convert_to camel-case' | camelCase }}
<!-- Returns 'convertToCamelCase' -->
Returns the character value at given position in a string.
Usage: string | charAt [ : position ]
Range of position is from 0 (default) to n-1, where n is length of the string.
{{ 'This is a sample string.' | charAt: 12 }}
<!-- Returns 'm' -->
Concatenates one or more string(s) to current string at the end.
Usage: string | concat: string1 [ : string2 ] ...
{{ 'This' | concat: ' is': ' a': ' string': '!' }}
<!-- Returns 'This is a string!' -->
Replaces marked up parameters surrounded by { and } delimiters in given string with target string values.
The indices of parameters start from 0 and increment by 1.
The target string values can be literals or variables and their position should match the index.
Usage: string | interpolate: string1 [ : string2 ] ...
{{ 'This {0} an {1} {2}!' | interpolate: 'is': 'interpolated': 'string' }}
<!-- Returns 'This is an interpolated string!' -->
Converts a given string to lower case.
Usage: string | lowerCase
{{ 'Convert TO LoWeR-case' | lowerCase }}
<!-- Returns 'convert to lower-case' -->
Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is appended to the given string.
Default fill string is space ' '
.
Usage: string | padEnd: maxLength [ : fillString ]
{{ This is a test string! | padEnd: 29: '---' }}
<!-- Returns 'This is a test string!-------' -->
Pads the given string with a fill string so that the resulting string reaches the specified max length. The fill string is prepended to the given string.
Default fill string is space ' '
.
Usage: string | padStart: maxLength [ : fillString ]
{{ This is a test string! | padStart: 27: '--' }}
<!-- Returns '-----This is a test string!' -->
Converts a string to pascal case and strips hyphens, underscores and whitespaces.
Usage: string | pascalCase
{{ 'convert_to PASCAL-case' | pascalCase }}
<!-- Returns 'ConvertToPascalCase' -->
Repeats a given string 'count' number of times separated by an optional delimiter.
Default count is 1
. Default delimiter is empty string ''
.
An error is thrown if the value of count is less than 1.
Usage: string | repeat [ : count ] [ : delimiter ]
{{ 'Repeated' | repeat: 5: '_' }}
<!-- Returns Repeated_Repeated_Repeated_Repeated_Repeated -->
Converts a string to sentence case.
Usage: string | sentenceCase
{{ 'convert TO Sentence case.' | sentenceCase }}
<!-- Returns 'Convert to sentence case.' -->
Slugifies a given string with an optional char separator.
Default separator char is hyphen '-'.
Special characters are stripped from string.
Usage: string | slugify [ : separator ]
{{ 'this_-is__a - string!' | slugify: '_' }}
<!-- Returns 'this_is_a_string' -->
Splits a given string into an array of sub-strings using an optional delimiter.
Default delimiter is space ' '
.
Optionally, you may also specify a limit (integer) on the number of splits.
Usage: string | split [ : delimiter ] [ : limit ]
{{ 'This_is_a_string_separated_with_underscore' | split: '_': 4 }}
<!-- Returns ['This', 'is', 'a', 'string'] -->
Converts a string to titleCase case.
Usage: string | titleCase
{{ 'convert TO title cASE.' | titleCase }}
<!-- Returns 'Convert To Title Case.' -->
Strips the leading and trailing whitespaces from a given string.
Usage: string | trim
{{ ' This is a test string! ' | trim }}
<!-- Returns 'This is a test string!' -->
Strips the leading whitespaces from a given string.
Usage: string | trimLeft
{{ ' This is a test string! ' | trimLeft }}
<!-- Returns 'This is a test string! ' -->
Strips the trailing whitespaces from a given string.
Usage: string | trimRight
{{ ' This is a test string! ' | trimRight }}
<!-- Returns ' This is a test string!' -->
Shortens the given string to specified length
followed by an ellipsis '...'
.
Optionally, you may also specify a suffix (string).
An error is thrown if the value of length
is less than 1.
Usage: string | truncate : length [ : suffix ]
{{ 'This is a test string!' | truncate : 14 }}
<!-- Returns 'This is a test...' -->
Converts a given string to upper case.
Usage: string | upperCase
{{ 'Convert TO UpPeR-case.' | upperCase }}
<!-- Returns 'CONVERT TO UPPER-CASE.' -->
A collection of pipes exported by NglrxNumberPipesModule
.
Returns the absolute value of given number.
Usage: number | abs
{{ -384 | abs }}
<!-- Returns 384 -->
Returns the average of all numbers in a given array.
Usage: array | avg
{{ [10, 45, 200, 5, 92] | avg }}
<!-- Returns 70.4 -->
Returns the smallest number with specified decimal places greater than or equal to given number. By default the value is rounded-up to the nearest integer.
Optionally, the number of decimal places to which the result should be rounded-up may also be specified.
Usage: number | ceil [ : decimalPlaces]
{{ 9876.54321 | ceil: 2 }}
<!-- Returns 9876.55 -->
Returns the greatest number with specified decimal places less than or equal to given number. By default the value is rounded-down to the nearest integer.
Optionally, the number of decimal places to which the result should be rounded-down may also be specified.
Usage: number | floor [ : decimalPlaces]
{{ 1234.56789 | floor: 3 }}
<!-- Returns 1234.567 -->
Finds the maximum from an array of numbers.
Usage: array | max
{{ [10, 45, 200, 5, 92] | max }}
<!-- Returns 200 -->
Finds the minimum from an array of numbers.
Usage: array | min
{{ [10, 45, 200, 5, 92] | min }}
<!-- Returns 5 -->
Returns how much percent is a number of the given total. If not specified default value is 100.
Optionally, number of decimal places (integer) may be specified to round-off the percentage.
Usage: number | pct [ : total ] [ : decimalPlaces ]
{{ 25 | pct: 483: 2 }}
<!-- Returns 5.18 -->
Returns the value of the base raised to a specified power.
Default value of exponent is 0.
Usage: base | pow [ : exponent ]
{{ 4 | pow: 3 }}
<!-- Returns 64 -->
Returns the rounded value of given number. By default the value is rounded to the nearest integer.
It also accepts an optional argument RoundType
for rounding the value up or down.
RoundType.Default
= Default rounding as in Math.round()
RoundType.Floor
= Round down as in Math.floor()
RoundType.Ceil
= Round up as in Math.ceil()
Optionally, the number of decimal places to which the result should be rounded may also be specified.
Usage: number | round [ : decimalPlaces] [ : roundType ]
{{ 1234.56789 | round }}
<!-- Returns 1235 -->
{{ 1234.56789 | round: 3: RoundType.Floor }}
<!-- Returns 1234.567 -->
{{ 9876.54321 | round: 2: RoundType.Ceil }}
<!-- Returns 9876.55 -->
Returns the square root of given number.
Usage: number | sqrt
{{ 625 | sqrt }}
<!-- Returns 25 -->
Returns the sum of all numbers in a given array.
Usage: array | sum
{{ [10, 45, 200, 5, 92] | sum }}
<!-- Returns 352 -->
A collection of pipes exported by NglrxArrayPipesModule
.
Combines an array with other array(s) or single items of same type.
Usage: array | combine [ : element | array ]...
{{ ['a', 'b', 'c'] | combine: ['d', 'e']: 'f' }}
<!-- Returns ['a', 'b', 'c', 'd', 'e', 'f'] -->
Copies the portion of array marked by start and end to position specified by target
within the same array.
If start
is not specified then it copies from the beginning of array. If end
is not specified then it copies till the end of array.
Negative values of start and end are treated as length start/end.
Usage: array | copyWithin : target [ : start ] [ : end ]
{{ [1, 2, 3, 4, 5, 6] | copyWithin: 4: 1: -3 }}
<!-- Returns [1, 2, 3, 4, 2, 3] -->
Checks whether all the elements of the given array satisfy the specified test.
A callbackFn
function must be specified that accepts up to three arguments. The callbackFn is invoked for each element in the given array until it returns a false, or until the last element of the array.
Optionally, a reference thisArg
to an object to which the this
keyword can refer in the callbackFn function may be passed.
Usage: array | every : callbackFn
{{ ['a', 'b', 'c', 'd', 'e'] | every: (n: string) => n !== '' }}
<!-- Returns true -->
{{ [10, 11, 12, 13, 14] | every: (n: number) => n % 2 === 0) }}
<!-- Returns false -->
Fills the portion of array marked by start and end with specified value
of same type as array.
If start
is not specified then it fills from the beginning of array. If end
is not specified then it fills till the end of array.
Negative values of start and end are treated as length start/end.
Usage: array | fill : value [ : start ] [ : end ]
{{ ['a', 'b', 'c', 'd', 'e', 'f'] | fill: '-': 2: -2 }}
<!-- Returns ['a', 'b', '-', '-', 'e', 'f'] -->
Returns the first count
elements from the given array.
If no count is specified, by default the first element is returned.
Negative value of count is treated as length count
and values except last length count
are returned.
No values are returned if either count is 0
or value of count is beyond the limits.
Usage: array | first [ : count ]
{{ ['a', 'b', 'c', 'd', 'e'] | first }}
<!-- Returns ['a'] -->
{{ [1, 2, 3, 4, 5] | first: 2 }}
<!-- Returns [1, 2] -->
{{ [1, 2, 3, 4, 5] | first: -2 }}
<!-- Returns [1, 2, 3] -->
Creates a string by concatenating all the strings in the given array using a separator.
If unspecified, the default separator is comma ','
.
Usage: array | join [ : separator ]
{{ ['This', 'is', 'a', 'string'] | join: '_' }}
<!-- Returns 'This_is_a_string' -->
Returns the last count
elements from the given array.
If no count is specified, by default the last element is returned.
Negative value of count is treated as length count
and values except first length count
are returned.
No values are returned if either count is 0
or value of count is beyond the limits.
Usage: array | last [ : count ]
{{ ['a', 'b', 'c', 'd', 'e'] | last }}
<!-- Returns ['e'] -->
{{ [1, 2, 3, 4, 5] | last: 2 }}
<!-- Returns [4, 5] -->
{{ [1, 2, 3, 4, 5] | last: -2 }}
<!-- Returns [3, 4, 5] -->
Calls the specified callback function on each element of the given array, and returns an array of results returned by callback function.
A callbackFn
function must be specified that accepts up to three arguments. The callbackFn is invoked for each element in the given array.
Optionally, a reference thisArg
to an object to which the this
keyword can refer in the callbackFn function may be passed.
Usage: array | map : callbackFn
{{ ['a', 'b', 'c', 'd', 'e'] | map: (n: string) => n.toUpperCase() }}
<!-- Returns ['A', 'B', 'C', 'D', 'E'] -->
{{ [1, 2, 3, 4, 5] | map: (n: number) => n * n) }}
<!-- Returns [1, 4, 9, 16, 25] -->
Checks whether some the elements of the given array satisfy the specified test.
A callbackFn
function must be specified that accepts up to three arguments. The callbackFn is invoked for each element in the given array until it returns a true, or until the last element of the array.
Optionally, a reference thisArg
to an object to which the this
keyword can refer in the callbackFn function may be passed.
Usage: array | some : callbackFn
{{ ['a', 'b', 'c', 'd', 'e'] | some: (n: string) => n === '' }}
<!-- Returns false -->
{{ [10, 11, 12, 13, 14] | some: (n: number) => n % 2 === 0) }}
<!-- Returns true -->
A collection of pipes exported by NglrxGenericPipesModule
.
Returns the length of a given value of any supported type.
Supported data types are string, array, number, boolean, or any data type which has own property 'length'.
For an array the number of elements is returned. For others the number of characters in value is returned.
Usage: value | length
{{ 'This is a test string!' | length }}
<!-- Returns 22 -->
{{ [10, 45, 200, 50, 92] | length }}
<!-- Returns 5 -->
Reverses a given value of any supported type.
Supported data types are string, array, number, boolean.
For an array the sequence of elements is reversed. For others the sequence of characters in value is reversed.
Usage: value | reverse
{{ 'This is a test string!' | reverse }}
<!-- Returns '!gnirts tset a si sihT' -->
{{ ['a', 'b', 'c', 'd', 'e'] | reverse }}
<!-- Returns ['e', 'd', 'c', 'b', 'a'] -->
Returns the type of given value.
Returns the name of the type in string. All types are supported.
Usage: value | typeOf
{{ 'This is a test string!' | typeOf }}
<!-- Returns 'string' -->
{{ { foo: 'bar' } | typeOf }}
<!-- Returns 'object' -->
For more information on pipes, refer to Angular - pipes documentation.