@elementor/utils 4.2.0-886 → 4.2.0-888
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 +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +20 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/use-debounced-callback.ts +25 -0
package/dist/index.d.mts
CHANGED
|
@@ -44,6 +44,13 @@ type UseDebounceStateResult = {
|
|
|
44
44
|
};
|
|
45
45
|
declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
|
|
46
46
|
|
|
47
|
+
declare function useDebouncedCallback<TArgs extends any[]>(callback: (...args: TArgs) => void, delay: number): {
|
|
48
|
+
(...args: TArgs): void;
|
|
49
|
+
flush: (...args: TArgs) => void;
|
|
50
|
+
cancel: () => void;
|
|
51
|
+
pending: () => boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
47
54
|
declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
|
|
48
55
|
(...args: TArgs): void;
|
|
49
56
|
flush: (...args: TArgs) => void;
|
|
@@ -93,4 +100,4 @@ interface CreateTranslateOptions {
|
|
|
93
100
|
}
|
|
94
101
|
declare function createTranslate({ configKey, defaultStrings }: CreateTranslateOptions): TranslateFunction;
|
|
95
102
|
|
|
96
|
-
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, capitalize, compareVersions, createError, createTranslate, debounce, decodeString, encodeString, ensureError, generateUniqueId, hasProInstalled, hash, hashString, isProActive, isProAtLeast, isVersionGreaterOrEqual, isVersionLessThan, throttle, useDebounceState, useSearchState };
|
|
103
|
+
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, capitalize, compareVersions, createError, createTranslate, debounce, decodeString, encodeString, ensureError, generateUniqueId, hasProInstalled, hash, hashString, isProActive, isProAtLeast, isVersionGreaterOrEqual, isVersionLessThan, throttle, useDebounceState, useDebouncedCallback, useSearchState };
|
package/dist/index.d.ts
CHANGED
|
@@ -44,6 +44,13 @@ type UseDebounceStateResult = {
|
|
|
44
44
|
};
|
|
45
45
|
declare function useDebounceState(options?: UseDebounceStateOptions): UseDebounceStateResult;
|
|
46
46
|
|
|
47
|
+
declare function useDebouncedCallback<TArgs extends any[]>(callback: (...args: TArgs) => void, delay: number): {
|
|
48
|
+
(...args: TArgs): void;
|
|
49
|
+
flush: (...args: TArgs) => void;
|
|
50
|
+
cancel: () => void;
|
|
51
|
+
pending: () => boolean;
|
|
52
|
+
};
|
|
53
|
+
|
|
47
54
|
declare function debounce<TArgs extends any[]>(fn: (...args: TArgs) => void, wait: number): {
|
|
48
55
|
(...args: TArgs): void;
|
|
49
56
|
flush: (...args: TArgs) => void;
|
|
@@ -93,4 +100,4 @@ interface CreateTranslateOptions {
|
|
|
93
100
|
}
|
|
94
101
|
declare function createTranslate({ configKey, defaultStrings }: CreateTranslateOptions): TranslateFunction;
|
|
95
102
|
|
|
96
|
-
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, capitalize, compareVersions, createError, createTranslate, debounce, decodeString, encodeString, ensureError, generateUniqueId, hasProInstalled, hash, hashString, isProActive, isProAtLeast, isVersionGreaterOrEqual, isVersionLessThan, throttle, useDebounceState, useSearchState };
|
|
103
|
+
export { type CreateErrorParams, ElementorError, type ElementorErrorOptions, type UseDebounceStateOptions, type UseDebounceStateResult, type UseSearchStateResult, capitalize, compareVersions, createError, createTranslate, debounce, decodeString, encodeString, ensureError, generateUniqueId, hasProInstalled, hash, hashString, isProActive, isProAtLeast, isVersionGreaterOrEqual, isVersionLessThan, throttle, useDebounceState, useDebouncedCallback, useSearchState };
|
package/dist/index.js
CHANGED
|
@@ -39,6 +39,7 @@ __export(index_exports, {
|
|
|
39
39
|
isVersionLessThan: () => isVersionLessThan,
|
|
40
40
|
throttle: () => throttle,
|
|
41
41
|
useDebounceState: () => useDebounceState,
|
|
42
|
+
useDebouncedCallback: () => useDebouncedCallback,
|
|
42
43
|
useSearchState: () => useSearchState
|
|
43
44
|
});
|
|
44
45
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -143,6 +144,25 @@ function useDebounceState(options = {}) {
|
|
|
143
144
|
};
|
|
144
145
|
}
|
|
145
146
|
|
|
147
|
+
// src/use-debounced-callback.ts
|
|
148
|
+
var import_react2 = require("react");
|
|
149
|
+
function useDebouncedCallback(callback, delay) {
|
|
150
|
+
const callbackRef = (0, import_react2.useRef)(callback);
|
|
151
|
+
(0, import_react2.useEffect)(() => {
|
|
152
|
+
callbackRef.current = callback;
|
|
153
|
+
}, [callback]);
|
|
154
|
+
const debounced = (0, import_react2.useMemo)(
|
|
155
|
+
() => debounce((...args) => callbackRef.current(...args), delay),
|
|
156
|
+
[delay]
|
|
157
|
+
);
|
|
158
|
+
(0, import_react2.useEffect)(() => {
|
|
159
|
+
return () => {
|
|
160
|
+
debounced.cancel();
|
|
161
|
+
};
|
|
162
|
+
}, [debounced]);
|
|
163
|
+
return debounced;
|
|
164
|
+
}
|
|
165
|
+
|
|
146
166
|
// src/throttle.ts
|
|
147
167
|
function throttle(fn, wait, shouldExecuteIgnoredCalls = false) {
|
|
148
168
|
let timer = null;
|
|
@@ -339,6 +359,7 @@ function createTranslate({ configKey, defaultStrings = {} }) {
|
|
|
339
359
|
isVersionLessThan,
|
|
340
360
|
throttle,
|
|
341
361
|
useDebounceState,
|
|
362
|
+
useDebouncedCallback,
|
|
342
363
|
useSearchState
|
|
343
364
|
});
|
|
344
365
|
//# 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/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts","../src/string-helpers.ts","../src/version.ts","../src/is-pro.ts","../src/translations.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, hashString } from './hash';\nexport { useSearchState, type UseSearchStateResult } from './use-search-state';\nexport { generateUniqueId } from './generate-unique-id';\nexport { capitalize } from './string-helpers';\nexport { compareVersions, isVersionLessThan, isVersionGreaterOrEqual } from './version';\nexport { hasProInstalled, isProActive, isProAtLeast } from './is-pro';\nexport { createTranslate } from './translations';\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","/* eslint-disable no-bitwise */\ntype 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\n// Inspired by:\n// https://github.com/darkskyapp/string-hash/blob/master/index.js\nexport function hashString( str: string, length?: number ): string {\n\tlet hashBasis = 5381;\n\n\tlet i = str.length;\n\twhile ( i ) {\n\t\thashBasis = ( hashBasis * 33 ) ^ str.charCodeAt( --i );\n\t}\n\n\t/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed\n\t * integers. Since we want the results to be always positive, convert the\n\t * signed int to an unsigned by doing an unsigned bitshift. */\n\tconst result = ( hashBasis >>> 0 ).toString( 36 );\n\n\tif ( length === undefined ) {\n\t\treturn result;\n\t}\n\n\tconst sliced = result.slice( -length );\n\treturn sliced.padStart( length, '0' );\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","export const capitalize = ( str: string ): string => {\n\treturn str.charAt( 0 ).toUpperCase() + str.slice( 1 );\n};\n","export const compareVersions = ( a: string | number, b: string | number ): number => {\n\tconst aParts = String( a || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\tconst bParts = String( b || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\n\tfor ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) {\n\t\tconst aVal = aParts[ i ] || 0;\n\t\tconst bVal = bParts[ i ] || 0;\n\t\tif ( aVal !== bVal ) {\n\t\t\treturn aVal - bVal;\n\t\t}\n\t}\n\treturn 0;\n};\n\nexport const isVersionLessThan = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) < 0;\n};\n\nexport const isVersionGreaterOrEqual = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) >= 0;\n};\n","export function hasProInstalled(): boolean {\n\treturn window.elementor?.helpers?.hasPro?.() ?? false;\n}\n\nexport function isProActive(): boolean {\n\tif ( ! hasProInstalled() ) {\n\t\treturn false;\n\t}\n\n\treturn window.elementorPro?.config?.isActive ?? false;\n}\n\nfunction getProVersion(): string {\n\treturn window.elementorPro?.config?.version ?? '0.0';\n}\n\nexport function isProAtLeast( targetVersion: string ): boolean {\n\tconst version = getProVersion();\n\n\tif ( ! version ) {\n\t\treturn false;\n\t}\n\n\tconst [ major, minor ] = version.split( '.' ).map( Number );\n\tconst [ targetMajor, targetMinor ] = targetVersion.split( '.' ).map( Number );\n\n\treturn major > targetMajor || ( major === targetMajor && minor >= targetMinor );\n}\n","type TranslateFunction = ( key: string, ...args: string[] ) => string;\n\ninterface CreateTranslateOptions {\n\tconfigKey: string;\n\tdefaultStrings?: Record< string, string >;\n}\n\nexport function createTranslate( { configKey, defaultStrings = {} }: CreateTranslateOptions ): TranslateFunction {\n\treturn ( key: string, ...args: string[] ): string => {\n\t\tconst appConfig = window.elementorAppConfig as\n\t\t\t| Record< string, { translations?: Record< string, string > } >\n\t\t\t| undefined;\n\t\tconst remoteStrings = appConfig?.[ configKey ]?.translations;\n\t\tconst strings: Record< string, string > = {\n\t\t\t...defaultStrings,\n\t\t\t...remoteStrings,\n\t\t};\n\n\t\tlet template = strings[ key ];\n\n\t\tif ( ! template ) {\n\t\t\treturn key;\n\t\t}\n\n\t\tfor ( let i = 0; i < args.length; i++ ) {\n\t\t\ttemplate = template.replace( `%${ i + 1 }$s`, args[ i ] );\n\t\t\ttemplate = template.replace( '%s', args[ i ] );\n\t\t}\n\n\t\treturn template;\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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;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9B,MAAK,CAAE,SAAU;AAChB,WAAO;AAAA,EACR;AAEA,QAAM,CAAE,OAAO,KAAM,IAAI,QAAQ,MAAO,GAAI,EAAE,IAAK,MAAO;AAC1D,QAAM,CAAE,aAAa,WAAY,IAAI,cAAc,MAAO,GAAI,EAAE,IAAK,MAAO;AAE5E,SAAO,QAAQ,eAAiB,UAAU,eAAe,SAAS;AACnE;;;ACpBO,SAAS,gBAAiB,EAAE,WAAW,iBAAiB,CAAC,EAAE,GAA+C;AAChH,SAAO,CAAE,QAAgB,SAA4B;AACpD,UAAM,YAAY,OAAO;AAGzB,UAAM,gBAAgB,YAAa,SAAU,GAAG;AAChD,UAAM,UAAoC;AAAA,MACzC,GAAG;AAAA,MACH,GAAG;AAAA,IACJ;AAEA,QAAI,WAAW,QAAS,GAAI;AAE5B,QAAK,CAAE,UAAW;AACjB,aAAO;AAAA,IACR;AAEA,aAAU,IAAI,GAAG,IAAI,KAAK,QAAQ,KAAM;AACvC,iBAAW,SAAS,QAAS,IAAK,IAAI,CAAE,MAAM,KAAM,CAAE,CAAE;AACxD,iBAAW,SAAS,QAAS,MAAM,KAAM,CAAE,CAAE;AAAA,IAC9C;AAEA,WAAO;AAAA,EACR;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/use-debounced-callback.ts","../src/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts","../src/string-helpers.ts","../src/version.ts","../src/is-pro.ts","../src/translations.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 { useDebouncedCallback } from './use-debounced-callback';\nexport { debounce } from './debounce';\nexport { throttle } from './throttle';\nexport { encodeString, decodeString } from './encoding';\nexport { hash, hashString } from './hash';\nexport { useSearchState, type UseSearchStateResult } from './use-search-state';\nexport { generateUniqueId } from './generate-unique-id';\nexport { capitalize } from './string-helpers';\nexport { compareVersions, isVersionLessThan, isVersionGreaterOrEqual } from './version';\nexport { hasProInstalled, isProActive, isProAtLeast } from './is-pro';\nexport { createTranslate } from './translations';\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","import { useEffect, useMemo, useRef } from 'react';\n\nimport { debounce } from './debounce';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useDebouncedCallback< TArgs extends any[] >( callback: ( ...args: TArgs ) => void, delay: number ) {\n\tconst callbackRef = useRef( callback );\n\n\tuseEffect( () => {\n\t\tcallbackRef.current = callback;\n\t}, [ callback ] );\n\n\tconst debounced = useMemo(\n\t\t() => debounce( ( ...args: TArgs ) => callbackRef.current( ...args ), delay ),\n\t\t[ delay ]\n\t);\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tdebounced.cancel();\n\t\t};\n\t}, [ debounced ] );\n\n\treturn debounced;\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","/* eslint-disable no-bitwise */\ntype 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\n// Inspired by:\n// https://github.com/darkskyapp/string-hash/blob/master/index.js\nexport function hashString( str: string, length?: number ): string {\n\tlet hashBasis = 5381;\n\n\tlet i = str.length;\n\twhile ( i ) {\n\t\thashBasis = ( hashBasis * 33 ) ^ str.charCodeAt( --i );\n\t}\n\n\t/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed\n\t * integers. Since we want the results to be always positive, convert the\n\t * signed int to an unsigned by doing an unsigned bitshift. */\n\tconst result = ( hashBasis >>> 0 ).toString( 36 );\n\n\tif ( length === undefined ) {\n\t\treturn result;\n\t}\n\n\tconst sliced = result.slice( -length );\n\treturn sliced.padStart( length, '0' );\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","export const capitalize = ( str: string ): string => {\n\treturn str.charAt( 0 ).toUpperCase() + str.slice( 1 );\n};\n","export const compareVersions = ( a: string | number, b: string | number ): number => {\n\tconst aParts = String( a || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\tconst bParts = String( b || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\n\tfor ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) {\n\t\tconst aVal = aParts[ i ] || 0;\n\t\tconst bVal = bParts[ i ] || 0;\n\t\tif ( aVal !== bVal ) {\n\t\t\treturn aVal - bVal;\n\t\t}\n\t}\n\treturn 0;\n};\n\nexport const isVersionLessThan = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) < 0;\n};\n\nexport const isVersionGreaterOrEqual = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) >= 0;\n};\n","export function hasProInstalled(): boolean {\n\treturn window.elementor?.helpers?.hasPro?.() ?? false;\n}\n\nexport function isProActive(): boolean {\n\tif ( ! hasProInstalled() ) {\n\t\treturn false;\n\t}\n\n\treturn window.elementorPro?.config?.isActive ?? false;\n}\n\nfunction getProVersion(): string {\n\treturn window.elementorPro?.config?.version ?? '0.0';\n}\n\nexport function isProAtLeast( targetVersion: string ): boolean {\n\tconst version = getProVersion();\n\n\tif ( ! version ) {\n\t\treturn false;\n\t}\n\n\tconst [ major, minor ] = version.split( '.' ).map( Number );\n\tconst [ targetMajor, targetMinor ] = targetVersion.split( '.' ).map( Number );\n\n\treturn major > targetMajor || ( major === targetMajor && minor >= targetMinor );\n}\n","type TranslateFunction = ( key: string, ...args: string[] ) => string;\n\ninterface CreateTranslateOptions {\n\tconfigKey: string;\n\tdefaultStrings?: Record< string, string >;\n}\n\nexport function createTranslate( { configKey, defaultStrings = {} }: CreateTranslateOptions ): TranslateFunction {\n\treturn ( key: string, ...args: string[] ): string => {\n\t\tconst appConfig = window.elementorAppConfig as\n\t\t\t| Record< string, { translations?: Record< string, string > } >\n\t\t\t| undefined;\n\t\tconst remoteStrings = appConfig?.[ configKey ]?.translations;\n\t\tconst strings: Record< string, string > = {\n\t\t\t...defaultStrings,\n\t\t\t...remoteStrings,\n\t\t};\n\n\t\tlet template = strings[ key ];\n\n\t\tif ( ! template ) {\n\t\t\treturn key;\n\t\t}\n\n\t\tfor ( let i = 0; i < args.length; i++ ) {\n\t\t\ttemplate = template.replace( `%${ i + 1 }$s`, args[ i ] );\n\t\t\ttemplate = template.replace( '%s', args[ i ] );\n\t\t}\n\n\t\treturn template;\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;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;A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AAA,EACR;AACD;","names":["import_react"]}
|
package/dist/index.mjs
CHANGED
|
@@ -98,6 +98,25 @@ function useDebounceState(options = {}) {
|
|
|
98
98
|
};
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
+
// src/use-debounced-callback.ts
|
|
102
|
+
import { useEffect as useEffect2, useMemo, useRef as useRef2 } from "react";
|
|
103
|
+
function useDebouncedCallback(callback, delay) {
|
|
104
|
+
const callbackRef = useRef2(callback);
|
|
105
|
+
useEffect2(() => {
|
|
106
|
+
callbackRef.current = callback;
|
|
107
|
+
}, [callback]);
|
|
108
|
+
const debounced = useMemo(
|
|
109
|
+
() => debounce((...args) => callbackRef.current(...args), delay),
|
|
110
|
+
[delay]
|
|
111
|
+
);
|
|
112
|
+
useEffect2(() => {
|
|
113
|
+
return () => {
|
|
114
|
+
debounced.cancel();
|
|
115
|
+
};
|
|
116
|
+
}, [debounced]);
|
|
117
|
+
return debounced;
|
|
118
|
+
}
|
|
119
|
+
|
|
101
120
|
// src/throttle.ts
|
|
102
121
|
function throttle(fn, wait, shouldExecuteIgnoredCalls = false) {
|
|
103
122
|
let timer = null;
|
|
@@ -293,6 +312,7 @@ export {
|
|
|
293
312
|
isVersionLessThan,
|
|
294
313
|
throttle,
|
|
295
314
|
useDebounceState,
|
|
315
|
+
useDebouncedCallback,
|
|
296
316
|
useSearchState
|
|
297
317
|
};
|
|
298
318
|
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
CHANGED
|
@@ -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/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts","../src/string-helpers.ts","../src/version.ts","../src/is-pro.ts","../src/translations.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","/* eslint-disable no-bitwise */\ntype 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\n// Inspired by:\n// https://github.com/darkskyapp/string-hash/blob/master/index.js\nexport function hashString( str: string, length?: number ): string {\n\tlet hashBasis = 5381;\n\n\tlet i = str.length;\n\twhile ( i ) {\n\t\thashBasis = ( hashBasis * 33 ) ^ str.charCodeAt( --i );\n\t}\n\n\t/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed\n\t * integers. Since we want the results to be always positive, convert the\n\t * signed int to an unsigned by doing an unsigned bitshift. */\n\tconst result = ( hashBasis >>> 0 ).toString( 36 );\n\n\tif ( length === undefined ) {\n\t\treturn result;\n\t}\n\n\tconst sliced = result.slice( -length );\n\treturn sliced.padStart( length, '0' );\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","export const capitalize = ( str: string ): string => {\n\treturn str.charAt( 0 ).toUpperCase() + str.slice( 1 );\n};\n","export const compareVersions = ( a: string | number, b: string | number ): number => {\n\tconst aParts = String( a || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\tconst bParts = String( b || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\n\tfor ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) {\n\t\tconst aVal = aParts[ i ] || 0;\n\t\tconst bVal = bParts[ i ] || 0;\n\t\tif ( aVal !== bVal ) {\n\t\t\treturn aVal - bVal;\n\t\t}\n\t}\n\treturn 0;\n};\n\nexport const isVersionLessThan = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) < 0;\n};\n\nexport const isVersionGreaterOrEqual = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) >= 0;\n};\n","export function hasProInstalled(): boolean {\n\treturn window.elementor?.helpers?.hasPro?.() ?? false;\n}\n\nexport function isProActive(): boolean {\n\tif ( ! hasProInstalled() ) {\n\t\treturn false;\n\t}\n\n\treturn window.elementorPro?.config?.isActive ?? false;\n}\n\nfunction getProVersion(): string {\n\treturn window.elementorPro?.config?.version ?? '0.0';\n}\n\nexport function isProAtLeast( targetVersion: string ): boolean {\n\tconst version = getProVersion();\n\n\tif ( ! version ) {\n\t\treturn false;\n\t}\n\n\tconst [ major, minor ] = version.split( '.' ).map( Number );\n\tconst [ targetMajor, targetMinor ] = targetVersion.split( '.' ).map( Number );\n\n\treturn major > targetMajor || ( major === targetMajor && minor >= targetMinor );\n}\n","type TranslateFunction = ( key: string, ...args: string[] ) => string;\n\ninterface CreateTranslateOptions {\n\tconfigKey: string;\n\tdefaultStrings?: Record< string, string >;\n}\n\nexport function createTranslate( { configKey, defaultStrings = {} }: CreateTranslateOptions ): TranslateFunction {\n\treturn ( key: string, ...args: string[] ): string => {\n\t\tconst appConfig = window.elementorAppConfig as\n\t\t\t| Record< string, { translations?: Record< string, string > } >\n\t\t\t| undefined;\n\t\tconst remoteStrings = appConfig?.[ configKey ]?.translations;\n\t\tconst strings: Record< string, string > = {\n\t\t\t...defaultStrings,\n\t\t\t...remoteStrings,\n\t\t};\n\n\t\tlet template = strings[ key ];\n\n\t\tif ( ! template ) {\n\t\t\treturn key;\n\t\t}\n\n\t\tfor ( let i = 0; i < args.length; i++ ) {\n\t\t\ttemplate = template.replace( `%${ i + 1 }$s`, args[ i ] );\n\t\t\ttemplate = template.replace( '%s', args[ i ] );\n\t\t}\n\n\t\treturn template;\n\t};\n}\n"],"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ources":["../src/errors/elementor-error.ts","../src/errors/create-error.ts","../src/errors/ensure-error.ts","../src/use-debounce-state.ts","../src/debounce.ts","../src/use-debounced-callback.ts","../src/throttle.ts","../src/encoding.ts","../src/hash.ts","../src/use-search-state.ts","../src/generate-unique-id.ts","../src/string-helpers.ts","../src/version.ts","../src/is-pro.ts","../src/translations.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","import { useEffect, useMemo, useRef } from 'react';\n\nimport { debounce } from './debounce';\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport function useDebouncedCallback< TArgs extends any[] >( callback: ( ...args: TArgs ) => void, delay: number ) {\n\tconst callbackRef = useRef( callback );\n\n\tuseEffect( () => {\n\t\tcallbackRef.current = callback;\n\t}, [ callback ] );\n\n\tconst debounced = useMemo(\n\t\t() => debounce( ( ...args: TArgs ) => callbackRef.current( ...args ), delay ),\n\t\t[ delay ]\n\t);\n\n\tuseEffect( () => {\n\t\treturn () => {\n\t\t\tdebounced.cancel();\n\t\t};\n\t}, [ debounced ] );\n\n\treturn debounced;\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","/* eslint-disable no-bitwise */\ntype 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\n// Inspired by:\n// https://github.com/darkskyapp/string-hash/blob/master/index.js\nexport function hashString( str: string, length?: number ): string {\n\tlet hashBasis = 5381;\n\n\tlet i = str.length;\n\twhile ( i ) {\n\t\thashBasis = ( hashBasis * 33 ) ^ str.charCodeAt( --i );\n\t}\n\n\t/* JavaScript does bitwise operations (like XOR, above) on 32-bit signed\n\t * integers. Since we want the results to be always positive, convert the\n\t * signed int to an unsigned by doing an unsigned bitshift. */\n\tconst result = ( hashBasis >>> 0 ).toString( 36 );\n\n\tif ( length === undefined ) {\n\t\treturn result;\n\t}\n\n\tconst sliced = result.slice( -length );\n\treturn sliced.padStart( length, '0' );\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","export const capitalize = ( str: string ): string => {\n\treturn str.charAt( 0 ).toUpperCase() + str.slice( 1 );\n};\n","export const compareVersions = ( a: string | number, b: string | number ): number => {\n\tconst aParts = String( a || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\tconst bParts = String( b || '0.0.0' )\n\t\t.split( '.' )\n\t\t.map( Number );\n\n\tfor ( let i = 0; i < Math.max( aParts.length, bParts.length ); i++ ) {\n\t\tconst aVal = aParts[ i ] || 0;\n\t\tconst bVal = bParts[ i ] || 0;\n\t\tif ( aVal !== bVal ) {\n\t\t\treturn aVal - bVal;\n\t\t}\n\t}\n\treturn 0;\n};\n\nexport const isVersionLessThan = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) < 0;\n};\n\nexport const isVersionGreaterOrEqual = ( a: string | number, b: string | number ): boolean => {\n\treturn compareVersions( a, b ) >= 0;\n};\n","export function hasProInstalled(): boolean {\n\treturn window.elementor?.helpers?.hasPro?.() ?? false;\n}\n\nexport function isProActive(): boolean {\n\tif ( ! hasProInstalled() ) {\n\t\treturn false;\n\t}\n\n\treturn window.elementorPro?.config?.isActive ?? false;\n}\n\nfunction getProVersion(): string {\n\treturn window.elementorPro?.config?.version ?? '0.0';\n}\n\nexport function isProAtLeast( targetVersion: string ): boolean {\n\tconst version = getProVersion();\n\n\tif ( ! version ) {\n\t\treturn false;\n\t}\n\n\tconst [ major, minor ] = version.split( '.' ).map( Number );\n\tconst [ targetMajor, targetMinor ] = targetVersion.split( '.' ).map( Number );\n\n\treturn major > targetMajor || ( major === targetMajor && minor >= targetMinor );\n}\n","type TranslateFunction = ( key: string, ...args: string[] ) => string;\n\ninterface CreateTranslateOptions {\n\tconfigKey: string;\n\tdefaultStrings?: Record< string, string >;\n}\n\nexport function createTranslate( { configKey, defaultStrings = {} }: CreateTranslateOptions ): TranslateFunction {\n\treturn ( key: string, ...args: string[] ): string => {\n\t\tconst appConfig = window.elementorAppConfig as\n\t\t\t| Record< string, { translations?: Record< string, string > } >\n\t\t\t| undefined;\n\t\tconst remoteStrings = appConfig?.[ configKey ]?.translations;\n\t\tconst strings: Record< string, string > = {\n\t\t\t...defaultStrings,\n\t\t\t...remoteStrings,\n\t\t};\n\n\t\tlet template = strings[ key ];\n\n\t\tif ( ! template ) {\n\t\t\treturn key;\n\t\t}\n\n\t\tfor ( let i = 0; i < args.length; i++ ) {\n\t\t\ttemplate = template.replace( `%${ i + 1 }$s`, args[ i ] );\n\t\t\ttemplate = template.replace( '%s', args[ i ] );\n\t\t}\n\n\t\treturn template;\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,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;A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
"name": "@elementor/utils",
|
|
3
3
|
"description": "This package contains utility functions that are being used across the Elementor packages",
|
|
4
|
-
"version": "4.2.0-
|
|
4
|
+
"version": "4.2.0-888",
|
|
5
5
|
"private": false,
|
|
6
6
|
"author": "Elementor Team",
|
|
7
7
|
"homepage": "https://elementor.com/",
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
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
|
+
export { useDebouncedCallback } from './use-debounced-callback';
|
|
4
5
|
export { debounce } from './debounce';
|
|
5
6
|
export { throttle } from './throttle';
|
|
6
7
|
export { encodeString, decodeString } from './encoding';
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import { debounce } from './debounce';
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
6
|
+
export function useDebouncedCallback< TArgs extends any[] >( callback: ( ...args: TArgs ) => void, delay: number ) {
|
|
7
|
+
const callbackRef = useRef( callback );
|
|
8
|
+
|
|
9
|
+
useEffect( () => {
|
|
10
|
+
callbackRef.current = callback;
|
|
11
|
+
}, [ callback ] );
|
|
12
|
+
|
|
13
|
+
const debounced = useMemo(
|
|
14
|
+
() => debounce( ( ...args: TArgs ) => callbackRef.current( ...args ), delay ),
|
|
15
|
+
[ delay ]
|
|
16
|
+
);
|
|
17
|
+
|
|
18
|
+
useEffect( () => {
|
|
19
|
+
return () => {
|
|
20
|
+
debounced.cancel();
|
|
21
|
+
};
|
|
22
|
+
}, [ debounced ] );
|
|
23
|
+
|
|
24
|
+
return debounced;
|
|
25
|
+
}
|