@himanshu-sorathiya/react-kit 1.0.18 → 1.0.20

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.
package/dist/index.d.ts CHANGED
@@ -3,6 +3,184 @@
3
3
  import React$1 from 'react';
4
4
  import { CSSProperties, RefObject } from 'react';
5
5
 
6
+ export type DateInput = Date | number | string;
7
+ export type WeekStartsOn = 0 | 1 | 2 | 3 | 4 | 5 | 6;
8
+ export type DateInterval = {
9
+ start: Date;
10
+ end: Date;
11
+ };
12
+ export type DateRange = {
13
+ start: Date;
14
+ end: Date;
15
+ };
16
+ export type Duration = {
17
+ years?: number;
18
+ months?: number;
19
+ weeks?: number;
20
+ days?: number;
21
+ hours?: number;
22
+ minutes?: number;
23
+ seconds?: number;
24
+ };
25
+ export declare function toDate(input: DateInput): Date;
26
+ export declare function isDate(value: unknown): value is Date;
27
+ export declare function isValid(date: Date): boolean;
28
+ export declare function createDate(year: number, month: number, day?: number): Date;
29
+ export declare function getYear(date: DateInput): number;
30
+ export declare function getMonth(date: DateInput): number;
31
+ export declare function getDate(date: DateInput): number;
32
+ export declare function getHours(date: DateInput): number;
33
+ export declare function getMinutes(date: DateInput): number;
34
+ export declare function getSeconds(date: DateInput): number;
35
+ export declare function getMilliseconds(date: DateInput): number;
36
+ export declare function getTimestamp(date: DateInput): number;
37
+ export declare function getDayOfWeek(date: DateInput): number;
38
+ export declare function getDaysInMonth(date: DateInput): number;
39
+ export declare function getQuarter(date: DateInput): number;
40
+ export declare function getDayOfYear(date: DateInput): number;
41
+ declare const TOKEN_FORMATTERS: {
42
+ yyyy: (d: Date) => string;
43
+ yy: (d: Date) => string;
44
+ MMMM: (d: Date, loc: string) => string;
45
+ MMM: (d: Date, loc: string) => string;
46
+ MM: (d: Date) => string;
47
+ M: (d: Date) => string;
48
+ dd: (d: Date) => string;
49
+ d: (d: Date) => string;
50
+ EEEE: (d: Date, loc: string) => string;
51
+ EEE: (d: Date, loc: string) => string;
52
+ E: (d: Date, loc: string) => string;
53
+ HH: (d: Date) => string;
54
+ H: (d: Date) => string;
55
+ hh: (d: Date) => string;
56
+ h: (d: Date) => string;
57
+ mm: (d: Date) => string;
58
+ ss: (d: Date) => string;
59
+ SSS: (d: Date) => string;
60
+ a: (d: Date, loc: string) => string;
61
+ Q: (d: Date) => string;
62
+ w: (d: Date) => string;
63
+ D: (d: Date) => string;
64
+ xxx: (d: Date) => string;
65
+ xx: (d: Date) => string;
66
+ x: (d: Date) => string;
67
+ };
68
+ export type FormatToken = keyof typeof TOKEN_FORMATTERS;
69
+ export declare function format(date: DateInput, formatString: string, options?: {
70
+ locale?: string;
71
+ }): string;
72
+ export declare function formatDate(date: DateInput, locale?: string, options?: Intl.DateTimeFormatOptions): string;
73
+ export declare function formatRelativeTime(date: DateInput, baseDate?: DateInput, locale?: string): string;
74
+ export declare function formatISO(date: DateInput, options?: {
75
+ format?: "extended" | "basic";
76
+ representation?: "complete" | "date" | "time";
77
+ }): string;
78
+ export declare function formatRFC3339(date: DateInput, options?: {
79
+ fractionDigits?: 0 | 1 | 2 | 3;
80
+ }): string;
81
+ export declare function formatISO9075(date: DateInput, options?: {
82
+ representation?: "complete" | "date" | "time";
83
+ }): string;
84
+ export declare function formatRFC2822(date: DateInput): string;
85
+ export declare function formatDuration(duration: Duration, options?: {
86
+ format?: ReadonlyArray<keyof Duration>;
87
+ zero?: boolean;
88
+ delimiter?: string;
89
+ locale?: string;
90
+ }): string;
91
+ export declare function toISOString(date: DateInput): string;
92
+ export declare function parseISO(isoString: string): Date;
93
+ export declare function addMilliseconds(date: DateInput, amount: number): Date;
94
+ export declare function addSeconds(date: DateInput, amount: number): Date;
95
+ export declare function addMinutes(date: DateInput, amount: number): Date;
96
+ export declare function addHours(date: DateInput, amount: number): Date;
97
+ export declare function addDays(date: DateInput, amount: number): Date;
98
+ export declare function addWeeks(date: DateInput, amount: number): Date;
99
+ export declare function addMonths(date: DateInput, amount: number): Date;
100
+ export declare function addYears(date: DateInput, amount: number): Date;
101
+ export declare function subMilliseconds(date: DateInput, amount: number): Date;
102
+ export declare function subSeconds(date: DateInput, amount: number): Date;
103
+ export declare function subMinutes(date: DateInput, amount: number): Date;
104
+ export declare function subHours(date: DateInput, amount: number): Date;
105
+ export declare function subDays(date: DateInput, amount: number): Date;
106
+ export declare function subWeeks(date: DateInput, amount: number): Date;
107
+ export declare function subMonths(date: DateInput, amount: number): Date;
108
+ export declare function subYears(date: DateInput, amount: number): Date;
109
+ export declare function setYear(date: DateInput, year: number): Date;
110
+ export declare function setMonth(date: DateInput, month: number): Date;
111
+ export declare function setDate(date: DateInput, day: number): Date;
112
+ export declare function setHours(date: DateInput, hours: number): Date;
113
+ export declare function setMinutes(date: DateInput, minutes: number): Date;
114
+ export declare function setSeconds(date: DateInput, seconds: number): Date;
115
+ export declare function setMilliseconds(date: DateInput, ms: number): Date;
116
+ export declare function startOfDay(date: DateInput): Date;
117
+ export declare function endOfDay(date: DateInput): Date;
118
+ export declare function startOfHour(date: DateInput): Date;
119
+ export declare function endOfHour(date: DateInput): Date;
120
+ export declare function startOfMinute(date: DateInput): Date;
121
+ export declare function endOfMinute(date: DateInput): Date;
122
+ export declare function startOfWeek(date: DateInput, weekStartsOn?: WeekStartsOn): Date;
123
+ export declare function endOfWeek(date: DateInput, weekStartsOn?: WeekStartsOn): Date;
124
+ export declare function startOfMonth(date: DateInput): Date;
125
+ export declare function endOfMonth(date: DateInput): Date;
126
+ export declare function startOfQuarter(date: DateInput): Date;
127
+ export declare function endOfQuarter(date: DateInput): Date;
128
+ export declare function startOfYear(date: DateInput): Date;
129
+ export declare function endOfYear(date: DateInput): Date;
130
+ export declare function differenceInMilliseconds(dateLeft: DateInput, dateRight: DateInput): number;
131
+ export declare function differenceInSeconds(dateLeft: DateInput, dateRight: DateInput): number;
132
+ export declare function differenceInMinutes(dateLeft: DateInput, dateRight: DateInput): number;
133
+ export declare function differenceInHours(dateLeft: DateInput, dateRight: DateInput): number;
134
+ export declare function differenceInDays(dateLeft: DateInput, dateRight: DateInput): number;
135
+ export declare function differenceInWeeks(dateLeft: DateInput, dateRight: DateInput): number;
136
+ export declare function differenceInMonths(dateLeft: DateInput, dateRight: DateInput): number;
137
+ export declare function differenceInYears(dateLeft: DateInput, dateRight: DateInput): number;
138
+ export declare function addBusinessDays(date: DateInput, amount: number): Date;
139
+ export declare function differenceInBusinessDays(dateLeft: DateInput, dateRight: DateInput): number;
140
+ export declare function clampDate(date: DateInput, min: DateInput, max: DateInput): Date;
141
+ export declare function min(dates: Date[]): Date | null;
142
+ export declare function max(dates: Date[]): Date | null;
143
+ export declare function closestTo(date: DateInput, datesArray: Date[]): Date | null;
144
+ export declare function eachDayOfInterval(interval: DateInterval): Date[];
145
+ export declare function eachMonthOfInterval(interval: DateInterval): Date[];
146
+ export declare function eachYearOfInterval(interval: DateInterval): Date[];
147
+ export declare function fromUnixTime(seconds: number): Date;
148
+ export declare function toUnixTime(date: DateInput): number;
149
+ export declare function roundToNearestMinutes(date: DateInput, step: number): Date;
150
+ export declare function getISOWeek(date: DateInput): number;
151
+ export declare function setISOWeek(date: DateInput, week: number): Date;
152
+ export declare function intervalToDuration(interval: DateInterval): Duration;
153
+ export declare function durationToMilliseconds(duration: Duration): number;
154
+ export declare function isSameDay(dateA: DateInput, dateB: DateInput): boolean;
155
+ export declare function isSameMonth(dateA: DateInput, dateB: DateInput): boolean;
156
+ export declare function isSameYear(dateA: DateInput, dateB: DateInput): boolean;
157
+ export declare function isSameQuarter(dateA: DateInput, dateB: DateInput): boolean;
158
+ export declare function isSameWeek(dateA: DateInput, dateB: DateInput, weekStartsOn?: WeekStartsOn): boolean;
159
+ export declare function isSameHour(dateA: DateInput, dateB: DateInput): boolean;
160
+ export declare function isSameMinute(dateA: DateInput, dateB: DateInput): boolean;
161
+ export declare function isSameSecond(dateA: DateInput, dateB: DateInput): boolean;
162
+ export declare function isSameTime(dateA: DateInput, dateB: DateInput): boolean;
163
+ export declare function isBefore(date: DateInput, compareDate: DateInput): boolean;
164
+ export declare function isAfter(date: DateInput, compareDate: DateInput): boolean;
165
+ export declare function isEqual(dateA: DateInput, dateB: DateInput): boolean;
166
+ export declare function isWithinRange(date: DateInput, start: DateInput, end: DateInput): boolean;
167
+ export declare function isOverlapping(rangeA: DateRange, rangeB: DateRange): boolean;
168
+ export declare function isToday(date: DateInput): boolean;
169
+ export declare function isYesterday(date: DateInput): boolean;
170
+ export declare function isTomorrow(date: DateInput): boolean;
171
+ export declare function isPast(date: DateInput): boolean;
172
+ export declare function isFuture(date: DateInput): boolean;
173
+ export declare function isThisWeek(date: DateInput, weekStartsOn?: WeekStartsOn): boolean;
174
+ export declare function isThisMonth(date: DateInput): boolean;
175
+ export declare function isThisYear(date: DateInput): boolean;
176
+ export declare function isFirstDayOfMonth(date: DateInput): boolean;
177
+ export declare function isLastDayOfMonth(date: DateInput): boolean;
178
+ export declare function isWeekend(date: DateInput): boolean;
179
+ export declare function isWeekday(date: DateInput): boolean;
180
+ export declare function isAM(date: DateInput): boolean;
181
+ export declare function isPM(date: DateInput): boolean;
182
+ export declare function isLeapYear(yearOrDate: number | Date): boolean;
183
+ export declare function isInLeapYear(date: DateInput): boolean;
6
184
  declare const validKeyEventTypes: readonly [
7
185
  "keydown",
8
186
  "keyup",
@@ -34,26 +212,80 @@ export interface DebounceOptions {
34
212
  leading?: boolean;
35
213
  trailing?: boolean;
36
214
  }
37
- export interface UseDebounceReturn<Args extends any[]> {
215
+ export interface UseDebounceReturn {
216
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
217
+ cancel: () => void;
218
+ flush: () => void;
219
+ isPending: boolean;
220
+ }
221
+ export declare function useDebounce(delay: number, options?: DebounceOptions): UseDebounceReturn;
222
+ export interface UseDebouncedCallbackReturn<Args extends unknown[]> {
38
223
  debouncedFunc: (...args: Args) => void;
39
224
  cancel: () => void;
40
225
  flush: () => void;
41
226
  isPending: boolean;
42
227
  }
43
- export declare function useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
228
+ export declare function useDebouncedCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebouncedCallbackReturn<Args>;
229
+ export type UseDebouncedStateReturn<T> = [
230
+ T,
231
+ (value: T) => void,
232
+ {
233
+ isPending: boolean;
234
+ cancel: () => void;
235
+ flush: () => void;
236
+ forceSetValue: (value: T) => void;
237
+ }
238
+ ];
239
+ export declare function useDebouncedState<T>(initialValue: T | (() => T), delay: number, options?: DebounceOptions): UseDebouncedStateReturn<T>;
44
240
  export type UseDebouncedValueReturn<T> = T;
45
241
  export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebounceOptions): UseDebouncedValueReturn<T>;
242
+ export interface RateLimitOptions {
243
+ onRateLimitReached?: () => void;
244
+ refillStrategy?: "burst" | "gradual";
245
+ }
246
+ export interface UseRateLimitReturn {
247
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
248
+ remaining: number;
249
+ isRateLimited: boolean;
250
+ }
251
+ export declare function useRateLimit(limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitReturn;
252
+ export interface UseRateLimitedCallbackReturn<Args extends unknown[]> {
253
+ rateLimitedFunc: (...args: Args) => void;
254
+ remaining: number;
255
+ isRateLimited: boolean;
256
+ }
257
+ export declare function useRateLimitedCallback<Args extends unknown[]>(func: (...args: Args) => void, limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitedCallbackReturn<Args>;
46
258
  export interface ThrottleOptions {
47
259
  leading?: boolean;
48
260
  trailing?: boolean;
49
261
  }
50
- export interface UseThrottleReturn<Args extends any[]> {
262
+ export interface UseThrottleReturn {
263
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
264
+ cancel: () => void;
265
+ flush: () => void;
266
+ isPending: boolean;
267
+ }
268
+ export declare function useThrottle(delay: number, options?: ThrottleOptions): UseThrottleReturn;
269
+ export interface UseThrottledCallbackReturn<Args extends unknown[]> {
51
270
  throttledFunc: (...args: Args) => void;
52
271
  cancel: () => void;
53
272
  flush: () => void;
54
273
  isPending: boolean;
55
274
  }
56
- export declare function useThrottle<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottleReturn<Args>;
275
+ export declare function useThrottledCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottledCallbackReturn<Args>;
276
+ export type UseThrottledStateReturn<T> = [
277
+ T,
278
+ (value: T) => void,
279
+ {
280
+ isPending: boolean;
281
+ cancel: () => void;
282
+ flush: () => void;
283
+ forceSetValue: (value: T) => void;
284
+ }
285
+ ];
286
+ export declare function useThrottledState<T>(initialValue: T | (() => T), delay: number, options?: ThrottleOptions): UseThrottledStateReturn<T>;
287
+ export type UseThrottledValueReturn<T> = T;
288
+ export declare function useThrottledValue<T>(value: T, delay: number, options?: ThrottleOptions): UseThrottledValueReturn<T>;
57
289
  export type FilterType = "text" | "number" | "boolean" | "date" | "select" | "multiselect" | "custom";
58
290
  export type TextOperator = "contains" | "equals" | "startsWith" | "endsWith" | "notContains";
59
291
  export type NumberOperator = "equals" | "greaterThan" | "lessThan" | "greaterThanOrEqual" | "lessThanOrEqual" | "between";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- import { t as e } from "./events2.js";
2
- import { n as t, r as n, t as r } from "./performance2.js";
3
- import { a as i, c as a, i as o, n as s, o as c, r as l, s as u, t as d } from "./state2.js";
4
- import { a as f, c as p, i as m, n as h, o as g, r as _, s as v, t as y } from "./ui2.js";
5
- export { p as FuzzyHighlighter, m as ModalLayout, n as useDebounce, t as useDebouncedValue, _ as useExpansion, a as useFilter, u as useFuzzySearch, c as useGrouping, e as useKey, f as useModal, g as useModalActions, v as useModalState, i as useMultipleSelection, o as useOrder, l as usePagination, h as usePin, s as useSingleSelection, d as useSort, r as useThrottle, y as useVisibility };
1
+ import { addBusinessDays as e, addDays as t, addHours as n, addMilliseconds as r, addMinutes as i, addMonths as a, addSeconds as o, addWeeks as s, addYears as c, clampDate as l, closestTo as u, createDate as d, differenceInBusinessDays as f, differenceInDays as p, differenceInHours as m, differenceInMilliseconds as h, differenceInMinutes as g, differenceInMonths as _, differenceInSeconds as v, differenceInWeeks as y, differenceInYears as b, durationToMilliseconds as x, eachDayOfInterval as S, eachMonthOfInterval as C, eachYearOfInterval as w, endOfDay as T, endOfHour as E, endOfMinute as D, endOfMonth as O, endOfQuarter as k, endOfWeek as A, endOfYear as j, format as M, formatDate as N, formatDuration as P, formatISO as F, formatISO9075 as I, formatRFC2822 as L, formatRFC3339 as R, formatRelativeTime as z, fromUnixTime as B, getDate as V, getDayOfWeek as H, getDayOfYear as U, getDaysInMonth as W, getHours as G, getISOWeek as K, getMilliseconds as q, getMinutes as J, getMonth as Y, getQuarter as X, getSeconds as Z, getTimestamp as Q, getYear as $, intervalToDuration as ee, isAM as te, isAfter as ne, isBefore as re, isDate as ie, isEqual as ae, isFirstDayOfMonth as oe, isFuture as se, isInLeapYear as ce, isLastDayOfMonth as le, isLeapYear as ue, isOverlapping as de, isPM as fe, isPast as pe, isSameDay as me, isSameHour as he, isSameMinute as ge, isSameMonth as _e, isSameQuarter as ve, isSameSecond as ye, isSameTime as be, isSameWeek as xe, isSameYear as Se, isThisMonth as Ce, isThisWeek as we, isThisYear as Te, isToday as Ee, isTomorrow as De, isValid as Oe, isWeekday as ke, isWeekend as Ae, isWithinRange as je, isYesterday as Me, max as Ne, min as Pe, parseISO as Fe, roundToNearestMinutes as Ie, setDate as Le, setHours as Re, setISOWeek as ze, setMilliseconds as Be, setMinutes as Ve, setMonth as He, setSeconds as Ue, setYear as We, startOfDay as Ge, startOfHour as Ke, startOfMinute as qe, startOfMonth as Je, startOfQuarter as Ye, startOfWeek as Xe, startOfYear as Ze, subDays as Qe, subHours as $e, subMilliseconds as et, subMinutes as tt, subMonths as nt, subSeconds as rt, subWeeks as it, subYears as at, toDate as ot, toISOString as st, toUnixTime as ct } from "./datetime.js";
2
+ import { t as lt } from "./events2.js";
3
+ import { a as ut, c as dt, i as ft, l as pt, n as mt, o as ht, r as gt, s as _t, t as vt, u as yt } from "./performance2.js";
4
+ import { a as bt, c as xt, i as St, n as Ct, o as wt, r as Tt, s as Et, t as Dt } from "./state2.js";
5
+ import { a as Ot, c as kt, i as At, n as jt, o as Mt, r as Nt, s as Pt, t as Ft } from "./ui2.js";
6
+ export { kt as FuzzyHighlighter, At as ModalLayout, e as addBusinessDays, t as addDays, n as addHours, r as addMilliseconds, i as addMinutes, a as addMonths, o as addSeconds, s as addWeeks, c as addYears, l as clampDate, u as closestTo, d as createDate, f as differenceInBusinessDays, p as differenceInDays, m as differenceInHours, h as differenceInMilliseconds, g as differenceInMinutes, _ as differenceInMonths, v as differenceInSeconds, y as differenceInWeeks, b as differenceInYears, x as durationToMilliseconds, S as eachDayOfInterval, C as eachMonthOfInterval, w as eachYearOfInterval, T as endOfDay, E as endOfHour, D as endOfMinute, O as endOfMonth, k as endOfQuarter, A as endOfWeek, j as endOfYear, M as format, N as formatDate, P as formatDuration, F as formatISO, I as formatISO9075, L as formatRFC2822, R as formatRFC3339, z as formatRelativeTime, B as fromUnixTime, V as getDate, H as getDayOfWeek, U as getDayOfYear, W as getDaysInMonth, G as getHours, K as getISOWeek, q as getMilliseconds, J as getMinutes, Y as getMonth, X as getQuarter, Z as getSeconds, Q as getTimestamp, $ as getYear, ee as intervalToDuration, te as isAM, ne as isAfter, re as isBefore, ie as isDate, ae as isEqual, oe as isFirstDayOfMonth, se as isFuture, ce as isInLeapYear, le as isLastDayOfMonth, ue as isLeapYear, de as isOverlapping, fe as isPM, pe as isPast, me as isSameDay, he as isSameHour, ge as isSameMinute, _e as isSameMonth, ve as isSameQuarter, ye as isSameSecond, be as isSameTime, xe as isSameWeek, Se as isSameYear, Ce as isThisMonth, we as isThisWeek, Te as isThisYear, Ee as isToday, De as isTomorrow, Oe as isValid, ke as isWeekday, Ae as isWeekend, je as isWithinRange, Me as isYesterday, Ne as max, Pe as min, Fe as parseISO, Ie as roundToNearestMinutes, Le as setDate, Re as setHours, ze as setISOWeek, Be as setMilliseconds, Ve as setMinutes, He as setMonth, Ue as setSeconds, We as setYear, Ge as startOfDay, Ke as startOfHour, qe as startOfMinute, Je as startOfMonth, Ye as startOfQuarter, Xe as startOfWeek, Ze as startOfYear, Qe as subDays, $e as subHours, et as subMilliseconds, tt as subMinutes, nt as subMonths, rt as subSeconds, it as subWeeks, at as subYears, ot as toDate, st as toISOString, ct as toUnixTime, yt as useDebounce, pt as useDebouncedCallback, dt as useDebouncedState, _t as useDebouncedValue, Nt as useExpansion, xt as useFilter, Et as useFuzzySearch, wt as useGrouping, lt as useKey, Ot as useModal, Mt as useModalActions, Pt as useModalState, bt as useMultipleSelection, St as useOrder, Tt as usePagination, jt as usePin, ht as useRateLimit, ut as useRateLimitedCallback, Ct as useSingleSelection, Dt as useSort, ft as useThrottle, gt as useThrottledCallback, mt as useThrottledState, vt as useThrottledValue, Ft as useVisibility };
@@ -5,25 +5,79 @@ export interface DebounceOptions {
5
5
  leading?: boolean;
6
6
  trailing?: boolean;
7
7
  }
8
- export interface UseDebounceReturn<Args extends any[]> {
8
+ export interface UseDebounceReturn {
9
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
10
+ cancel: () => void;
11
+ flush: () => void;
12
+ isPending: boolean;
13
+ }
14
+ export declare function useDebounce(delay: number, options?: DebounceOptions): UseDebounceReturn;
15
+ export interface UseDebouncedCallbackReturn<Args extends unknown[]> {
9
16
  debouncedFunc: (...args: Args) => void;
10
17
  cancel: () => void;
11
18
  flush: () => void;
12
19
  isPending: boolean;
13
20
  }
14
- export declare function useDebounce<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebounceReturn<Args>;
21
+ export declare function useDebouncedCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: DebounceOptions): UseDebouncedCallbackReturn<Args>;
22
+ export type UseDebouncedStateReturn<T> = [
23
+ T,
24
+ (value: T) => void,
25
+ {
26
+ isPending: boolean;
27
+ cancel: () => void;
28
+ flush: () => void;
29
+ forceSetValue: (value: T) => void;
30
+ }
31
+ ];
32
+ export declare function useDebouncedState<T>(initialValue: T | (() => T), delay: number, options?: DebounceOptions): UseDebouncedStateReturn<T>;
15
33
  export type UseDebouncedValueReturn<T> = T;
16
34
  export declare function useDebouncedValue<T>(value: T, delay: number, options?: DebounceOptions): UseDebouncedValueReturn<T>;
35
+ export interface RateLimitOptions {
36
+ onRateLimitReached?: () => void;
37
+ refillStrategy?: "burst" | "gradual";
38
+ }
39
+ export interface UseRateLimitReturn {
40
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
41
+ remaining: number;
42
+ isRateLimited: boolean;
43
+ }
44
+ export declare function useRateLimit(limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitReturn;
45
+ export interface UseRateLimitedCallbackReturn<Args extends unknown[]> {
46
+ rateLimitedFunc: (...args: Args) => void;
47
+ remaining: number;
48
+ isRateLimited: boolean;
49
+ }
50
+ export declare function useRateLimitedCallback<Args extends unknown[]>(func: (...args: Args) => void, limit: number, windowMs: number, options?: RateLimitOptions): UseRateLimitedCallbackReturn<Args>;
17
51
  export interface ThrottleOptions {
18
52
  leading?: boolean;
19
53
  trailing?: boolean;
20
54
  }
21
- export interface UseThrottleReturn<Args extends any[]> {
55
+ export interface UseThrottleReturn {
56
+ run: <Args extends unknown[]>(func: (...args: Args) => void, ...args: Args) => void;
57
+ cancel: () => void;
58
+ flush: () => void;
59
+ isPending: boolean;
60
+ }
61
+ export declare function useThrottle(delay: number, options?: ThrottleOptions): UseThrottleReturn;
62
+ export interface UseThrottledCallbackReturn<Args extends unknown[]> {
22
63
  throttledFunc: (...args: Args) => void;
23
64
  cancel: () => void;
24
65
  flush: () => void;
25
66
  isPending: boolean;
26
67
  }
27
- export declare function useThrottle<Args extends any[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottleReturn<Args>;
68
+ export declare function useThrottledCallback<Args extends unknown[]>(func: (...args: Args) => void, delay: number, options?: ThrottleOptions): UseThrottledCallbackReturn<Args>;
69
+ export type UseThrottledStateReturn<T> = [
70
+ T,
71
+ (value: T) => void,
72
+ {
73
+ isPending: boolean;
74
+ cancel: () => void;
75
+ flush: () => void;
76
+ forceSetValue: (value: T) => void;
77
+ }
78
+ ];
79
+ export declare function useThrottledState<T>(initialValue: T | (() => T), delay: number, options?: ThrottleOptions): UseThrottledStateReturn<T>;
80
+ export type UseThrottledValueReturn<T> = T;
81
+ export declare function useThrottledValue<T>(value: T, delay: number, options?: ThrottleOptions): UseThrottledValueReturn<T>;
28
82
 
29
83
  export {};
@@ -1,2 +1,2 @@
1
- import { n as e, r as t, t as n } from "./performance2.js";
2
- export { t as useDebounce, e as useDebouncedValue, n as useThrottle };
1
+ import { a as e, c as t, i as n, l as r, n as i, o as a, r as o, s, t as c, u as l } from "./performance2.js";
2
+ export { l as useDebounce, r as useDebouncedCallback, t as useDebouncedState, s as useDebouncedValue, a as useRateLimit, e as useRateLimitedCallback, n as useThrottle, o as useThrottledCallback, i as useThrottledState, c as useThrottledValue };
@@ -1,77 +1,198 @@
1
1
  import { useCallback as e, useEffect as t, useRef as n, useState as r } from "react";
2
2
  //#region src/performance/useDebounce/useDebounce.ts
3
- function i(i, a, o = {}) {
4
- let [s, c] = r(!1), l = o.maxWait, u = !!o.leading, d = !u && o.trailing === !1 ? !0 : o.trailing ?? !u, f = n(i), p = n(null), m = n(-1), h = n(-1), g = n(null);
5
- t(() => {
6
- f.current = i;
7
- }, [i]);
8
- let _ = e(() => {
9
- p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, g.current = null, c(!1);
10
- }, []), v = e(() => {
11
- p.current && g.current && (clearTimeout(p.current), p.current = null, f.current(...g.current), m.current = Date.now(), h.current = -1, g.current = null, c(!1));
3
+ function i(i, a = {}) {
4
+ let [o, s] = r(!1), c = a.maxWait, l = !!a.leading, u = !l && a.trailing === !1 ? !0 : a.trailing ?? !l, d = n(null), f = n(null), p = n(null), m = n(-1), h = n(-1), g = e(() => {
5
+ p.current &&= (clearTimeout(p.current), null), m.current = -1, h.current = -1, f.current = null, d.current = null, s(!1);
6
+ }, []), _ = e(() => {
7
+ p.current && d.current && f.current && (clearTimeout(p.current), p.current = null, d.current(...f.current), m.current = Date.now(), h.current = -1, f.current = null, d.current = null, s(!1));
12
8
  }, []);
13
9
  return t(() => () => {
14
- _();
15
- }, [_]), {
16
- debouncedFunc: e((...e) => {
17
- c(!0), h.current = Date.now(), g.current = e, m.current === -1 && (m.current = Date.now(), u === !0 && f.current(...e)), l !== void 0 && Date.now() - m.current >= l ? (p.current && clearTimeout(p.current), f.current(...e), m.current = -1, c(!1)) : (p.current && clearTimeout(p.current), p.current = setTimeout(() => {
18
- h.current !== m.current && d !== !1 && f.current(...e), m.current = -1, c(!1);
19
- }, a));
10
+ g();
11
+ }, [g]), {
12
+ run: e((e, ...t) => {
13
+ s(!0), h.current = Date.now(), f.current = t, d.current = e, m.current === -1 && (m.current = Date.now(), l === !0 && d.current(...t)), c !== void 0 && Date.now() - m.current >= c ? (p.current && clearTimeout(p.current), d.current && d.current(...t), m.current = -1, s(!1)) : (p.current && clearTimeout(p.current), p.current = setTimeout(() => {
14
+ h.current !== m.current && u !== !1 && d.current && f.current && d.current(...f.current), m.current = -1, s(!1);
15
+ }, i));
20
16
  }, [
21
- a,
22
- l,
23
- d,
24
- u
17
+ i,
18
+ c,
19
+ u,
20
+ l
25
21
  ]),
26
- cancel: _,
27
- flush: v,
28
- isPending: s
22
+ cancel: g,
23
+ flush: _,
24
+ isPending: o
25
+ };
26
+ }
27
+ //#endregion
28
+ //#region src/performance/useDebounce/useDebouncedCallback.ts
29
+ function a(r, a, o = {}) {
30
+ let s = n(r);
31
+ t(() => {
32
+ s.current = r;
33
+ }, [r]);
34
+ let { run: c, cancel: l, flush: u, isPending: d } = i(a, o);
35
+ return {
36
+ debouncedFunc: e((...e) => {
37
+ c(s.current, ...e);
38
+ }, [c]),
39
+ cancel: l,
40
+ flush: u,
41
+ isPending: d
29
42
  };
30
43
  }
31
44
  //#endregion
45
+ //#region src/performance/useDebounce/useDebouncedState.ts
46
+ function o(e, t, n = {}) {
47
+ let [i, o] = r(e), { debouncedFunc: s, cancel: c, flush: l, isPending: u } = a((e) => {
48
+ o(e);
49
+ }, t, n);
50
+ return [
51
+ i,
52
+ s,
53
+ {
54
+ isPending: u,
55
+ cancel: c,
56
+ flush: l,
57
+ forceSetValue: o
58
+ }
59
+ ];
60
+ }
61
+ //#endregion
32
62
  //#region src/performance/useDebounce/useDebouncedValue.ts
33
- function a(e, n, a = {}) {
34
- let [o, s] = r(e), { debouncedFunc: c } = i((e) => {
63
+ function s(e, n, i = {}) {
64
+ let [o, s] = r(e), { debouncedFunc: c } = a((e) => {
35
65
  s(e);
36
- }, n, a);
66
+ }, n, i);
37
67
  return t(() => {
38
68
  c(e);
39
69
  }, [e, c]), o;
40
70
  }
41
71
  //#endregion
42
- //#region src/performance/useThrottle/useThrottle.ts
43
- function o(i, a, o = {}) {
44
- let [s, c] = r(!1), l = o.leading ?? !0, u = o.trailing ?? !0, d = n(i), f = n(null), p = n(-1), m = n(null);
72
+ //#region src/performance/useRateLimit/useRateLimit.ts
73
+ function c(i, a, o) {
74
+ let [s, c] = r(i), l = o?.refillStrategy ?? "burst", u = n(o?.onRateLimitReached);
45
75
  t(() => {
46
- d.current = i;
47
- }, [i]);
48
- let h = e(() => {
49
- f.current &&= (clearTimeout(f.current), null), p.current = -1, m.current = null, c(!1);
50
- }, []), g = e(() => {
51
- m.current && (f.current &&= (clearTimeout(f.current), null), d.current(...m.current), p.current = l ? Date.now() : -1, m.current = null, c(!1));
52
- }, [l]);
76
+ u.current = o?.onRateLimitReached;
77
+ }, [o?.onRateLimitReached]);
78
+ let d = n(i), [f] = r(() => Date.now()), p = n(f), m = n(null);
53
79
  return t(() => () => {
54
- h();
55
- }, [h]), {
56
- throttledFunc: e((...e) => {
57
- c(!0);
58
- let t = Date.now();
59
- if (m.current = e, p.current === -1 ? l : t - p.current >= a) f.current &&= (clearTimeout(f.current), null), d.current(...e), p.current = t, m.current = null, c(!1);
80
+ m.current && clearTimeout(m.current);
81
+ }, []), {
82
+ run: e((e, ...t) => {
83
+ let n = Date.now(), r = n - p.current;
84
+ if (l === "burst") r >= a && (d.current = i, p.current = n);
85
+ else {
86
+ let e = a / i, t = Math.floor(r / e);
87
+ t > 0 && (d.current = Math.min(i, d.current + t), p.current += t * e);
88
+ }
89
+ if (d.current > 0 ? (--d.current, c(d.current), e(...t)) : u.current && u.current(), m.current && clearTimeout(m.current), d.current < i) {
90
+ let e = a / i, t = l === "burst" ? a - (Date.now() - p.current) : e - (Date.now() - p.current);
91
+ m.current = setTimeout(() => {
92
+ c((e) => Math.min(i, e + 1)), p.current = Date.now(), d.current = Math.min(i, d.current + 1);
93
+ }, Math.max(0, t));
94
+ }
95
+ }, [
96
+ i,
97
+ a,
98
+ l
99
+ ]),
100
+ remaining: s,
101
+ isRateLimited: s === 0
102
+ };
103
+ }
104
+ //#endregion
105
+ //#region src/performance/useRateLimit/useRateLimitedCallback.ts
106
+ function l(r, i, a, o) {
107
+ let s = n(r);
108
+ t(() => {
109
+ s.current = r;
110
+ }, [r]);
111
+ let { run: l, remaining: u, isRateLimited: d } = c(i, a, o);
112
+ return {
113
+ rateLimitedFunc: e((...e) => {
114
+ l(s.current, ...e);
115
+ }, [l]),
116
+ remaining: u,
117
+ isRateLimited: d
118
+ };
119
+ }
120
+ //#endregion
121
+ //#region src/performance/useThrottle/useThrottle.ts
122
+ function u(i, a = {}) {
123
+ let [o, s] = r(!1), c = a.leading ?? !0, l = a.trailing ?? !0;
124
+ c === !1 && l === !1 && (c = !0, l = !0);
125
+ let u = n(null), d = n(null), f = n(null), p = n(-1), m = e(() => {
126
+ f.current &&= (clearTimeout(f.current), null), p.current = -1, d.current = null, u.current = null, s(!1);
127
+ }, []), h = e(() => {
128
+ d.current && u.current && (f.current &&= (clearTimeout(f.current), null), u.current(...d.current), p.current = c ? Date.now() : -1, d.current = null, u.current = null, s(!1));
129
+ }, [c]);
130
+ return t(() => () => {
131
+ m();
132
+ }, [m]), {
133
+ run: e((e, ...t) => {
134
+ s(!0);
135
+ let n = Date.now();
136
+ if (d.current = t, u.current = e, p.current === -1 ? c : n - p.current >= i) f.current &&= (clearTimeout(f.current), null), u.current(...t), p.current = n, d.current = null, s(!1);
60
137
  else if (!f.current) {
61
- let e = a - (p.current === -1 ? 0 : t - p.current);
138
+ let e = i - (p.current === -1 ? 0 : n - p.current);
62
139
  f.current = setTimeout(() => {
63
- u && m.current ? (d.current(...m.current), p.current = l ? Date.now() : -1) : p.current = -1, m.current = null, f.current = null, c(!1);
140
+ l && d.current && u.current ? (u.current(...d.current), p.current = c ? Date.now() : -1) : p.current = -1, d.current = null, u.current = null, f.current = null, s(!1);
64
141
  }, e);
65
142
  }
66
143
  }, [
67
- a,
68
- l,
69
- u
144
+ i,
145
+ c,
146
+ l
70
147
  ]),
71
- cancel: h,
72
- flush: g,
73
- isPending: s
148
+ cancel: m,
149
+ flush: h,
150
+ isPending: o
74
151
  };
75
152
  }
76
153
  //#endregion
77
- export { a as n, i as r, o as t };
154
+ //#region src/performance/useThrottle/useThrottledCallback.ts
155
+ function d(r, i, a = {}) {
156
+ let o = n(r);
157
+ t(() => {
158
+ o.current = r;
159
+ }, [r]);
160
+ let { run: s, cancel: c, flush: l, isPending: d } = u(i, a);
161
+ return {
162
+ throttledFunc: e((...e) => {
163
+ s(o.current, ...e);
164
+ }, [s]),
165
+ cancel: c,
166
+ flush: l,
167
+ isPending: d
168
+ };
169
+ }
170
+ //#endregion
171
+ //#region src/performance/useThrottle/useThrottledState.ts
172
+ function f(e, t, n = {}) {
173
+ let [i, a] = r(e), { throttledFunc: o, isPending: s, cancel: c, flush: l } = d((e) => {
174
+ a(e);
175
+ }, t, n);
176
+ return [
177
+ i,
178
+ o,
179
+ {
180
+ isPending: s,
181
+ cancel: c,
182
+ flush: l,
183
+ forceSetValue: a
184
+ }
185
+ ];
186
+ }
187
+ //#endregion
188
+ //#region src/performance/useThrottle/useThrottledValue.ts
189
+ function p(e, n, i = {}) {
190
+ let [a, o] = r(e), { throttledFunc: s } = d((e) => {
191
+ o(e);
192
+ }, n, i);
193
+ return t(() => {
194
+ s(e);
195
+ }, [e, s]), a;
196
+ }
197
+ //#endregion
198
+ export { l as a, o as c, u as i, a as l, f as n, c as o, d as r, s, p as t, i as u };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@himanshu-sorathiya/react-kit",
3
- "version": "1.0.18",
3
+ "version": "1.0.20",
4
4
  "description": "An opinionated collection of react hooks, and reusable UI components.",
5
5
  "type": "module",
6
6
  "module": "./dist/index.js",
@@ -29,11 +29,15 @@
29
29
  "types": "./dist/events.d.ts",
30
30
  "import": "./dist/events.js"
31
31
  },
32
+ "./datetime": {
33
+ "types": "./dist/datetime.d.ts",
34
+ "import": "./dist/datetime.js"
35
+ },
32
36
  "./styles.css": "./dist/index.css"
33
37
  },
34
38
  "scripts": {
35
39
  "dev": "vite",
36
- "build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts",
40
+ "build": "vite build && dts-bundle-generator --project tsconfig.app.json -o dist/index.d.ts src/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/ui.d.ts src/ui/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/state.d.ts src/state/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/performance.d.ts src/performance/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/events.d.ts src/events/index.ts && dts-bundle-generator --project tsconfig.app.json -o dist/datetime.d.ts src/datetime/index.ts",
37
41
  "lint": "eslint .",
38
42
  "preview": "vite preview",
39
43
  "format": "prettier . --write"