@ashetm/ng-utility
v15.0.1
Published
``@ashetm/ng-utility`` is a library that provide some utilities classes, like pipes.
Downloads
4
Maintainers
Readme
@ashetm/ng-utility
@ashetm/ng-utility
is a library that provide some utilities classes, like pipes.
It works with Angular 14 and above
Install
You can install it with npm:
npm install @ashetm/ng-utility
Import
You only need to import UtilityModule
.
...
import { UtilityModule } from '@ashetm/ng-utility';
...
@NgModule({
...
imports: [
...
UtilityModule,
...
]
...
})
export class AppModule { }
API
@ashetm/ng-utility
exposes the following:
Modules
UtilityModule
, that needs to import in order to use the library.
Pipes
All pipes are standalone pipes.
ArrayFilter
<T extends string | number | boolean> transform(
value: T[],
filterBy?: string | number | boolean
): (string | number | boolean)[] { ... }
Use on array of string, number or boolean items, and needs an optional argument of type string, number or boolean. If no argument provided, it will return the same array.
Example:
{{ [0, 'test', false, 100, 'texts'] | arrayfilter }} // [0, 'test', false, 100, 'texts']
{{ [0, 'test', false, 100, 'texts'] | arrayfilter:'te' }} // ['test', 'texts']
ArrayFrom
transform(length: number, fill?: number): number[] { ... }
Use on a number, and needs an optional argument of type number. If no argument provided, it will return an array with index as value.
Example:
{{ 5 | arrayFrom }} // [0, 1, 2, 3, 4]
{{ 5 | arrayFrom:10 }} // [10, 10, 10, 10, 10]
ArrayPluck
transform(
value: Array<Record<string | number, any>>,
key: (string | number)
): Array<any> { ... }
Use on array of object items, and needs an argument of type number or string.
Example:
{{ [{ a: 1, b: 5 }, { a: 2, c: 4 }, { a: true, b: 'test', c: {} }] | arrayPluck:'b' }} // [5, 'test']
{{ [{ a: 1, b: 5 }, { a: 2, c: 4 }, { a: true, b: 'test', c: {} }] | arrayPluck:'a' }} // [1, 2, true]
CapitalCase
transform(value: string): string { ... }
Use on string.
Example:
{{ 'Lorum ipsum' | capitalCase }} // 'Lorum Ipson'
CoerciveBoolean
transform(value?: any): boolean { ... }
Use on any type.
Example:
{{ undefined | coerciveBoolean }} // true
{{ null | coerciveBoolean }} // false
{{ 'true' | coerciveBoolean }} // true
ControlErrors
transform(controls: AbstractControl): (string | any)[] { ... }
Use on any AbstractControl
type.
Example:
formControl = new FormControl(null, {
validators: [Validators.required, () => ({ customValidator: 'Custom Error Message in any key name!' })]
});
...
{{ formControl | controlErrors }} // ['This field is required', 'Custom Error Message in any key name!']
ControlValue
transform(value: AbstractControl): any { ... }
Use on any AbstractControl
type.
Example:
formGroup = new FormGroup({
test1: new FormControl('test'),
test2: new FormControl(null),
});
...
{{ formGroup | controlValue }} // { test1: 'test', test2: null }
Initial
transform(value: string, max: number = Infinity): string { ... }
Use on a string, and needs an optional argument of type number. If no argument provided, it will return string with initial of all words.
Example:
{{ 'abc Def Ghi Jkl' | initial }} // 'aDGJ'
{{ 'abc Def Ghi Jkl' | initial:2 }} // 'aD'
IsDate
transform(value: any): boolean { ... }
Use on a string, and needs an optional argument of type number. If no argument provided, it will return string with initial of all words.
Example:
{{ 1676808350561 | isDate }} // true
{{ 'Sun Feb 19 2023 13:05:50 GMT+0100 (UTC+01:00)' | isDate }} // true
{{ 'WRONG' | isDate }} // false
ObjectEntries
transform(value: Record<string | number, any>): [string | number, any][] { ... }
Use on a object.
Example:
Same as Object.entries
.
ObjectKeys
transform(value: Record<string | number, any>): Array<string> { ... }
Use on a object.
Example:
Same as Object.keys
.
ObjectValues
transform(value: Record<string | number, any>): any[] { ... }
Use on a string, and needs an optional argument of type number.
Example:
Same as Object.values
.
Issue
LOOKING FOR MAINTAINER OR IF THERE IS AN ISSUE OR ANY IDEA TO ADD. PLEASE CREATE ISSUE IN GITHUB REPOSITORY.