AI-native design editor. Open-source Figma alternative.
refactor(core): use es-toolkit for diff tool collection helpers (#854)
Danila Poyarkov1a2200596025
diff --git a/bun.lock b/bun.lockindex 3e725645e..67ada07d2 100644--- a/bun.lock+++ b/bun.lock@@ -177,6 +177,7 @@"agentfmt": "^0.1.3","canvaskit-wasm": "^0.41.1","citty": "^0.1.6",+ "es-toolkit": "^1.51.0","js-base64": "^3.9.3","valibot": "^1.4.2",},diff --git a/packages/cli/package.json b/packages/cli/package.jsonindex 03be591db..db91f677b 100644--- a/packages/cli/package.json+++ b/packages/cli/package.json@@ -44,6 +44,7 @@"agentfmt": "^0.1.3","canvaskit-wasm": "^0.41.1","citty": "^0.1.6",+ "es-toolkit": "^1.51.0","js-base64": "^3.9.3","valibot": "^1.4.2"},diff --git a/packages/cli/src/commands/diff/visual.ts b/packages/cli/src/commands/diff/visual.tsindex a4cdc8b42..996433950 100644--- a/packages/cli/src/commands/diff/visual.ts+++ b/packages/cli/src/commands/diff/visual.ts@@ -1,6 +1,7 @@import { writeFile } from 'node:fs/promises'import { defineCommand } from 'citty'+import { isUndefined, omitBy } from 'es-toolkit'import { toUint8Array } from 'js-base64'import type { Rect } from '@open-pencil/scene-graph/primitives'@@ -40,14 +41,15 @@ export default defineCommand({}},async run({ args }) {- const toolArgs = Object.fromEntries(- Object.entries({+ const toolArgs = omitBy(+ {from: args.from,to: args.to,scale: optionalNumber(args.scale),maxEdge: optionalNumber(args['max-edge']),threshold: optionalNumber(args.threshold)- }).filter(([, value]) => value !== undefined)+ },+ isUndefined)const data = await runToolData(args.file, 'diff_visual', toolArgs, args)const result = data.result as VisualDiffResultdiff --git a/packages/core/src/tools/analyze/diff/apply.ts b/packages/core/src/tools/analyze/diff/apply.tsindex 7152fd1f6..b9209a090 100644--- a/packages/core/src/tools/analyze/diff/apply.ts+++ b/packages/core/src/tools/analyze/diff/apply.ts@@ -1,4 +1,4 @@-import { groupBy, isEqual } from 'es-toolkit'+import { difference, groupBy, isEqual, pickBy, uniq } from 'es-toolkit'import {buildComponent,@@ -112,18 +112,13 @@ function nodeUpdate(const parentId = node.parentId ?? ''const from = jsxNodeFields(graph, node.type, before, parentId)const to = jsxNodeFields(graph, node.type, after, parentId)- const fields = Object.fromEntries(- Object.entries(to.fields).filter(- ([key, value]) => !isEqual(from.fields[key as keyof SceneNode], value)- )- )return {id: node.id,- fields,+ fields: pickBy(to.fields, (value, key) => !isEqual(from.fields[key], value)),bind: Object.fromEntries(Object.entries(to.bindings).filter(([field, id]) => from.bindings[field] !== id)),- unbind: Object.keys(from.bindings).filter((field) => !(field in to.bindings))+ unbind: difference(Object.keys(from.bindings), Object.keys(to.bindings))}}@@ -150,7 +145,7 @@ function planUpdate(const toSources = (map: Map<string, string>) =>[...map].map(([name, source]) => ({ name, source }))const update = nodeUpdate(graph, node, attributes, toSources(target))- const changes = [...new Set([...removed, ...added].map((attribute) => attribute.name))]+ const changes = uniq([...removed, ...added].map((attribute) => attribute.name))const empty =Object.keys(update.fields).length === 0 &&Object.keys(update.bind).length === 0 &&diff --git a/packages/core/src/tools/analyze/diff/documents.ts b/packages/core/src/tools/analyze/diff/documents.tsindex 7fcc3e384..006507d4e 100644--- a/packages/core/src/tools/analyze/diff/documents.ts+++ b/packages/core/src/tools/analyze/diff/documents.ts@@ -1,3 +1,5 @@+import { union } from 'es-toolkit'+import { sceneNodeToJSX } from '@open-pencil/design-jsx'import type { SceneGraph } from '@open-pencil/scene-graph'@@ -55,7 +57,7 @@ export function diffDocuments(const project = { match: 'path' as const, depth: options.depth }const beforePages = new Map(before.getPages().map((page) => [page.name, page]))const afterPages = new Map(after.getPages().map((page) => [page.name, page]))- const names = [...new Set([...beforePages.keys(), ...afterPages.keys()])].filter(+ const names = union([...beforePages.keys()], [...afterPages.keys()]).filter((name) => options.page === undefined || name === options.page)diff --git a/packages/core/src/tools/analyze/diff/operations.ts b/packages/core/src/tools/analyze/diff/operations.tsindex cda65c7b9..410e39df0 100644--- a/packages/core/src/tools/analyze/diff/operations.ts+++ b/packages/core/src/tools/analyze/diff/operations.ts@@ -1,3 +1,4 @@+import { union } from 'es-toolkit'import type { Delta } from 'jsondiffpatch'import type { ProjectedNode } from './projection'@@ -33,7 +34,7 @@ function childPath(parentPath: string, node: ProjectedNode): string {}function attributeChanges(before: ProjectedNode, after: ProjectedNode) {- const names = new Set([...Object.keys(before.attributes), ...Object.keys(after.attributes)])+ const names = union(Object.keys(before.attributes), Object.keys(after.attributes))const removed: string[] = []const added: string[] = []for (const name of names) {