@companix/utils-js 0.0.30 → 0.0.31
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/dist/array.d.ts +1 -0
- package/dist/bundle.cjs.js +1 -1
- package/dist/bundle.esm.js +1 -1
- package/dist/index.d.ts +3 -0
- package/dist/primitives/date.d.ts +14 -0
- package/dist/primitives/fullname.d.ts +9 -0
- package/dist/strings.d.ts +1 -0
- package/dist/utils.d.ts +0 -2
- package/package.json +1 -1
package/dist/array.d.ts
CHANGED
|
@@ -2,3 +2,4 @@ export declare const range: (from: number, to: number, step?: number) => number[
|
|
|
2
2
|
export declare const splitByChunks: <T>(items: T[], chunkSize: number) => T[][];
|
|
3
3
|
export declare const separateArray: <T>(array: T[], parts: number) => T[][];
|
|
4
4
|
export declare const roundSeparateArray: <T>(array: T[], size: number) => T[][];
|
|
5
|
+
export declare const contain: <T>(items: T[][]) => T[];
|
package/dist/bundle.cjs.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var t=require("axios");const e=(t,e)=>{if(e<=0)throw new Error("Number of parts must be greater than 0");const r=[],s=Math.floor(t.length/e);let o=t.length%e,n=0;for(let
|
|
1
|
+
"use strict";var t=require("axios");const e=(t,e)=>{if(e<=0)throw new Error("Number of parts must be greater than 0");const r=[],s=Math.floor(t.length/e);let o=t.length%e,n=0;for(let a=0;a<e;a++){const e=n+s+(o>0?1:0),a=t.slice(n,e);a.length>0&&r.push(a),n=e,o--}return r};const r=(t,e)=>{const r=Math.pow(10,e);return Math.round(t*r)/r},s=(t,e=1)=>t>0?[0,".",...new Array(t-1).fill(0),e].join(""):`${e}`,o=t=>Object.keys(t);const n=(t,e)=>Math.floor(Math.random()*(e-t+1))+t;var a;!function(t){t.Get="GET",t.Post="POST"}(a||(a={}));const i=({response:t})=>{throw t};const c=(t,e="token")=>{const r=t??localStorage.getItem(e);return r?`Bearer ${r}`:""},u=(t,{noPennies:e,currency:r,locale:s})=>t.toLocaleString(s,{style:"currency",currency:r,...e?{minimumFractionDigits:0,maximumFractionDigits:0}:{}}),p={email:/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,numbers:/^[0-9]+$/},l={email:t=>p.email.test(t),numbers:t=>p.numbers.test(t)},h=t=>t.toString().padStart(2,"0"),x=(t,{utc:e}={})=>{const r=new Date(t),s=e?"UTC":"";return[h(r[`get${s}Hours`]()),h(r[`get${s}Minutes`]())].join(":")},m=(t,{utc:e}={})=>{const r=new Date(t),s=e?"UTC":"";return[h(r[`get${s}Date`]()),h(r[`get${s}Month`]()+1),h(r[`get${s}FullYear`]())].join(".")},g={"1s":1e3,"1m":6e4,"3m":18e4,"5m":3e5},d=(t,e)=>Math.floor(t/g[e])*g[e],b=t=>t[0].toUpperCase()+t.slice(1);const f={getDate:function(t){return t?new Date(t.year,t.month-1,t.day):null},getFormat:function(t){return t?{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate()}:null}},y=({name:t,patronymic:e},r="")=>[b(t[0]),b(e[0])].join(r);exports.EventBroadcaster=class{subscribers=[];emit(t){this.subscribers.forEach(e=>{e(t)})}subscribe(t){return this.subscribers.push(t),()=>{this.unsubscribe(t)}}unsubscribe(t){const e=this.subscribers.findIndex(e=>e===t);-1!==e&&this.subscribers.splice(e,1)}},exports.EventDispatcher=class{store={};emit(t,e){this.store[t]&&this.store[t](e)}on(t,e){return this.store[t]=e,()=>{this.rm(t)}}rm(t){delete this.store[t]}},exports.EventEmmiter=class{store={};emit(t,e){this.store[t]&&this.store[t].forEach(t=>t(e))}subscribe(t,e){return this.store[t]||(this.store[t]=[]),this.store[t].push(e),()=>{this.unsubscribe(t,e)}}unsubscribe(t,e){if(this.store[t]){const r=this.store[t].findIndex(t=>t===e);-1!==r&&(this.store[t].splice(r,1),0===this.store[t].length&&delete this.store[t])}}},exports.HttpAPI=class{http;handleException;constructor({handleException:e=i,...r}){this.http=t.create(r),this.handleException=e}async request(t){const{url:e,body:r={},method:s,config:o={}}=t,n=s===a.Get?"params":"data";return this.http({url:e,method:s,[n]:r,...o}).then(({data:t})=>t).catch(this.handleException)}useContext(t){const e=this.request.bind(this);return{post:(r,s,o)=>e({method:a.Post,url:t+"/"+r.toString(),config:o,body:s}),get:(r,s,o)=>e({method:a.Get,url:t+"/"+r.toString(),config:o,body:s}),blob(t,e){return this.post(t,e,{responseType:"arraybuffer"}).then(t=>new Blob([t]))}}}},exports.QueueManager=class{active=!1;queue=[];add(t){return new Promise((e,r)=>{this.queue.push({resolve:e,reject:r,target:t}),this.active||this.next()})}async next(){const t=this.queue[0];t?(this.active=!0,await t.target().then(e=>t.resolve(e)).catch(e=>t.reject(e)).finally(()=>this.queue.shift()),await this.next()):this.active=!1}},exports.applyAlphaToHex=(t,e)=>{t.startsWith("#")&&(t=t.slice(1));return`rgba(${parseInt(t.slice(0,2),16)}, ${parseInt(t.slice(2,4),16)}, ${parseInt(t.slice(4,6),16)}, ${Math.max(0,Math.min(100,e))/100})`},exports.capitalize=b,exports.cashAmount=t=>{const e=t/1e3;if(e>=1e3){const t=e/1e3;return t>=1e3?t/1e3+" МЛРД. ₽":`${t} МЛН. ₽`}return`${e} ТЫС. ₽`},exports.checkPattern=l,exports.contain=t=>t.reduce((t,e)=>[...t,...e],[]),exports.cutNumber=(t,e)=>{const r=Math.pow(10,e);return Math.floor(t*r)/r},exports.dateFormat=f,exports.dollars=(t,e={})=>u(t,{locale:"us-US",currency:"USD",...e}),exports.formatBytes=t=>{const e=t/1024;return e>1024?+(e/1024).toFixed(1)+" MB":e<1?t+" B":Math.trunc(+e.toFixed(2))+" KB"},exports.formatTime=h,exports.generateCode=t=>{const e="A0B1C2D3E4F5G6H7W89K0L1M2N34P5Q6R7S8T9U0V3XYZ",r=[];for(let s=0;s<t;s++){const t=Math.floor(45*Math.random());r.push(e[t])}return r.join("")},exports.getAuthToken=c,exports.getCurrentCandleTime=d,exports.getDate=m,exports.getDateTime=(t,e={})=>[m(t,e),x(t,e)].join(" "),exports.getFloatDigits=t=>{if(t.includes("e")){const[e,r]=t.split("e");return Math.max((e.split(".")[1]?.length||0)-+r,0)}return t.split(".")[1]?.length||0},exports.getFullName=({name:t,surname:e,patronymic:r})=>`${e} ${t} ${r}`,exports.getHeaders=({token:t,tokenSource:e,multipart:r}={})=>{const s=c(t,e),o={"Content-Type":r?"multipart/form-data; charset=utf-8":"application/json"};return s&&(o.Authorization=s),o},exports.getInitials=y,exports.getLastClosedCandleTime=(t,e)=>d(t,e)-g[e],exports.getNextCandleTime=(t,e)=>d(t,e)+g[e],exports.getNoun=(t,e,r,s)=>{let o=Math.abs(t);return o%=100,o>=5&&o<=20?s:(o%=10,1===o?e:o>=2&&o<=4?r:s)},exports.getNum=t=>isNaN(Number(t))?null:+t,exports.getPrecisionStep=s,exports.getRandomInt=n,exports.getRandomIntString=t=>new Array(t).fill(0).map(()=>n(0,9)).join(""),exports.getRandomItemFromArray=(t,e)=>{const r=e&&e.exceptions.length<t.length?t.filter(t=>!e.exceptions.some(r=>e.isEqual(t,r))):t;return r[r.length*Math.random()|0]},exports.getShortName=({name:t,surname:e,patronymic:r})=>`${e} ${y({name:t,patronymic:r},".")}.`,exports.getTime=x,exports.getTimes=t=>{t<0&&(t=0);const e=Math.floor(t/1e3);return{hours:Math.floor(e/3600),minutes:Math.floor(e%3600/60),seconds:e%60}},exports.hash=()=>{const t="undefined"==typeof performance?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{const r=(16*Math.random()+t)%16|0;return("x"===e?r:3&r|8).toString(16)})},exports.intervals=g,exports.isEmpty=t=>{for(const e in t)return!1;return!0},exports.isNamesNotEqual=(t,e)=>t.name!==e.name||t.surname!==e.surname||t.patronymic!==e.patronymic,exports.isValidPrecision=(t,e)=>{const r=t.toString();return!r.includes(".")||0!==e&&r.split(".")[1].length<=e},exports.keys=o,exports.keysLength=t=>o(t).length,exports.matchPattern=(t,e)=>t.toLowerCase().indexOf(e.trim().toLowerCase())>=0,exports.nonNullable=t=>null!=t,exports.normalize=r,exports.omit=(t,e)=>(delete t[e],t),exports.partial=t=>{const e={};for(const r in t)t[r]&&(e[r]=t[r]);return e},exports.range=(t,e,r=1)=>{const s=t<e?1:-1,o=Math.abs(t-e)+1,n=Math.ceil(o/r),a=Array(n);for(let e=0;e<a.length;e++)a[e]=t+e*r*s;return a},exports.regex=p,exports.rgbaToHex=t=>{const e=t.match(/rgba?\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([\d.]+)\s*\)/);if(!e)throw new Error("Некорректный формат RGBA");const[,r,s,o,n]=e,a=t=>parseInt(t).toString(16).padStart(2,"0"),i=Math.round(255*parseFloat(n));return`#${a(r)}${a(s)}${a(o)}${a(i.toString())}`},exports.roundSeparateArray=(t,r)=>{const s=Math.round(t.length/r);return s>1?e(t,s):[t]},exports.roundUp2=(t,e)=>{const o=t.toFixed(e);return t>+o?r(+o+ +s(e),e):+o},exports.rubles=(t,e={})=>u(t,{locale:"ru-RU",currency:"RUB",...e}),exports.separateArray=e,exports.sleep=t=>new Promise(e=>setTimeout(e,t)),exports.splitByChunks=(t,e)=>{const r=[];for(let s=0;s<t.length;s+=e)r.push(t.slice(s,s+e));return r},exports.truncateNumber=(t,e)=>{const r=t.toFixed(e);return+r>t?(+r-Math.sign(t)*Math.pow(.1,e)).toFixed(e):r};
|
package/dist/bundle.esm.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import t from"axios";const e=(t,e,r=1)=>{const
|
|
1
|
+
import t from"axios";const e=(t,e,r=1)=>{const n=t<e?1:-1,s=Math.abs(t-e)+1,o=Math.ceil(s/r),i=Array(o);for(let e=0;e<i.length;e++)i[e]=t+e*r*n;return i},r=(t,e)=>{const r=[];for(let n=0;n<t.length;n+=e)r.push(t.slice(n,n+e));return r},n=(t,e)=>{if(e<=0)throw new Error("Number of parts must be greater than 0");const r=[],n=Math.floor(t.length/e);let s=t.length%e,o=0;for(let i=0;i<e;i++){const e=o+n+(s>0?1:0),i=t.slice(o,e);i.length>0&&r.push(i),o=e,s--}return r},s=(t,e)=>{const r=Math.round(t.length/e);return r>1?n(t,r):[t]},o=t=>t.reduce((t,e)=>[...t,...e],[]),i=(t,e)=>{t.startsWith("#")&&(t=t.slice(1));return`rgba(${parseInt(t.slice(0,2),16)}, ${parseInt(t.slice(2,4),16)}, ${parseInt(t.slice(4,6),16)}, ${Math.max(0,Math.min(100,e))/100})`},a=t=>{const e=t.match(/rgba?\s*\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*([\d.]+)\s*\)/);if(!e)throw new Error("Некорректный формат RGBA");const[,r,n,s,o]=e,i=t=>parseInt(t).toString(16).padStart(2,"0"),a=Math.round(255*parseFloat(o));return`#${i(r)}${i(n)}${i(s)}${i(a.toString())}`};class c{store={};emit(t,e){this.store[t]&&this.store[t](e)}on(t,e){return this.store[t]=e,()=>{this.rm(t)}}rm(t){delete this.store[t]}}class u{store={};emit(t,e){this.store[t]&&this.store[t].forEach(t=>t(e))}subscribe(t,e){return this.store[t]||(this.store[t]=[]),this.store[t].push(e),()=>{this.unsubscribe(t,e)}}unsubscribe(t,e){if(this.store[t]){const r=this.store[t].findIndex(t=>t===e);-1!==r&&(this.store[t].splice(r,1),0===this.store[t].length&&delete this.store[t])}}}class h{subscribers=[];emit(t){this.subscribers.forEach(e=>{e(t)})}subscribe(t){return this.subscribers.push(t),()=>{this.unsubscribe(t)}}unsubscribe(t){const e=this.subscribers.findIndex(e=>e===t);-1!==e&&this.subscribers.splice(e,1)}}const l=t=>{if(t.includes("e")){const[e,r]=t.split("e");return Math.max((e.split(".")[1]?.length||0)-+r,0)}return t.split(".")[1]?.length||0},m=(t,e)=>{const r=Math.pow(10,e);return Math.round(t*r)/r},p=(t,e)=>{const r=Math.pow(10,e);return Math.floor(t*r)/r},x=(t,e)=>{const r=t.toFixed(e);return t>+r?m(+r+ +g(e),e):+r},d=(t,e)=>{const r=t.toFixed(e);return+r>t?(+r-Math.sign(t)*Math.pow(.1,e)).toFixed(e):r},g=(t,e=1)=>t>0?[0,".",...new Array(t-1).fill(0),e].join(""):`${e}`,f=(t,e)=>{const r=t.toString();return!r.includes(".")||0!==e&&r.split(".")[1].length<=e},b=t=>isNaN(Number(t))?null:+t,y=t=>Object.keys(t),M=t=>y(t).length,$=t=>{for(const e in t)return!1;return!0},w=(t,e)=>(delete t[e],t),S=t=>{const e={};for(const r in t)t[r]&&(e[r]=t[r]);return e};class D{active=!1;queue=[];add(t){return new Promise((e,r)=>{this.queue.push({resolve:e,reject:r,target:t}),this.active||this.next()})}async next(){const t=this.queue[0];t?(this.active=!0,await t.target().then(e=>t.resolve(e)).catch(e=>t.reject(e)).finally(()=>this.queue.shift()),await this.next()):this.active=!1}}const F=t=>{const e="A0B1C2D3E4F5G6H7W89K0L1M2N34P5Q6R7S8T9U0V3XYZ",r=[];for(let n=0;n<t;n++){const t=Math.floor(45*Math.random());r.push(e[t])}return r.join("")},j=(t,e)=>Math.floor(Math.random()*(e-t+1))+t,E=t=>new Array(t).fill(0).map(()=>j(0,9)).join(""),A=()=>{const t="undefined"==typeof performance?Date.now():1e3*performance.now();return"xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g,e=>{const r=(16*Math.random()+t)%16|0;return("x"===e?r:3&r|8).toString(16)})},B=(t,e)=>{const r=e&&e.exceptions.length<t.length?t.filter(t=>!e.exceptions.some(r=>e.isEqual(t,r))):t;return r[r.length*Math.random()|0]},C=t=>new Promise(e=>setTimeout(e,t)),T=t=>null!=t;var U;!function(t){t.Get="GET",t.Post="POST"}(U||(U={}));const q=({response:t})=>{throw t};class v{http;handleException;constructor({handleException:e=q,...r}){this.http=t.create(r),this.handleException=e}async request(t){const{url:e,body:r={},method:n,config:s={}}=t,o=n===U.Get?"params":"data";return this.http({url:e,method:n,[o]:r,...s}).then(({data:t})=>t).catch(this.handleException)}useContext(t){const e=this.request.bind(this);return{post:(r,n,s)=>e({method:U.Post,url:t+"/"+r.toString(),config:s,body:n}),get:(r,n,s)=>e({method:U.Get,url:t+"/"+r.toString(),config:s,body:n}),blob(t,e){return this.post(t,e,{responseType:"arraybuffer"}).then(t=>new Blob([t]))}}}}const I=({token:t,tokenSource:e,multipart:r}={})=>{const n=P(t,e),s={"Content-Type":r?"multipart/form-data; charset=utf-8":"application/json"};return n&&(s.Authorization=n),s},P=(t,e="token")=>{const r=t??localStorage.getItem(e);return r?`Bearer ${r}`:""},G=t=>{const e=t/1e3;if(e>=1e3){const t=e/1e3;return t>=1e3?t/1e3+" МЛРД. ₽":`${t} МЛН. ₽`}return`${e} ТЫС. ₽`},N=(t,{noPennies:e,currency:r,locale:n})=>t.toLocaleString(n,{style:"currency",currency:r,...e?{minimumFractionDigits:0,maximumFractionDigits:0}:{}}),k=(t,e={})=>N(t,{locale:"ru-RU",currency:"RUB",...e}),L=(t,e={})=>N(t,{locale:"us-US",currency:"USD",...e}),R=(t,e,r,n)=>{let s=Math.abs(t);return s%=100,s>=5&&s<=20?n:(s%=10,1===s?e:s>=2&&s<=4?r:n)},z={email:/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,numbers:/^[0-9]+$/},O={email:t=>z.email.test(t),numbers:t=>z.numbers.test(t)},Y=t=>{const e=t/1024;return e>1024?+(e/1024).toFixed(1)+" MB":e<1?t+" B":Math.trunc(+e.toFixed(2))+" KB"},Z=t=>t.toString().padStart(2,"0"),H=t=>{t<0&&(t=0);const e=Math.floor(t/1e3);return{hours:Math.floor(e/3600),minutes:Math.floor(e%3600/60),seconds:e%60}},K=(t,{utc:e}={})=>{const r=new Date(t),n=e?"UTC":"";return[Z(r[`get${n}Hours`]()),Z(r[`get${n}Minutes`]())].join(":")},W=(t,{utc:e}={})=>{const r=new Date(t),n=e?"UTC":"";return[Z(r[`get${n}Date`]()),Z(r[`get${n}Month`]()+1),Z(r[`get${n}FullYear`]())].join(".")},Q=(t,e={})=>[W(t,e),K(t,e)].join(" "),V={"1s":1e3,"1m":6e4,"3m":18e4,"5m":3e5},X=(t,e)=>J(t,e)-V[e],J=(t,e)=>Math.floor(t/V[e])*V[e],_=(t,e)=>J(t,e)+V[e],tt=(t,e)=>t.toLowerCase().indexOf(e.trim().toLowerCase())>=0,et=t=>t[0].toUpperCase()+t.slice(1);const rt={getDate:function(t){return t?new Date(t.year,t.month-1,t.day):null},getFormat:function(t){return t?{year:t.getFullYear(),month:t.getMonth()+1,day:t.getDate()}:null}},nt=(t,e)=>t.name!==e.name||t.surname!==e.surname||t.patronymic!==e.patronymic,st=({name:t,patronymic:e},r="")=>[et(t[0]),et(e[0])].join(r),ot=({name:t,surname:e,patronymic:r})=>`${e} ${st({name:t,patronymic:r},".")}.`,it=({name:t,surname:e,patronymic:r})=>`${e} ${t} ${r}`;export{h as EventBroadcaster,c as EventDispatcher,u as EventEmmiter,v as HttpAPI,D as QueueManager,i as applyAlphaToHex,et as capitalize,G as cashAmount,O as checkPattern,o as contain,p as cutNumber,rt as dateFormat,L as dollars,Y as formatBytes,Z as formatTime,F as generateCode,P as getAuthToken,J as getCurrentCandleTime,W as getDate,Q as getDateTime,l as getFloatDigits,it as getFullName,I as getHeaders,st as getInitials,X as getLastClosedCandleTime,_ as getNextCandleTime,R as getNoun,b as getNum,g as getPrecisionStep,j as getRandomInt,E as getRandomIntString,B as getRandomItemFromArray,ot as getShortName,K as getTime,H as getTimes,A as hash,V as intervals,$ as isEmpty,nt as isNamesNotEqual,f as isValidPrecision,y as keys,M as keysLength,tt as matchPattern,T as nonNullable,m as normalize,w as omit,S as partial,e as range,z as regex,a as rgbaToHex,s as roundSeparateArray,x as roundUp2,k as rubles,n as separateArray,C as sleep,r as splitByChunks,d as truncateNumber};
|
package/dist/index.d.ts
CHANGED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface DateFormat {
|
|
2
|
+
year: number;
|
|
3
|
+
month: number;
|
|
4
|
+
day: number;
|
|
5
|
+
}
|
|
6
|
+
declare function getFormat(date: Date): DateFormat;
|
|
7
|
+
declare function getFormat(date: Date | null): DateFormat | null;
|
|
8
|
+
declare function getDate(date: DateFormat): Date;
|
|
9
|
+
declare function getDate(date: DateFormat | null): Date | null;
|
|
10
|
+
export declare const dateFormat: {
|
|
11
|
+
getDate: typeof getDate;
|
|
12
|
+
getFormat: typeof getFormat;
|
|
13
|
+
};
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface FullName {
|
|
2
|
+
name: string;
|
|
3
|
+
surname: string;
|
|
4
|
+
patronymic: string;
|
|
5
|
+
}
|
|
6
|
+
export declare const isNamesNotEqual: (A: FullName, B: FullName) => boolean;
|
|
7
|
+
export declare const getInitials: ({ name, patronymic }: Omit<FullName, "surname">, joinChart?: string) => string;
|
|
8
|
+
export declare const getShortName: ({ name, surname, patronymic }: FullName) => string;
|
|
9
|
+
export declare const getFullName: ({ name, surname, patronymic }: FullName) => string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const capitalize: (str: string) => string;
|
package/dist/utils.d.ts
CHANGED