Skip to content

Commit

Permalink
fix: prototype getting lost during deepmerge
Browse files Browse the repository at this point in the history
  • Loading branch information
segunadebayo committed Sep 13, 2024
1 parent a73bd8f commit 8d4d226
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .changeset/long-cougars-juggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
"@zag-js/date-picker": patch
---

Fix issue in Vue.js where input value could not be changed by typing.
- Fix issue in Vue.js where input value could not be changed by typing.
- Fix issue where setting controlled `min` and `max` values throws an error.
4 changes: 2 additions & 2 deletions packages/core/src/deep-merge.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { compact, isObject } from "@zag-js/utils"
import { compact, isPlainObject } from "@zag-js/utils"

export function deepMerge<T extends Record<string, any>>(source: T, ...objects: T[]): T {
for (const obj of objects) {
const target = compact(obj)
for (const key in target) {
if (isObject(obj[key])) {
if (isPlainObject(obj[key])) {
if (!source[key]) {
source[key] = {} as any
}
Expand Down
15 changes: 14 additions & 1 deletion packages/utilities/core/src/guard.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
export const isDev = () => process.env.NODE_ENV !== "production"
export const isArray = (v: any): v is any[] => Array.isArray(v)
export const isBoolean = (v: any): v is boolean => v === true || v === false
export const isObject = (v: any): v is Record<string, any> => !(v == null || typeof v !== "object" || isArray(v))
export const isObjectLike = (v: any): v is Record<string, any> => v != null && typeof v === "object"
export const isObject = (v: any): v is Record<string, any> => isObjectLike(v) && !isArray(v)
export const isNumber = (v: any): v is number => typeof v === "number" && !Number.isNaN(v)
export const isString = (v: any): v is string => typeof v === "string"
export const isFunction = (v: any): v is Function => typeof v === "function"
export const isNull = (v: any): v is null | undefined => v == null

export const hasProp = <T extends string>(obj: any, prop: T): obj is Record<T, any> =>
Object.prototype.hasOwnProperty.call(obj, prop)

const baseGetTag = (v: any) => Object.prototype.toString.call(v)
const fnToString = Function.prototype.toString
const objectCtorString = fnToString.call(Object)

export const isPlainObject = (v: any) => {
if (!isObjectLike(v) || baseGetTag(v) != "[object Object]") return false
const proto = Object.getPrototypeOf(v)
if (proto === null) return true
const Ctor = hasProp(proto, "constructor") && proto.constructor
return typeof Ctor == "function" && Ctor instanceof Ctor && fnToString.call(Ctor) == objectCtorString
}

0 comments on commit 8d4d226

Please sign in to comment.