Explorar el Código

Relocate isDeepEquals

Shun Miyazawa hace 1 año
padre
commit
a62f4e5e98

+ 2 - 1
apps/app/src/features/openai/server/services/openai.ts

@@ -5,6 +5,7 @@ import { pipeline } from 'stream/promises';
 import {
   PageGrant, getIdForRef, getIdStringForRef, isPopulated, type IUserHasId,
 } from '@growi/core';
+import { deepEquals } from '@growi/core/dist/utils';
 import { isGrobPatternPath } from '@growi/core/dist/utils/page-path-utils';
 import escapeStringRegexp from 'escape-string-regexp';
 import createError from 'http-errors';
@@ -22,7 +23,6 @@ import type { PageDocument, PageModel } from '~/server/models/page';
 import UserGroupRelation from '~/server/models/user-group-relation';
 import { configManager } from '~/server/service/config-manager';
 import { createBatchStream } from '~/server/util/batch-stream';
-import { isDeepEquals } from '~/utils/is-deep-equal';
 import loggerFactory from '~/utils/logger';
 
 import { OpenaiServiceTypes } from '../../interfaces/ai';
@@ -36,6 +36,7 @@ import { getClient } from './client-delegator';
 // import { splitMarkdownIntoChunks } from './markdown-splitter/markdown-token-splitter';
 import { openaiApiErrorHandler } from './openai-api-error-handler';
 
+const { isDeepEquals } = deepEquals;
 
 const BATCH_SIZE = 100;
 

+ 1 - 0
packages/core/src/utils/index.ts

@@ -9,6 +9,7 @@ export * as objectIdUtils from './objectid-utils';
 export * as pagePathUtils from './page-path-utils';
 export * as pathUtils from './path-utils';
 export * as pageUtils from './page-utils';
+export * as deepEquals from './is-deep-equals';
 
 export * from './browser-utils';
 export * from './growi-theme-metadata';

+ 30 - 0
packages/core/src/utils/is-deep-equals.ts

@@ -0,0 +1,30 @@
+export const isDeepEquals = <T extends object>(obj1: T, obj2: T): boolean => {
+  const typedKeys1 = Object.keys(obj1) as (keyof T)[];
+  const typedKeys2 = Object.keys(obj2) as (keyof T)[];
+
+  if (typedKeys1.length !== typedKeys2.length) {
+    return false;
+  }
+
+  return typedKeys1.every((key) => {
+    const val1 = obj1[key];
+    const val2 = obj2[key];
+
+    if (typeof val1 === 'object' && typeof val2 === 'object') {
+      if (val1 === null || val2 === null) {
+        return val1 === val2;
+      }
+
+      // if array
+      if (Array.isArray(val1) && Array.isArray(val2)) {
+        return val1.length === val2.length && val1.every((item, i) => val2[i] === item);
+      }
+
+      // if object
+      return isDeepEquals(val1, val2);
+    }
+
+    // if primitive
+    return val1 === val2;
+  });
+};

+ 3 - 6
packages/editor/src/client/stores/codemirror-editor.ts

@@ -1,23 +1,20 @@
 import { useMemo, useRef } from 'react';
 
 import { useSWRStatic } from '@growi/core/dist/swr';
+import { deepEquals } from '@growi/core/dist/utils';
 import type { ReactCodeMirrorProps, UseCodeMirror } from '@uiw/react-codemirror';
 import type { SWRResponse } from 'swr';
 import deepmerge from 'ts-deepmerge';
 
 import { type UseCodeMirrorEditor, useCodeMirrorEditor } from '../services';
 
+const { isDeepEquals } = deepEquals;
+
 
 const isValid = (u: UseCodeMirrorEditor) => {
   return u.state != null && u.view != null;
 };
 
-const isDeepEquals = <T extends object>(obj1: T, obj2: T): boolean => {
-  const typedKeys = Object.keys(obj1) as (keyof typeof obj1)[];
-  return typedKeys.every(key => obj1[key] === obj2[key]);
-};
-
-
 export const useCodeMirrorEditorIsolated = (
     key: string | null, container?: HTMLDivElement | null, props?: ReactCodeMirrorProps,
 ): SWRResponse<UseCodeMirrorEditor> => {