diff options
Diffstat (limited to 'src/app/pipes')
-rw-r--r-- | src/app/pipes/colon.pipe.ts | 29 | ||||
-rw-r--r-- | src/app/pipes/guard-type.pipe.ts | 32 | ||||
-rw-r--r-- | src/app/pipes/has-permission.pipe.ts | 58 | ||||
-rw-r--r-- | src/app/pipes/in.pipe.ts | 36 | ||||
-rw-r--r-- | src/app/pipes/is-today.pipe.ts | 37 | ||||
-rw-r--r-- | src/app/pipes/map.pipe.ts | 48 | ||||
-rw-r--r-- | src/app/pipes/translate-mock.pipe.ts | 42 |
7 files changed, 282 insertions, 0 deletions
diff --git a/src/app/pipes/colon.pipe.ts b/src/app/pipes/colon.pipe.ts new file mode 100644 index 0000000..fe0b50b --- /dev/null +++ b/src/app/pipes/colon.pipe.ts @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'colon', +}) +export class ColonPipe implements PipeTransform { + transform(value: string): string { + return `${value}: `; + } +} diff --git a/src/app/pipes/guard-type.pipe.ts b/src/app/pipes/guard-type.pipe.ts new file mode 100644 index 0000000..0c2b847 --- /dev/null +++ b/src/app/pipes/guard-type.pipe.ts @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from '@angular/core'; + +export type TypeGuard<A, B extends A> = (a: A) => a is B; + +// https://github.com/angular/angular/issues/34522#issuecomment-762973301 +@Pipe({ + name: 'guardType', +}) +export class GuardTypePipe implements PipeTransform { + transform<A, B extends A>(value: A, typeGuard: TypeGuard<A, B>): B | undefined { + return typeGuard(value) ? value : undefined; + } +} diff --git a/src/app/pipes/has-permission.pipe.ts b/src/app/pipes/has-permission.pipe.ts new file mode 100644 index 0000000..a22b034 --- /dev/null +++ b/src/app/pipes/has-permission.pipe.ts @@ -0,0 +1,58 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Inject, Pipe, PipeTransform } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; +import { ACL_CONFIG, AclConfig } from '../modules/auth/injection-tokens'; +import { AuthService } from '../services/auth.service'; +import { Observable } from 'rxjs'; +import { map, take } from 'rxjs/operators'; + +/* + hasPermission pipe returns Promise<boolean | void> value based on the authentication file (acl.json) and user's role. + Using the pipe we are able to show/hide the elements in the app for specific user role. + Input parameter is the string from the acl.json + USAGE: *ngIf="'dashboard.tile.KPI_DASHBOARD_TILE' | hasPermission | async" +*/ +@Pipe({ + name: 'hasPermission', +}) +export class HasPermissionPipe implements PipeTransform { + constructor( + readonly httpClient: HttpClient, + readonly authService: AuthService, + @Inject(ACL_CONFIG) readonly acl: AclConfig, + ) {} + + transform(value: string): Observable<boolean | void> { + return this.authService + .loadCachedUserProfile() + .pipe( + take(1), + map((userProfile) => { + const intersectionOfRoles = Object.keys(this.acl).filter(role => userProfile?.roles.includes(role)); + for (const role of intersectionOfRoles) { + if (this.acl[role].includes(value)) { + return true; + } + } + return false; + })) + } +} diff --git a/src/app/pipes/in.pipe.ts b/src/app/pipes/in.pipe.ts new file mode 100644 index 0000000..df6e8cc --- /dev/null +++ b/src/app/pipes/in.pipe.ts @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'in', +}) +export class InPipe implements PipeTransform { + transform(value: string, array: string[] | Set<any>): boolean { + if (array instanceof Array) { + return array.includes(value); + } + if (array instanceof Set) { + return array.has(value); + } else { + throw new Error('unsupported type'); + } + } +} diff --git a/src/app/pipes/is-today.pipe.ts b/src/app/pipes/is-today.pipe.ts new file mode 100644 index 0000000..1d2f17a --- /dev/null +++ b/src/app/pipes/is-today.pipe.ts @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from '@angular/core'; + +// return true if given parameter is today else return false + +@Pipe({ + name: 'isToday', +}) +export class IsTodayPipe implements PipeTransform { + transform(value: string | Date | number): boolean { + const date = new Date(value); + const today = new Date(); + return ( + date.getDate() == today.getDate() && + date.getMonth() == today.getMonth() && + date.getFullYear() == today.getFullYear() + ); + } +} diff --git a/src/app/pipes/map.pipe.ts b/src/app/pipes/map.pipe.ts new file mode 100644 index 0000000..e2c88a9 --- /dev/null +++ b/src/app/pipes/map.pipe.ts @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from '@angular/core'; + +@Pipe({ + name: 'map', + pure: false +}) +/* + MapPipe allows us to run a function in a template + for example, you need to filter out some elements from the array before displaying these elements. You can call a function for getting filtered array + directly from template, but this function will be triggered everytime when user interacts with page. + MapPipe allows you to call a function through this pipe, so function will be called only when necessary. + Usage: + we have function in .ts file that's called filterZeroValues + in template we can use this function: + *ngFor="let item in elements | map : filterZeroValues" + where the first parameter of map pipe is function to be called, and other parameters will be passed as arguments to this function + Important note: as you can see from implementation, elements array will be passed to your function as a first argument +*/ +export class MapPipe implements PipeTransform { + + transform<T, R>( + thisArg: T, + project: (t:T, ...others: any[]) => R, + ...args: any[] + ): R { + return project(thisArg, ...args); + } + +} diff --git a/src/app/pipes/translate-mock.pipe.ts b/src/app/pipes/translate-mock.pipe.ts new file mode 100644 index 0000000..80c68a7 --- /dev/null +++ b/src/app/pipes/translate-mock.pipe.ts @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022. Deutsche Telekom AG + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + + +import { Pipe, PipeTransform } from "@angular/core"; +/** + * This class can be used to mock the `translate` pipe in jasmine test cases. + * + * Usage: + * ``` typescript + * TestBed.configureTestingModule({ + declarations: [TranslatePipeMock,...], + providers: [{ provide: TranslatePipe, useClass: TranslatePipeMock },...] +}).compileComponents(); +* ``` +*/ +// Courtesy of: https://github.com/ngx-translate/core/issues/636#issuecomment-451137902 +@Pipe({ + name: 'translate' +}) +export class TranslatePipeMock implements PipeTransform { + public name = 'translate'; + + public transform(query: string): any { + return query; + } +} |