@sohanemon/utils 5.0.7 → 5.0.8
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/components/html-injector.d.ts +2 -2
- package/dist/components/html-injector.js +13 -13
- package/dist/components/index.d.ts +1 -1
- package/dist/components/index.js +1 -1
- package/dist/functions/index.d.ts +2 -239
- package/dist/functions/index.js +2 -417
- package/dist/functions/shield.d.ts +18 -0
- package/dist/functions/shield.js +14 -0
- package/dist/functions/utils.d.ts +240 -0
- package/dist/functions/utils.js +417 -0
- package/dist/hooks/async.js +1 -1
- package/dist/hooks/index.js +1 -1
- package/dist/types/guards.d.ts +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import * as React from
|
|
1
|
+
import * as React from 'react';
|
|
2
2
|
/**
|
|
3
3
|
* Props for the HtmlInjector component
|
|
4
4
|
*/
|
|
5
|
-
type HtmlInjectorProps = Omit<React.ComponentProps<
|
|
5
|
+
type HtmlInjectorProps = Omit<React.ComponentProps<'div'>, 'dangerouslySetInnerHTML'> & {
|
|
6
6
|
/** The HTML content to inject and render */
|
|
7
7
|
html: string;
|
|
8
8
|
/**
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
'use client';
|
|
2
2
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
-
import * as React from
|
|
4
|
-
import { cn } from
|
|
3
|
+
import * as React from 'react';
|
|
4
|
+
import { cn } from '../functions';
|
|
5
5
|
/**
|
|
6
6
|
* A robust component for safely injecting and rendering HTML content with optional script execution.
|
|
7
7
|
*
|
|
@@ -46,11 +46,11 @@ export function HtmlInjector({ className, html, sanitize = false, executeScripts
|
|
|
46
46
|
if (!executeScripts || !html)
|
|
47
47
|
return;
|
|
48
48
|
try {
|
|
49
|
-
const tempContainer = document.createElement(
|
|
49
|
+
const tempContainer = document.createElement('div');
|
|
50
50
|
tempContainer.innerHTML = html;
|
|
51
|
-
const scripts = tempContainer.querySelectorAll(
|
|
51
|
+
const scripts = tempContainer.querySelectorAll('script');
|
|
52
52
|
scripts.forEach((oldScript) => {
|
|
53
|
-
const newScript = document.createElement(
|
|
53
|
+
const newScript = document.createElement('script');
|
|
54
54
|
// HACK: Copy text content
|
|
55
55
|
if (oldScript.textContent) {
|
|
56
56
|
newScript.textContent = oldScript.textContent;
|
|
@@ -60,14 +60,14 @@ export function HtmlInjector({ className, html, sanitize = false, executeScripts
|
|
|
60
60
|
newScript.setAttribute(attr.name, attr.value);
|
|
61
61
|
});
|
|
62
62
|
newScript.onerror = (error) => {
|
|
63
|
-
console.error(
|
|
63
|
+
console.error('Script injection error:', error);
|
|
64
64
|
};
|
|
65
65
|
document.body.appendChild(newScript);
|
|
66
66
|
injectedScriptsRef.current.push(newScript);
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
69
|
catch (error) {
|
|
70
|
-
console.error(
|
|
70
|
+
console.error('HTML injection error:', error);
|
|
71
71
|
}
|
|
72
72
|
}, [html, executeScripts]);
|
|
73
73
|
React.useEffect(() => {
|
|
@@ -81,16 +81,16 @@ export function HtmlInjector({ className, html, sanitize = false, executeScripts
|
|
|
81
81
|
}, []);
|
|
82
82
|
const processedHtml = React.useMemo(() => {
|
|
83
83
|
if (!html)
|
|
84
|
-
return
|
|
84
|
+
return '';
|
|
85
85
|
if (sanitize) {
|
|
86
86
|
// Basic sanitization - remove potentially dangerous elements
|
|
87
|
-
const container = document.createElement(
|
|
87
|
+
const container = document.createElement('div');
|
|
88
88
|
container.innerHTML = html;
|
|
89
89
|
// Remove script tags if sanitize is enabled
|
|
90
|
-
container.querySelectorAll(
|
|
90
|
+
container.querySelectorAll('script').forEach((script) => script.remove());
|
|
91
91
|
// Remove potentially dangerous attributes
|
|
92
|
-
const dangerousAttrs = [
|
|
93
|
-
container.querySelectorAll(
|
|
92
|
+
const dangerousAttrs = ['onclick', 'onload', 'onerror', 'onmouseover'];
|
|
93
|
+
container.querySelectorAll('*').forEach((el) => {
|
|
94
94
|
dangerousAttrs.forEach((attr) => {
|
|
95
95
|
if (el.hasAttribute(attr)) {
|
|
96
96
|
el.removeAttribute(attr);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
export { Icon as Iconify } from '@iconify/react';
|
|
2
|
-
export { MediaWrapper } from './media-wrapper';
|
|
3
2
|
export { HtmlInjector } from './html-injector';
|
|
3
|
+
export { MediaWrapper } from './media-wrapper';
|
|
4
4
|
export { ResponsiveIndicator, ResponsiveIndicator as TailwindIndicator, } from './responsive-indicator';
|
package/dist/components/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
'use client';
|
|
2
2
|
export { Icon as Iconify } from '@iconify/react';
|
|
3
|
-
export { MediaWrapper } from './media-wrapper';
|
|
4
3
|
export { HtmlInjector } from './html-injector';
|
|
4
|
+
export { MediaWrapper } from './media-wrapper';
|
|
5
5
|
export { ResponsiveIndicator, ResponsiveIndicator as TailwindIndicator, } from './responsive-indicator';
|
|
@@ -1,241 +1,4 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Utility to merge class names with Tailwind CSS and additional custom merging logic.
|
|
3
|
-
*
|
|
4
|
-
* @param {...ClassValue[]} inputs - Class names to merge.
|
|
5
|
-
* @returns {string} - A string of merged class names.
|
|
6
|
-
*/
|
|
7
|
-
import { type ClassValue } from 'clsx';
|
|
8
|
-
import type * as React from 'react';
|
|
9
1
|
export * from './cookie';
|
|
10
2
|
export * from './object';
|
|
11
|
-
export
|
|
12
|
-
|
|
13
|
-
* @deprecated Use isLinkActive instead.
|
|
14
|
-
*
|
|
15
|
-
* Determines if a navigation link is active based on the current path.
|
|
16
|
-
*
|
|
17
|
-
* @param href - The target URL.
|
|
18
|
-
* @param path - The current browser path.
|
|
19
|
-
* @returns - True if the navigation is active, false otherwise.
|
|
20
|
-
*/
|
|
21
|
-
export declare function isNavActive(href: string, path: string): boolean;
|
|
22
|
-
/**
|
|
23
|
-
* Checks if a link is active, considering optional localization prefixes.
|
|
24
|
-
*
|
|
25
|
-
* @param {Object} params - Parameters object.
|
|
26
|
-
* @param {string} params.path - The target path of the link.
|
|
27
|
-
* @param {string} params.currentPath - The current browser path.
|
|
28
|
-
* @param {string[]} [params.locales=['en', 'es', 'de', 'zh', 'bn', 'fr', 'it', 'nl']] - Supported locale prefixes.
|
|
29
|
-
* @returns {boolean} - True if the link is active, false otherwise.
|
|
30
|
-
*/
|
|
31
|
-
export declare function isLinkActive({ path, currentPath, locales, exact, }: {
|
|
32
|
-
path: string;
|
|
33
|
-
currentPath: string;
|
|
34
|
-
locales?: string[];
|
|
35
|
-
exact?: boolean;
|
|
36
|
-
}): boolean;
|
|
37
|
-
/**
|
|
38
|
-
* Cleans a file path by removing the `/public/` prefix if present.
|
|
39
|
-
*
|
|
40
|
-
* @param src - The source path to clean.
|
|
41
|
-
* @returns - The cleaned path.
|
|
42
|
-
*/
|
|
43
|
-
export declare function cleanSrc(src: string): string;
|
|
44
|
-
/**
|
|
45
|
-
* Smoothly scrolls to the top or bottom of a specified container.
|
|
46
|
-
*
|
|
47
|
-
* @param containerSelector - The CSS selector or React ref for the container.
|
|
48
|
-
* @param to - Specifies whether to scroll to the top or bottom.
|
|
49
|
-
*/
|
|
50
|
-
export declare const scrollTo: (containerSelector: string | React.RefObject<HTMLDivElement>, to: "top" | "bottom") => void;
|
|
51
|
-
/**
|
|
52
|
-
* Copies a given string to the clipboard.
|
|
53
|
-
*
|
|
54
|
-
* @param value - The value to copy to the clipboard.
|
|
55
|
-
* @param [onSuccess=() => {}] - Optional callback executed after successful copy.
|
|
56
|
-
*/
|
|
57
|
-
export declare const copyToClipboard: (value: string, onSuccess?: () => void) => void;
|
|
58
|
-
/**
|
|
59
|
-
* Converts camelCase, PascalCase, kebab-case, snake_case into normal case.
|
|
60
|
-
*
|
|
61
|
-
* @param inputString - The string need to be converted into normal case
|
|
62
|
-
* @returns - Normal Case
|
|
63
|
-
*/
|
|
64
|
-
export declare function convertToNormalCase(inputString: string): string;
|
|
65
|
-
/**
|
|
66
|
-
* Converts a string to a URL-friendly slug by trimming, converting to lowercase,
|
|
67
|
-
* replacing diacritics, removing invalid characters, and replacing spaces with hyphens.
|
|
68
|
-
* @param {string} [str] - The input string to convert.
|
|
69
|
-
* @returns {string} The generated slug.
|
|
70
|
-
* @example
|
|
71
|
-
* convertToSlug("Hello World!"); // "hello-world"
|
|
72
|
-
* convertToSlug("Déjà Vu"); // "deja-vu"
|
|
73
|
-
*/
|
|
74
|
-
export declare const convertToSlug: (str: string) => string;
|
|
75
|
-
/**
|
|
76
|
-
* Checks if the code is running in a server-side environment.
|
|
77
|
-
*
|
|
78
|
-
* @returns - True if the code is executed in SSR (Server-Side Rendering) context, false otherwise
|
|
79
|
-
*/
|
|
80
|
-
export declare const isSSR: boolean;
|
|
81
|
-
/**
|
|
82
|
-
* Converts an SVG string to a Base64-encoded string.
|
|
83
|
-
*
|
|
84
|
-
* @param str - The SVG string to encode
|
|
85
|
-
* @returns - Base64-encoded string representation of the SVG
|
|
86
|
-
*/
|
|
87
|
-
export declare const svgToBase64: (str: string) => string;
|
|
88
|
-
/**
|
|
89
|
-
* Pauses execution for the specified time.
|
|
90
|
-
*
|
|
91
|
-
* @param time - Time in milliseconds to sleep (default is 1000ms)
|
|
92
|
-
* @returns - A Promise that resolves after the specified time
|
|
93
|
-
*/
|
|
94
|
-
export declare const sleep: (time?: number) => Promise<unknown>;
|
|
95
|
-
type DebouncedFunction<F extends (...args: any[]) => any> = {
|
|
96
|
-
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
97
|
-
readonly isPending: boolean;
|
|
98
|
-
};
|
|
99
|
-
/**
|
|
100
|
-
* Creates a debounced function that delays invoking the provided function until
|
|
101
|
-
* after the specified `wait` time has elapsed since the last invocation.
|
|
102
|
-
*
|
|
103
|
-
* If the `immediate` option is set to `true`, the function will be invoked immediately
|
|
104
|
-
* on the leading edge of the wait interval. Subsequent calls during the wait interval
|
|
105
|
-
* will reset the timer but not invoke the function until the interval elapses again.
|
|
106
|
-
*
|
|
107
|
-
* The returned function includes the `isPending` property to check if the debounce
|
|
108
|
-
* timer is currently active.
|
|
109
|
-
*
|
|
110
|
-
* @typeParam F - The type of the function to debounce.
|
|
111
|
-
*
|
|
112
|
-
* @param function_ - The function to debounce.
|
|
113
|
-
* @param wait - The number of milliseconds to delay (default is 100ms).
|
|
114
|
-
* @param options - An optional object with the following properties:
|
|
115
|
-
* - `immediate` (boolean): If `true`, invokes the function on the leading edge
|
|
116
|
-
* of the wait interval instead of the trailing edge.
|
|
117
|
-
*
|
|
118
|
-
* @returns A debounced version of the provided function, enhanced with the `isPending` property.
|
|
119
|
-
*
|
|
120
|
-
* @throws {TypeError} If the first parameter is not a function.
|
|
121
|
-
* @throws {RangeError} If the `wait` parameter is negative.
|
|
122
|
-
*
|
|
123
|
-
* @example
|
|
124
|
-
* const log = debounce((message: string) => console.log(message), 200);
|
|
125
|
-
* log('Hello'); // Logs "Hello" after 200ms if no other call is made.
|
|
126
|
-
* console.log(log.isPending); // true if the timer is active.
|
|
127
|
-
*/
|
|
128
|
-
export declare function debounce<F extends (...args: any[]) => any>(function_: F, wait?: number, options?: {
|
|
129
|
-
immediate: boolean;
|
|
130
|
-
}): DebouncedFunction<F>;
|
|
131
|
-
type ThrottledFunction<F extends (...args: any[]) => any> = {
|
|
132
|
-
(...args: Parameters<F>): ReturnType<F> | undefined;
|
|
133
|
-
readonly isPending: boolean;
|
|
134
|
-
};
|
|
135
|
-
/**
|
|
136
|
-
* Creates a throttled function that invokes the provided function at most once
|
|
137
|
-
* every `wait` milliseconds.
|
|
138
|
-
*
|
|
139
|
-
* If the `leading` option is set to `true`, the function will be invoked immediately
|
|
140
|
-
* on the leading edge of the throttle interval. If the `trailing` option is set to `true`,
|
|
141
|
-
* the function will also be invoked at the end of the throttle interval if additional
|
|
142
|
-
* calls were made during the interval.
|
|
143
|
-
*
|
|
144
|
-
* The returned function includes the `isPending` property to check if the throttle
|
|
145
|
-
* timer is currently active.
|
|
146
|
-
*
|
|
147
|
-
* @typeParam F - The type of the function to throttle.
|
|
148
|
-
*
|
|
149
|
-
* @param function_ - The function to throttle.
|
|
150
|
-
* @param wait - The number of milliseconds to wait between invocations (default is 100ms).
|
|
151
|
-
* @param options - An optional object with the following properties:
|
|
152
|
-
* - `leading` (boolean): If `true`, invokes the function on the leading edge of the interval.
|
|
153
|
-
* - `trailing` (boolean): If `true`, invokes the function on the trailing edge of the interval.
|
|
154
|
-
*
|
|
155
|
-
* @returns A throttled version of the provided function, enhanced with the `isPending` property.
|
|
156
|
-
*
|
|
157
|
-
* @throws {TypeError} If the first parameter is not a function.
|
|
158
|
-
* @throws {RangeError} If the `wait` parameter is negative.
|
|
159
|
-
*
|
|
160
|
-
* @example
|
|
161
|
-
* const log = throttle((message: string) => console.log(message), 200);
|
|
162
|
-
* log('Hello'); // Logs "Hello" immediately if leading is true.
|
|
163
|
-
* console.log(log.isPending); // true if the timer is active.
|
|
164
|
-
*/
|
|
165
|
-
export declare function throttle<F extends (...args: any[]) => any>(function_: F, wait?: number, options?: {
|
|
166
|
-
leading?: boolean;
|
|
167
|
-
trailing?: boolean;
|
|
168
|
-
}): ThrottledFunction<F>;
|
|
169
|
-
/**
|
|
170
|
-
* Formats a string by replacing each '%s' placeholder with the corresponding argument.
|
|
171
|
-
* This function mimics the basic behavior of C's printf for %s substitution.
|
|
172
|
-
*
|
|
173
|
-
* It supports both calls like `printf(format, ...args)` and `printf(format, argsArray)`.
|
|
174
|
-
*
|
|
175
|
-
* @param format - The format string containing '%s' placeholders.
|
|
176
|
-
* @param args - The values to substitute into the placeholders, either as separate arguments or as a single array.
|
|
177
|
-
* @returns The formatted string with all '%s' replaced by the provided arguments.
|
|
178
|
-
*
|
|
179
|
-
* @example
|
|
180
|
-
* ```ts
|
|
181
|
-
* const message = printf("%s love %s", "I", "Bangladesh");
|
|
182
|
-
* // message === "I love Bangladesh"
|
|
183
|
-
*
|
|
184
|
-
* const arr = ["I", "Bangladesh"];
|
|
185
|
-
* const message2 = printf("%s love %s", arr);
|
|
186
|
-
* // message2 === "I love Bangladesh"
|
|
187
|
-
*
|
|
188
|
-
* // If there are too few arguments:
|
|
189
|
-
* const incomplete = printf("Bangladesh is %s %s", "beautiful");
|
|
190
|
-
* // incomplete === "Bangladesh is beautiful"
|
|
191
|
-
* ```
|
|
192
|
-
*/
|
|
193
|
-
export declare function printf(format: string, ...args: unknown[]): string;
|
|
194
|
-
/**
|
|
195
|
-
* Merges multiple refs into a single ref callback.
|
|
196
|
-
*
|
|
197
|
-
* @param refs - An array of refs to merge.
|
|
198
|
-
*
|
|
199
|
-
* @returns - A function that updates the merged ref with the provided value.
|
|
200
|
-
*/
|
|
201
|
-
export type MergeRefs = <T>(...refs: Array<React.Ref<T> | undefined>) => React.RefCallback<T>;
|
|
202
|
-
export declare const mergeRefs: MergeRefs;
|
|
203
|
-
/**
|
|
204
|
-
* Navigates to the specified client-side hash without ssr.
|
|
205
|
-
* use `scroll-margin-top` with css to add margins
|
|
206
|
-
*
|
|
207
|
-
* @param id - The ID of the element without # to navigate to.
|
|
208
|
-
*
|
|
209
|
-
* @example goToClientSideHash('my-element');
|
|
210
|
-
*/
|
|
211
|
-
export declare function goToClientSideHash(id: string, opts?: ScrollIntoViewOptions): void;
|
|
212
|
-
/**
|
|
213
|
-
* Escapes a string for use in a regular expression.
|
|
214
|
-
*
|
|
215
|
-
* @param str - The string to escape
|
|
216
|
-
* @returns - The escaped string
|
|
217
|
-
*
|
|
218
|
-
* @example
|
|
219
|
-
* const escapedString = escapeRegExp('Hello, world!');
|
|
220
|
-
* // escapedString === 'Hello\\, world!'
|
|
221
|
-
*/
|
|
222
|
-
export declare function escapeRegExp(str: string): string;
|
|
223
|
-
/**
|
|
224
|
-
* Normalizes a string by:
|
|
225
|
-
* - Applying Unicode normalization (NFC)
|
|
226
|
-
* - Optionally removing diacritic marks (accents)
|
|
227
|
-
* - Optionally trimming leading/trailing non-alphanumeric characters
|
|
228
|
-
* - Optionally converting to lowercase
|
|
229
|
-
*
|
|
230
|
-
* @param str - The string to normalize
|
|
231
|
-
* @param options - Normalization options
|
|
232
|
-
* @param options.lowercase - Whether to convert the result to lowercase (default: true)
|
|
233
|
-
* @param options.removeAccents - Whether to remove diacritic marks like accents (default: true)
|
|
234
|
-
* @param options.removeNonAlphanumeric - Whether to trim non-alphanumeric characters from the edges (default: true)
|
|
235
|
-
* @returns The normalized string
|
|
236
|
-
*/
|
|
237
|
-
export declare function normalizeText(str?: string | null, options?: {
|
|
238
|
-
lowercase?: boolean;
|
|
239
|
-
removeAccents?: boolean;
|
|
240
|
-
removeNonAlphanumeric?: boolean;
|
|
241
|
-
}): string;
|
|
3
|
+
export * from './shield';
|
|
4
|
+
export * from './utils';
|