Skip to content

Build your own AI office with Univer Office SDK. Bring editing, calculation, collaboration, and file processing into your product for people and AI agents. Built on an extensible open-source core.

feat: implement some array util

Wenzhao Hub35b08e5f7f8
diff --git a/packages/core/src/common/array.ts b/packages/core/src/common/array.ts
index 2cd78f2969..25ee51f13d 100644
--- a/packages/core/src/common/array.ts
+++ b/packages/core/src/common/array.ts
@@ -1,3 +1,9 @@
+/**
+ * Remove an item from an array.
+ * @param arr The array to remove item from.
+ * @param item The item to be removed.
+ * @returns Returns `true` if the item is removed successfully. Returns `false` if the item does not exist in the array.
+ */
export function remove<T>(arr: T[], item: T): boolean {
const index = arr.indexOf(item);
if (index > -1) {
@@ -6,3 +12,20 @@ export function remove<T>(arr: T[], item: T): boolean {
}
return false;
}
+
+/**
+ * Deduplicate an array.
+ * @param arr The array to be dedupe.
+ * @returns Return the deduplicated array.
+ */
+export function dedupe<T>(arr: T[]): T[] {
+ const deduplicated = new Set<T>();
+ const result: T[] = [];
+ for (const element of arr) {
+ if (!deduplicated.has(element)) {
+ deduplicated.add(element);
+ result.push(element);
+ }
+ }
+ return result;
+}
diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts
index 36cea35a92..ea05dbe46e 100644
--- a/packages/core/src/index.ts
+++ b/packages/core/src/index.ts
@@ -1,4 +1,5 @@
export * from './Basics';
+export { dedupe, remove } from './common/array';
export * from './Docs/Domain';
export * from './Observer';
export { Plugin, PluginType } from './plugin/plugin';