@pushwoosh/kit-helpers 4.8.1 → 4.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ type NumberFormat = 'spaced' | 'short';
2
+ export declare const formatNumber: (number: number, format: NumberFormat) => string;
3
+ export {};
@@ -0,0 +1,21 @@
1
+ const getNumberAsSpacedString = number => number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ');
2
+ const getNumberAsShortString = num => {
3
+ if (num < 10000) {
4
+ return getNumberAsSpacedString(num);
5
+ }
6
+ const formatter = new Intl.NumberFormat('en', {
7
+ notation: 'compact',
8
+ maximumFractionDigits: 1
9
+ });
10
+ return formatter.format(num);
11
+ };
12
+ export const formatNumber = (number, format) => {
13
+ switch (format) {
14
+ case 'spaced':
15
+ return getNumberAsSpacedString(number);
16
+ case 'short':
17
+ return getNumberAsShortString(number);
18
+ default:
19
+ return number.toString();
20
+ }
21
+ };
@@ -1 +1,47 @@
1
- export default function usePersistentFunction<T extends (...args: any[]) => any>(callback: T): T;
1
+ /**
2
+ * Creates a stable function reference that always calls the latest version of a callback.
3
+ *
4
+ * This hook is useful when you need to pass a callback to a child component or as a dependency
5
+ * to another hook, but want to avoid unnecessary re-renders when the callback changes.
6
+ *
7
+ * Unlike using plain `useCallback`, this approach ensures that the latest version of the
8
+ * callback is always used, even if the reference remains stable.
9
+ *
10
+ * @param callback - The function that should be persisted
11
+ * @returns A stable function reference that will always call the most recent version of the provided callback
12
+ *
13
+ * @example
14
+ * // Using with event handlers
15
+ * const MyComponent = () => {
16
+ * const [count, setCount] = useState(0);
17
+ *
18
+ * // This function reference remains stable across renders
19
+ * // but will always use the current value of `count`
20
+ * const handleClick = usePersistentFunction(() => {
21
+ * console.log(`Current count: ${count}`);
22
+ * });
23
+ *
24
+ * return <ChildComponent onClick={handleClick} />;
25
+ * };
26
+ *
27
+ * @example
28
+ * // Using with cleanup functions
29
+ * const MyComponent = () => {
30
+ * const [resource, setResource] = useState(null);
31
+ *
32
+ * // The cleanup function remains stable but always uses the latest resource
33
+ * const cleanup = usePersistentFunction(() => {
34
+ * if (resource) {
35
+ * resource.dispose();
36
+ * }
37
+ * });
38
+ *
39
+ * useEffect(() => {
40
+ * // Some setup code...
41
+ * return cleanup;
42
+ * }, []); // No need to include cleanup in dependencies
43
+ *
44
+ * return <div>...</div>;
45
+ * };
46
+ */
47
+ export default function usePersistentFunction<Args extends any[], Return>(callback: (...args: Args) => Return): (...args: Args) => Return;
@@ -1,6 +1,55 @@
1
1
  import { useCallback, useRef } from 'react';
2
+ /**
3
+ * Creates a stable function reference that always calls the latest version of a callback.
4
+ *
5
+ * This hook is useful when you need to pass a callback to a child component or as a dependency
6
+ * to another hook, but want to avoid unnecessary re-renders when the callback changes.
7
+ *
8
+ * Unlike using plain `useCallback`, this approach ensures that the latest version of the
9
+ * callback is always used, even if the reference remains stable.
10
+ *
11
+ * @param callback - The function that should be persisted
12
+ * @returns A stable function reference that will always call the most recent version of the provided callback
13
+ *
14
+ * @example
15
+ * // Using with event handlers
16
+ * const MyComponent = () => {
17
+ * const [count, setCount] = useState(0);
18
+ *
19
+ * // This function reference remains stable across renders
20
+ * // but will always use the current value of `count`
21
+ * const handleClick = usePersistentFunction(() => {
22
+ * console.log(`Current count: ${count}`);
23
+ * });
24
+ *
25
+ * return <ChildComponent onClick={handleClick} />;
26
+ * };
27
+ *
28
+ * @example
29
+ * // Using with cleanup functions
30
+ * const MyComponent = () => {
31
+ * const [resource, setResource] = useState(null);
32
+ *
33
+ * // The cleanup function remains stable but always uses the latest resource
34
+ * const cleanup = usePersistentFunction(() => {
35
+ * if (resource) {
36
+ * resource.dispose();
37
+ * }
38
+ * });
39
+ *
40
+ * useEffect(() => {
41
+ * // Some setup code...
42
+ * return cleanup;
43
+ * }, []); // No need to include cleanup in dependencies
44
+ *
45
+ * return <div>...</div>;
46
+ * };
47
+ */
2
48
  export default function usePersistentFunction(callback) {
3
- const ref = useRef(callback);
4
- ref.current = callback;
5
- return useCallback((...args) => ref.current(...args), []);
49
+ // Store the latest callback in a ref
50
+ const callbackRef = useRef(callback);
51
+ // Update the ref to the latest callback on each render
52
+ callbackRef.current = callback;
53
+ // Create a stable function reference that calls the latest callback
54
+ return useCallback((...args) => callbackRef.current(...args), []);
6
55
  }
package/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './grid';
2
2
  export * from './expand';
3
3
  export * from './hooks/useClickAway';
4
+ export { formatNumber } from './formatNumber';
4
5
  export { default as usePersistentFunction } from './hooks/usePersistentFunction';
5
6
  export { default as useEffectOnce } from './hooks/useEffectOnce';
6
7
  export { default as useMountedState } from './hooks/useMountedState';
package/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './grid';
2
2
  export * from './expand';
3
3
  export * from './hooks/useClickAway';
4
+ export { formatNumber } from './formatNumber';
4
5
  export { default as usePersistentFunction } from './hooks/usePersistentFunction';
5
6
  export { default as useEffectOnce } from './hooks/useEffectOnce';
6
7
  export { default as useMountedState } from './hooks/useMountedState';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pushwoosh/kit-helpers",
3
- "version": "4.8.1",
3
+ "version": "4.8.3",
4
4
  "description": "React component for build Pushwoosh products",
5
5
  "main": "index.js",
6
6
  "module": "index.js",