mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-20 16:42:56 +00:00
refactor: update openstapps/api
This commit is contained in:
@@ -19,13 +19,8 @@ import {Injectable} from '@angular/core';
|
||||
|
||||
export abstract class ThingTranslateParser {
|
||||
/**
|
||||
* Gets a value from an object by composed key
|
||||
* parser.getValue({ key1: { keyA: 'valueI' }}, 'key1.keyA') ==> 'valueI'
|
||||
*/
|
||||
abstract getValue(target: unknown, key: string): string;
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* Gets a value from an object given a keypath
|
||||
* paser.getValueFromKeyPath(anObject, 'property.subarray[42].etc');
|
||||
*/
|
||||
abstract getValueFromKeyPath(instance: object, keypath: string): unknown;
|
||||
}
|
||||
@@ -33,34 +28,9 @@ export abstract class ThingTranslateParser {
|
||||
@Injectable()
|
||||
export class ThingTranslateDefaultParser extends ThingTranslateParser {
|
||||
|
||||
/**
|
||||
* TODO
|
||||
*
|
||||
*/
|
||||
getValue(target: unknown, key: string): string {
|
||||
const keys = typeof key === 'string' ? key.split('.') : [key];
|
||||
let aKey = '';
|
||||
// tslint:disable-next-line: no-any
|
||||
let newTarget = target as any;
|
||||
do {
|
||||
aKey += keys.shift();
|
||||
if (isDefined(newTarget) && isDefined(newTarget[aKey]) && (typeof newTarget[aKey] === 'object' || keys.length === 0)) {
|
||||
newTarget = newTarget[aKey];
|
||||
aKey = '';
|
||||
} else if (keys.length === 0) {
|
||||
newTarget = undefined;
|
||||
} else {
|
||||
aKey += '.';
|
||||
}
|
||||
} while (keys.length > 0);
|
||||
|
||||
return newTarget;
|
||||
}
|
||||
|
||||
|
||||
getValueFromKeyPath(instance: object, keypath: string): unknown {
|
||||
// keypath = aproperty[0].anotherproperty["arrayproperty"][42].finalproperty
|
||||
let path = keypath.replace(/(?:\"|\')'"/gmi, '');
|
||||
let path = keypath.replace(/(?:\"|\')'"/gmi, '.');
|
||||
// path = aproperty[0].anotherproperty[.arrayproperty.][42].finalproperty
|
||||
path = path.replace(/(?:\[|\])/gmi, '.');
|
||||
// path = aproperty.0..anotherproperty..arrayproperty...42..finalproperty
|
||||
@@ -80,7 +50,6 @@ export class ThingTranslateDefaultParser extends ThingTranslateParser {
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: no-any
|
||||
export function isDefined(value: any): boolean {
|
||||
export function isDefined<T>(value?: T | null): value is T {
|
||||
return typeof value !== 'undefined' && value !== null;
|
||||
}
|
||||
|
||||
@@ -42,7 +42,7 @@ export class ThingTranslatePipe implements PipeTransform, OnDestroy {
|
||||
this.value = this.thingTranslate.get(thing, key);
|
||||
}
|
||||
|
||||
transform(query: string, thing: SCThings): unknown {
|
||||
transform(query: unknown, thing: SCThings): unknown {
|
||||
if (typeof query !== 'string' || query.length <= 0) {
|
||||
return query;
|
||||
}
|
||||
@@ -99,7 +99,6 @@ export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy
|
||||
onLangChange: Subscription;
|
||||
|
||||
constructor(private readonly translate: TranslateService,
|
||||
// private readonly _ref: ChangeDetectorRef,
|
||||
private readonly thingTranslate: ThingTranslateService) {
|
||||
}
|
||||
|
||||
@@ -107,7 +106,7 @@ export class ThingPropertyNameTranslatePipe implements PipeTransform, OnDestroy
|
||||
this.value = this.thingTranslate.getPropertyName(type as SCThingType, key);
|
||||
}
|
||||
|
||||
transform(query: string, thingOrType: SCThings | string): unknown {
|
||||
transform(query: unknown, thingOrType: SCThings | string | unknown): unknown {
|
||||
if (typeof query !== 'string' || query.length <= 0) {
|
||||
return query;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user