Skip to content

Commit

Permalink
fix(module): remove fast-deep-equal in favor of custom isEqual
Browse files Browse the repository at this point in the history
  • Loading branch information
benjamincanac committed Nov 7, 2024
1 parent 557e0c9 commit 37a3597
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@
"embla-carousel-fade": "^8.3.1",
"embla-carousel-vue": "^8.3.1",
"embla-carousel-wheel-gestures": "^8.0.1",
"fast-deep-equal": "^3.1.3",
"fuse.js": "^7.0.0",
"get-port-please": "^3.1.2",
"knitwork": "^1.1.0",
Expand Down
2 changes: 1 addition & 1 deletion playground-vue/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ export default defineConfig({
],
optimizeDeps: {
// prevents reloading page when navigating between components
include: ['radix-vue/namespaced', 'vaul-vue', 'fast-deep-equal', 'embla-carousel-vue', 'embla-carousel-autoplay', 'embla-carousel-auto-scroll', 'embla-carousel-auto-height', 'embla-carousel-class-names', 'embla-carousel-fade', 'embla-carousel-wheel-gestures']
include: ['radix-vue/namespaced', 'vaul-vue', 'embla-carousel-vue', 'embla-carousel-autoplay', 'embla-carousel-auto-scroll', 'embla-carousel-auto-height', 'embla-carousel-class-names', 'embla-carousel-fade', 'embla-carousel-wheel-gestures']
}
})
3 changes: 0 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,6 @@ export default defineNuxtModule<ModuleOptions>({

addTemplates(options, nuxt, resolve)

nuxt.options.vite = defu(nuxt.options?.vite, { optimizeDeps: { include: ['fast-deep-equal'] } })

if (nuxt.options.dev && nuxt.options.devtools.enabled && options.devtools?.enabled) {
const templates = buildTemplates(options)
nuxt.options.vite = defu(nuxt.options?.vite, { plugins: [devtoolsMetaPlugin({ resolve, templates, options })] })
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/components/InputMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,12 @@ extendDevtoolsMeta({ defaultProps: { items: ['Option 1', 'Option 2', 'Option 3']
import { computed, ref, toRef, onMounted } from 'vue'
import { ComboboxRoot, ComboboxArrow, ComboboxAnchor, ComboboxInput, ComboboxTrigger, ComboboxPortal, ComboboxContent, ComboboxViewport, ComboboxEmpty, ComboboxGroup, ComboboxLabel, ComboboxSeparator, ComboboxItem, ComboboxItemIndicator, TagsInputRoot, TagsInputItem, TagsInputItemText, TagsInputItemDelete, TagsInputInput, useForwardPropsEmits } from 'radix-vue'
import { defu } from 'defu'
import * as isEqual from 'fast-deep-equal'
import { reactivePick } from '@vueuse/core'
import { useAppConfig } from '#imports'
import { useButtonGroup } from '../composables/useButtonGroup'
import { useComponentIcons } from '../composables/useComponentIcons'
import { useFormField } from '../composables/useFormField'
import { get, escapeRegExp } from '../utils'
import { get, isEqual, escapeRegExp } from '../utils'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
import UChip from './Chip.vue'
Expand Down Expand Up @@ -180,7 +179,7 @@ const ui = computed(() => inputMenu({
}))
function displayValue(value: AcceptableValue): string {
const item = items.value.find(item => props.valueKey ? isEqual.default(get(item as Record<string, any>, props.valueKey as string), value) : isEqual.default(item, value))
const item = items.value.find(item => props.valueKey ? isEqual(get(item as Record<string, any>, props.valueKey as string), value) : isEqual(item, value))
return item && (typeof item === 'object' ? get(item, props.labelKey as string) : item)
}
Expand Down
5 changes: 2 additions & 3 deletions src/runtime/components/SelectMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,13 +121,12 @@ extendDevtoolsMeta({ defaultProps: { items: ['Option 1', 'Option 2', 'Option 3']
import { computed, toRef } from 'vue'
import { ComboboxRoot, ComboboxArrow, ComboboxAnchor, ComboboxInput, ComboboxTrigger, ComboboxPortal, ComboboxContent, ComboboxViewport, ComboboxEmpty, ComboboxGroup, ComboboxLabel, ComboboxSeparator, ComboboxItem, ComboboxItemIndicator, useForwardPropsEmits } from 'radix-vue'
import { defu } from 'defu'
import * as isEqual from 'fast-deep-equal'
import { reactivePick } from '@vueuse/core'
import { useAppConfig } from '#imports'
import { useButtonGroup } from '../composables/useButtonGroup'
import { useComponentIcons } from '../composables/useComponentIcons'
import { useFormField } from '../composables/useFormField'
import { get, escapeRegExp } from '../utils'
import { get, isEqual, escapeRegExp } from '../utils'
import UIcon from './Icon.vue'
import UAvatar from './Avatar.vue'
import UChip from './Chip.vue'
Expand Down Expand Up @@ -173,7 +172,7 @@ function displayValue(value: T | T[]): string {
return value.map(v => displayValue(v)).join(', ')
}
const item = items.value.find(item => props.valueKey ? isEqual.default(get(item as Record<string, any>, props.valueKey as string), value) : isEqual.default(item, value))
const item = items.value.find(item => props.valueKey ? isEqual(get(item as Record<string, any>, props.valueKey as string), value) : isEqual(item, value))
return item && (typeof item === 'object' ? get(item, props.labelKey as string) : item)
}
Expand Down
28 changes: 28 additions & 0 deletions src/runtime/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,34 @@ export function set(object: Record<string, any>, path: (string | number)[] | str
}, object)
}

export function isEqual(a: any, b: any): boolean {
// Handle primitive types and referential equality
if (a === b) return true

// Handle null/undefined cases
if (a == null || b == null) return a === b

// Handle different types
if (typeof a !== typeof b) return false

// Handle arrays
if (Array.isArray(a) && Array.isArray(b)) {
if (a.length !== b.length) return false
return a.every((item, index) => isEqual(item, b[index]))
}

// Handle objects
if (typeof a === 'object') {
const keysA = Object.keys(a)
const keysB = Object.keys(b)

if (keysA.length !== keysB.length) return false
return keysA.every(key => key in b && isEqual(a[key], b[key]))
}

return false
}

export function looseToNumber(val: any): any {
const n = Number.parseFloat(val)
return Number.isNaN(n) ? val : n
Expand Down

0 comments on commit 37a3597

Please sign in to comment.