minimal-shared 0.0.3 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Minimal UI ([https://minimals.cc/](https://minimals.cc/))
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -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 +1,2 @@
1
1
  export { UseCookiesOptions, UseCookiesReturn, useCookies } from './use-cookies.js';
2
+ 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 {Object} [options] - Optional settings.
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 h,useState as g,useEffect as x,useCallback as T}from"react";function k(o){try{let e=`${o}=`,n=decodeURIComponent(document.cookie).split("; ");for(let r of n)if(r.startsWith(e)){let s=r.substring(e.length);try{return JSON.parse(s)}catch{return s}}}catch{return null}return null}function l(o,e,i=0){try{let n=encodeURIComponent(JSON.stringify(e)),r=`${o}=${n}; path=/`;if(i>0){let s=new Date(Date.now()+i*24*60*60*1e3);r+=`; expires=${s.toUTCString()}`}document.cookie=r}catch(n){console.error("Error while setting cookie:",n)}}function C(o){try{document.cookie=`${o}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`}catch(e){console.error("Error while removing cookie:",e)}}function b(o,e,i){let{initializeWithValue:n=!0,daysUntilExpiration:r}=i??{},s=e&&typeof e=="object",[d,a]=g(e);x(()=>{let t=k(o);t?a(s?c=>({...c,...t}):t):e&&n&&l(o,e,r)},[]);let u=T(t=>{s?a(c=>{let m={...c,...t};return l(o,m,r),m}):(l(o,t,r),a(t))},[s,o,r]),p=T((t,c)=>{s&&u({[t]:c})},[s,u]),f=T(t=>{a(t??e),C(o)},[e,o]);return h(()=>({state:d,setState:u,setField:p,resetState:f}),[f,p,u,d])}export{b as useCookies};
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 {any | null} - The parsed value of the cookie, or null if not found or an error occurs.
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): any | null;
18
+ declare function getCookie<T>(key: string): T | null;
12
19
  /**
13
- * Sets a cookie with a specified key, value, and expiration time.
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 {number} [daysUntilExpiration=0] - The number of days until the cookie expires. Defaults to session cookie if not set.
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, daysUntilExpiration?: number): void;
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 i(o){try{let e=`${o}=`,t=decodeURIComponent(document.cookie).split("; ");for(let r of t)if(r.startsWith(e)){let n=r.substring(e.length);try{return JSON.parse(n)}catch{return n}}}catch{return null}return null}function s(o,e,c=0){try{let t=encodeURIComponent(JSON.stringify(e)),r=`${o}=${t}; path=/`;if(c>0){let n=new Date(Date.now()+c*24*60*60*1e3);r+=`; expires=${n.toUTCString()}`}document.cookie=r}catch(t){console.error("Error while setting cookie:",t)}}function u(o){try{document.cookie=`${o}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`}catch(e){console.error("Error while removing cookie:",e)}}export{i as getCookie,u as removeCookie,s as setCookie};
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';
@@ -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';
@@ -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.3",
4
+ "version": "0.0.6",
5
5
  "description": "Shared hooks and utils used by Mnimal UI and Zone UI.",
6
6
  "keywords": [
7
7
  "typescript",
@@ -52,18 +52,6 @@
52
52
  "default": "./dist/utils/*/index.js"
53
53
  }
54
54
  },
55
- "scripts": {
56
- "dev": "NODE_OPTIONS='--max-old-space-size=16384' tsup --watch",
57
- "build": "pnpm test && NODE_OPTIONS='--max-old-space-size=16384' tsup",
58
- "test": "vitest run",
59
- "test:watch": "vitest",
60
- "lint": "eslint \"**/*.{js,jsx,ts,tsx}\"",
61
- "lint:fix": "eslint --fix \"**/*.{js,jsx,ts,tsx}\"",
62
- "lint:print": "npx eslint --print-config eslint.config.mjs > eslint-current-config.json",
63
- "clean": "rm -rf node_modules .turbo .next out dist build",
64
- "re:build": "pnpm clean && pnpm install && pnpm build",
65
- "tsc:print": "npx tsc --showConfig"
66
- },
67
55
  "dependencies": {
68
56
  "es-toolkit": "^1.26.1"
69
57
  },
@@ -77,15 +65,27 @@
77
65
  "fast-glob": "^3.3.2",
78
66
  "fs-extra": "^11.2.0",
79
67
  "glob": "^11.0.0",
80
- "internal-eslint-config": "workspace:*",
81
- "internal-ts-config": "workspace:*",
82
68
  "jsdom": "^25.0.1",
83
69
  "react": "^18.3.1",
84
70
  "tsup": "^8.3.5",
85
71
  "typescript": "^5.5.4",
86
- "vitest": "^2.1.4"
72
+ "vitest": "^2.1.4",
73
+ "internal-eslint-config": "1.0.0",
74
+ "internal-ts-config": "1.0.0"
87
75
  },
88
76
  "peerDependencies": {
89
77
  "react": "^17 || ^18"
78
+ },
79
+ "scripts": {
80
+ "dev": "NODE_OPTIONS='--max-old-space-size=16384' tsup --watch",
81
+ "build": "pnpm test && NODE_OPTIONS='--max-old-space-size=16384' tsup",
82
+ "test": "vitest run",
83
+ "test:watch": "vitest",
84
+ "lint": "eslint \"**/*.{js,jsx,ts,tsx}\"",
85
+ "lint:fix": "eslint --fix \"**/*.{js,jsx,ts,tsx}\"",
86
+ "lint:print": "npx eslint --print-config eslint.config.mjs > eslint-current-config.json",
87
+ "clean": "rm -rf node_modules .turbo .next out dist build",
88
+ "re:build": "pnpm clean && pnpm install && pnpm build",
89
+ "tsc:print": "npx tsc --showConfig"
90
90
  }
91
- }
91
+ }