UNPKG

first-npm-package-nicule

Version:

This isi first npm package

71 lines (59 loc) 2.54 kB
import {filter, merge} from 'rxjs/operators'; import { Injectable, OnDestroy } from '@angular/core'; import { ActivatedRoute, ActivatedRouteSnapshot, ActivationEnd, Router } from '@angular/router'; import { Subscription , Observable, of } from 'rxjs'; @Injectable() export class LabelingService implements OnDestroy { private pageName: string; private routerSubscription: Subscription; constructor(private router: Router, private activatedRoute: ActivatedRoute) { this.routerSubscription = router.events.pipe( filter(routerEvent => routerEvent instanceof ActivationEnd)) .pipe(merge(of('init'))) .subscribe(_ => { const pageName = this.getChildMostActivatedRouteSnapshot(activatedRoute.snapshot).data.name; this.pageName = pageName || this.computePageNameFromRoute(window.location.pathname); }); } getChildMostActivatedRouteSnapshot(activatedRouteSnapshot: ActivatedRouteSnapshot): ActivatedRouteSnapshot { return activatedRouteSnapshot.children.length > 0 ? this.getChildMostActivatedRouteSnapshot(activatedRouteSnapshot.children[0]) : activatedRouteSnapshot; } ngOnDestroy(): void { this.routerSubscription.unsubscribe(); } computePageNameFromRoute(str: string): string { if (!str || str === '') { return 'global'; } const pascal = str.split('/') .filter(s => !/[0-9]/g.test(s)) .map(substr => { return substr.split('-') .map(s => s.charAt(0) .toUpperCase() + s.slice(1)) .join(''); }) .join(''); const firstCharacter = pascal.charAt(0) .toLowerCase(); return `${firstCharacter + pascal.slice(1)}Page`; } label(name: string, actionName: string, prefix: string = 'label'): string { return `${this.simplifyActionName(actionName)}.${name}.${prefix}`; } form(name: string, actionName: string): string { return `${this.simplifyActionName(actionName)}.form.${name}`; } simplifyActionName(actionName: string): string { const value = actionName.split('.') .reverse()[0]; return value.charAt(0) .toUpperCase() + value.slice(1); } page(name: string): string { return `${this.pageName}.${name}`; } }