Skip to content

Commit

Permalink
fix(type-helpers): update isObject and isPlainObject functions
Browse files Browse the repository at this point in the history
refactor(core): use isPlainObject instead of isObject in syncStateWithStorage
refactor(react): use isPlainObject instead of isObject in useSearch
  • Loading branch information
Ryan-Zayne committed Nov 22, 2024
1 parent 8d237d3 commit b61e915
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 16 deletions.
5 changes: 0 additions & 5 deletions .changeset/hungry-kangaroos-lay.md

This file was deleted.

7 changes: 7 additions & 0 deletions .changeset/shaggy-frogs-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@zayne-labs/toolkit": patch
---

fix(type-helpers): update isObject and isPlainObject functions
refactor(core): use isPlainObject instead of isObject in syncStateWithStorage
refactor(react): use isPlainObject instead of isObject in useSearch
7 changes: 7 additions & 0 deletions dev/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# dev

## 0.0.9

### Patch Changes

- Updated dependencies [8d237d3]
- @zayne-labs/toolkit@0.6.7

## 0.0.8

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion dev/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "dev",
"type": "module",
"version": "0.0.8",
"version": "0.0.9",
"private": true,
"scripts": {
"build": "tsc && vite build",
Expand Down
6 changes: 6 additions & 0 deletions packages/toolkit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @zayne-labs/toolkit

## 0.6.7

### Patch Changes

- 8d237d3: fix(toolkit): only call onSuccess callback when there are valid files

## 0.6.6

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/toolkit/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zayne-labs/toolkit",
"type": "module",
"version": "0.6.6",
"version": "0.6.7",
"packageManager": "[email protected]",
"description": "A collection of utility functions, types and composables used by my other projects. Nothing too fancy but can be useful.",
"author": "Ryan Zayne",
Expand Down
6 changes: 3 additions & 3 deletions packages/toolkit/src/core/syncStateWithStorage.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray, isObject } from "@/type-helpers";
import { isArray, isPlainObject } from "@/type-helpers";
import { pickKeys } from "./pickKeys";

type SyncStorageParams =
Expand All @@ -22,14 +22,14 @@ const syncStateWithStorage: SyncStateWithStorage = (...params: SyncStorageParams
const [storageKey, state, keysToOmit] = params;

switch (true) {
case isObject(state) && keysToOmit !== undefined: {
case isPlainObject(state) && keysToOmit !== undefined: {
const stateSlice = pickKeys(state, keysToOmit);

localStorage.setItem(storageKey, JSON.stringify(stateSlice));
break;
}

case isObject(state) || isArray(state): {
case isPlainObject(state) || isArray(state): {
localStorage.setItem(storageKey, JSON.stringify(state));
break;
}
Expand Down
4 changes: 2 additions & 2 deletions packages/toolkit/src/react/hooks/useSearch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isObject } from "@/type-helpers";
import { isPlainObject } from "@/type-helpers";
import { useState } from "react";
import { useAfterMountEffect } from "./effects/useAfterMountEffect";
import { useDebouncedFn } from "./useDebounce";
Expand Down Expand Up @@ -28,7 +28,7 @@ const useSearch = <TData>(initialData: TData[], delay?: number) => {
return item.toString().toLowerCase().includes(query);
}

if (isObject(item)) {
if (isPlainObject(item)) {
return checkObjectPropsForQuery(item, query);
}

Expand Down
13 changes: 9 additions & 4 deletions packages/toolkit/src/type-helpers/guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ export const isArray = <TArray>(value: unknown): value is TArray[] => Array.isAr

export const isFormData = (value: unknown) => value instanceof FormData;

export const isObject = <TObject extends AnyObject>(value: unknown): value is TObject => {
return typeof value === "object" && value !== null && !isArray(value);
export const isObject = (value: unknown) => {
return typeof value === "object" && value !== null;
};

// eslint-disable-next-line ts-eslint/no-unsafe-function-type
export const isPlainObject = (value: unknown, Class?: Function) => {
export const isPlainObject = <TObject extends AnyObject>(
value: unknown,
// eslint-disable-next-line ts-eslint/no-unsafe-function-type
Class?: Function
): value is TObject => {
if (!isObject(value)) {
return false;
}
Expand All @@ -38,3 +41,5 @@ export const isAsyncFunction = <TAsyncFunction extends AnyAsyncFunction>(
): value is TAsyncFunction => {
return isFunction(value) && value.constructor.name === "AsyncFunction";
};

export const isFile = (value: unknown) => value instanceof File;

0 comments on commit b61e915

Please sign in to comment.