@elementor/utils 3.33.0-99 → 3.35.0-325

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.mts CHANGED
@@ -27,9 +27,7 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
27
27
  stack?: string;
28
28
  cause?: unknown;
29
29
  };
30
- captureStackTrace(targetObject: object, constructorOpt?: Function): void;
31
- prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
32
- stackTraceLimit: number;
30
+ isError(error: unknown): error is Error;
33
31
  };
34
32
 
35
33
  declare const ensureError: (error: unknown) => Error;
@@ -53,21 +51,28 @@ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wai
53
51
  pending: () => boolean;
54
52
  };
55
53
 
54
+ declare function throttle<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number, shouldExecuteIgnoredCalls?: boolean): {
55
+ (...args: TArgs): void;
56
+ flush: (...args: TArgs) => void;
57
+ cancel: () => void;
58
+ pending: () => boolean;
59
+ };
60
+
56
61
  declare const encodeString: (value: string) => string;
57
- declare const decodeString: (value: string, fallback?: string) => string;
62
+ declare const decodeString: <T = string>(value: string, fallback?: T) => string | T;
58
63
 
59
64
  type UnknownObject = Record<string, unknown>;
60
65
  declare function hash(obj: UnknownObject): string;
61
66
 
62
- type MixpanelEvent = {
63
- location: string;
64
- secondaryLocation: string;
65
- trigger: string;
66
- widget_type: string;
67
- eventName: string;
68
- } & {
69
- [key: string]: unknown;
67
+ type UseSearchStateResult = UseDebounceStateResult;
68
+ declare function useSearchState({ localStorageKey }: {
69
+ localStorageKey?: string;
70
+ }): {
71
+ debouncedValue: string;
72
+ inputValue: string;
73
+ handleChange: (val: string) => void;
70
74
  };
71
- declare const sendMixpanelEvent: (event: MixpanelEvent) => void;
72
75
 
73
- export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type MixpanelEvent, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, decodeString, encodeString, ensureError, hash, sendMixpanelEvent, useDebounceState };
76
+ declare function generateUniqueId(prefix?: string): string;
77
+
78
+ export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, createError, debounce, decodeString, encodeString, ensureError, generateUniqueId, hash, throttle, useDebounceState, useSearchState };
package/dist/index.d.ts CHANGED
@@ -27,9 +27,7 @@ declare const createError: <T extends ElementorErrorOptions["context"]>({ code,
27
27
  stack?: string;
28
28
  cause?: unknown;
29
29
  };
30
- captureStackTrace(targetObject: object, constructorOpt?: Function): void;
31
- prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): any;
32
- stackTraceLimit: number;
30
+ isError(error: unknown): error is Error;
33
31
  };
34
32
 
35
33
  declare const ensureError: (error: unknown) => Error;
@@ -53,21 +51,28 @@ declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wai
53
51
  pending: () => boolean;
54
52
  };
55
53
 
54
+ declare function throttle<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number, shouldExecuteIgnoredCalls?: boolean): {
55
+ (...args: TArgs): void;
56
+ flush: (...args: TArgs) => void;
57
+ cancel: () => void;
58
+ pending: () => boolean;
59
+ };
60
+
56
61
  declare const encodeString: (value: string) => string;
57
- declare const decodeString: (value: string, fallback?: string) => string;
62
+ declare const decodeString: <T = string>(value: string, fallback?: T) => string | T;
58
63
 
59
64
  type UnknownObject = Record<string, unknown>;
60
65
  declare function hash(obj: UnknownObject): string;
61
66
 
62
- type MixpanelEvent = {
63
- location: string;
64
- secondaryLocation: string;
65
- trigger: string;
66
- widget_type: string;
67
- eventName: string;
68
- } & {
69
- [key: string]: unknown;
67
+ type UseSearchStateResult = UseDebounceStateResult;
68
+ declare function useSearchState({ localStorageKey }: {
69
+ localStorageKey?: string;
70
+ }): {
71
+ debouncedValue: string;
72
+ inputValue: string;
73
+ handleChange: (val: string) => void;
70
74
  };
71
- declare const sendMixpanelEvent: (event: MixpanelEvent) => void;
72
75
 
73
- export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type MixpanelEvent, type UseDebounceStateOptions, type UseDebounceStateResult, createError, debounce, decodeString, encodeString, ensureError, hash, sendMixpanelEvent, useDebounceState };
76
+ declare function generateUniqueId(prefix?: string): string;
77
+
78
+ export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, createError, debounce, decodeString, encodeString, ensureError, generateUniqueId, hash, throttle, useDebounceState, useSearchState };
package/dist/index.js CHANGED
@@ -26,9 +26,11 @@ __export(index_exports, {
26
26
  decodeString: () => decodeString,
27
27
  encodeString: () => encodeString,
28
28
  ensureError: () => ensureError,
29
+ generateUniqueId: () => generateUniqueId,
29
30
  hash: () => hash,
30
- sendMixpanelEvent: () => sendMixpanelEvent,
31
- useDebounceState: () => useDebounceState
31
+ throttle: () => throttle,
32
+ useDebounceState: () => useDebounceState,
33
+ useSearchState: () => useSearchState
32
34
  });
33
35
  module.exports = __toCommonJS(index_exports);
34
36
 
@@ -132,15 +134,54 @@ function useDebounceState(options = {}) {
132
134
  };
133
135
  }
134
136
 
137
+ // src/throttle.ts
138
+ function throttle(fn, wait, shouldExecuteIgnoredCalls = false) {
139
+ let timer = null;
140
+ let ignoredExecution = false;
141
+ const cancel = () => {
142
+ if (!timer) {
143
+ return;
144
+ }
145
+ clearTimeout(timer);
146
+ timer = null;
147
+ };
148
+ const flush = (...args) => {
149
+ cancel();
150
+ fn(...args);
151
+ };
152
+ const run = (...args) => {
153
+ if (timer) {
154
+ ignoredExecution = true;
155
+ return;
156
+ }
157
+ fn(...args);
158
+ timer = setTimeout(() => {
159
+ timer = null;
160
+ if (ignoredExecution && shouldExecuteIgnoredCalls) {
161
+ fn(...args);
162
+ }
163
+ ignoredExecution = false;
164
+ }, wait);
165
+ };
166
+ const pending = () => !!timer;
167
+ run.flush = flush;
168
+ run.cancel = cancel;
169
+ run.pending = pending;
170
+ return run;
171
+ }
172
+
135
173
  // src/encoding.ts
136
174
  var encodeString = (value) => {
137
- return btoa(value);
175
+ const binary = Array.from(new TextEncoder().encode(value), (b) => String.fromCharCode(b)).join("");
176
+ return btoa(binary);
138
177
  };
139
- var decodeString = (value, fallback = "") => {
178
+ var decodeString = (value, fallback) => {
140
179
  try {
141
- return atob(value);
180
+ const binary = atob(value);
181
+ const bytes = new Uint8Array(Array.from(binary, (char) => char.charCodeAt(0)));
182
+ return new TextDecoder().decode(bytes);
142
183
  } catch {
143
- return fallback;
184
+ return fallback !== void 0 ? fallback : "";
144
185
  }
145
186
  };
146
187
 
@@ -158,16 +199,34 @@ function isPlainObject(value) {
158
199
  return !!value && typeof value === "object" && !Array.isArray(value);
159
200
  }
160
201
 
161
- // src/mixpanel-tracking.ts
162
- var sendMixpanelEvent = (event) => {
163
- const extendedWindow = window;
164
- if (extendedWindow.elementorCommon?.eventsManager) {
165
- try {
166
- extendedWindow.elementorCommon.eventsManager.dispatchEvent(event.eventName, event);
167
- } catch {
202
+ // src/use-search-state.ts
203
+ function useSearchState({ localStorageKey }) {
204
+ const getInitialSearchValue = () => {
205
+ if (localStorageKey) {
206
+ const storedValue = localStorage.getItem(localStorageKey);
207
+ if (storedValue) {
208
+ localStorage.removeItem(localStorageKey);
209
+ return storedValue;
210
+ }
168
211
  }
169
- }
170
- };
212
+ return "";
213
+ };
214
+ const { debouncedValue, inputValue, handleChange } = useDebounceState({
215
+ delay: 300,
216
+ initialValue: getInitialSearchValue()
217
+ });
218
+ return {
219
+ debouncedValue,
220
+ inputValue,
221
+ handleChange
222
+ };
223
+ }
224
+
225
+ // src/generate-unique-id.ts
226
+ function generateUniqueId(prefix = "") {
227
+ const prefixStr = prefix ? `${prefix}-` : "";
228
+ return `${prefixStr}${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
229
+ }
171
230
  // Annotate the CommonJS export names for ESM import in node:
172
231
  0 && (module.exports = {
173
232
  ElementorError,
@@ -176,8 +235,10 @@ var sendMixpanelEvent = (event) => {
176
235
  decodeString,
177
236
  encodeString,
178
237
  ensureError,
238
+ generateUniqueId,
179
239
  hash,
180
- sendMixpanelEvent,
181
- useDebounceState
240
+ throttle,
241
+ useDebounceState,
242
+ useSearchState
182
243
  });
183
244
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/encoding.ts","../src/hash.ts","../src/mixpanel-tracking.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\nexport { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';\nexport { debounce } from './debounce';\nexport { encodeString, decodeString } from './encoding';\nexport { hash } from './hash';\nexport { sendMixpanelEvent, type MixpanelEvent } from './mixpanel-tracking';\n","export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\treturn btoa( value );\n};\n\nexport const decodeString = ( value: string, fallback: string = '' ): string => {\n\ttry {\n\t\treturn atob( value );\n\t} catch {\n\t\treturn fallback;\n\t}\n};\n","type UnknownObject = Record< string, unknown >;\n\n// Inspired by:\n// https://github.com/TanStack/query/blob/66ea5f2fc/packages/query-core/src/utils.ts#L212\nexport function hash( obj: UnknownObject ): string {\n\treturn JSON.stringify( obj, ( _, value ) =>\n\t\tisPlainObject( value )\n\t\t\t? Object.keys( value )\n\t\t\t\t\t.sort()\n\t\t\t\t\t.reduce< UnknownObject >( ( result, key ) => {\n\t\t\t\t\t\tresult[ key ] = value[ key ];\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} )\n\t\t\t: value\n\t);\n}\n\nfunction isPlainObject( value: unknown ): value is UnknownObject {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n","type ExtendedWindow = Window & {\n\telementorCommon?: {\n\t\teventsManager?: {\n\t\t\tdispatchEvent: ( name: string, data: MixpanelEvent ) => void;\n\t\t};\n\t};\n};\n\nexport type MixpanelEvent = {\n\tlocation: string;\n\tsecondaryLocation: string;\n\ttrigger: string;\n\twidget_type: string;\n\teventName: string;\n} & { [ key: string ]: unknown };\n\nexport const sendMixpanelEvent = ( event: MixpanelEvent ) => {\n\tconst extendedWindow: ExtendedWindow = window;\n\n\tif ( extendedWindow.elementorCommon?.eventsManager ) {\n\t\ttry {\n\t\t\textendedWindow.elementorCommon.event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I,MAAO,GAAI;AAE3B,aAAO;AAAA,IACR,GAAG,CAAC,CAAE,IACN;AAAA,EACJ;AACD;AAEA,SAAS,cAAe,OAAyC;AAChE,SAAO,CAAC,CAAE,SAAS,OAAO,UAAU,YAAY,CAAE,MAAM,QAAS,KAAM;AACxE;;;ACJO,IAAM,oBAAoB,CAAE,UAA0B;AAC5D,QAAM,iBAAiC;AAEvC,MAAK,eAAe,iBAAiB,eAAgB;AACpD,QAAI;AACH,qBAAe,gBAAgB,cAAc,cAAe,MAAM,WAAW,KAAM;AAAA,IACpF,QAAQ;AAAA,IAAC;AAAA,EACV;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/index.ts","../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts"],"sourcesContent":["export { ElementorError, createError, ensureError } from './errors';\nexport type { ElementorErrorOptions, CreateErrorParams } from './errors';\nexport { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';\nexport { debounce } from './debounce';\nexport { throttle } from './throttle';\nexport { encodeString, decodeString } from './encoding';\nexport { hash } from './hash';\nexport { useSearchState, type UseSearchStateResult } from './use-search-state';\nexport { generateUniqueId } from './generate-unique-id';\n","export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function throttle< TArgs extends any[] >(\n\tfn: ( ...args: TArgs ) => void,\n\twait: number,\n\tshouldExecuteIgnoredCalls: boolean = false\n) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\tlet ignoredExecution: boolean = false;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tif ( timer ) {\n\t\t\tignoredExecution = true;\n\t\t\treturn;\n\t\t}\n\n\t\tfn( ...args );\n\n\t\ttimer = setTimeout( () => {\n\t\t\ttimer = null;\n\n\t\t\tif ( ignoredExecution && shouldExecuteIgnoredCalls ) {\n\t\t\t\tfn( ...args );\n\t\t\t}\n\n\t\t\tignoredExecution = false;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\tconst binary = Array.from( new TextEncoder().encode( value ), ( b ) => String.fromCharCode( b ) ).join( '' );\n\treturn btoa( binary );\n};\n\nexport const decodeString = < T = string >( value: string, fallback?: T ): string | T => {\n\ttry {\n\t\tconst binary = atob( value );\n\t\tconst bytes = new Uint8Array( Array.from( binary, ( char ) => char.charCodeAt( 0 ) ) );\n\t\treturn new TextDecoder().decode( bytes );\n\t} catch {\n\t\treturn fallback !== undefined ? fallback : '';\n\t}\n};\n","type UnknownObject = Record< string, unknown >;\n\n// Inspired by:\n// https://github.com/TanStack/query/blob/66ea5f2fc/packages/query-core/src/utils.ts#L212\nexport function hash( obj: UnknownObject ): string {\n\treturn JSON.stringify( obj, ( _, value ) =>\n\t\tisPlainObject( value )\n\t\t\t? Object.keys( value )\n\t\t\t\t\t.sort()\n\t\t\t\t\t.reduce< UnknownObject >( ( result, key ) => {\n\t\t\t\t\t\tresult[ key ] = value[ key ];\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} )\n\t\t\t: value\n\t);\n}\n\nfunction isPlainObject( value: unknown ): value is UnknownObject {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n","import { useDebounceState, type UseDebounceStateResult } from './use-debounce-state';\n\nexport type UseSearchStateResult = UseDebounceStateResult;\n\nexport function useSearchState( { localStorageKey }: { localStorageKey?: string } ) {\n\tconst getInitialSearchValue = () => {\n\t\tif ( localStorageKey ) {\n\t\t\tconst storedValue = localStorage.getItem( localStorageKey );\n\t\t\tif ( storedValue ) {\n\t\t\t\tlocalStorage.removeItem( localStorageKey );\n\t\t\t\treturn storedValue;\n\t\t\t}\n\t\t}\n\t\treturn '';\n\t};\n\tconst { debouncedValue, inputValue, handleChange } = useDebounceState( {\n\t\tdelay: 300,\n\t\tinitialValue: getInitialSearchValue(),\n\t} );\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t};\n}\n","export function generateUniqueId( prefix: string = '' ): string {\n\tconst prefixStr = prefix ? `${ prefix }-` : '';\n\n\treturn `${ prefixStr }${ Date.now() }-${ Math.random().toString( 36 ).substring( 2, 9 ) }`;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;AAAA,EACjC,SAAU,GAAI;AACb,YAAQ;AACR,cAAU;AAAA,EACX;AACA,SAAO,IAAI,MAAO,gCAAiC,OAAQ,IAAI,EAAE,MAAM,CAAE;AAC1E;;;ACfA,mBAAyD;;;ACClD,SAAS,SAAiC,IAAgC,MAAe;AAC/F,MAAI,QAAgD;AAEpD,QAAM,SAAS,MAAM;AACpB,QAAK,CAAE,OAAQ;AACd;AAAA,IACD;AAEA,iBAAc,KAAM;AACpB,YAAQ;AAAA,EACT;AAEA,QAAM,QAAQ,IAAK,SAAiB;AACnC,WAAO;AAEP,OAAI,GAAG,IAAK;AAAA,EACb;AA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AkC;AACnF,QAAM,wBAAwB,MAAM;AACnC,QAAK,iBAAkB;AACtB,YAAM,cAAc,aAAa,QAAS,eAAgB;AAC1D,UAAK,aAAc;AAClB,qBAAa,WAAY,eAAgB;AACzC,eAAO;AAAA,MACR;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACA,QAAM,EAAE,gBAAgB,YAAY,aAAa,IAAI,iBAAkB;AAAA,IACtE,OAAO;AAAA,IACP,cAAc,sBAAsB;AAAA,EACrC,CAAE;AACF,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;ACxBO,SAAS,iBAAkB,SAAiB,IAAa;AAC/D,QAAM,YAAY,SAAS,GAAI,MAAO,MAAM;AAE5C,SAAO,GAAI,SAAU,GAAI,KAAK,IAAI,CAAE,IAAK,KAAK,OAAO,EAAE,SAAU,EAAG,EAAE,UAAW,GAAG,CAAE,CAAE;AACzF;","names":[]}
package/dist/index.mjs CHANGED
@@ -98,15 +98,54 @@ function useDebounceState(options = {}) {
98
98
  };
99
99
  }
100
100
 
101
+ // src/throttle.ts
102
+ function throttle(fn, wait, shouldExecuteIgnoredCalls = false) {
103
+ let timer = null;
104
+ let ignoredExecution = false;
105
+ const cancel = () => {
106
+ if (!timer) {
107
+ return;
108
+ }
109
+ clearTimeout(timer);
110
+ timer = null;
111
+ };
112
+ const flush = (...args) => {
113
+ cancel();
114
+ fn(...args);
115
+ };
116
+ const run = (...args) => {
117
+ if (timer) {
118
+ ignoredExecution = true;
119
+ return;
120
+ }
121
+ fn(...args);
122
+ timer = setTimeout(() => {
123
+ timer = null;
124
+ if (ignoredExecution && shouldExecuteIgnoredCalls) {
125
+ fn(...args);
126
+ }
127
+ ignoredExecution = false;
128
+ }, wait);
129
+ };
130
+ const pending = () => !!timer;
131
+ run.flush = flush;
132
+ run.cancel = cancel;
133
+ run.pending = pending;
134
+ return run;
135
+ }
136
+
101
137
  // src/encoding.ts
102
138
  var encodeString = (value) => {
103
- return btoa(value);
139
+ const binary = Array.from(new TextEncoder().encode(value), (b) => String.fromCharCode(b)).join("");
140
+ return btoa(binary);
104
141
  };
105
- var decodeString = (value, fallback = "") => {
142
+ var decodeString = (value, fallback) => {
106
143
  try {
107
- return atob(value);
144
+ const binary = atob(value);
145
+ const bytes = new Uint8Array(Array.from(binary, (char) => char.charCodeAt(0)));
146
+ return new TextDecoder().decode(bytes);
108
147
  } catch {
109
- return fallback;
148
+ return fallback !== void 0 ? fallback : "";
110
149
  }
111
150
  };
112
151
 
@@ -124,16 +163,34 @@ function isPlainObject(value) {
124
163
  return !!value && typeof value === "object" && !Array.isArray(value);
125
164
  }
126
165
 
127
- // src/mixpanel-tracking.ts
128
- var sendMixpanelEvent = (event) => {
129
- const extendedWindow = window;
130
- if (extendedWindow.elementorCommon?.eventsManager) {
131
- try {
132
- extendedWindow.elementorCommon.eventsManager.dispatchEvent(event.eventName, event);
133
- } catch {
166
+ // src/use-search-state.ts
167
+ function useSearchState({ localStorageKey }) {
168
+ const getInitialSearchValue = () => {
169
+ if (localStorageKey) {
170
+ const storedValue = localStorage.getItem(localStorageKey);
171
+ if (storedValue) {
172
+ localStorage.removeItem(localStorageKey);
173
+ return storedValue;
174
+ }
134
175
  }
135
- }
136
- };
176
+ return "";
177
+ };
178
+ const { debouncedValue, inputValue, handleChange } = useDebounceState({
179
+ delay: 300,
180
+ initialValue: getInitialSearchValue()
181
+ });
182
+ return {
183
+ debouncedValue,
184
+ inputValue,
185
+ handleChange
186
+ };
187
+ }
188
+
189
+ // src/generate-unique-id.ts
190
+ function generateUniqueId(prefix = "") {
191
+ const prefixStr = prefix ? `${prefix}-` : "";
192
+ return `${prefixStr}${Date.now()}-${Math.random().toString(36).substring(2, 9)}`;
193
+ }
137
194
  export {
138
195
  ElementorError,
139
196
  createError,
@@ -141,8 +198,10 @@ export {
141
198
  decodeString,
142
199
  encodeString,
143
200
  ensureError,
201
+ generateUniqueId,
144
202
  hash,
145
- sendMixpanelEvent,
146
- useDebounceState
203
+ throttle,
204
+ useDebounceState,
205
+ useSearchState
147
206
  };
148
207
  //# sourceMappingURL=index.mjs.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/encoding.ts","../src/hash.ts","../src/mixpanel-tracking.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\treturn btoa( value );\n};\n\nexport const decodeString = ( value: string, fallback: string = '' ): string => {\n\ttry {\n\t\treturn atob( value );\n\t} catch {\n\t\treturn fallback;\n\t}\n};\n","type UnknownObject = Record< string, unknown >;\n\n// Inspired by:\n// https://github.com/TanStack/query/blob/66ea5f2fc/packages/query-core/src/utils.ts#L212\nexport function hash( obj: UnknownObject ): string {\n\treturn JSON.stringify( obj, ( _, value ) =>\n\t\tisPlainObject( value )\n\t\t\t? Object.keys( value )\n\t\t\t\t\t.sort()\n\t\t\t\t\t.reduce< UnknownObject >( ( result, key ) => {\n\t\t\t\t\t\tresult[ key ] = value[ key ];\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} )\n\t\t\t: value\n\t);\n}\n\nfunction isPlainObject( value: unknown ): value is UnknownObject {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n","type ExtendedWindow = Window & {\n\telementorCommon?: {\n\t\teventsManager?: {\n\t\t\tdispatchEvent: ( name: string, data: MixpanelEvent ) => void;\n\t\t};\n\t};\n};\n\nexport type MixpanelEvent = {\n\tlocation: string;\n\tsecondaryLocation: string;\n\ttrigger: string;\n\twidget_type: string;\n\teventName: string;\n} & { [ key: string ]: unknown };\n\nexport const sendMixpanelEvent = ( event: MixpanelEvent ) => {\n\tconst extendedWindow: ExtendedWindow = window;\n\n\tif ( extendedWindow.elementorCommon?.eventsManager ) {\n\t\ttry {\n\t\t\textendedWindow.elementorCommon.eventsManager.dispatchEvent( event.eventName, event );\n\t\t} catch {}\n\t}\n};\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EA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AC1B;AAAA,EACD,GAAG,CAAC,CAAE;AAEN,QAAM,oBAAoB;AAAA,IACzB,CAAE,QAAiB;AAClB,aAAO,SAAS,SAAS;AACzB,aAAO,UAAU,SAAU,MAAM;AAChC,0BAAmB,GAAI;AAAA,MACxB,GAAG,KAAM;AACT,aAAO,QAAQ;AAAA,IAChB;AAAA,IACA,CAAE,KAAM;AAAA,EACT;AAEA,QAAM,eAAe,CAAE,QAAiB;AACvC,kBAAe,GAAI;AACnB,sBAAmB,GAAI;AAAA,EACxB;AAEA,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;AErDO,IAAM,eAAe,CAAE,UAA2B;AACxD,SAAO,KAAM,KAAM;AACpB;AAEO,IAAM,eAAe,CAAE,OAAe,WAAmB,OAAgB;AAC/E,MAAI;AACH,WAAO,KAAM,KAAM;AAAA,EACpB,QAAQ;AACP,WAAO;AAAA,EACR;AACD;;;ACNO,SAAS,KAAM,KAA6B;AAClD,SAAO,KAAK;AAAA,IAAW;AAAA,IAAK,CAAE,GAAG,UAChC,cAAe,KAAM,IAClB,OAAO,KAAM,KAAM,EAClB,KAAK,EACL,OAAyB,CAAE,QAAQ,QAAS;AAC5C,aAAQ,GAAI,IAAI,MAAO,GAAI;AAE3B,aAAO;AAAA,IACR,GAAG,CAAC,CAAE,IACN;AAAA,EACJ;AACD;AAEA,SAAS,cAAe,OAAyC;AAChE,SAAO,CAAC,CAAE,SAAS,OAAO,UAAU,YAAY,CAAE,MAAM,QAAS,KAAM;AACxE;;;ACJO,IAAM,oBAAoB,CAAE,UAA0B;AAC5D,QAAM,iBAAiC;AAEvC,MAAK,eAAe,iBAAiB,eAAgB;AACpD,QAAI;AACH,qBAAe,gBAAgB,cAAc,cAAe,MAAM,WAAW,KAAM;AAAA,IACpF,QAAQ;AAAA,IAAC;AAAA,EACV;AACD;","names":[]}
1
+ {"version":3,"sources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts"],"sourcesContent":["export type ElementorErrorOptions = {\n\tcause?: Error[ 'cause' ];\n\tcontext?: Record< string, unknown > | null;\n\tcode: string;\n};\n\nexport class ElementorError extends Error {\n\treadonly context: ElementorErrorOptions[ 'context' ];\n\treadonly code: ElementorErrorOptions[ 'code' ];\n\n\tconstructor( message: string, { code, context = null, cause = null }: ElementorErrorOptions ) {\n\t\tsuper( message, { cause } );\n\t\tthis.context = context;\n\t\tthis.code = code;\n\t}\n}\n","import { ElementorError, type ElementorErrorOptions } from './elementor-error';\n\nexport type CreateErrorParams = {\n\tcode: ElementorErrorOptions[ 'code' ];\n\tmessage: string;\n};\n\nexport const createError = < T extends ElementorErrorOptions[ 'context' ] >( { code, message }: CreateErrorParams ) => {\n\treturn class extends ElementorError {\n\t\tconstructor( { cause, context }: { cause?: ElementorErrorOptions[ 'cause' ]; context?: T } = {} ) {\n\t\t\tsuper( message, { cause, code, context } );\n\t\t}\n\t};\n};\n","export const ensureError = ( error: unknown ) => {\n\tif ( error instanceof Error ) {\n\t\treturn error;\n\t}\n\n\tlet message: string;\n\tlet cause: unknown = null;\n\n\ttry {\n\t\tmessage = JSON.stringify( error );\n\t} catch ( e ) {\n\t\tcause = e;\n\t\tmessage = 'Unable to stringify the thrown value';\n\t}\n\treturn new Error( `Unexpected non-error thrown: ${ message }`, { cause } );\n};\n","import { useCallback, useEffect, useRef, useState } from 'react';\nimport type * as React from 'react';\n\nimport { debounce } from './debounce';\n\nexport type UseDebounceStateOptions = {\n\tdelay?: number;\n\tinitialValue?: string;\n};\n\nexport type UseDebounceStateResult = {\n\tdebouncedValue: string;\n\tinputValue: string;\n\thandleChange: ( val: string ) => void;\n\tsetInputValue: React.Dispatch< React.SetStateAction< string > >;\n};\n\nexport function useDebounceState( options: UseDebounceStateOptions = {} ): UseDebounceStateResult {\n\tconst { delay = 300, initialValue = '' } = options;\n\n\tconst [ debouncedValue, setDebouncedValue ] = useState( initialValue );\n\tconst [ inputValue, setInputValue ] = useState( initialValue );\n\n\tconst runRef = useRef< ReturnType< typeof debounce > | null >( null );\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\trunRef.current?.cancel?.();\n\t\t};\n\t}, [] );\n\n\tconst debouncedSetValue = useCallback(\n\t\t( val: string ) => {\n\t\t\trunRef.current?.cancel?.();\n\t\t\trunRef.current = debounce( () => {\n\t\t\t\tsetDebouncedValue( val );\n\t\t\t}, delay );\n\t\t\trunRef.current();\n\t\t},\n\t\t[ delay ]\n\t);\n\n\tconst handleChange = ( val: string ) => {\n\t\tsetInputValue( val );\n\t\tdebouncedSetValue( val );\n\t};\n\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t\tsetInputValue,\n\t};\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function debounce< TArgs extends any[] >( fn: ( ...args: TArgs ) => void, wait: number ) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\ttimer = setTimeout( () => {\n\t\t\tfn( ...args );\n\n\t\t\ttimer = null;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function throttle< TArgs extends any[] >(\n\tfn: ( ...args: TArgs ) => void,\n\twait: number,\n\tshouldExecuteIgnoredCalls: boolean = false\n) {\n\tlet timer: ReturnType< typeof setTimeout > | null = null;\n\tlet ignoredExecution: boolean = false;\n\n\tconst cancel = () => {\n\t\tif ( ! timer ) {\n\t\t\treturn;\n\t\t}\n\n\t\tclearTimeout( timer );\n\t\ttimer = null;\n\t};\n\n\tconst flush = ( ...args: TArgs ) => {\n\t\tcancel();\n\n\t\tfn( ...args );\n\t};\n\n\tconst run = ( ...args: TArgs ) => {\n\t\tif ( timer ) {\n\t\t\tignoredExecution = true;\n\t\t\treturn;\n\t\t}\n\n\t\tfn( ...args );\n\n\t\ttimer = setTimeout( () => {\n\t\t\ttimer = null;\n\n\t\t\tif ( ignoredExecution && shouldExecuteIgnoredCalls ) {\n\t\t\t\tfn( ...args );\n\t\t\t}\n\n\t\t\tignoredExecution = false;\n\t\t}, wait );\n\t};\n\n\tconst pending = () => !! timer;\n\n\trun.flush = flush;\n\trun.cancel = cancel;\n\trun.pending = pending;\n\n\treturn run;\n}\n","export const encodeString = ( value: string ): string => {\n\tconst binary = Array.from( new TextEncoder().encode( value ), ( b ) => String.fromCharCode( b ) ).join( '' );\n\treturn btoa( binary );\n};\n\nexport const decodeString = < T = string >( value: string, fallback?: T ): string | T => {\n\ttry {\n\t\tconst binary = atob( value );\n\t\tconst bytes = new Uint8Array( Array.from( binary, ( char ) => char.charCodeAt( 0 ) ) );\n\t\treturn new TextDecoder().decode( bytes );\n\t} catch {\n\t\treturn fallback !== undefined ? fallback : '';\n\t}\n};\n","type UnknownObject = Record< string, unknown >;\n\n// Inspired by:\n// https://github.com/TanStack/query/blob/66ea5f2fc/packages/query-core/src/utils.ts#L212\nexport function hash( obj: UnknownObject ): string {\n\treturn JSON.stringify( obj, ( _, value ) =>\n\t\tisPlainObject( value )\n\t\t\t? Object.keys( value )\n\t\t\t\t\t.sort()\n\t\t\t\t\t.reduce< UnknownObject >( ( result, key ) => {\n\t\t\t\t\t\tresult[ key ] = value[ key ];\n\n\t\t\t\t\t\treturn result;\n\t\t\t\t\t}, {} )\n\t\t\t: value\n\t);\n}\n\nfunction isPlainObject( value: unknown ): value is UnknownObject {\n\treturn !! value && typeof value === 'object' && ! Array.isArray( value );\n}\n","import { useDebounceState, type UseDebounceStateResult } from './use-debounce-state';\n\nexport type UseSearchStateResult = UseDebounceStateResult;\n\nexport function useSearchState( { localStorageKey }: { localStorageKey?: string } ) {\n\tconst getInitialSearchValue = () => {\n\t\tif ( localStorageKey ) {\n\t\t\tconst storedValue = localStorage.getItem( localStorageKey );\n\t\t\tif ( storedValue ) {\n\t\t\t\tlocalStorage.removeItem( localStorageKey );\n\t\t\t\treturn storedValue;\n\t\t\t}\n\t\t}\n\t\treturn '';\n\t};\n\tconst { debouncedValue, inputValue, handleChange } = useDebounceState( {\n\t\tdelay: 300,\n\t\tinitialValue: getInitialSearchValue(),\n\t} );\n\treturn {\n\t\tdebouncedValue,\n\t\tinputValue,\n\t\thandleChange,\n\t};\n}\n","export function generateUniqueId( prefix: string = '' ): string {\n\tconst prefixStr = prefix ? `${ prefix }-` : '';\n\n\treturn `${ prefixStr }${ Date.now() }-${ Math.random().toString( 36 ).substring( 2, 9 ) }`;\n}\n"],"mappings":";AAMO,IAAM,iBAAN,cAA6B,MAAM;AAAA,EAChC;AAAA,EACA;AAAA,EAET,YAAa,SAAiB,EAAE,MAAM,UAAU,MAAM,QAAQ,KAAK,GAA2B;AAC7F,UAAO,SAAS,EAAE,MAAM,CAAE;AAC1B,SAAK,UAAU;AACf,SAAK,OAAO;AAAA,EACb;AACD;;;ACRO,IAAM,cAAc,CAAkD,EAAE,MAAM,QAAQ,MAA0B;AACtH,SAAO,cAAc,eAAe;AAAA,IACnC,YAAa,EAAE,OAAO,QAAQ,IAA+D,CAAC,GAAI;AACjG,YAAO,SAAS,EAAE,OAAO,MAAM,QAAQ,CAAE;AAAA,IAC1C;AAAA,EACD;AACD;;;ACbO,IAAM,cAAc,CAAE,UAAoB;AAChD,MAAK,iBAAiB,OAAQ;AAC7B,WAAO;AAAA,EACR;AAEA,MAAI;AACJ,MAAI,QAAiB;AAErB,MAAI;AACH,cAAU,KAAK,UAAW,KAAM;A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;ACTO,SAAS,KAAM,KAA6B;AAClD,SAAO,KAAK;AAAA,IAAW;AAAA,IAAK,CAAE,GAAG,UAChC,cAAe,KAAM,IAClB,OAAO,KAAM,KAAM,EAClB,KAAK,EACL,OAAyB,CAAE,QAAQ,QAAS;AAC5C,aAAQ,GAAI,IAAI,MAAO,GAAI;AAE3B,aAAO;AAAA,IACR,GAAG,CAAC,CAAE,IACN;AAAA,EACJ;AACD;AAEA,SAAS,cAAe,OAAyC;AAChE,SAAO,CAAC,CAAE,SAAS,OAAO,UAAU,YAAY,CAAE,MAAM,QAAS,KAAM;AACxE;;;AChBO,SAAS,eAAgB,EAAE,gBAAgB,GAAkC;AACnF,QAAM,wBAAwB,MAAM;AACnC,QAAK,iBAAkB;AACtB,YAAM,cAAc,aAAa,QAAS,eAAgB;AAC1D,UAAK,aAAc;AAClB,qBAAa,WAAY,eAAgB;AACzC,eAAO;AAAA,MACR;AAAA,IACD;AACA,WAAO;AAAA,EACR;AACA,QAAM,EAAE,gBAAgB,YAAY,aAAa,IAAI,iBAAkB;AAAA,IACtE,OAAO;AAAA,IACP,cAAc,sBAAsB;AAAA,EACrC,CAAE;AACF,SAAO;AAAA,IACN;AAAA,IACA;AAAA,IACA;AAAA,EACD;AACD;;;ACxBO,SAAS,iBAAkB,SAAiB,IAAa;AAC/D,QAAM,YAAY,SAAS,GAAI,MAAO,MAAM;AAE5C,SAAO,GAAI,SAAU,GAAI,KAAK,IAAI,CAAE,IAAK,KAAK,OAAO,EAAE,SAAU,EAAG,EAAE,UAAW,GAAG,CAAE,CAAE;AACzF;","names":[]}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@elementor/utils",
3
3
  "description": "This package contains utility functions that are being used across the Elementor packages",
4
- "version": "3.33.0-99",
4
+ "version": "3.35.0-325",
5
5
  "private": false,
6
6
  "author": "Elementor Team",
7
7
  "homepage": "https://elementor.com/",
package/src/encoding.ts CHANGED
@@ -1,11 +1,14 @@
1
1
  export const encodeString = ( value: string ): string => {
2
- return btoa( value );
2
+ const binary = Array.from( new TextEncoder().encode( value ), ( b ) => String.fromCharCode( b ) ).join( '' );
3
+ return btoa( binary );
3
4
  };
4
5
 
5
- export const decodeString = ( value: string, fallback: string = '' ): string => {
6
+ export const decodeString = < T = string >( value: string, fallback?: T ): string | T => {
6
7
  try {
7
- return atob( value );
8
+ const binary = atob( value );
9
+ const bytes = new Uint8Array( Array.from( binary, ( char ) => char.charCodeAt( 0 ) ) );
10
+ return new TextDecoder().decode( bytes );
8
11
  } catch {
9
- return fallback;
12
+ return fallback !== undefined ? fallback : '';
10
13
  }
11
14
  };
@@ -0,0 +1,5 @@
1
+ export function generateUniqueId( prefix: string = '' ): string {
2
+ const prefixStr = prefix ? `${ prefix }-` : '';
3
+
4
+ return `${ prefixStr }${ Date.now() }-${ Math.random().toString( 36 ).substring( 2, 9 ) }`;
5
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,8 @@ export { ElementorError, createError, ensureError } from './errors';
2
2
  export type { ElementorErrorOptions, CreateErrorParams } from './errors';
3
3
  export { useDebounceState, type UseDebounceStateOptions, type UseDebounceStateResult } from './use-debounce-state';
4
4
  export { debounce } from './debounce';
5
+ export { throttle } from './throttle';
5
6
  export { encodeString, decodeString } from './encoding';
6
7
  export { hash } from './hash';
7
- export { sendMixpanelEvent, type MixpanelEvent } from './mixpanel-tracking';
8
+ export { useSearchState, type UseSearchStateResult } from './use-search-state';
9
+ export { generateUniqueId } from './generate-unique-id';
@@ -0,0 +1,51 @@
1
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
2
+ export function throttle< TArgs extends any[] >(
3
+ fn: ( ...args: TArgs ) => void,
4
+ wait: number,
5
+ shouldExecuteIgnoredCalls: boolean = false
6
+ ) {
7
+ let timer: ReturnType< typeof setTimeout > | null = null;
8
+ let ignoredExecution: boolean = false;
9
+
10
+ const cancel = () => {
11
+ if ( ! timer ) {
12
+ return;
13
+ }
14
+
15
+ clearTimeout( timer );
16
+ timer = null;
17
+ };
18
+
19
+ const flush = ( ...args: TArgs ) => {
20
+ cancel();
21
+
22
+ fn( ...args );
23
+ };
24
+
25
+ const run = ( ...args: TArgs ) => {
26
+ if ( timer ) {
27
+ ignoredExecution = true;
28
+ return;
29
+ }
30
+
31
+ fn( ...args );
32
+
33
+ timer = setTimeout( () => {
34
+ timer = null;
35
+
36
+ if ( ignoredExecution && shouldExecuteIgnoredCalls ) {
37
+ fn( ...args );
38
+ }
39
+
40
+ ignoredExecution = false;
41
+ }, wait );
42
+ };
43
+
44
+ const pending = () => !! timer;
45
+
46
+ run.flush = flush;
47
+ run.cancel = cancel;
48
+ run.pending = pending;
49
+
50
+ return run;
51
+ }
@@ -0,0 +1,25 @@
1
+ import { useDebounceState, type UseDebounceStateResult } from './use-debounce-state';
2
+
3
+ export type UseSearchStateResult = UseDebounceStateResult;
4
+
5
+ export function useSearchState( { localStorageKey }: { localStorageKey?: string } ) {
6
+ const getInitialSearchValue = () => {
7
+ if ( localStorageKey ) {
8
+ const storedValue = localStorage.getItem( localStorageKey );
9
+ if ( storedValue ) {
10
+ localStorage.removeItem( localStorageKey );
11
+ return storedValue;
12
+ }
13
+ }
14
+ return '';
15
+ };
16
+ const { debouncedValue, inputValue, handleChange } = useDebounceState( {
17
+ delay: 300,
18
+ initialValue: getInitialSearchValue(),
19
+ } );
20
+ return {
21
+ debouncedValue,
22
+ inputValue,
23
+ handleChange,
24
+ };
25
+ }
@@ -1,25 +0,0 @@
1
- type ExtendedWindow = Window & {
2
- elementorCommon?: {
3
- eventsManager?: {
4
- dispatchEvent: ( name: string, data: MixpanelEvent ) => void;
5
- };
6
- };
7
- };
8
-
9
- export type MixpanelEvent = {
10
- location: string;
11
- secondaryLocation: string;
12
- trigger: string;
13
- widget_type: string;
14
- eventName: string;
15
- } & { [ key: string ]: unknown };
16
-
17
- export const sendMixpanelEvent = ( event: MixpanelEvent ) => {
18
- const extendedWindow: ExtendedWindow = window;
19
-
20
- if ( extendedWindow.elementorCommon?.eventsManager ) {
21
- try {
22
- extendedWindow.elementorCommon.eventsManager.dispatchEvent( event.eventName, event );
23
- } catch {}
24
- }
25
- };