add value in enum tools function in object tools lib ...
This commit is contained in:
@@ -16,6 +16,7 @@ import {
|
|||||||
import { Type } from '@angular/core';
|
import { Type } from '@angular/core';
|
||||||
import { isSameDate } from './x-date-time.tools';
|
import { isSameDate } from './x-date-time.tools';
|
||||||
import { countChild, hasChild, lastItem, onlyUniques } from './x-array.tools';
|
import { countChild, hasChild, lastItem, onlyUniques } from './x-array.tools';
|
||||||
|
import { toXString } from './x-type-convert.tools';
|
||||||
|
|
||||||
//
|
//
|
||||||
//#region Object Tools ...
|
//#region Object Tools ...
|
||||||
@@ -260,7 +261,7 @@ export function mergeAll<T>(
|
|||||||
*/
|
*/
|
||||||
export function toPartialObject<T>(
|
export function toPartialObject<T>(
|
||||||
source: T,
|
source: T,
|
||||||
modelKeys: Array<Partial<keyof T>>
|
modelKeys: Array<Partial<keyof T>>,
|
||||||
) {
|
) {
|
||||||
//
|
//
|
||||||
if (!source || !hasChild(modelKeys)) {
|
if (!source || !hasChild(modelKeys)) {
|
||||||
@@ -313,7 +314,7 @@ export function getFieldNames<T>(object: T): XModelFieldTypeIndex<T> {
|
|||||||
{},
|
{},
|
||||||
...(Object.keys(object).map((k) => {
|
...(Object.keys(object).map((k) => {
|
||||||
return assign({}, { key: k, value: k });
|
return assign({}, { key: k, value: k });
|
||||||
}) as XModelFieldTypeIndex<T>[])
|
}) as XModelFieldTypeIndex<T>[]),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -366,7 +367,7 @@ export function setPropertyValue<T>(
|
|||||||
object: T,
|
object: T,
|
||||||
propertyPath: string,
|
propertyPath: string,
|
||||||
value?: any,
|
value?: any,
|
||||||
forceCreate?: boolean
|
forceCreate?: boolean,
|
||||||
) {
|
) {
|
||||||
//
|
//
|
||||||
if (!object || isNullOrEmptyString(propertyPath)) {
|
if (!object || isNullOrEmptyString(propertyPath)) {
|
||||||
@@ -410,7 +411,7 @@ export function getPropertyPaths<T = any>(
|
|||||||
object: T,
|
object: T,
|
||||||
prefix?: string,
|
prefix?: string,
|
||||||
extractSchema: boolean = false,
|
extractSchema: boolean = false,
|
||||||
containsChildsObjectHeaders: boolean = false
|
containsChildsObjectHeaders: boolean = false,
|
||||||
) {
|
) {
|
||||||
//
|
//
|
||||||
// Create empty result Array ...
|
// Create empty result Array ...
|
||||||
@@ -483,7 +484,7 @@ export function getPropertyPaths<T = any>(
|
|||||||
(value as any[])[0],
|
(value as any[])[0],
|
||||||
`${keyPath}.0`,
|
`${keyPath}.0`,
|
||||||
extractSchema,
|
extractSchema,
|
||||||
containsChildsObjectHeaders
|
containsChildsObjectHeaders,
|
||||||
)
|
)
|
||||||
: //
|
: //
|
||||||
// for Object Values retrieve all including keys ...
|
// for Object Values retrieve all including keys ...
|
||||||
@@ -491,7 +492,7 @@ export function getPropertyPaths<T = any>(
|
|||||||
value,
|
value,
|
||||||
keyPath,
|
keyPath,
|
||||||
extractSchema,
|
extractSchema,
|
||||||
containsChildsObjectHeaders
|
containsChildsObjectHeaders,
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
//
|
//
|
||||||
@@ -501,7 +502,7 @@ export function getPropertyPaths<T = any>(
|
|||||||
value,
|
value,
|
||||||
keyPath,
|
keyPath,
|
||||||
extractSchema,
|
extractSchema,
|
||||||
containsChildsObjectHeaders
|
containsChildsObjectHeaders,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -544,7 +545,7 @@ export function getSchemaPaths<T = any>(object: T) {
|
|||||||
export function generateObjectFromPaths<T = any>(
|
export function generateObjectFromPaths<T = any>(
|
||||||
paths: string[],
|
paths: string[],
|
||||||
assginPropertyName: boolean = false,
|
assginPropertyName: boolean = false,
|
||||||
defaultValue: any = null
|
defaultValue: any = null,
|
||||||
): XDeep<T> {
|
): XDeep<T> {
|
||||||
//
|
//
|
||||||
// Check paths contains childs ...
|
// Check paths contains childs ...
|
||||||
@@ -605,7 +606,7 @@ export function generateObjectFromPaths<T = any>(
|
|||||||
export function generatePartialObjectFromPaths<T = any>(
|
export function generatePartialObjectFromPaths<T = any>(
|
||||||
paths: string[],
|
paths: string[],
|
||||||
assginPropertyName: boolean = false,
|
assginPropertyName: boolean = false,
|
||||||
defaultValue: any = null
|
defaultValue: any = null,
|
||||||
): XDeepPartial<T> {
|
): XDeepPartial<T> {
|
||||||
//
|
//
|
||||||
// Check paths contains childs ...
|
// Check paths contains childs ...
|
||||||
@@ -665,7 +666,7 @@ export function generatePartialObjectFromPaths<T = any>(
|
|||||||
*/
|
*/
|
||||||
export function getSchema<T = any>(
|
export function getSchema<T = any>(
|
||||||
object: T,
|
object: T,
|
||||||
defaultValue: any = null
|
defaultValue: any = null,
|
||||||
): XDeep<T> {
|
): XDeep<T> {
|
||||||
//
|
//
|
||||||
const paths = getSchemaPaths(object);
|
const paths = getSchemaPaths(object);
|
||||||
@@ -684,7 +685,7 @@ export function getSchema<T = any>(
|
|||||||
*/
|
*/
|
||||||
export function getPartialSchema<T = any>(
|
export function getPartialSchema<T = any>(
|
||||||
object: T,
|
object: T,
|
||||||
defaultValue: any = null
|
defaultValue: any = null,
|
||||||
): XDeepPartial<T> {
|
): XDeepPartial<T> {
|
||||||
//
|
//
|
||||||
const paths = getSchemaPaths(object);
|
const paths = getSchemaPaths(object);
|
||||||
@@ -773,10 +774,10 @@ export function isSameType(source: any, dest: any, forceMemberTypes = false) {
|
|||||||
|
|
||||||
//
|
//
|
||||||
const isAllSourceKeyExistsInDestKeys = sourceKeys.every((k) =>
|
const isAllSourceKeyExistsInDestKeys = sourceKeys.every((k) =>
|
||||||
destKeys.includes(k)
|
destKeys.includes(k),
|
||||||
);
|
);
|
||||||
const isAllDestKeyExistsInSourceKeys = destKeys.every((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(
|
export function isSameArray(
|
||||||
source: any[],
|
source: any[],
|
||||||
dest: any[],
|
dest: any[],
|
||||||
forceCheckType?: boolean
|
forceCheckType?: boolean,
|
||||||
): boolean {
|
): boolean {
|
||||||
//
|
//
|
||||||
if (!source || !dest) {
|
if (!source || !dest) {
|
||||||
@@ -877,7 +878,7 @@ export function isSameArray(
|
|||||||
export function isSameObject(
|
export function isSameObject(
|
||||||
source: object,
|
source: object,
|
||||||
dest: object,
|
dest: object,
|
||||||
forceCheckType?: boolean
|
forceCheckType?: boolean,
|
||||||
): boolean {
|
): boolean {
|
||||||
//
|
//
|
||||||
if (!source || !dest) {
|
if (!source || !dest) {
|
||||||
@@ -935,9 +936,7 @@ export function isSameObject(
|
|||||||
*/
|
*/
|
||||||
export function isOfType<T>(value: any, keys: (keyof T)[]): value is T {
|
export function isOfType<T>(value: any, keys: (keyof T)[]): value is T {
|
||||||
return (
|
return (
|
||||||
typeof value === "object" &&
|
typeof value === 'object' && value !== null && keys.every((k) => k in value)
|
||||||
value !== null &&
|
|
||||||
keys.every(k => k in value)
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -949,9 +948,9 @@ export function isOfType<T>(value: any, keys: (keyof T)[]): value is T {
|
|||||||
*/
|
*/
|
||||||
export function typeGuard<T>(keys: (keyof T)[]) {
|
export function typeGuard<T>(keys: (keyof T)[]) {
|
||||||
return (value: any): value is T =>
|
return (value: any): value is T =>
|
||||||
typeof value === "object" &&
|
typeof value === 'object' &&
|
||||||
value !== null &&
|
value !== null &&
|
||||||
keys.every(k => k in value);
|
keys.every((k) => k in value);
|
||||||
}
|
}
|
||||||
//#endregion
|
//#endregion
|
||||||
|
|
||||||
@@ -1004,6 +1003,46 @@ export function getVal(T: Object, value: number | string) {
|
|||||||
export function toObject(T: Object) {
|
export function toObject(T: Object) {
|
||||||
return Object.assign({}, T);
|
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<TValue>(
|
||||||
|
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
|
//#endregion
|
||||||
|
|
||||||
//
|
//
|
||||||
@@ -1037,7 +1076,7 @@ export function getComponentName<T>(component: Type<T>): string | null {
|
|||||||
*/
|
*/
|
||||||
export function isXKeyOf<T extends object>(
|
export function isXKeyOf<T extends object>(
|
||||||
obj: T,
|
obj: T,
|
||||||
key: PropertyKey
|
key: PropertyKey,
|
||||||
): key is keyof T {
|
): key is keyof T {
|
||||||
return key in obj;
|
return key in obj;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user