mirror of
https://gitlab.com/openstapps/openstapps.git
synced 2026-01-22 17:42:57 +00:00
refactor: update to recent changes in translator
This commit is contained in:
86
src/app/translation/thing-translate.parser.ts
Normal file
86
src/app/translation/thing-translate.parser.ts
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright (C) 2020-2021 StApps
|
||||
* This program is free software: you can redistribute it and/or modify it
|
||||
* under the terms of the GNU General Public License as published by the Free
|
||||
* Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
* more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along with
|
||||
* this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
import {Injectable} from '@angular/core';
|
||||
|
||||
// tslint:disable: member-ordering prefer-function-over-method completed-docs
|
||||
|
||||
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
|
||||
*/
|
||||
abstract getValueFromKeyPath(instance: object, keypath: string): unknown;
|
||||
}
|
||||
|
||||
@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, '');
|
||||
// path = aproperty[0].anotherproperty[.arrayproperty.][42].finalproperty
|
||||
path = path.replace(/(?:\[|\])/gmi, '.');
|
||||
// path = aproperty.0..anotherproperty..arrayproperty...42..finalproperty
|
||||
path = path.replace(/\.{2,}/gmi, '.');
|
||||
// path = aproperty.0.anotherproperty.arrayproperty.42.finalproperty
|
||||
|
||||
const keypathChain = keypath.split('.');
|
||||
|
||||
// tslint:disable-next-line: no-any
|
||||
let property = instance as any;
|
||||
|
||||
for(const key of keypathChain) {
|
||||
property = property[key] ?? undefined;
|
||||
}
|
||||
|
||||
return property;
|
||||
}
|
||||
}
|
||||
|
||||
// tslint:disable-next-line: no-any
|
||||
export function isDefined(value: any): boolean {
|
||||
return typeof value !== 'undefined' && value !== null;
|
||||
}
|
||||
Reference in New Issue
Block a user