From e359e5fbea829d168f6253ca345212ce4265f72b Mon Sep 17 00:00:00 2001 From: Mykyta Maliarchuk Date: Fri, 14 Jun 2024 08:46:55 +0200 Subject: [PATCH] [ACS-8001] move isDefined to object-utils --- .../src/lib/common/utils/object-utils.spec.ts | 16 ++++++- lib/core/src/lib/common/utils/object-utils.ts | 47 +++++++++---------- .../content-enrichment-menu.component.ts | 9 ++-- 3 files changed, 39 insertions(+), 33 deletions(-) diff --git a/lib/core/src/lib/common/utils/object-utils.spec.ts b/lib/core/src/lib/common/utils/object-utils.spec.ts index 4794504062d..6fa5bc01742 100644 --- a/lib/core/src/lib/common/utils/object-utils.spec.ts +++ b/lib/core/src/lib/common/utils/object-utils.spec.ts @@ -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('✅ testOnetest\n❌ 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); + }); + }); }); diff --git a/lib/core/src/lib/common/utils/object-utils.ts b/lib/core/src/lib/common/utils/object-utils.ts index 67cf7dddea6..7d647846347 100644 --- a/lib/core/src/lib/common/utils/object-utils.ts +++ b/lib/core/src/lib/common/utils/object-utils.ts @@ -25,7 +25,6 @@ export class ObjectUtils { * @returns object property value */ static getValue(target: any, key: string): any { - if (!target || !key) { return undefined; } @@ -80,47 +79,43 @@ 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 = '✅'; const redCrossSymbol = '❌'; - target = Object.keys(target).map((key) => { - if (target[key]) { + target = Object.keys(target) + .map((key) => { + if (target[key]) { + if (enhancer) { + return `${greenBorderWhiteCheckSymbol} ${enhancer(key)}`; + } else { + return `${greenBorderWhiteCheckSymbol} ${key}`; + } + } + if (enhancer) { - return `${greenBorderWhiteCheckSymbol} ${enhancer(key)}`; + return `${redCrossSymbol} ${enhancer(key)}`; } else { - return `${greenBorderWhiteCheckSymbol} ${key}`; + return `${redCrossSymbol} ${key}`; } - - } - - if (enhancer) { - return `${redCrossSymbol} ${enhancer(key)}`; - } else { - return `${redCrossSymbol} ${key}`; - } - - }).join('\n'); + }) + .join('\n'); return target; } + + static isValueDefined(value: any): boolean { + return value !== undefined && value !== null; + } } diff --git a/lib/core/src/lib/prediction/components/content-enrichment-menu/content-enrichment-menu.component.ts b/lib/core/src/lib/prediction/components/content-enrichment-menu/content-enrichment-menu.component.ts index 01c86b9f0cd..cb1b9286af2 100644 --- a/lib/core/src/lib/prediction/components/content-enrichment-menu/content-enrichment-menu.component.ts +++ b/lib/core/src/lib/prediction/components/content-enrichment-menu/content-enrichment-menu.component.ts @@ -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; - } }