From fb701301d8fc072df7a7712a2af868cdacddd120 Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Fri, 21 Aug 2026 18:46:47 +0330 Subject: [PATCH] add value in enum tools function in object tools lib ... --- .../src/lib/tools/x-object.tools.ts | 81 ++++++++++++++----- 1 file changed, 60 insertions(+), 21 deletions(-) diff --git a/projects/x-framework-core/src/lib/tools/x-object.tools.ts b/projects/x-framework-core/src/lib/tools/x-object.tools.ts index ef4cbab..967d5a8 100644 --- a/projects/x-framework-core/src/lib/tools/x-object.tools.ts +++ b/projects/x-framework-core/src/lib/tools/x-object.tools.ts @@ -16,6 +16,7 @@ import { import { Type } from '@angular/core'; import { isSameDate } from './x-date-time.tools'; import { countChild, hasChild, lastItem, onlyUniques } from './x-array.tools'; +import { toXString } from './x-type-convert.tools'; // //#region Object Tools ... @@ -260,7 +261,7 @@ export function mergeAll( */ export function toPartialObject( source: T, - modelKeys: Array> + modelKeys: Array>, ) { // if (!source || !hasChild(modelKeys)) { @@ -313,7 +314,7 @@ export function getFieldNames(object: T): XModelFieldTypeIndex { {}, ...(Object.keys(object).map((k) => { return assign({}, { key: k, value: k }); - }) as XModelFieldTypeIndex[]) + }) as XModelFieldTypeIndex[]), ); } @@ -366,7 +367,7 @@ export function setPropertyValue( object: T, propertyPath: string, value?: any, - forceCreate?: boolean + forceCreate?: boolean, ) { // if (!object || isNullOrEmptyString(propertyPath)) { @@ -410,7 +411,7 @@ export function getPropertyPaths( object: T, prefix?: string, extractSchema: boolean = false, - containsChildsObjectHeaders: boolean = false + containsChildsObjectHeaders: boolean = false, ) { // // Create empty result Array ... @@ -483,7 +484,7 @@ export function getPropertyPaths( (value as any[])[0], `${keyPath}.0`, extractSchema, - containsChildsObjectHeaders + containsChildsObjectHeaders, ) : // // for Object Values retrieve all including keys ... @@ -491,7 +492,7 @@ export function getPropertyPaths( value, keyPath, extractSchema, - containsChildsObjectHeaders + containsChildsObjectHeaders, ); } else { // @@ -501,7 +502,7 @@ export function getPropertyPaths( value, keyPath, extractSchema, - containsChildsObjectHeaders + containsChildsObjectHeaders, ); } @@ -544,7 +545,7 @@ export function getSchemaPaths(object: T) { export function generateObjectFromPaths( paths: string[], assginPropertyName: boolean = false, - defaultValue: any = null + defaultValue: any = null, ): XDeep { // // Check paths contains childs ... @@ -605,7 +606,7 @@ export function generateObjectFromPaths( export function generatePartialObjectFromPaths( paths: string[], assginPropertyName: boolean = false, - defaultValue: any = null + defaultValue: any = null, ): XDeepPartial { // // Check paths contains childs ... @@ -665,7 +666,7 @@ export function generatePartialObjectFromPaths( */ export function getSchema( object: T, - defaultValue: any = null + defaultValue: any = null, ): XDeep { // const paths = getSchemaPaths(object); @@ -684,7 +685,7 @@ export function getSchema( */ export function getPartialSchema( object: T, - defaultValue: any = null + defaultValue: any = null, ): XDeepPartial { // const paths = getSchemaPaths(object); @@ -773,10 +774,10 @@ export function isSameType(source: any, dest: any, forceMemberTypes = false) { // const isAllSourceKeyExistsInDestKeys = sourceKeys.every((k) => - destKeys.includes(k) + destKeys.includes(k), ); const isAllDestKeyExistsInSourceKeys = destKeys.every((k) => - sourceKeys.includes(k) + sourceKeys.includes(k), ); // @@ -819,7 +820,7 @@ export function isSameType(source: any, dest: any, forceMemberTypes = false) { export function isSameArray( source: any[], dest: any[], - forceCheckType?: boolean + forceCheckType?: boolean, ): boolean { // if (!source || !dest) { @@ -877,7 +878,7 @@ export function isSameArray( export function isSameObject( source: object, dest: object, - forceCheckType?: boolean + forceCheckType?: boolean, ): boolean { // if (!source || !dest) { @@ -935,9 +936,7 @@ export function isSameObject( */ export function isOfType(value: any, keys: (keyof T)[]): value is T { return ( - typeof value === "object" && - value !== null && - keys.every(k => k in value) + typeof value === 'object' && value !== null && keys.every((k) => k in value) ); } @@ -949,9 +948,9 @@ export function isOfType(value: any, keys: (keyof T)[]): value is T { */ export function typeGuard(keys: (keyof T)[]) { return (value: any): value is T => - typeof value === "object" && + typeof value === 'object' && value !== null && - keys.every(k => k in value); + keys.every((k) => k in value); } //#endregion @@ -1004,6 +1003,46 @@ export function getVal(T: Object, value: number | string) { export function toObject(T: Object) { return Object.assign({}, T); } + +/** + * Generic Tools Function to Check A Value inside enum Values or not ... + * + * @param value + * @param values + * @param forceExact + */ +export function isValueInEnum( + value: TValue, + values: Object, + forceExact: boolean = true, +) { + // + let result = false; + + // + if ( + !isNullOrUndefined(value) && + !isNullOrUndefined(values) && + !isNullOrEmptyString(toXString(value)) + ) { + // + const tVals = Object.values(values).map((tv) => tv as TValue); + + // + result = tVals.includes(value); + + // + // Check Includes ... + if (!forceExact && !result) { + // + result = + tVals.findIndex((t) => toXString(value).includes(toXString(t))) >= 0; + } + } + + // + return result; +} //#endregion // @@ -1037,7 +1076,7 @@ export function getComponentName(component: Type): string | null { */ export function isXKeyOf( obj: T, - key: PropertyKey + key: PropertyKey, ): key is keyof T { return key in obj; }