Skip to content

Commit

Permalink
fix: fix hashStoreKey function
Browse files Browse the repository at this point in the history
  • Loading branch information
afiiif committed Feb 28, 2024
1 parent 6a07de9 commit bb7622f
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/react/create-stores.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,30 @@ export type CreateStoresOptions<
hashKeyFn?: (obj: TKey) => string;
};

export const hashStoreKey = (obj?: any) => JSON.stringify(obj, Object.keys(obj).sort());
const hasObjectPrototype = (value: any) => {
return Object.prototype.toString.call(value) === '[object Object]';
};
const isPlainObject = (value: any) => {
if (!hasObjectPrototype(value)) return false;
const ctor = value.constructor;
if (typeof ctor === 'undefined') return true;
const prot = ctor.prototype;
if (!hasObjectPrototype(prot)) return false;
if (!prot.hasOwnProperty('isPrototypeOf')) return false;
return true;
};
export const hashStoreKey = (value?: any) =>
// Copied from: https://github.com/TanStack/query/blob/main/packages/query-core/src/utils.ts
JSON.stringify(value, (_, val) =>
isPlainObject(val)
? Object.keys(val)
.sort()
.reduce((result, key) => {
result[key] = val[key];
return result;
}, {} as any)
: val,
);

/**
* @see https://floppy-disk.vercel.app/docs/api#createstores
Expand Down

0 comments on commit bb7622f

Please sign in to comment.