[ACS-8001] move isDefined to object-utils

This commit is contained in:
Mykyta Maliarchuk
2024-06-14 08:46:55 +02:00
committed by Mykyta Maliarchuk
parent 1e79753ad0
commit e359e5fbea
3 changed files with 40 additions and 34 deletions

View File

@@ -18,7 +18,6 @@
import { ObjectUtils } from './object-utils'; import { ObjectUtils } from './object-utils';
describe('ObjectUtils', () => { describe('ObjectUtils', () => {
it('should get top level property value', () => { it('should get top level property value', () => {
const obj = { const obj = {
id: 1 id: 1
@@ -246,4 +245,19 @@ describe('ObjectUtils', () => {
expect(ObjectUtils.booleanPrettify(obj, enhancer)).toBe('&#9989 testOnetest\n&#10060 testTwotest'); expect(ObjectUtils.booleanPrettify(obj, enhancer)).toBe('&#9989 testOnetest\n&#10060 testTwotest');
}); });
}); });
describe('isValueDefined', () => {
it('should return true for defined values', () => {
expect(ObjectUtils.isValueDefined(0)).toBe(true);
expect(ObjectUtils.isValueDefined('')).toBe(true);
expect(ObjectUtils.isValueDefined([])).toBe(true);
expect(ObjectUtils.isValueDefined({})).toBe(true);
expect(ObjectUtils.isValueDefined(false)).toBe(true);
});
it('should return false for undefined or null values', () => {
expect(ObjectUtils.isValueDefined(undefined)).toBe(false);
expect(ObjectUtils.isValueDefined(null)).toBe(false);
});
});
}); });

View File

@@ -25,7 +25,6 @@ export class ObjectUtils {
* @returns object property value * @returns object property value
*/ */
static getValue(target: any, key: string): any { static getValue(target: any, key: string): any {
if (!target || !key) { if (!target || !key) {
return undefined; return undefined;
} }
@@ -80,47 +79,43 @@ export class ObjectUtils {
} }
static isBooleanObject(target: any): boolean { static isBooleanObject(target: any): boolean {
return Object.values(target).every(value => typeof value === 'boolean'); return Object.values(target).every((value) => typeof value === 'boolean');
} }
static booleanPrettify(target: any, enhancer?: (param: string) => string): string { static booleanPrettify(target: any, enhancer?: (param: string) => string): string {
if (!target || ObjectUtils.isEmpty(target) || !ObjectUtils.isBooleanObject(target)) {
if (
!target ||
ObjectUtils.isEmpty(target) ||
!ObjectUtils.isBooleanObject(target)
) {
return ''; return '';
} }
if ( if (!ObjectUtils.isObject(target) || !ObjectUtils.hasKeys(target)) {
!ObjectUtils.isObject(target) ||
!ObjectUtils.hasKeys(target)
) {
return target.toString(); return target.toString();
} }
const greenBorderWhiteCheckSymbol = '&#9989'; const greenBorderWhiteCheckSymbol = '&#9989';
const redCrossSymbol = '&#10060'; const redCrossSymbol = '&#10060';
target = Object.keys(target).map((key) => { target = Object.keys(target)
if (target[key]) { .map((key) => {
if (enhancer) { if (target[key]) {
return `${greenBorderWhiteCheckSymbol} ${enhancer(key)}`; if (enhancer) {
} else { return `${greenBorderWhiteCheckSymbol} ${enhancer(key)}`;
return `${greenBorderWhiteCheckSymbol} ${key}`; } else {
return `${greenBorderWhiteCheckSymbol} ${key}`;
}
} }
} if (enhancer) {
return `${redCrossSymbol} ${enhancer(key)}`;
if (enhancer) { } else {
return `${redCrossSymbol} ${enhancer(key)}`; return `${redCrossSymbol} ${key}`;
} else { }
return `${redCrossSymbol} ${key}`; })
} .join('\n');
}).join('\n');
return target; return target;
} }
static isValueDefined(value: any): boolean {
return value !== undefined && value !== null;
}
} }

View File

@@ -27,6 +27,7 @@ import { Prediction, ReviewStatus } from '@alfresco/js-api';
import { IconComponent } from '../../../icon'; import { IconComponent } from '../../../icon';
import { LocalizedDatePipe } from '../../../pipes'; import { LocalizedDatePipe } from '../../../pipes';
import { PredictionService } from '../../services'; import { PredictionService } from '../../services';
import { ObjectUtils } from '../../../common';
@Component({ @Component({
standalone: true, standalone: true,
@@ -65,8 +66,8 @@ export class ContentEnrichmentMenuComponent implements OnInit {
ngOnInit() { ngOnInit() {
this.confidencePercentage = this.prediction?.confidenceLevel * 100 || 0; this.confidencePercentage = this.prediction?.confidenceLevel * 100 || 0;
this.previousValue = this.isDefined(this.prediction?.previousValue) ? this.prediction.previousValue : ''; this.previousValue = ObjectUtils.isValueDefined(this.prediction?.previousValue) ? this.prediction.previousValue : '';
this.predictionValue = this.isDefined(this.prediction?.predictionValue) ? this.prediction.predictionValue : ''; this.predictionValue = ObjectUtils.isValueDefined(this.prediction?.predictionValue) ? this.prediction.predictionValue : '';
this.predictionDateTime = this.prediction?.predictionDateTime || null; this.predictionDateTime = this.prediction?.predictionDateTime || null;
} }
@@ -94,8 +95,4 @@ export class ContentEnrichmentMenuComponent implements OnInit {
this.focusTrap.destroy(); this.focusTrap.destroy();
this.focusTrap = null; this.focusTrap = null;
} }
private isDefined(value: any): boolean {
return value !== undefined && value !== null;
}
} }