[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';
describe('ObjectUtils', () => {
it('should get top level property value', () => {
const obj = {
id: 1
@@ -246,4 +245,19 @@ describe('ObjectUtils', () => {
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
*/
static getValue(target: any, key: string): any {
if (!target || !key) {
return undefined;
}
@@ -80,37 +79,29 @@ export class ObjectUtils {
}
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 {
if (
!target ||
ObjectUtils.isEmpty(target) ||
!ObjectUtils.isBooleanObject(target)
) {
if (!target || ObjectUtils.isEmpty(target) || !ObjectUtils.isBooleanObject(target)) {
return '';
}
if (
!ObjectUtils.isObject(target) ||
!ObjectUtils.hasKeys(target)
) {
if (!ObjectUtils.isObject(target) || !ObjectUtils.hasKeys(target)) {
return target.toString();
}
const greenBorderWhiteCheckSymbol = '&#9989';
const redCrossSymbol = '&#10060';
target = Object.keys(target).map((key) => {
target = Object.keys(target)
.map((key) => {
if (target[key]) {
if (enhancer) {
return `${greenBorderWhiteCheckSymbol} ${enhancer(key)}`;
} else {
return `${greenBorderWhiteCheckSymbol} ${key}`;
}
}
if (enhancer) {
@@ -118,9 +109,13 @@ export class ObjectUtils {
} else {
return `${redCrossSymbol} ${key}`;
}
}).join('\n');
})
.join('\n');
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 { LocalizedDatePipe } from '../../../pipes';
import { PredictionService } from '../../services';
import { ObjectUtils } from '../../../common';
@Component({
standalone: true,
@@ -65,8 +66,8 @@ export class ContentEnrichmentMenuComponent implements OnInit {
ngOnInit() {
this.confidencePercentage = this.prediction?.confidenceLevel * 100 || 0;
this.previousValue = this.isDefined(this.prediction?.previousValue) ? this.prediction.previousValue : '';
this.predictionValue = this.isDefined(this.prediction?.predictionValue) ? this.prediction.predictionValue : '';
this.previousValue = ObjectUtils.isValueDefined(this.prediction?.previousValue) ? this.prediction.previousValue : '';
this.predictionValue = ObjectUtils.isValueDefined(this.prediction?.predictionValue) ? this.prediction.predictionValue : '';
this.predictionDateTime = this.prediction?.predictionDateTime || null;
}
@@ -94,8 +95,4 @@ export class ContentEnrichmentMenuComponent implements OnInit {
this.focusTrap.destroy();
this.focusTrap = null;
}
private isDefined(value: any): boolean {
return value !== undefined && value !== null;
}
}