Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Fix] Object Utils #8749

Merged
merged 2 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
IURLMetaData
} from '@gauzy/contracts';
import { ActivityService, DateRangePickerBuilderService, Store, TimesheetFilterService } from '@gauzy/ui-core/core';
import { distinctUntilChange, isEmpty, isJsObject, toLocal, toUTC } from '@gauzy/ui-core/common';
import { distinctUntilChange, isEmpty, isObject, toLocal, toUTC } from '@gauzy/ui-core/common';
import { BaseSelectorFilterComponent, GauzyFiltersComponent, TimeZoneService } from '@gauzy/ui-core/shared';

@UntilDestroy({ checkProperties: true })
Expand Down Expand Up @@ -158,7 +158,7 @@ export class AppUrlActivityComponent extends BaseSelectorFilterComponent impleme

if (typeof activity.metaData === 'string') {
metaData = JSON.parse(activity.metaData) as IURLMetaData;
} else if (isJsObject(activity.metaData)) {
} else if (isObject(activity.metaData)) {
metaData = activity.metaData as IURLMetaData;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Index as TypeOrmIndex, IndexOptions as TypeOrmIndexOptions } from 'typeorm';
import { Index as MikroOrmIndex, IndexOptions as MikroOrmIndexOptions, Unique as MikroUnique } from '@mikro-orm/core';
import { isObject } from '@gauzy/utils';
import { isPlainObject } from '@gauzy/utils';

// Extend your TypeOrmIndexOptions to include MikroOrm options as well
type CombinedIndexOptions<T> =
Expand Down Expand Up @@ -40,10 +40,10 @@ export function ColumnIndex<T>(
? <string[]>maybeFieldsOrOptions
: (nameOrFieldsOrOptions as string[]);

let options = isObject ? nameOrFieldsOrOptions : maybeOptions;
let options = isPlainObject(nameOrFieldsOrOptions) ? nameOrFieldsOrOptions : maybeOptions;

if (!options) {
options = isObject ? (maybeFieldsOrOptions as TypeOrmIndexOptions) : maybeOptions;
options = isPlainObject(maybeFieldsOrOptions) ? (maybeFieldsOrOptions as TypeOrmIndexOptions) : maybeOptions;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity as MikroOrmEntity } from '@mikro-orm/core';
import { Entity as TypeOrmEntity } from 'typeorm';
import { isObject } from '@gauzy/utils';
import { isPlainObject } from '@gauzy/utils';
import { MikroOrmEntityOptions, TypeOrmEntityOptions } from './entity-options.types';
import { parseMikroOrmEntityOptions } from './entity.helper';

Expand Down Expand Up @@ -31,14 +31,16 @@ export function MultiORMEntity<T>(
maybeOptions?: TypeOrmEntityOptions
): ClassDecorator {
// Extract MikroORM options based on the type of nameOrOptions
const mikroOrmOptions: any = isObject(nameOrOptions)
const mikroOrmOptions: any = isPlainObject(nameOrOptions)
? nameOrOptions
: typeof nameOrOptions === 'string'
? { tableName: nameOrOptions, ...maybeOptions }
: {};

// Extract TypeORM options based on the type of nameOrOptions
const typeOrmOptions: any = isObject(nameOrOptions) ? (nameOrOptions as TypeOrmEntityOptions) : nameOrOptions || {};
const typeOrmOptions: any = isPlainObject(nameOrOptions)
? (nameOrOptions as TypeOrmEntityOptions)
: nameOrOptions || {};

/**
* Class decorator for creating entities with both MikroORM and TypeORM decorators.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventSubscriber } from 'typeorm';
import { isJsObject } from '@gauzy/utils';
import { isObject } from '@gauzy/utils';
import { isBetterSqlite3, isSqlite } from '@gauzy/config';
import { BaseEntityEventSubscriber } from '../../core/entities/subscribers/base-entity-event.subscriber';
import { Activity } from './activity.entity';
Expand All @@ -24,7 +24,7 @@ export class ActivitySubscriber extends BaseEntityEventSubscriber<Activity> {
async beforeEntityCreate(entity: Activity): Promise<void> {
try {
// Check if the database is SQLite and the entity's metaData is a JavaScript object
if ((isSqlite() || isBetterSqlite3()) && isJsObject(entity.metaData)) {
if ((isSqlite() || isBetterSqlite3()) && isObject(entity.metaData)) {
entity.metaData = JSON.stringify(entity.metaData);
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { EventSubscriber } from 'typeorm';
import { IDBConnectionOptions } from '@gauzy/common';
import { getConfig } from '@gauzy/config';
import { isJsObject } from '@gauzy/utils';
import { isObject } from '@gauzy/utils';
import { BaseEntityEventSubscriber } from '../../core/entities/subscribers/base-entity-event.subscriber';
import { Screenshot } from './screenshot.entity';
import { FileStorage } from './../../core/file-storage';
Expand Down Expand Up @@ -40,7 +40,7 @@ export class ScreenshotSubscriber extends BaseEntityEventSubscriber<Screenshot>
const options: Partial<IDBConnectionOptions> = em.connection.options || getConfig().dbConnectionOptions;

// If the database is SQLite and the entity has an 'apps' property, convert it to a JSON string
if (isSqliteDB(options) && isJsObject(entity.apps)) {
if (isSqliteDB(options) && isObject(entity.apps)) {
try {
entity.apps = JSON.stringify(entity.apps);
} catch (error) {
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ScreenshotSubscriber extends BaseEntityEventSubscriber<Screenshot>
const options: Partial<IDBConnectionOptions> = em.connection.options || getConfig().dbConnectionOptions;

// If the database is SQLite and the entity has an 'apps' property, convert it to a JSON string
if (isSqliteDB(options) && isJsObject(entity.apps)) {
if (isSqliteDB(options) && isObject(entity.apps)) {
try {
entity.apps = JSON.stringify(entity.apps);
} catch (error) {
Expand Down
10 changes: 5 additions & 5 deletions packages/ui-core/common/src/lib/utils/shared-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function isNotNullOrUndefinedOrEmpty<T>(value: T | undefined | null): boo
export function toParams(query: any) {
let params: HttpParams = new HttpParams();
Object.keys(query).forEach((key) => {
if (isJsObject(query[key])) {
if (isObject(query[key])) {
params = toSubParams(params, key, query[key]);
} else {
params = params.append(key.toString(), query[key]);
Expand All @@ -52,7 +52,7 @@ export function toParams(query: any) {
* @param object The value to check.
* @returns `true` if the value is a JavaScript object, `false` otherwise.
*/
export function isJsObject(object: any): boolean {
export function isObject(object: any): boolean {
return object !== null && object !== undefined && typeof object === 'object';
}

Expand Down Expand Up @@ -88,7 +88,7 @@ export function isEmpty(item: any): boolean {

function toSubParams(params: HttpParams, key: string, object: any) {
Object.keys(object).forEach((childKey) => {
if (isJsObject(object[childKey])) {
if (isObject(object[childKey])) {
params = toSubParams(params, `${key}[${childKey}]`, object[childKey]);
} else {
params = params.append(`${key}[${childKey}]`, object[childKey]);
Expand Down Expand Up @@ -305,9 +305,9 @@ export function mergeDeep(target: any, ...sources: any) {
if (!sources.length) return target;
const source = sources.shift();

if (isJsObject(target) && isJsObject(source)) {
if (isObject(target) && isObject(source)) {
for (const key in source) {
if (isJsObject(source[key])) {
if (isObject(source[key])) {
if (!target[key])
Object.assign(target, {
[key]: {}
Expand Down
16 changes: 8 additions & 8 deletions packages/utils/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export * from './lib/array-to-object';
export * from './lib/array-sum';
export * from './lib/array-random-element';
export * from './lib/array-sum';
export * from './lib/array-to-object';
export * from './lib/average';
export * from './lib/boolean-mapper';
export * from './lib/build-query-string';
export * from './lib/camel-to-snake-case';
export * from './lib/chunks';
Expand All @@ -12,28 +13,27 @@ export * from './lib/deduplicate';
export * from './lib/deep-clone';
export * from './lib/deep-merge';
export * from './lib/ensure-http-prefix';
export * from './lib/extract-name-from-email';
export * from './lib/generate-encryption-key';
export * from './lib/generate-sha256-hash';
export * from './lib/extract-name-from-email';
export * from './lib/password-generator';
export * from './lib/is-array';
export * from './lib/is-class-instance';
export * from './lib/is-date';
export * from './lib/is-defined';
export * from './lib/is-empty';
export * from './lib/is-function';
export * from './lib/is-json';
export * from './lib/is-js-object';
export * from './lib/is-not-empty';
export * from './lib/is-not-null-or-undefined';
export * from './lib/is-null-or-undefined';
export * from './lib/is-object';
export * from './lib/is-plain-object';
export * from './lib/is-string';
export * from './lib/match';
export * from './lib/boolean-mapper';
export * from './lib/parse-to-boolean';
export * from './lib/trim-if-not-empty';
export * from './lib/uc-first';
export * from './lib/password-generator';
export * from './lib/slugify';
export * from './lib/sleep';
export * from './lib/trim-if-not-empty';
export * from './lib/uc-first';
export * from './lib/version';
4 changes: 2 additions & 2 deletions packages/utils/src/lib/deep-clone.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { isClassInstance } from './is-class-instance';
import { isEmpty } from './is-empty';
import { isObject } from './is-object';
import { isPlainObject } from './is-plain-object';

/**
* Deeply clones an input value.
Expand All @@ -10,7 +10,7 @@ import { isObject } from './is-object';
*/
export function deepClone<T extends string | number | any[] | Object>(input: T): T {
// Return the input itself if it's not an object or is empty
if (!isObject(input) || isEmpty(input)) {
if (!isPlainObject(input) || isEmpty(input)) {
return input;
}

Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/lib/deep-merge.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { deepClone } from './deep-clone';
import { isClassInstance } from './is-class-instance';
import { isObject } from './is-object';
import { isPlainObject } from './is-plain-object';

/**
* Deeply merges two objects.
*
Expand All @@ -21,11 +22,11 @@ export function deepMerge(target: any, source: any, depth = 0): any {
}

// Merge objects recursively
if (isObject(target) && isObject(source)) {
if (isPlainObject(target) && isPlainObject(source)) {
for (const key in source) {
if (Object.prototype.hasOwnProperty.call(source, key)) {
// If the source value is an object, recursively merge
if (isObject(source[key])) {
if (isPlainObject(source[key])) {
if (!target[key]) {
target[key] = {};
}
Expand Down
4 changes: 2 additions & 2 deletions packages/utils/src/lib/is-class-instance.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from './is-object';
import { isPlainObject } from './is-plain-object';

/**
* Check if the item is a class instance (not a plain object).
Expand All @@ -7,5 +7,5 @@ import { isObject } from './is-object';
* @returns {boolean} - Returns true if the item is a class instance, otherwise false.
*/
export function isClassInstance(item: any): boolean {
return isObject(item) && item.constructor.name !== 'Object';
return isPlainObject(item) && item.constructor.name !== 'Object';
}
9 changes: 0 additions & 9 deletions packages/utils/src/lib/is-js-object.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/utils/src/lib/is-object-or-function.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isFunction } from './is-function'; // Ensure the import path is correct.
import { isObject } from './is-object';
import { isPlainObject } from './is-plain-object';

/**
* Check if the item is either an object or a function.
Expand All @@ -8,5 +8,5 @@ import { isObject } from './is-object';
* @returns {boolean} - Returns true if the item is an object or function, otherwise false.
*/
export function isObjectOrFunction(item: any): boolean {
return isFunction(item) || isObject(item);
return isFunction(item) || isPlainObject(item);
}
23 changes: 15 additions & 8 deletions packages/utils/src/lib/is-object.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/**
/**
* Simple object check.
* @param val - The val to check.
* @returns {boolean} - True if the val is an object and not an array, otherwise false.
* Checks if the given value is a valid object.
*
* Unlike `instanceof Object`, this method avoids cross-context issues.
* Unlike `typeof`, it correctly excludes `null` and `undefined`, which are incorrectly classified as objects.
*
* @param {unknown} val - The value to check.
* @returns {boolean} - Returns `true` if the value is an object, otherwise `false`.
*
* This implementation is based on common JavaScript patterns for type checking.
* Reference: https://stackoverflow.com/a/34749873/772859
* @example
* console.log(isObject({})); // true
* console.log(isObject(null)); // false
* console.log(isObject(undefined)); // false
* console.log(isObject([])); // true (arrays are also objects)
* console.log(isObject(new Date())); // true
*/
export function isObject(val: any): boolean {
return val !== null && typeof val === 'object' && !Array.isArray(val);
export function isObject(val: unknown): val is object {
return val !== null && val !== undefined && typeof val === 'object';
}
15 changes: 15 additions & 0 deletions packages/utils/src/lib/is-plain-object.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Determines whether the given value is a plain object (not an array, null, or other non-object types).
*
* @param {unknown} item - The value to check.
* @returns {boolean} - Returns `true` if the value is a non-array object, otherwise `false`.
*
* @example
* console.log(isPlainObject({})); // true
* console.log(isPlainObject([])); // false
* console.log(isPlainObject(null)); // false
* console.log(isPlainObject('string')); // false
*/
export function isPlainObject(item: unknown): boolean {
return !!item && typeof item === 'object' && !Array.isArray(item);
}
Loading