@yamada-ui/utils 0.1.1 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ declare const filterEmpty: <T extends any[]>(array: T) => any[];
2
+
3
+ export { filterEmpty };
@@ -0,0 +1,16 @@
1
+ import { Dict } from './index.types.mjs';
2
+
3
+ declare const isNumber: (value: any) => value is number;
4
+ declare const isNotNumber: (value: any) => boolean;
5
+ declare const isNumeric: (value: any) => boolean;
6
+ declare const isString: (value: any) => value is string;
7
+ declare const isUndefined: (value: any) => value is undefined;
8
+ declare const isNull: (value: any) => value is null;
9
+ declare const isObject: <T extends Dict>(value: any) => value is T;
10
+ declare const isArray: <T extends any[]>(value: any) => value is T;
11
+ declare const isEmpty: (value: any) => boolean;
12
+ declare const isFunction: <T extends Function = Function>(value: any) => value is T;
13
+ declare const isUnit: (value: any) => boolean;
14
+ declare const cast: <T>(value: any) => T;
15
+
16
+ export { cast, isArray, isEmpty, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit };
@@ -6,7 +6,7 @@ declare const isNumeric: (value: any) => boolean;
6
6
  declare const isString: (value: any) => value is string;
7
7
  declare const isUndefined: (value: any) => value is undefined;
8
8
  declare const isNull: (value: any) => value is null;
9
- declare const isObject: <T extends Dict<any>>(value: any) => value is T;
9
+ declare const isObject: <T extends Dict>(value: any) => value is T;
10
10
  declare const isArray: <T extends any[]>(value: any) => value is T;
11
11
  declare const isEmpty: (value: any) => boolean;
12
12
  declare const isFunction: <T extends Function = Function>(value: any) => value is T;
@@ -0,0 +1,17 @@
1
+ type Operand = string | number;
2
+ type Calc = {
3
+ add: (...args: Operand[]) => Calc;
4
+ subtract: (...args: Operand[]) => Calc;
5
+ multiply: (...args: Operand[]) => Calc;
6
+ divide: (...args: Operand[]) => Calc;
7
+ negate: () => Calc;
8
+ };
9
+ declare const calc: ((x: Operand) => Calc) & {
10
+ add: (...args: Operand[]) => string;
11
+ subtract: (...args: Operand[]) => string;
12
+ multiply: (...args: Operand[]) => string;
13
+ divide: (...args: Operand[]) => string;
14
+ negate: (value: Operand) => string;
15
+ };
16
+
17
+ export { Operand, calc };
@@ -118,6 +118,47 @@ var useUpdateEffect = (callback, deps) => {
118
118
  };
119
119
  }, []);
120
120
  };
121
+ var useAsync = (func, deps = []) => {
122
+ const [state, callback] = useAsyncFunc(func, deps, { loading: true });
123
+ React.useEffect(() => {
124
+ callback();
125
+ }, [callback]);
126
+ return state;
127
+ };
128
+ var useAsyncFunc = (func, deps = [], initialState = { loading: false }) => {
129
+ const lastCallId = React.useRef(0);
130
+ const isMounted = useIsMounted();
131
+ const [state, setState] = React.useState(initialState);
132
+ const callback = React.useCallback((...args) => {
133
+ const callId = ++lastCallId.current;
134
+ if (!state.loading)
135
+ setState((prevState) => ({ ...prevState, loading: true }));
136
+ return func(...args).then(
137
+ (value) => {
138
+ if (isMounted.current && callId === lastCallId.current)
139
+ setState({ value, loading: false });
140
+ return value;
141
+ },
142
+ (error) => {
143
+ if (isMounted.current && callId === lastCallId.current)
144
+ setState({ error, loading: false });
145
+ return error;
146
+ }
147
+ );
148
+ }, deps);
149
+ return [state, callback];
150
+ };
151
+ var useAsyncRetry = (func, deps = []) => {
152
+ const [attempt, setAttempt] = React.useState(0);
153
+ const state = useAsync(func, [...deps, attempt]);
154
+ const stateLoading = state.loading;
155
+ const retry = React.useCallback(() => {
156
+ if (stateLoading)
157
+ return;
158
+ setAttempt((currentAttempt) => currentAttempt + 1);
159
+ }, [...deps, stateLoading]);
160
+ return { ...state, retry };
161
+ };
121
162
 
122
163
  // src/color.ts
123
164
  import { toHex, parseToRgba, transparentize, mix, darken, lighten } from "color2k";
@@ -389,6 +430,9 @@ export {
389
430
  useMergeRefs,
390
431
  useCallbackRef,
391
432
  useUpdateEffect,
433
+ useAsync,
434
+ useAsyncFunc,
435
+ useAsyncRetry,
392
436
  getColor,
393
437
  lightenColor,
394
438
  darkenColor,
@@ -0,0 +1,18 @@
1
+ import { Dict } from './index.types.mjs';
2
+
3
+ declare const getColor: (color: string, fallback?: string) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
4
+ declare const lightenColor: (color: string, amount: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
5
+ declare const darkenColor: (color: string, amount: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
6
+ declare const tintColor: (color: string, amount: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
7
+ declare const shadeColor: (color: string, amount: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
8
+ declare const transparentizeColor: (color: string, alpha: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
9
+ declare const toneColor: (color: string, l: number) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => string;
10
+ declare const randomColor: ({ string, colors }?: {
11
+ string?: string | undefined;
12
+ colors?: string[] | undefined;
13
+ }) => string;
14
+ declare const isTone: (color: string) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => "light" | "dark";
15
+ declare const isLight: (color: string) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => boolean;
16
+ declare const isDark: (color: string) => (theme: Dict, colorMode: 'light' | 'dark' | undefined) => boolean;
17
+
18
+ export { darkenColor, getColor, isDark, isLight, isTone, lightenColor, randomColor, shadeColor, tintColor, toneColor, transparentizeColor };
package/dist/color.mjs CHANGED
@@ -10,7 +10,7 @@ import {
10
10
  tintColor,
11
11
  toneColor,
12
12
  transparentizeColor
13
- } from "./chunk-HUEOJZVC.mjs";
13
+ } from "./chunk-UUOSA3BW.mjs";
14
14
  import "./chunk-SLJ4M7XC.mjs";
15
15
  import "./chunk-VYMGBE25.mjs";
16
16
  import "./chunk-BZAW2D6U.mjs";
package/dist/dom.d.mts ADDED
@@ -0,0 +1,33 @@
1
+ import React__default from 'react';
2
+
3
+ declare const createdDom: () => boolean;
4
+ declare const getPlatform: () => string;
5
+ declare const vendor: (v: RegExp) => boolean;
6
+ declare const platform: (v: RegExp) => boolean;
7
+ declare const isMac: () => boolean;
8
+ declare const isApple: () => boolean;
9
+ declare const isSafari: () => boolean;
10
+ declare const isElement: (el: any) => el is Element;
11
+ declare const isHTMLElement: (el: any) => el is HTMLElement;
12
+ declare const isHidden: (el: HTMLElement) => boolean;
13
+ declare const isDisabled: (el: HTMLElement) => boolean;
14
+ declare const hasTabIndex: (el: HTMLElement) => boolean;
15
+ declare const isContentEditable: (el: HTMLElement) => boolean;
16
+ declare const isContains: (parent: HTMLElement | null, child: HTMLElement | null) => boolean | undefined;
17
+ declare const getEventRelatedTarget: (ev: React__default.FocusEvent | React__default.MouseEvent) => HTMLElement | null;
18
+ type Booleanish = boolean | 'true' | 'false';
19
+ declare const dataAttr: (condition: boolean | undefined) => Booleanish;
20
+ declare const ariaAttr: (condition: boolean | undefined) => boolean | undefined;
21
+ type FocusableElement = {
22
+ focus: (options?: FocusOptions) => void;
23
+ };
24
+ declare const getAllFocusable: <T extends HTMLElement>(container: T) => T[];
25
+ declare const isFocusable: (el: HTMLElement) => boolean;
26
+ declare const hasNegativeTabIndex: (el: HTMLElement) => boolean;
27
+ declare const isTabbable: (el?: HTMLElement | null) => boolean;
28
+ declare const getOwnerWindow: (node?: Element | null) => Window & typeof globalThis;
29
+ declare const getOwnerDocument: (el?: Element | null) => Document;
30
+ declare const getActiveElement: (el?: HTMLElement) => HTMLElement;
31
+ declare const isActiveElement: (el: HTMLElement) => boolean;
32
+
33
+ export { FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, platform, vendor };
@@ -0,0 +1,30 @@
1
+ type AnyPointerEvent = MouseEvent | TouchEvent | PointerEvent;
2
+ type PointType = 'page' | 'client';
3
+ type Point = {
4
+ x: number;
5
+ y: number;
6
+ };
7
+ type PointerEventInfo = {
8
+ point: Point;
9
+ };
10
+ type MixedEventListener = (e: AnyPointerEvent, info: PointerEventInfo) => void;
11
+ declare const isMouseEvent: (ev: any) => ev is MouseEvent;
12
+ declare const isTouchEvent: (ev: AnyPointerEvent) => ev is TouchEvent;
13
+ declare const isMultiTouchEvent: (ev: AnyPointerEvent) => boolean;
14
+ declare const getEventWindow: (ev: Event) => typeof globalThis;
15
+ declare const pointFromTouch: (e: TouchEvent, type?: PointType) => {
16
+ x: number;
17
+ y: number;
18
+ };
19
+ declare const pointFromMouse: (point: MouseEvent | PointerEvent, type?: PointType) => {
20
+ x: number;
21
+ y: number;
22
+ };
23
+ declare const getEventPoint: (ev: AnyPointerEvent, type?: PointType) => {
24
+ x: number;
25
+ y: number;
26
+ };
27
+ declare const addDomEvent: (target: EventTarget, type: string, cb: EventListener, options?: AddEventListenerOptions) => () => void;
28
+ declare const addPointerEvent: (target: EventTarget, type: string, cb: MixedEventListener, options?: AddEventListenerOptions) => () => void;
29
+
30
+ export { AnyPointerEvent, MixedEventListener, Point, PointType, PointerEventInfo, addDomEvent, addPointerEvent, getEventPoint, getEventWindow, isMouseEvent, isMultiTouchEvent, isTouchEvent, pointFromMouse, pointFromTouch };
@@ -0,0 +1,6 @@
1
+ declare const noop: () => void;
2
+ declare const runIfFunc: <T, U extends any[]>(valOrFunc: T | ((...funcArgs: U) => T), ...args: U) => T;
3
+ declare const handlerAll: <T extends (event: any) => void>(...funcs: (T | undefined)[]) => (event: (T extends (...args: infer R) => any ? R : never)[0]) => void;
4
+ declare const funcAll: <T extends (...args: any[]) => any>(...funcs: (T | undefined)[]) => (arg: (T extends (...args: infer R) => any ? R : never)[0]) => void;
5
+
6
+ export { funcAll, handlerAll, noop, runIfFunc };
package/dist/function.mjs CHANGED
@@ -3,7 +3,7 @@ import {
3
3
  handlerAll,
4
4
  noop,
5
5
  runIfFunc
6
- } from "./chunk-HUEOJZVC.mjs";
6
+ } from "./chunk-UUOSA3BW.mjs";
7
7
  import "./chunk-SLJ4M7XC.mjs";
8
8
  import "./chunk-VYMGBE25.mjs";
9
9
  import "./chunk-BZAW2D6U.mjs";
@@ -0,0 +1,13 @@
1
+ export { Dict, DynamicRecord, Length, Path, StringLiteral, Union } from './index.types.mjs';
2
+ export { cast, isArray, isEmpty, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit } from './assertion.mjs';
3
+ export { assignAfter, filterObject, filterUndefined, flattenObject, getMemoizedObject, getObject, keysFormObject, memoizeObject, merge, objectFromEntries, omitObject, pickObject, replaceObject, splitObject } from './object.mjs';
4
+ export { funcAll, handlerAll, noop, runIfFunc } from './function.mjs';
5
+ export { AsyncFnReturn, AsyncState, AsyncStateRetry, DOMAttributes, FunctionReturningPromise, MaybeRenderProp, PromiseType, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useAsync, useAsyncFunc, useAsyncRetry, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect } from './react.mjs';
6
+ export { FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, platform, vendor } from './dom.mjs';
7
+ export { escape } from './string.mjs';
8
+ export { Operand, calc } from './calc.mjs';
9
+ export { darkenColor, getColor, isDark, isLight, isTone, lightenColor, randomColor, shadeColor, tintColor, toneColor, transparentizeColor } from './color.mjs';
10
+ export { filterEmpty } from './array.mjs';
11
+ export { clampNumber, countDecimal, percentToValue, roundNumberToStep, toPrecision, valueToPercent } from './number.mjs';
12
+ export { AnyPointerEvent, MixedEventListener, Point, PointType, PointerEventInfo, addDomEvent, addPointerEvent, getEventPoint, getEventWindow, isMouseEvent, isMultiTouchEvent, isTouchEvent, pointFromMouse, pointFromTouch } from './event.mjs';
13
+ import 'react';
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export { Dict, DynamicRecord, Length, Path, StringLiteral, Union } from './index
2
2
  export { cast, isArray, isEmpty, isFunction, isNotNumber, isNull, isNumber, isNumeric, isObject, isString, isUndefined, isUnit } from './assertion.js';
3
3
  export { assignAfter, filterObject, filterUndefined, flattenObject, getMemoizedObject, getObject, keysFormObject, memoizeObject, merge, objectFromEntries, omitObject, pickObject, replaceObject, splitObject } from './object.js';
4
4
  export { funcAll, handlerAll, noop, runIfFunc } from './function.js';
5
- export { DOMAttributes, MaybeRenderProp, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect } from './react.js';
5
+ export { AsyncFnReturn, AsyncState, AsyncStateRetry, DOMAttributes, FunctionReturningPromise, MaybeRenderProp, PromiseType, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useAsync, useAsyncFunc, useAsyncRetry, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect } from './react.js';
6
6
  export { FocusableElement, ariaAttr, createdDom, dataAttr, getActiveElement, getAllFocusable, getEventRelatedTarget, getOwnerDocument, getOwnerWindow, getPlatform, hasNegativeTabIndex, hasTabIndex, isActiveElement, isApple, isContains, isContentEditable, isDisabled, isElement, isFocusable, isHTMLElement, isHidden, isMac, isSafari, isTabbable, platform, vendor } from './dom.js';
7
7
  export { escape } from './string.js';
8
8
  export { Operand, calc } from './calc.js';
package/dist/index.js CHANGED
@@ -123,6 +123,9 @@ __export(src_exports, {
123
123
  toPrecision: () => toPrecision,
124
124
  toneColor: () => toneColor,
125
125
  transparentizeColor: () => transparentizeColor,
126
+ useAsync: () => useAsync,
127
+ useAsyncFunc: () => useAsyncFunc,
128
+ useAsyncRetry: () => useAsyncRetry,
126
129
  useCallbackRef: () => useCallbackRef,
127
130
  useIsMounted: () => useIsMounted,
128
131
  useMergeRefs: () => useMergeRefs,
@@ -392,6 +395,47 @@ var useUpdateEffect = (callback, deps) => {
392
395
  };
393
396
  }, []);
394
397
  };
398
+ var useAsync = (func, deps = []) => {
399
+ const [state, callback] = useAsyncFunc(func, deps, { loading: true });
400
+ React.useEffect(() => {
401
+ callback();
402
+ }, [callback]);
403
+ return state;
404
+ };
405
+ var useAsyncFunc = (func, deps = [], initialState = { loading: false }) => {
406
+ const lastCallId = React.useRef(0);
407
+ const isMounted = useIsMounted();
408
+ const [state, setState] = React.useState(initialState);
409
+ const callback = React.useCallback((...args) => {
410
+ const callId = ++lastCallId.current;
411
+ if (!state.loading)
412
+ setState((prevState) => ({ ...prevState, loading: true }));
413
+ return func(...args).then(
414
+ (value) => {
415
+ if (isMounted.current && callId === lastCallId.current)
416
+ setState({ value, loading: false });
417
+ return value;
418
+ },
419
+ (error) => {
420
+ if (isMounted.current && callId === lastCallId.current)
421
+ setState({ error, loading: false });
422
+ return error;
423
+ }
424
+ );
425
+ }, deps);
426
+ return [state, callback];
427
+ };
428
+ var useAsyncRetry = (func, deps = []) => {
429
+ const [attempt, setAttempt] = React.useState(0);
430
+ const state = useAsync(func, [...deps, attempt]);
431
+ const stateLoading = state.loading;
432
+ const retry = React.useCallback(() => {
433
+ if (stateLoading)
434
+ return;
435
+ setAttempt((currentAttempt) => currentAttempt + 1);
436
+ }, [...deps, stateLoading]);
437
+ return { ...state, retry };
438
+ };
395
439
 
396
440
  // src/dom.ts
397
441
  var createdDom = () => !!(typeof window !== "undefined" && window.document && window.document.createElement);
@@ -787,6 +831,9 @@ var addPointerEvent = (target, type, cb, options) => addDomEvent(target, type, w
787
831
  toPrecision,
788
832
  toneColor,
789
833
  transparentizeColor,
834
+ useAsync,
835
+ useAsyncFunc,
836
+ useAsyncRetry,
790
837
  useCallbackRef,
791
838
  useIsMounted,
792
839
  useMergeRefs,
package/dist/index.mjs CHANGED
@@ -39,13 +39,16 @@ import {
39
39
  tintColor,
40
40
  toneColor,
41
41
  transparentizeColor,
42
+ useAsync,
43
+ useAsyncFunc,
44
+ useAsyncRetry,
42
45
  useCallbackRef,
43
46
  useIsMounted,
44
47
  useMergeRefs,
45
48
  useSafeLayoutEffect,
46
49
  useUnmountEffect,
47
50
  useUpdateEffect
48
- } from "./chunk-HUEOJZVC.mjs";
51
+ } from "./chunk-UUOSA3BW.mjs";
49
52
  import "./chunk-SLJ4M7XC.mjs";
50
53
  import {
51
54
  clampNumber,
@@ -210,6 +213,9 @@ export {
210
213
  toPrecision,
211
214
  toneColor,
212
215
  transparentizeColor,
216
+ useAsync,
217
+ useAsyncFunc,
218
+ useAsyncRetry,
213
219
  useCallbackRef,
214
220
  useIsMounted,
215
221
  useMergeRefs,
@@ -0,0 +1,14 @@
1
+ type Primitive = null | undefined | string | number | boolean | symbol | bigint;
2
+ type PathImpl<K extends string | number | symbol, V> = K extends string | number ? V extends Primitive ? `${K}` : `${K}.${Path<V>}` : ``;
3
+ type Path<T> = {
4
+ [K in keyof T]-?: PathImpl<K, T[K]>;
5
+ }[keyof T];
6
+ type Dict<T = any> = Record<string, T>;
7
+ type StringLiteral = string & {};
8
+ type Union<T> = T | StringLiteral;
9
+ type Length = string | 0 | number;
10
+ type DynamicRecord<T> = {
11
+ [K in keyof T]-?: T[K] extends Primitive ? string | number : DynamicRecord<T[K]>;
12
+ };
13
+
14
+ export { Dict, DynamicRecord, Length, Path, StringLiteral, Union };
@@ -0,0 +1,8 @@
1
+ declare const toPrecision: (n: number, precision?: number) => string;
2
+ declare const countDecimal: (n: number) => number;
3
+ declare const roundNumberToStep: (n: number, from: number, step: number) => string;
4
+ declare const valueToPercent: (n: number, min: number, max: number) => number;
5
+ declare const percentToValue: (n: number, min: number, max: number) => number;
6
+ declare const clampNumber: (n: number, min: number, max: number) => number;
7
+
8
+ export { clampNumber, countDecimal, percentToValue, roundNumberToStep, toPrecision, valueToPercent };
@@ -0,0 +1,18 @@
1
+ import { Dict } from './index.types.mjs';
2
+
3
+ declare const omitObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => Omit<T, K>;
4
+ declare const pickObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => { [P in K]: T[P]; };
5
+ declare const splitObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => [{ [P in K]: T[P]; }, Omit<T, K>];
6
+ declare const filterObject: <T extends Dict, K extends Dict>(obj: T, func: (key: keyof T, value: T[keyof T], obj: T) => boolean) => K;
7
+ declare const filterUndefined: <T extends Dict>(obj: T) => T;
8
+ declare const merge: <T extends Dict>(target: any, source: any, overrideArray?: boolean) => T;
9
+ declare const flattenObject: <T extends Dict>(obj: any, maxDepth?: number) => T;
10
+ declare const objectFromEntries: <T extends Dict>(entries: any[][]) => T;
11
+ declare const keysFormObject: <T extends Dict>(obj: T) => (keyof T)[];
12
+ declare const replaceObject: <T extends unknown>(objOrArray: T, callBack: (value: any) => any) => T;
13
+ declare const getObject: (obj: Dict, path: string | number, fallback?: any, i?: number) => any;
14
+ declare const memoizeObject: (func: typeof getObject) => (obj: Dict, path: string | number, fallback?: any, i?: number) => any;
15
+ declare const getMemoizedObject: (obj: Dict, path: string | number, fallback?: any, i?: number) => any;
16
+ declare const assignAfter: (target: Record<string, any>, ...sources: any[]) => Record<string, unknown>;
17
+
18
+ export { assignAfter, filterObject, filterUndefined, flattenObject, getMemoizedObject, getObject, keysFormObject, memoizeObject, merge, objectFromEntries, omitObject, pickObject, replaceObject, splitObject };
package/dist/object.d.ts CHANGED
@@ -1,14 +1,14 @@
1
1
  import { Dict } from './index.types.js';
2
2
 
3
- declare const omitObject: <T extends Dict<any>, K extends keyof T>(obj: T, keys: K[]) => Omit<T, K>;
4
- declare const pickObject: <T extends Dict<any>, K extends keyof T>(obj: T, keys: K[]) => { [P in K]: T[P]; };
5
- declare const splitObject: <T extends Dict<any>, K extends keyof T>(obj: T, keys: K[]) => [{ [P in K]: T[P]; }, Omit<T, K>];
6
- declare const filterObject: <T extends Dict<any>, K extends Dict<any>>(obj: T, func: (key: keyof T, value: T[keyof T], obj: T) => boolean) => K;
7
- declare const filterUndefined: <T extends Dict<any>>(obj: T) => T;
8
- declare const merge: <T extends Dict<any>>(target: any, source: any, overrideArray?: boolean) => T;
9
- declare const flattenObject: <T extends Dict<any>>(obj: any, maxDepth?: number) => T;
10
- declare const objectFromEntries: <T extends Dict<any>>(entries: any[][]) => T;
11
- declare const keysFormObject: <T extends Dict<any>>(obj: T) => (keyof T)[];
3
+ declare const omitObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => Omit<T, K>;
4
+ declare const pickObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => { [P in K]: T[P]; };
5
+ declare const splitObject: <T extends Dict, K extends keyof T>(obj: T, keys: K[]) => [{ [P in K]: T[P]; }, Omit<T, K>];
6
+ declare const filterObject: <T extends Dict, K extends Dict>(obj: T, func: (key: keyof T, value: T[keyof T], obj: T) => boolean) => K;
7
+ declare const filterUndefined: <T extends Dict>(obj: T) => T;
8
+ declare const merge: <T extends Dict>(target: any, source: any, overrideArray?: boolean) => T;
9
+ declare const flattenObject: <T extends Dict>(obj: any, maxDepth?: number) => T;
10
+ declare const objectFromEntries: <T extends Dict>(entries: any[][]) => T;
11
+ declare const keysFormObject: <T extends Dict>(obj: T) => (keyof T)[];
12
12
  declare const replaceObject: <T extends unknown>(objOrArray: T, callBack: (value: any) => any) => T;
13
13
  declare const getObject: (obj: Dict, path: string | number, fallback?: any, i?: number) => any;
14
14
  declare const memoizeObject: (func: typeof getObject) => (obj: Dict, path: string | number, fallback?: any, i?: number) => any;
package/dist/object.mjs CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  pickObject,
14
14
  replaceObject,
15
15
  splitObject
16
- } from "./chunk-HUEOJZVC.mjs";
16
+ } from "./chunk-UUOSA3BW.mjs";
17
17
  import "./chunk-SLJ4M7XC.mjs";
18
18
  import "./chunk-VYMGBE25.mjs";
19
19
  import "./chunk-BZAW2D6U.mjs";
@@ -0,0 +1,94 @@
1
+ import * as React from 'react';
2
+
3
+ type DOMElement = Element & HTMLOrSVGElement;
4
+ type DataAttributes = {
5
+ [dataAttr: string]: any;
6
+ };
7
+ type DOMAttributes<Y = DOMElement> = React.AriaAttributes & React.DOMAttributes<Y> & DataAttributes & {
8
+ id?: string;
9
+ role?: React.AriaRole;
10
+ tabIndex?: number;
11
+ style?: React.CSSProperties;
12
+ };
13
+ type Merge<Y, M> = M extends Record<string, unknown> ? Y : Omit<Y, keyof M> & M;
14
+ type PropGetter<Y = Record<string, unknown>, M = DOMAttributes> = (props?: Merge<DOMAttributes, Y>, ref?: React.Ref<any>) => M & React.RefAttributes<any>;
15
+ type RequiredPropGetter<Y = Record<string, unknown>, M = DOMAttributes> = (props: Merge<DOMAttributes, Y>, ref?: React.Ref<any>) => M & React.RefAttributes<any>;
16
+ type MaybeRenderProp<Y> = React.ReactNode | ((props: Y) => React.ReactNode);
17
+ type Options = {
18
+ strict?: boolean;
19
+ errorMessage?: string;
20
+ name?: string;
21
+ };
22
+ type CreateContextReturn<T> = [React.Provider<T>, () => T, React.Context<T>];
23
+ declare const createContext: <ContextType extends unknown = any>({ strict, errorMessage, name, }?: Options) => CreateContextReturn<ContextType>;
24
+ declare const useSafeLayoutEffect: typeof React.useLayoutEffect;
25
+ declare const useUnmountEffect: (callback: () => void) => void;
26
+ declare const useIsMounted: () => React.MutableRefObject<boolean>;
27
+ declare const getValidChildren: (children: React.ReactNode) => React.ReactElement[];
28
+ declare const isValidElement: (child: any) => child is React.ReactNode;
29
+ declare const findChildren: (children: React.ReactElement<any, string | React.JSXElementConstructor<any>>[], ...types: React.JSXElementConstructor<any>[]) => [React.ReactElement | undefined, ...React.ReactElement[]];
30
+ declare const includesChildren: (children: React.ReactElement<any, string | React.JSXElementConstructor<any>>[], ...types: React.JSXElementConstructor<any>[]) => boolean;
31
+ declare const omitChildren: (children: React.ReactElement<any, string | React.JSXElementConstructor<any>>[], ...types: React.JSXElementConstructor<any>[]) => React.ReactElement[];
32
+ declare const pickChildren: (children: React.ReactElement<any, string | React.JSXElementConstructor<any>>[], ...types: React.JSXElementConstructor<any>[]) => React.ReactElement[];
33
+ declare const cx: (...classNames: (string | undefined)[]) => string;
34
+ type ReactRef<T> = React.Ref<T> | React.MutableRefObject<T>;
35
+ declare const isRefObject: (val: any) => val is {
36
+ current: any;
37
+ };
38
+ declare const assignRef: <T extends unknown = any>(ref: ReactRef<T> | undefined, value: T) => void;
39
+ declare const mergeRefs: <T extends unknown = any>(...refs: (ReactRef<T> | undefined)[]) => (node: T | null) => void;
40
+ declare const useMergeRefs: <T extends unknown = any>(...refs: (ReactRef<T> | undefined)[]) => (node: T | null) => void;
41
+ declare const useCallbackRef: <T extends (...args: any[]) => any>(callback: T | undefined, deps?: React.DependencyList) => T;
42
+ declare const useUpdateEffect: (callback: React.EffectCallback, deps: React.DependencyList) => void;
43
+ type FunctionReturningPromise = (...args: any[]) => Promise<any>;
44
+ declare const useAsync: <T extends FunctionReturningPromise>(func: T, deps?: React.DependencyList) => StateFromFunctionReturningPromise<T>;
45
+ type AsyncState<T> = {
46
+ loading: boolean;
47
+ error?: undefined;
48
+ value?: undefined;
49
+ } | {
50
+ loading: true;
51
+ error?: Error | undefined;
52
+ value?: T;
53
+ } | {
54
+ loading: false;
55
+ error: Error;
56
+ value?: undefined;
57
+ } | {
58
+ loading: false;
59
+ error?: undefined;
60
+ value: T;
61
+ };
62
+ type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never;
63
+ type StateFromFunctionReturningPromise<T extends FunctionReturningPromise> = AsyncState<PromiseType<ReturnType<T>>>;
64
+ type AsyncFnReturn<T extends FunctionReturningPromise = FunctionReturningPromise> = [
65
+ StateFromFunctionReturningPromise<T>,
66
+ T
67
+ ];
68
+ declare const useAsyncFunc: <T extends FunctionReturningPromise>(func: T, deps?: React.DependencyList, initialState?: StateFromFunctionReturningPromise<T>) => AsyncFnReturn<T>;
69
+ type AsyncStateRetry<T> = AsyncState<T> & {
70
+ retry(): void;
71
+ };
72
+ declare const useAsyncRetry: <T>(func: () => Promise<T>, deps?: React.DependencyList) => {
73
+ retry: () => void;
74
+ loading: boolean;
75
+ error?: undefined;
76
+ value?: undefined;
77
+ } | {
78
+ retry: () => void;
79
+ loading: false;
80
+ error: Error;
81
+ value?: undefined;
82
+ } | {
83
+ retry: () => void;
84
+ loading: true;
85
+ error?: Error | undefined;
86
+ value?: T | undefined;
87
+ } | {
88
+ retry: () => void;
89
+ loading: false;
90
+ error?: undefined;
91
+ value: T;
92
+ };
93
+
94
+ export { AsyncFnReturn, AsyncState, AsyncStateRetry, DOMAttributes, FunctionReturningPromise, MaybeRenderProp, PromiseType, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useAsync, useAsyncFunc, useAsyncRetry, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect };
package/dist/react.d.ts CHANGED
@@ -40,5 +40,55 @@ declare const mergeRefs: <T extends unknown = any>(...refs: (ReactRef<T> | undef
40
40
  declare const useMergeRefs: <T extends unknown = any>(...refs: (ReactRef<T> | undefined)[]) => (node: T | null) => void;
41
41
  declare const useCallbackRef: <T extends (...args: any[]) => any>(callback: T | undefined, deps?: React.DependencyList) => T;
42
42
  declare const useUpdateEffect: (callback: React.EffectCallback, deps: React.DependencyList) => void;
43
+ type FunctionReturningPromise = (...args: any[]) => Promise<any>;
44
+ declare const useAsync: <T extends FunctionReturningPromise>(func: T, deps?: React.DependencyList) => StateFromFunctionReturningPromise<T>;
45
+ type AsyncState<T> = {
46
+ loading: boolean;
47
+ error?: undefined;
48
+ value?: undefined;
49
+ } | {
50
+ loading: true;
51
+ error?: Error | undefined;
52
+ value?: T;
53
+ } | {
54
+ loading: false;
55
+ error: Error;
56
+ value?: undefined;
57
+ } | {
58
+ loading: false;
59
+ error?: undefined;
60
+ value: T;
61
+ };
62
+ type PromiseType<P extends Promise<any>> = P extends Promise<infer T> ? T : never;
63
+ type StateFromFunctionReturningPromise<T extends FunctionReturningPromise> = AsyncState<PromiseType<ReturnType<T>>>;
64
+ type AsyncFnReturn<T extends FunctionReturningPromise = FunctionReturningPromise> = [
65
+ StateFromFunctionReturningPromise<T>,
66
+ T
67
+ ];
68
+ declare const useAsyncFunc: <T extends FunctionReturningPromise>(func: T, deps?: React.DependencyList, initialState?: StateFromFunctionReturningPromise<T>) => AsyncFnReturn<T>;
69
+ type AsyncStateRetry<T> = AsyncState<T> & {
70
+ retry(): void;
71
+ };
72
+ declare const useAsyncRetry: <T>(func: () => Promise<T>, deps?: React.DependencyList) => {
73
+ retry: () => void;
74
+ loading: boolean;
75
+ error?: undefined;
76
+ value?: undefined;
77
+ } | {
78
+ retry: () => void;
79
+ loading: false;
80
+ error: Error;
81
+ value?: undefined;
82
+ } | {
83
+ retry: () => void;
84
+ loading: true;
85
+ error?: Error | undefined;
86
+ value?: T | undefined;
87
+ } | {
88
+ retry: () => void;
89
+ loading: false;
90
+ error?: undefined;
91
+ value: T;
92
+ };
43
93
 
44
- export { DOMAttributes, MaybeRenderProp, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect };
94
+ export { AsyncFnReturn, AsyncState, AsyncStateRetry, DOMAttributes, FunctionReturningPromise, MaybeRenderProp, PromiseType, PropGetter, RequiredPropGetter, assignRef, createContext, cx, findChildren, getValidChildren, includesChildren, isRefObject, isValidElement, mergeRefs, omitChildren, pickChildren, useAsync, useAsyncFunc, useAsyncRetry, useCallbackRef, useIsMounted, useMergeRefs, useSafeLayoutEffect, useUnmountEffect, useUpdateEffect };
package/dist/react.js CHANGED
@@ -41,6 +41,9 @@ __export(react_exports, {
41
41
  mergeRefs: () => mergeRefs,
42
42
  omitChildren: () => omitChildren,
43
43
  pickChildren: () => pickChildren,
44
+ useAsync: () => useAsync,
45
+ useAsyncFunc: () => useAsyncFunc,
46
+ useAsyncRetry: () => useAsyncRetry,
44
47
  useCallbackRef: () => useCallbackRef,
45
48
  useIsMounted: () => useIsMounted,
46
49
  useMergeRefs: () => useMergeRefs,
@@ -154,6 +157,47 @@ var useUpdateEffect = (callback, deps) => {
154
157
  };
155
158
  }, []);
156
159
  };
160
+ var useAsync = (func, deps = []) => {
161
+ const [state, callback] = useAsyncFunc(func, deps, { loading: true });
162
+ React.useEffect(() => {
163
+ callback();
164
+ }, [callback]);
165
+ return state;
166
+ };
167
+ var useAsyncFunc = (func, deps = [], initialState = { loading: false }) => {
168
+ const lastCallId = React.useRef(0);
169
+ const isMounted = useIsMounted();
170
+ const [state, setState] = React.useState(initialState);
171
+ const callback = React.useCallback((...args) => {
172
+ const callId = ++lastCallId.current;
173
+ if (!state.loading)
174
+ setState((prevState) => ({ ...prevState, loading: true }));
175
+ return func(...args).then(
176
+ (value) => {
177
+ if (isMounted.current && callId === lastCallId.current)
178
+ setState({ value, loading: false });
179
+ return value;
180
+ },
181
+ (error) => {
182
+ if (isMounted.current && callId === lastCallId.current)
183
+ setState({ error, loading: false });
184
+ return error;
185
+ }
186
+ );
187
+ }, deps);
188
+ return [state, callback];
189
+ };
190
+ var useAsyncRetry = (func, deps = []) => {
191
+ const [attempt, setAttempt] = React.useState(0);
192
+ const state = useAsync(func, [...deps, attempt]);
193
+ const stateLoading = state.loading;
194
+ const retry = React.useCallback(() => {
195
+ if (stateLoading)
196
+ return;
197
+ setAttempt((currentAttempt) => currentAttempt + 1);
198
+ }, [...deps, stateLoading]);
199
+ return { ...state, retry };
200
+ };
157
201
  // Annotate the CommonJS export names for ESM import in node:
158
202
  0 && (module.exports = {
159
203
  assignRef,
@@ -167,6 +211,9 @@ var useUpdateEffect = (callback, deps) => {
167
211
  mergeRefs,
168
212
  omitChildren,
169
213
  pickChildren,
214
+ useAsync,
215
+ useAsyncFunc,
216
+ useAsyncRetry,
170
217
  useCallbackRef,
171
218
  useIsMounted,
172
219
  useMergeRefs,
package/dist/react.mjs CHANGED
@@ -10,13 +10,16 @@ import {
10
10
  mergeRefs,
11
11
  omitChildren,
12
12
  pickChildren,
13
+ useAsync,
14
+ useAsyncFunc,
15
+ useAsyncRetry,
13
16
  useCallbackRef,
14
17
  useIsMounted,
15
18
  useMergeRefs,
16
19
  useSafeLayoutEffect,
17
20
  useUnmountEffect,
18
21
  useUpdateEffect
19
- } from "./chunk-HUEOJZVC.mjs";
22
+ } from "./chunk-UUOSA3BW.mjs";
20
23
  import "./chunk-SLJ4M7XC.mjs";
21
24
  import "./chunk-VYMGBE25.mjs";
22
25
  import "./chunk-BZAW2D6U.mjs";
@@ -37,6 +40,9 @@ export {
37
40
  mergeRefs,
38
41
  omitChildren,
39
42
  pickChildren,
43
+ useAsync,
44
+ useAsyncFunc,
45
+ useAsyncRetry,
40
46
  useCallbackRef,
41
47
  useIsMounted,
42
48
  useMergeRefs,
@@ -0,0 +1,3 @@
1
+ declare const escape: (value: string, replaceValue?: string) => string;
2
+
3
+ export { escape };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yamada-ui/utils",
3
- "version": "0.1.1",
3
+ "version": "0.1.3",
4
4
  "description": "Yamada UI utils",
5
5
  "keywords": [
6
6
  "utils",