@minsize/utils 0.0.1 → 0.0.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.
package/README.md CHANGED
@@ -1,56 +1,47 @@
1
- ## Mutex
2
- [![npm version](https://img.shields.io/npm/v/@minsize/mutex)](https://www.npmjs.com/package/@minsize/mutex)
1
+ ## Utils
2
+ [![npm version](https://img.shields.io/npm/v/@minsize/utils)](https://www.npmjs.com/package/@minsize/utils)
3
3
 
4
4
  ```
5
- npm i @minsize/mutex
5
+ npm i @minsize/utils
6
6
 
7
- yarn add @minsize/mutex
7
+ yarn add @minsize/utils
8
8
  ```
9
9
 
10
- ### Usage
10
+ ### Functions
11
11
  ```js
12
- import { Mutex } from '@minsize/mutex'
13
-
14
- const mutex = Mutex({
15
- globalLimit: 1 // Global limit on how many functions a mutex can process at once
16
- })
17
-
18
- const fn = async () => {
19
- const release = await mutex.wait();
20
-
21
- try {
22
- // ...
23
- } finally {
24
- release();
25
- // or
26
- mutex.release();
27
- }
28
- }
29
-
12
+ import {
13
+ chunks,
14
+ clamp,
15
+ decWord,
16
+ alignTo,
17
+ toShort,
18
+ timeAgo,
19
+ formatNumber,
20
+ shuffle,
21
+ random,
22
+ isType,
23
+ omit,
24
+ pick,
25
+ sleep,
26
+ copyText
27
+ } from '@minsize/utils'
30
28
  ```
31
29
 
30
+ | Function | Description |
31
+ | --------------------- | ------------------------------------------------------------------------------------------------------------ |
32
+ | `chunks` | Splits an array into pieces of the given size. |
33
+ | `clamp` | Limits the number to the specified minimum and maximum value. |
34
+ | `decWord` | The function returns a string representing the correct ending of the word depending on the number. |
35
+ | `alignTo` | The function returns an aligned number. |
36
+ | `toShort` | The function returns a string representing the number in short form. |
37
+ | `timeAgo` | The function returns a string describing the time elapsed since timestamp. |
38
+ | `formatNumber` | Formats a number into a delimited string. |
39
+ | `shuffle` | Shuffles the elements of an array in random order. |
40
+ | `random` | Generates a random number within the specified range. |
41
+ | `isType` | Checks if the value is of the specified type. |
42
+ | `omit` | Returns a new object with no specified keys. |
43
+ | `pick` | Returns a new object with the selected keys. |
44
+ | `sleep` | Waits for the specified number of milliseconds. |
45
+ | `copyText` | The function allows you to copy text to the clipboard. |
46
+ | `createLinksFromText` | The function returns an array containing text fragments and the results of the callback call for each block. |
32
47
 
33
- ### Use with key
34
- ```js
35
- import { Mutex } from '@minsize/mutex'
36
-
37
- const mutex = Mutex({
38
- globalLimit: 1 // Global limit on how many functions a mutex can process at once
39
- })
40
-
41
- const fn = async (userId) => {
42
- const release = await mutex.wait({
43
- key: userId, // Whom Mutex will be used
44
- limit: 1 // Works the same as the global limit, but only for 1 key
45
- });
46
-
47
- try {
48
- // ...
49
- } finally {
50
- release();
51
- // or
52
- mutex.release({ key: userId });
53
- }
54
- }
55
-
56
- ```
package/index.d.ts CHANGED
@@ -73,4 +73,13 @@ 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
- export { alignTo, chunks, clamp, decWord, formatNumber, isType, omit, pick, random, shuffle, sleep, timeAgo, toShort };
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
+ export { 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,i=a/10**(3*s);return parseFloat(`${n?"-":""}${i%1?(Math.floor(10*i)/10).toFixed(r):i}`)+o[s]};
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.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 o=(e,t,r=1)=>{const n=t||["","k","M","G","T","P"],o=e<0,a=Math.abs(e),s=Math.log10(a)/3|0,i=a/10**(3*s);return parseFloat(`${o?"-":""}${i%1?(Math.floor(10*i)/10).toFixed(r):i}`)+n[s]},a=e=>{if(!e)return"только что";const t=new Date(e),n=Math.floor((Date.now()-t.getTime())/1e3),o=()=>new Intl.DateTimeFormat("RU-ru",{day:"numeric",month:"short"}).format(t).replace(".",""),a=()=>t.toLocaleTimeString([],{hour:"numeric",minute:"2-digit"}),s=(e,t,n)=>Array.isArray(e)?`${n} ${r(n,e)} ${t}`:`${e} ${t}`;switch(!0){case n<0:return"скоро";case n<60:return s(["секунду","секунды","секунд"],"назад",n);case n<3600:return s(["минуту","минуты","минут"],"назад",Math.floor(n/60));case n<7200:return s("час","назад",Math.floor(n/3600));case n<10800:return s("два часа","назад",Math.floor(n/3600));case n<14400:return s("три часа","назад",Math.floor(n/3600));case n<86400:return s(`сегодня в ${a()}`,"",Math.floor(n/3600));case n<172800:return s(`вчера в ${a()}`,"",Math.floor(n/86400));case n<259200:return s("два дня","назад",Math.floor(n/86400));case n<345600:return s("три дня","назад",Math.floor(n/86400));case n<31536e3:return s(`${o()} в ${a()}`,"",Math.floor(n/86400));case n>=31536e3:return s(`${o()} ${t.getFullYear()} г.`,"",Math.floor(n/31536e3))}return"только что"},s=e=>(e||0).toString().replace(/\B(?=(\d{3})+(?!\d))/g,"."),i=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},c=(e,t)=>Math.floor(Math.random()*(t-e+1)+e),u={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(u))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 m=async e=>await new Promise((t=>setTimeout(t,e)));export{n as alignTo,e as chunks,t as clamp,r as decWord,s as formatNumber,f as isType,l as omit,h as pick,c as random,i as shuffle,m as sleep,a as timeAgo,o as toShort};
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 o=(e,t,r=1)=>{const n=t||["","k","M","G","T","P"],o=e<0,a=Math.abs(e),u=Math.log10(a)/3|0,c=a/10**(3*u);return parseFloat(`${o?"-":""}${c%1?(Math.floor(10*c)/10).toFixed(r):c}`)+n[u]},a=e=>{if(!e)return"только что";const t=new Date(e),n=Math.floor((Date.now()-t.getTime())/1e3),o=()=>new Intl.DateTimeFormat("RU-ru",{day:"numeric",month:"short"}).format(t).replace(".",""),a=()=>t.toLocaleTimeString([],{hour:"numeric",minute:"2-digit"}),u=(e,t,n)=>Array.isArray(e)?`${n} ${r(n,e)} ${t}`:`${e} ${t}`;switch(!0){case n<0:return"скоро";case n<60:return u(["секунду","секунды","секунд"],"назад",n);case n<3600:return u(["минуту","минуты","минут"],"назад",Math.floor(n/60));case n<7200:return u("час","назад",Math.floor(n/3600));case n<10800:return u("два часа","назад",Math.floor(n/3600));case n<14400:return u("три часа","назад",Math.floor(n/3600));case n<86400:return u(`сегодня в ${a()}`,"",Math.floor(n/3600));case n<172800:return u(`вчера в ${a()}`,"",Math.floor(n/86400));case n<259200:return u("два дня","назад",Math.floor(n/86400));case n<345600:return u("три дня","назад",Math.floor(n/86400));case n<31536e3:return u(`${o()} в ${a()}`,"",Math.floor(n/86400));case n>=31536e3:return u(`${o()} ${t.getFullYear()} г.`,"",Math.floor(n/31536e3))}return"только что"},u=e=>(e||0).toString().replace(/\B(?=(\d{3})+(?!\d))/g,"."),c=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},s=(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 m=async e=>await new Promise((t=>setTimeout(t,e))),d=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 o,a=0;for(;null!==(o=n.exec(e));)r.push(e.substring(a,o.index)),a=o.index+o[0].length,r.push({key:o[1],text:o[2]});return r.push(e.substring(a)),r.map((e=>"string"==typeof e?e:t(e.key,e.text)))};export{n as alignTo,e as chunks,t as clamp,d as copyText,p as createLinksFromText,r as decWord,u as formatNumber,f as isType,l as omit,h as pick,s as random,c as shuffle,m as sleep,a as timeAgo,o as toShort};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@minsize/utils",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Frequently used utilities",
5
5
  "license": "MIT",
6
6
  "types": "index.ts",