@minsize/utils 0.0.2 → 0.0.4
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/README.md +20 -34
- package/index.d.ts +22 -1
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -7,38 +7,24 @@ npm i @minsize/utils
|
|
|
7
7
|
yarn add @minsize/utils
|
|
8
8
|
```
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
|
30
|
-
| -------------- | -------------------------------------------------------------------------------------------------- |
|
|
31
|
-
| `chunks` | Splits an array into pieces of the given size. |
|
|
32
|
-
| `clamp` | Limits the number to the specified minimum and maximum value. |
|
|
33
|
-
| `decWord` | The function returns a string representing the correct ending of the word depending on the number. |
|
|
34
|
-
| `alignTo` | The function returns an aligned number. |
|
|
35
|
-
| `toShort` | The function returns a string representing the number in short form. |
|
|
36
|
-
| `timeAgo` | The function returns a string describing the time elapsed since timestamp. |
|
|
37
|
-
| `formatNumber` | Formats a number into a delimited string. |
|
|
38
|
-
| `shuffle` | Shuffles the elements of an array in random order. |
|
|
39
|
-
| `random` | Generates a random number within the specified range. |
|
|
40
|
-
| `isType` | Checks if the value is of the specified type. |
|
|
41
|
-
| `omit` | Returns a new object with no specified keys. |
|
|
42
|
-
| `pick` | Returns a new object with the selected keys. |
|
|
43
|
-
| `sleep` | Waits for the specified number of milliseconds. |
|
|
10
|
+
| Function | Description |
|
|
11
|
+
| --------------------- | ----------------------------------------------------------------------------------------------------------------------------- |
|
|
12
|
+
| `chunks` | Splits an array into pieces of the given size. |
|
|
13
|
+
| `clamp` | Limits the number to the specified minimum and maximum value. |
|
|
14
|
+
| `decWord` | The function returns a string representing the correct ending of the word depending on the number. |
|
|
15
|
+
| `alignTo` | The function returns an aligned number. |
|
|
16
|
+
| `toShort` | The function returns a string representing the number in short form. |
|
|
17
|
+
| `timeAgo` | The function returns a string describing the time elapsed since timestamp. |
|
|
18
|
+
| `formatNumber` | Formats a number into a delimited string. |
|
|
19
|
+
| `shuffle` | Shuffles the elements of an array in random order. |
|
|
20
|
+
| `random` | Generates a random number within the specified range. |
|
|
21
|
+
| `isType` | Checks if the value is of the specified type. |
|
|
22
|
+
| `omit` | Returns a new object with no specified keys. |
|
|
23
|
+
| `pick` | Returns a new object with the selected keys. |
|
|
24
|
+
| `sleep` | Waits for the specified number of milliseconds. |
|
|
25
|
+
| `copyText` | The function allows you to copy text to the clipboard. |
|
|
26
|
+
| `createLinksFromText` | The function returns an array containing text fragments and the results of the callback call for each block. |
|
|
27
|
+
| `HSVtoRGB` | The function converts color from HSV color model (hue, saturation, brightness) to RGB color model (red, green, blue). |
|
|
28
|
+
| `RGBtoHEX` | The function converts color from the RGB color model (red, green, blue) to hexadecimal format (HEX). |
|
|
29
|
+
| `RGBtoHSV` | The function converts color from the RGB (red, green, blue) color model to the HSV (hue, saturation, brightness) color model. |
|
|
44
30
|
|
package/index.d.ts
CHANGED
|
@@ -73,4 +73,25 @@ declare function pick<T extends object, K extends keyof T>(object: T, keys: K[])
|
|
|
73
73
|
*/
|
|
74
74
|
declare const sleep: (time: number) => Promise<unknown>;
|
|
75
75
|
|
|
76
|
-
|
|
76
|
+
/**
|
|
77
|
+
*
|
|
78
|
+
* @example
|
|
79
|
+
* copyText("Hello world") // return: boolean
|
|
80
|
+
*/
|
|
81
|
+
declare const copyText: (text?: string) => boolean;
|
|
82
|
+
|
|
83
|
+
declare const createLinksFromText: <T extends string, R extends unknown>(text: string, callback: (key: T, value: string) => R) => (string | R)[];
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* @returns [r,g,b]
|
|
87
|
+
*/
|
|
88
|
+
declare const HSVtoRGB: (h: number, s: number, v: number) => number[];
|
|
89
|
+
|
|
90
|
+
declare const RGBtoHEX: (r: number, g: number, b: number) => string;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @returns [h,s,v]
|
|
94
|
+
*/
|
|
95
|
+
declare const RGBtoHSV: (r: number, g: number, b: number) => number[];
|
|
96
|
+
|
|
97
|
+
export { HSVtoRGB, RGBtoHEX, RGBtoHSV, alignTo, chunks, clamp, copyText, createLinksFromText, decWord, formatNumber, isType, omit, pick, random, shuffle, sleep, timeAgo, toShort };
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const e=(e,t)=>t[e%10==1&&e%100!=11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2];const t={date:e=>e instanceof Date,regexp:e=>e instanceof RegExp,error:e=>e instanceof Error,map:e=>e instanceof Map,set:e=>e instanceof Set,weakmap:e=>e instanceof WeakMap,weakset:e=>e instanceof WeakSet,promise:e=>e instanceof Promise,buffer:e=>e instanceof Buffer,undefined:e=>void 0===e,string:e=>"string"==typeof e,bigint:e=>"bigint"==typeof e,number:e=>"number"==typeof e&&!isNaN(e),nan:e=>"number"==typeof e&&isNaN(e),boolean:e=>"boolean"==typeof e,array:e=>Array.isArray(e),object:e=>"object"==typeof e&&!Array.isArray(e)&&null!==e,function:e=>"function"==typeof e,null:e=>null===e,symbol:e=>"symbol"==typeof e,unknown:()=>!0};exports.alignTo=function(e,t){return e<=0?t:e+(t-e%t)%t},exports.chunks=(e,t)=>{const r=[];for(let o=0;o<t.length;o+=e)r.push(t.slice(o,o+e));return r},exports.clamp=(e,t,r)=>e>t?e<r?e:r:t,exports.decWord=e,exports.formatNumber=e=>(e||0).toString().replace(/\B(?=(\d{3})+(?!\d))/g,"."),exports.isType=function(e,r){for(const[o,n]of Object.entries(t))if(n(e))return void 0!==r?o===r:o;return void 0===r&&"unknown"},exports.omit=function(e,t){return Object.keys(e).reduce(((r,o)=>(t.includes(o)||(r[o]=e[o]),r)),{})},exports.pick=function(e,t){return Object.keys(e).reduce(((r,o)=>(t.includes(o)&&(r[o]=e[o]),r)),{})},exports.random=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),exports.shuffle=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},exports.sleep=async e=>await new Promise((t=>setTimeout(t,e))),exports.timeAgo=t=>{if(!t)return"только что";const r=new Date(t),o=Math.floor((Date.now()-r.getTime())/1e3),n=()=>new Intl.DateTimeFormat("RU-ru",{day:"numeric",month:"short"}).format(r).replace(".",""),a=()=>r.toLocaleTimeString([],{hour:"numeric",minute:"2-digit"}),s=(t,r,o)=>Array.isArray(t)?`${o} ${e(o,t)} ${r}`:`${t} ${r}`;switch(!0){case o<0:return"скоро";case o<60:return s(["секунду","секунды","секунд"],"назад",o);case o<3600:return s(["минуту","минуты","минут"],"назад",Math.floor(o/60));case o<7200:return s("час","назад",Math.floor(o/3600));case o<10800:return s("два часа","назад",Math.floor(o/3600));case o<14400:return s("три часа","назад",Math.floor(o/3600));case o<86400:return s(`сегодня в ${a()}`,"",Math.floor(o/3600));case o<172800:return s(`вчера в ${a()}`,"",Math.floor(o/86400));case o<259200:return s("два дня","назад",Math.floor(o/86400));case o<345600:return s("три дня","назад",Math.floor(o/86400));case o<31536e3:return s(`${n()} в ${a()}`,"",Math.floor(o/86400));case o>=31536e3:return s(`${n()} ${r.getFullYear()} г.`,"",Math.floor(o/31536e3))}return"только что"},exports.toShort=(e,t,r=1)=>{const o=t||["","k","M","G","T","P"],n=e<0,a=Math.abs(e),s=Math.log10(a)/3|0,
|
|
1
|
+
"use strict";const e=(e,t)=>t[e%10==1&&e%100!=11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2];const t={date:e=>e instanceof Date,regexp:e=>e instanceof RegExp,error:e=>e instanceof Error,map:e=>e instanceof Map,set:e=>e instanceof Set,weakmap:e=>e instanceof WeakMap,weakset:e=>e instanceof WeakSet,promise:e=>e instanceof Promise,buffer:e=>e instanceof Buffer,undefined:e=>void 0===e,string:e=>"string"==typeof e,bigint:e=>"bigint"==typeof e,number:e=>"number"==typeof e&&!isNaN(e),nan:e=>"number"==typeof e&&isNaN(e),boolean:e=>"boolean"==typeof e,array:e=>Array.isArray(e),object:e=>"object"==typeof e&&!Array.isArray(e)&&null!==e,function:e=>"function"==typeof e,null:e=>null===e,symbol:e=>"symbol"==typeof e,unknown:()=>!0};const r=e=>{const t=e.toString(16);return 1==t.length?"0"+t:t};exports.HSVtoRGB=(e,t,r)=>{var o,n,a,s,c,u,i,f;switch(u=r*(1-t),i=r*(1-(c=6*e-(s=Math.floor(6*e)))*t),f=r*(1-(1-c)*t),s%6){case 0:o=r,n=f,a=u;break;case 1:o=i,n=r,a=u;break;case 2:o=u,n=r,a=f;break;case 3:o=u,n=i,a=r;break;case 4:o=f,n=u,a=r;break;case 5:o=r,n=u,a=i}return[Math.round(255*o),Math.round(255*n),Math.round(255*a)]},exports.RGBtoHEX=(e,t,o)=>"#"+r(e)+r(t)+r(o),exports.RGBtoHSV=(e,t,r)=>{var o=Math.max(e,t,r),n=Math.min(e,t,r),a=o-n,s=0,c=0===o?0:a/o,u=o/255;switch(o){case n:s=0;break;case e:s=t-r+a*(t<r?6:0),s/=6*a;break;case t:s=r-e+2*a,s/=6*a;break;case r:s=e-t+4*a,s/=6*a}return[s,c,u]},exports.alignTo=function(e,t){return e<=0?t:e+(t-e%t)%t},exports.chunks=(e,t)=>{const r=[];for(let o=0;o<t.length;o+=e)r.push(t.slice(o,o+e));return r},exports.clamp=(e,t,r)=>e>t?e<r?e:r:t,exports.copyText=e=>{if(!e)return!1;try{return navigator.clipboard?.writeText(e),!0}catch{}try{var t=document.createElement("input");return t.value=e,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t),!0}catch{}return!1},exports.createLinksFromText=(e,t)=>{const r=[],o=/{{([^}]+):([^}]+)}}/g;let n,a=0;for(;null!==(n=o.exec(e));)r.push(e.substring(a,n.index)),a=n.index+n[0].length,r.push({key:n[1],text:n[2]});return r.push(e.substring(a)),r.map((e=>"string"==typeof e?e:t(e.key,e.text)))},exports.decWord=e,exports.formatNumber=e=>(e||0).toString().replace(/\B(?=(\d{3})+(?!\d))/g,"."),exports.isType=function(e,r){for(const[o,n]of Object.entries(t))if(n(e))return void 0!==r?o===r:o;return void 0===r&&"unknown"},exports.omit=function(e,t){return Object.keys(e).reduce(((r,o)=>(t.includes(o)||(r[o]=e[o]),r)),{})},exports.pick=function(e,t){return Object.keys(e).reduce(((r,o)=>(t.includes(o)&&(r[o]=e[o]),r)),{})},exports.random=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),exports.shuffle=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},exports.sleep=async e=>await new Promise((t=>setTimeout(t,e))),exports.timeAgo=t=>{if(!t)return"только что";const r=new Date(t),o=Math.floor((Date.now()-r.getTime())/1e3),n=()=>new Intl.DateTimeFormat("RU-ru",{day:"numeric",month:"short"}).format(r).replace(".",""),a=()=>r.toLocaleTimeString([],{hour:"numeric",minute:"2-digit"}),s=(t,r,o)=>Array.isArray(t)?`${o} ${e(o,t)} ${r}`:`${t} ${r}`;switch(!0){case o<0:return"скоро";case o<60:return s(["секунду","секунды","секунд"],"назад",o);case o<3600:return s(["минуту","минуты","минут"],"назад",Math.floor(o/60));case o<7200:return s("час","назад",Math.floor(o/3600));case o<10800:return s("два часа","назад",Math.floor(o/3600));case o<14400:return s("три часа","назад",Math.floor(o/3600));case o<86400:return s(`сегодня в ${a()}`,"",Math.floor(o/3600));case o<172800:return s(`вчера в ${a()}`,"",Math.floor(o/86400));case o<259200:return s("два дня","назад",Math.floor(o/86400));case o<345600:return s("три дня","назад",Math.floor(o/86400));case o<31536e3:return s(`${n()} в ${a()}`,"",Math.floor(o/86400));case o>=31536e3:return s(`${n()} ${r.getFullYear()} г.`,"",Math.floor(o/31536e3))}return"только что"},exports.toShort=(e,t,r=1)=>{const o=t||["","k","M","G","T","P"],n=e<0,a=Math.abs(e),s=Math.log10(a)/3|0,c=a/10**(3*s);return parseFloat(`${n?"-":""}${c%1?(Math.floor(10*c)/10).toFixed(r):c}`)+o[s]};
|
package/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
const e=(e,t)=>{const r=[];for(let n=0;n<t.length;n+=e)r.push(t.slice(n,n+e));return r},t=(e,t,r)=>e>t?e<r?e:r:t,r=(e,t)=>t[e%10==1&&e%100!=11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2];function n(e,t){return e<=0?t:e+(t-e%t)%t}const
|
|
1
|
+
const e=(e,t)=>{const r=[];for(let n=0;n<t.length;n+=e)r.push(t.slice(n,n+e));return r},t=(e,t,r)=>e>t?e<r?e:r:t,r=(e,t)=>t[e%10==1&&e%100!=11?0:e%10>=2&&e%10<=4&&(e%100<10||e%100>=20)?1:2];function n(e,t){return e<=0?t:e+(t-e%t)%t}const a=(e,t,r=1)=>{const n=t||["","k","M","G","T","P"],a=e<0,o=Math.abs(e),c=Math.log10(o)/3|0,s=o/10**(3*c);return parseFloat(`${a?"-":""}${s%1?(Math.floor(10*s)/10).toFixed(r):s}`)+n[c]},o=e=>{if(!e)return"только что";const t=new Date(e),n=Math.floor((Date.now()-t.getTime())/1e3),a=()=>new Intl.DateTimeFormat("RU-ru",{day:"numeric",month:"short"}).format(t).replace(".",""),o=()=>t.toLocaleTimeString([],{hour:"numeric",minute:"2-digit"}),c=(e,t,n)=>Array.isArray(e)?`${n} ${r(n,e)} ${t}`:`${e} ${t}`;switch(!0){case n<0:return"скоро";case n<60:return c(["секунду","секунды","секунд"],"назад",n);case n<3600:return c(["минуту","минуты","минут"],"назад",Math.floor(n/60));case n<7200:return c("час","назад",Math.floor(n/3600));case n<10800:return c("два часа","назад",Math.floor(n/3600));case n<14400:return c("три часа","назад",Math.floor(n/3600));case n<86400:return c(`сегодня в ${o()}`,"",Math.floor(n/3600));case n<172800:return c(`вчера в ${o()}`,"",Math.floor(n/86400));case n<259200:return c("два дня","назад",Math.floor(n/86400));case n<345600:return c("три дня","назад",Math.floor(n/86400));case n<31536e3:return c(`${a()} в ${o()}`,"",Math.floor(n/86400));case n>=31536e3:return c(`${a()} ${t.getFullYear()} г.`,"",Math.floor(n/31536e3))}return"только что"},c=e=>(e||0).toString().replace(/\B(?=(\d{3})+(?!\d))/g,"."),s=e=>{for(let t=e.length-1;t>0;t--){const r=Math.floor(Math.random()*(t+1));[e[t],e[r]]=[e[r],e[t]]}return e},u=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),i={date:e=>e instanceof Date,regexp:e=>e instanceof RegExp,error:e=>e instanceof Error,map:e=>e instanceof Map,set:e=>e instanceof Set,weakmap:e=>e instanceof WeakMap,weakset:e=>e instanceof WeakSet,promise:e=>e instanceof Promise,buffer:e=>e instanceof Buffer,undefined:e=>void 0===e,string:e=>"string"==typeof e,bigint:e=>"bigint"==typeof e,number:e=>"number"==typeof e&&!isNaN(e),nan:e=>"number"==typeof e&&isNaN(e),boolean:e=>"boolean"==typeof e,array:e=>Array.isArray(e),object:e=>"object"==typeof e&&!Array.isArray(e)&&null!==e,function:e=>"function"==typeof e,null:e=>null===e,symbol:e=>"symbol"==typeof e,unknown:()=>!0};function f(e,t){for(const[r,n]of Object.entries(i))if(n(e))return void 0!==t?r===t:r;return void 0===t&&"unknown"}function l(e,t){return Object.keys(e).reduce(((r,n)=>(t.includes(n)||(r[n]=e[n]),r)),{})}function h(e,t){return Object.keys(e).reduce(((r,n)=>(t.includes(n)&&(r[n]=e[n]),r)),{})}const d=async e=>await new Promise((t=>setTimeout(t,e))),m=e=>{if(!e)return!1;try{return navigator.clipboard?.writeText(e),!0}catch{}try{var t=document.createElement("input");return t.value=e,document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t),!0}catch{}return!1},p=(e,t)=>{const r=[],n=/{{([^}]+):([^}]+)}}/g;let a,o=0;for(;null!==(a=n.exec(e));)r.push(e.substring(o,a.index)),o=a.index+a[0].length,r.push({key:a[1],text:a[2]});return r.push(e.substring(o)),r.map((e=>"string"==typeof e?e:t(e.key,e.text)))},b=(e,t,r)=>{var n,a,o,c,s,u,i,f;switch(u=r*(1-t),i=r*(1-(s=6*e-(c=Math.floor(6*e)))*t),f=r*(1-(1-s)*t),c%6){case 0:n=r,a=f,o=u;break;case 1:n=i,a=r,o=u;break;case 2:n=u,a=r,o=f;break;case 3:n=u,a=i,o=r;break;case 4:n=f,a=u,o=r;break;case 5:n=r,a=u,o=i}return[Math.round(255*n),Math.round(255*a),Math.round(255*o)]},y=e=>{const t=e.toString(16);return 1==t.length?"0"+t:t},M=(e,t,r)=>"#"+y(e)+y(t)+y(r),g=(e,t,r)=>{var n=Math.max(e,t,r),a=Math.min(e,t,r),o=n-a,c=0,s=0===n?0:o/n,u=n/255;switch(n){case a:c=0;break;case e:c=t-r+o*(t<r?6:0),c/=6*o;break;case t:c=r-e+2*o,c/=6*o;break;case r:c=e-t+4*o,c/=6*o}return[c,s,u]};export{b as HSVtoRGB,M as RGBtoHEX,g as RGBtoHSV,n as alignTo,e as chunks,t as clamp,m as copyText,p as createLinksFromText,r as decWord,c as formatNumber,f as isType,l as omit,h as pick,u as random,s as shuffle,d as sleep,o as timeAgo,a as toShort};
|