minimal-shared 0.0.5 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/hooks/index.d.ts +1 -0
- package/dist/hooks/use-cookies/index.d.ts +1 -0
- package/dist/hooks/use-cookies/use-cookies.d.ts +5 -5
- package/dist/hooks/use-cookies/use-cookies.js +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/utils/cookies/cookies.d.ts +17 -9
- package/dist/utils/cookies/cookies.js +1 -1
- package/dist/utils/cookies/index.d.ts +1 -1
- package/dist/utils/index.d.ts +2 -1
- package/dist/utils/index.js +1 -8
- package/dist/utils/transform-number/index.d.ts +1 -0
- package/dist/utils/transform-number/index.js +1 -0
- package/dist/utils/transform-number/transform-number.d.ts +31 -0
- package/dist/utils/transform-number/transform-number.js +1 -0
- package/package.json +6 -6
package/dist/hooks/index.d.ts
CHANGED
@@ -16,3 +16,4 @@ export { UseScrollOffsetTopReturn, useScrollOffsetTop } from './use-scroll-offse
|
|
16
16
|
export { UseCountdownSecondsReturn, useCountdownSeconds } from './use-countdown-seconds/use-countdown-seconds.js';
|
17
17
|
export { CopiedValue, CopyFn, UseCopyToClipboardReturn, useCopyToClipboard } from './use-copy-to-clipboard/use-copy-to-clipboard.js';
|
18
18
|
import 'react';
|
19
|
+
import '../utils/cookies/cookies.js';
|
@@ -1,11 +1,12 @@
|
|
1
|
+
import { CookieOptions } from '../../utils/cookies/cookies.js';
|
2
|
+
|
1
3
|
/**
|
2
4
|
* Custom hook to manage state with cookies.
|
3
5
|
*
|
6
|
+
* @template T
|
4
7
|
* @param {string} key - The key for the cookie.
|
5
8
|
* @param {T} initialState - The initial state value.
|
6
|
-
* @param {
|
7
|
-
* @param {boolean} [options.initializeWithValue=true] - Whether to initialize the cookie with the initial state value.
|
8
|
-
* @param {number} [options.daysUntilExpiration] - Number of days until the cookie expires.
|
9
|
+
* @param {UseCookiesOptions} [options] - Optional settings.
|
9
10
|
*
|
10
11
|
* @returns {UseCookiesReturn<T>} - An object containing:
|
11
12
|
* - `state`: The current state.
|
@@ -25,9 +26,8 @@
|
|
25
26
|
* </div>
|
26
27
|
* );
|
27
28
|
*/
|
28
|
-
type UseCookiesOptions = {
|
29
|
+
type UseCookiesOptions = CookieOptions & {
|
29
30
|
initializeWithValue?: boolean;
|
30
|
-
daysUntilExpiration?: number;
|
31
31
|
};
|
32
32
|
type UseCookiesReturn<T> = {
|
33
33
|
state: T;
|
@@ -1 +1 @@
|
|
1
|
-
import{useMemo as
|
1
|
+
import{useMemo as g,useState as h,useEffect as y,useCallback as f}from"react";function m(e){if(!e||typeof e!="string")return console.warn("Invalid cookie key provided"),null;try{let o=`${e}=`,t=decodeURIComponent(document.cookie).split("; ").find(d=>d.startsWith(o));if(!t)return null;let r=t.substring(o.length);try{return JSON.parse(r)}catch{return r}}catch(o){return console.error("Error retrieving cookie:",o),null}}function T(e,o,c){if(!e||typeof e!="string"){console.error("Invalid cookie key provided");return}let{daysUntilExpiration:n=0,sameSite:t="Strict",secure:r=!1,path:d="/",domain:s}=c??{};try{let a=encodeURIComponent(typeof o=="string"?o:JSON.stringify(o)),p=[`${e}=${a}`,`path=${d}`,t&&`SameSite=${t}`,r&&"Secure",s&&`domain=${s}`];if(n>0){let l=new Date(Date.now()+n*24*60*60*1e3);p.push(`expires=${l.toUTCString()}`)}document.cookie=p.filter(Boolean).join("; ")}catch(a){console.error("Error setting cookie:",a)}}function C(e,o){if(!e||typeof e!="string"){console.error("Invalid cookie key provided");return}let{path:c="/",domain:n}=o??{};try{let t=[`${e}=`,"expires=Thu, 01 Jan 1970 00:00:00 GMT",`path=${c}`,n&&`domain=${n}`,"Secure"];document.cookie=t.filter(Boolean).join("; ")}catch(t){console.error("Error removing cookie:",t)}}function $(e,o,c){let{initializeWithValue:n=!0,...t}=c??{},r=o&&typeof o=="object",[d,s]=h(o);y(()=>{let i=m(e);i?s(r?u=>({...u,...i}):i):o&&n&&T(e,o,t)},[]);let a=f(i=>{r?s(u=>{let k={...u,...i};return T(e,k,t),k}):(T(e,i,t),s(i))},[t,r,e]),p=f((i,u)=>{r&&a({[i]:u})},[r,a]),l=f(i=>{s(i??o),C(e)},[o,e]);return g(()=>({state:d,setState:a,setField:p,resetState:l}),[l,p,a,d])}export{$ as useCookies};
|
package/dist/index.d.ts
CHANGED
@@ -4,9 +4,10 @@ export { ChannelPalette, InputPalette, createPaletteChannel, hexToRgbChannel, va
|
|
4
4
|
export { hasKeys } from './utils/object/object.js';
|
5
5
|
export { uuidv4 } from './utils/uuidv4/uuidv4.js';
|
6
6
|
export { StateProps, mergeClasses } from './utils/classes/classes.js';
|
7
|
-
export { getCookie, removeCookie, setCookie } from './utils/cookies/cookies.js';
|
7
|
+
export { CookieOptions, getCookie, removeCookie, setCookie } from './utils/cookies/cookies.js';
|
8
8
|
export { isActiveLink } from './utils/active-link/active-link.js';
|
9
9
|
export { getStorage, localStorageAvailable, removeStorage, setStorage } from './utils/local-storage/local-storage.js';
|
10
|
+
export { InputValue, transformNumber, transformValue, transformValueOnBlur, transformValueOnChange } from './utils/transform-number/transform-number.js';
|
10
11
|
export { UseTabsReturn, useTabs } from './hooks/use-tabs/use-tabs.js';
|
11
12
|
export { UseBooleanReturn, useBoolean } from './hooks/use-boolean/use-boolean.js';
|
12
13
|
export { UsePopoverReturn, usePopover } from './hooks/use-popover/use-popover.js';
|
@@ -1,34 +1,42 @@
|
|
1
|
+
type CookieOptions = {
|
2
|
+
secure?: boolean;
|
3
|
+
daysUntilExpiration?: number;
|
4
|
+
sameSite?: 'Strict' | 'Lax' | 'None';
|
5
|
+
domain?: string;
|
6
|
+
path?: string;
|
7
|
+
};
|
1
8
|
/**
|
2
9
|
* Retrieves a cookie value by key.
|
3
10
|
*
|
4
11
|
* @param {string} key - The key of the cookie to retrieve.
|
5
|
-
* @returns {
|
12
|
+
* @returns {T | null} - The parsed value of the cookie, or null if not found or an error occurs.
|
6
13
|
*
|
7
14
|
* @example
|
8
|
-
* const user = getCookie('user');
|
15
|
+
* const user = getCookie<{ name: string, age: number }>('user');
|
9
16
|
* console.log(user); // { name: 'John', age: 30 }
|
10
17
|
*/
|
11
|
-
declare function getCookie(key: string):
|
18
|
+
declare function getCookie<T>(key: string): T | null;
|
12
19
|
/**
|
13
|
-
* Sets a cookie with a specified key, value, and
|
20
|
+
* Sets a cookie with a specified key, value, and options.
|
14
21
|
*
|
15
22
|
* @template T
|
16
23
|
* @param {string} key - The key of the cookie to set.
|
17
24
|
* @param {T} value - The value of the cookie to set.
|
18
|
-
* @param {
|
25
|
+
* @param {CookieOptions} [options] - The options for the cookie.
|
19
26
|
*
|
20
27
|
* @example
|
21
|
-
* setCookie('user', { name: 'John', age: 30 }, 7);
|
28
|
+
* setCookie('user', { name: 'John', age: 30 }, { daysUntilExpiration: 7, sameSite: 'Lax', secure: true });
|
22
29
|
*/
|
23
|
-
declare function setCookie<T>(key: string, value: T,
|
30
|
+
declare function setCookie<T>(key: string, value: T, options?: CookieOptions): void;
|
24
31
|
/**
|
25
32
|
* Removes a cookie by key.
|
26
33
|
*
|
27
34
|
* @param {string} key - The key of the cookie to remove.
|
35
|
+
* @param {Pick<CookieOptions, 'path' | 'domain'>} [options] - The options for the cookie removal.
|
28
36
|
*
|
29
37
|
* @example
|
30
38
|
* removeCookie('user');
|
31
39
|
*/
|
32
|
-
declare function removeCookie(key: string): void;
|
40
|
+
declare function removeCookie(key: string, options?: Pick<CookieOptions, 'path' | 'domain'>): void;
|
33
41
|
|
34
|
-
export { getCookie, removeCookie, setCookie };
|
42
|
+
export { type CookieOptions, getCookie, removeCookie, setCookie };
|
@@ -1 +1 @@
|
|
1
|
-
function
|
1
|
+
function l(o){if(!o||typeof o!="string")return console.warn("Invalid cookie key provided"),null;try{let e=`${o}=`,r=decodeURIComponent(document.cookie).split("; ").find(c=>c.startsWith(e));if(!r)return null;let n=r.substring(e.length);try{return JSON.parse(n)}catch{return n}}catch(e){return console.error("Error retrieving cookie:",e),null}}function u(o,e,i){if(!o||typeof o!="string"){console.error("Invalid cookie key provided");return}let{daysUntilExpiration:t=0,sameSite:r="Strict",secure:n=!1,path:c="/",domain:a}=i??{};try{let s=encodeURIComponent(typeof e=="string"?e:JSON.stringify(e)),d=[`${o}=${s}`,`path=${c}`,r&&`SameSite=${r}`,n&&"Secure",a&&`domain=${a}`];if(t>0){let p=new Date(Date.now()+t*24*60*60*1e3);d.push(`expires=${p.toUTCString()}`)}document.cookie=d.filter(Boolean).join("; ")}catch(s){console.error("Error setting cookie:",s)}}function k(o,e){if(!o||typeof o!="string"){console.error("Invalid cookie key provided");return}let{path:i="/",domain:t}=e??{};try{let r=[`${o}=`,"expires=Thu, 01 Jan 1970 00:00:00 GMT",`path=${i}`,t&&`domain=${t}`,"Secure"];document.cookie=r.filter(Boolean).join("; ")}catch(r){console.error("Error removing cookie:",r)}}export{l as getCookie,k as removeCookie,u as setCookie};
|
@@ -1 +1 @@
|
|
1
|
-
export { getCookie, removeCookie, setCookie } from './cookies.js';
|
1
|
+
export { CookieOptions, getCookie, removeCookie, setCookie } from './cookies.js';
|
package/dist/utils/index.d.ts
CHANGED
@@ -4,6 +4,7 @@ export { ChannelPalette, InputPalette, createPaletteChannel, hexToRgbChannel, va
|
|
4
4
|
export { hasKeys } from './object/object.js';
|
5
5
|
export { uuidv4 } from './uuidv4/uuidv4.js';
|
6
6
|
export { StateProps, mergeClasses } from './classes/classes.js';
|
7
|
-
export { getCookie, removeCookie, setCookie } from './cookies/cookies.js';
|
7
|
+
export { CookieOptions, getCookie, removeCookie, setCookie } from './cookies/cookies.js';
|
8
8
|
export { isActiveLink } from './active-link/active-link.js';
|
9
9
|
export { getStorage, localStorageAvailable, removeStorage, setStorage } from './local-storage/local-storage.js';
|
10
|
+
export { InputValue, transformNumber, transformValue, transformValueOnBlur, transformValueOnChange } from './transform-number/transform-number.js';
|
package/dist/utils/index.js
CHANGED
@@ -1,17 +1,10 @@
|
|
1
1
|
export * from './url';
|
2
|
-
|
3
2
|
export * from './font';
|
4
|
-
|
5
3
|
export * from './color';
|
6
|
-
|
7
4
|
export * from './object';
|
8
|
-
|
9
5
|
export * from './uuidv4';
|
10
|
-
|
11
6
|
export * from './classes';
|
12
|
-
|
13
7
|
export * from './cookies';
|
14
|
-
|
15
8
|
export * from './active-link';
|
16
|
-
|
17
9
|
export * from './local-storage';
|
10
|
+
export * from './transform-number';
|
@@ -0,0 +1 @@
|
|
1
|
+
export { InputValue, transformNumber, transformValue, transformValueOnBlur, transformValueOnChange } from './transform-number.js';
|
@@ -0,0 +1 @@
|
|
1
|
+
export * from './transform-number';
|
@@ -0,0 +1,31 @@
|
|
1
|
+
type InputValue = string | number | null | undefined;
|
2
|
+
/**
|
3
|
+
* Transforms the input value to a string.
|
4
|
+
*
|
5
|
+
* @param {InputValue} value - The value to transform.
|
6
|
+
* @param {string} [defaultValue=''] - The default value to return if the input value is null, undefined, or NaN.
|
7
|
+
* @returns {string} - The transformed string value.
|
8
|
+
*/
|
9
|
+
declare function transformValue(value: InputValue, defaultValue?: string): string;
|
10
|
+
/**
|
11
|
+
* Transforms the output value on change event.
|
12
|
+
*
|
13
|
+
* @param {string | number} value - The value to transform.
|
14
|
+
* @returns {string} - The cleaned and transformed value.
|
15
|
+
*/
|
16
|
+
declare function transformValueOnChange(value: string | number): string;
|
17
|
+
/**
|
18
|
+
* Transforms the output value on blur event.
|
19
|
+
*
|
20
|
+
* @param {InputValue} value - The value to transform.
|
21
|
+
* @param {string | number} [defaultValue=''] - The default value to return if the input value is null, undefined, or NaN.
|
22
|
+
* @returns {string | number} - The transformed numeric value or the default value.
|
23
|
+
*/
|
24
|
+
declare function transformValueOnBlur(value: InputValue, defaultValue?: string | number): string | number;
|
25
|
+
declare const transformNumber: {
|
26
|
+
onChange: typeof transformValueOnChange;
|
27
|
+
onBlur: typeof transformValueOnBlur;
|
28
|
+
value: typeof transformValue;
|
29
|
+
};
|
30
|
+
|
31
|
+
export { type InputValue, transformNumber, transformValue, transformValueOnBlur, transformValueOnChange };
|
@@ -0,0 +1 @@
|
|
1
|
+
function i(n,r=""){return n==null||typeof n=="number"&&Number.isNaN(n)?r:n.toString()}function o(n){let t=i(n).replace(/[^0-9.]/g,""),[e,...u]=t.split(".");return u.length>0?`${e}.${u.join("")}`:e}function s(n,r=""){if(n==null||typeof n=="number"&&Number.isNaN(n))return r;let t=parseFloat(n.toString());return Number.isNaN(t)?r:t}var a={onChange:o,onBlur:s,value:i};export{a as transformNumber,i as transformValue,s as transformValueOnBlur,o as transformValueOnChange};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "minimal-shared",
|
3
3
|
"author": "Minimals",
|
4
|
-
"version": "0.0
|
4
|
+
"version": "1.0.0",
|
5
5
|
"description": "Shared hooks and utils used by Mnimal UI and Zone UI.",
|
6
6
|
"keywords": [
|
7
7
|
"typescript",
|
@@ -53,13 +53,13 @@
|
|
53
53
|
}
|
54
54
|
},
|
55
55
|
"dependencies": {
|
56
|
-
"es-toolkit": "^1.
|
56
|
+
"es-toolkit": "^1.27.0"
|
57
57
|
},
|
58
58
|
"devDependencies": {
|
59
59
|
"@testing-library/jest-dom": "^6.6.3",
|
60
60
|
"@testing-library/react": "^16.0.1",
|
61
61
|
"@types/fs-extra": "^11.0.4",
|
62
|
-
"@types/node": "^22.
|
62
|
+
"@types/node": "^22.10.0",
|
63
63
|
"@types/react": "18.3.12",
|
64
64
|
"chalk": "^5.3.0",
|
65
65
|
"fast-glob": "^3.3.2",
|
@@ -68,13 +68,13 @@
|
|
68
68
|
"jsdom": "^25.0.1",
|
69
69
|
"react": "^18.3.1",
|
70
70
|
"tsup": "^8.3.5",
|
71
|
-
"typescript": "^5.
|
72
|
-
"vitest": "^2.1.
|
71
|
+
"typescript": "^5.7.2",
|
72
|
+
"vitest": "^2.1.6",
|
73
73
|
"internal-eslint-config": "1.0.0",
|
74
74
|
"internal-ts-config": "1.0.0"
|
75
75
|
},
|
76
76
|
"peerDependencies": {
|
77
|
-
"react": "^
|
77
|
+
"react": "^18"
|
78
78
|
},
|
79
79
|
"scripts": {
|
80
80
|
"dev": "NODE_OPTIONS='--max-old-space-size=16384' tsup --watch",
|