Skip to content

Commit

Permalink
[ACS-8001] move isDefined to object-utils
Browse files Browse the repository at this point in the history
  • Loading branch information
nikita-web-ua committed Jul 30, 2024
1 parent 1e79753 commit e359e5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 33 deletions.
16 changes: 15 additions & 1 deletion lib/core/src/lib/common/utils/object-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import { ObjectUtils } from './object-utils';

describe('ObjectUtils', () => {

it('should get top level property value', () => {
const obj = {
id: 1
Expand Down Expand Up @@ -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);
});
});
});
47 changes: 21 additions & 26 deletions lib/core/src/lib/common/utils/object-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ export class ObjectUtils {
* @returns object property value
*/
static getValue(target: any, key: string): any {

if (!target || !key) {
return undefined;
}
Expand Down Expand Up @@ -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 = '&#9989';
const redCrossSymbol = '&#10060';

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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}

0 comments on commit e359e5f

Please sign in to comment.