@aliexme/js-utils 2.0.0 → 2.1.1

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.
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=(n,i,...r)=>{if(n.length===0||i.length===0||r.some(e=>e.length===0))return[];let t=n.filter(e=>i.includes(e));return r.forEach(e=>{t=t.filter(l=>e.includes(l))}),t};exports.arraysIntersection=o;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=(s,u)=>{const e={};return s.forEach(r=>{const t=r[u],o=e[t];o?o.push(r):e[t]=[r]}),e};exports.groupArrayItems=a;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=t=>{const r=Math.floor(Math.random()*t.length);return t[r]};exports.randomArrayItem=e;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=(t,e)=>t.length===0||e.length===0?t:t.filter(r=>!e.includes(r));exports.subtractArrays=n;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=e=>[...new Set(e)];exports.uniquify=t;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./asyncify.cjs"),e=n=>s.asyncify(n)();exports.asyncResult=e;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=e=>(...t)=>new Promise(r=>{setTimeout(()=>r(e(...t)),0)});exports.asyncify=n;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=async e=>new Promise(t=>setTimeout(t,e));exports.sleep=s;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const f=require("../number/clamp.cjs"),C=(l,c,m={})=>{const{maxWait:i,withLeading:d=!1}=m;let e=null,t=null;const o=()=>{e!==null&&(clearTimeout(e),e=null)},n=()=>{t=null},T=()=>{o(),n()},s=(...r)=>{o();const u=Date.now(),b=t?u-t:0,a=i?f.clamp(i-b,0,c):c;if(!t&&(t=u,d)){l(...r),e=setTimeout(n,a);return}e=setTimeout(()=>{l(...r),n()},a)};return s.cancel=T,s};exports.debounce=C;
@@ -0,0 +1,9 @@
1
+ export interface DebouncedFunc<T extends (...args: unknown[]) => unknown> {
2
+ (...args: Parameters<T>): void;
3
+ cancel(): void;
4
+ }
5
+ export interface DebounceOptions {
6
+ maxWait?: number;
7
+ withLeading?: boolean;
8
+ }
9
+ export declare const debounce: <T extends (...args: unknown[]) => unknown>(func: T, delay: number, options?: DebounceOptions) => DebouncedFunc<T>;
@@ -0,0 +1,26 @@
1
+ import { clamp as C } from "../number/clamp.js";
2
+ const p = (l, i, m = {}) => {
3
+ const { maxWait: c, withLeading: f = !1 } = m;
4
+ let t = null, e = null;
5
+ const o = () => {
6
+ t !== null && (clearTimeout(t), t = null);
7
+ }, n = () => {
8
+ e = null;
9
+ }, T = () => {
10
+ o(), n();
11
+ }, s = (...r) => {
12
+ o();
13
+ const a = Date.now(), d = e ? a - e : 0, u = c ? C(c - d, 0, i) : i;
14
+ if (!e && (e = a, f)) {
15
+ l(...r), t = setTimeout(n, u);
16
+ return;
17
+ }
18
+ t = setTimeout(() => {
19
+ l(...r), n();
20
+ }, u);
21
+ };
22
+ return s.cancel = T, s;
23
+ };
24
+ export {
25
+ p as debounce
26
+ };
@@ -1 +1,3 @@
1
1
  export * from './noop';
2
+ export * from './debounce';
3
+ export * from './throttle';
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=()=>{};exports.noop=o;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const d=(n,r,s={})=>{const{withTrailing:T=!1}=s;let e=null,t=null,l=!1;const o=()=>{e!==null&&(clearTimeout(e),e=null)},a=()=>{t!==null&&(clearTimeout(t),t=null)},c=()=>{l=!1},m=()=>{o(),a(),c()},i=(...u)=>{if(l){T&&(o(),e=setTimeout(()=>n(...u),r));return}o(),n(...u),l=!0,t=setTimeout(c,r)};return i.cancel=m,i};exports.throttle=d;
@@ -0,0 +1,8 @@
1
+ export interface ThrottledFunc<T extends (...args: unknown[]) => unknown> {
2
+ (...args: Parameters<T>): void;
3
+ cancel(): void;
4
+ }
5
+ export interface ThrottleOptions {
6
+ withTrailing?: boolean;
7
+ }
8
+ export declare const throttle: <T extends (...args: unknown[]) => unknown>(func: T, delay: number, options?: ThrottleOptions) => ThrottledFunc<T>;
@@ -0,0 +1,23 @@
1
+ const f = (n, c, s = {}) => {
2
+ const { withTrailing: T = !1 } = s;
3
+ let e = null, l = null, t = !1;
4
+ const o = () => {
5
+ e !== null && (clearTimeout(e), e = null);
6
+ }, m = () => {
7
+ l !== null && (clearTimeout(l), l = null);
8
+ }, r = () => {
9
+ t = !1;
10
+ }, a = () => {
11
+ o(), m(), r();
12
+ }, i = (...u) => {
13
+ if (t) {
14
+ T && (o(), e = setTimeout(() => n(...u), c));
15
+ return;
16
+ }
17
+ o(), n(...u), t = !0, l = setTimeout(r, c);
18
+ };
19
+ return i.cancel = a, i;
20
+ };
21
+ export {
22
+ f as throttle
23
+ };
package/dist/index.cjs ADDED
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./string/capitalize.cjs"),r=require("./string/secureString.cjs"),t=require("./string/randomString.cjs"),n=require("./number/round.cjs"),o=require("./number/clamp.cjs"),c=require("./number/randomInt.cjs"),i=require("./array/uniquify.cjs"),s=require("./array/subtractArrays.cjs"),u=require("./array/arraysIntersection.cjs"),a=require("./array/groupArrayItems.cjs"),y=require("./array/randomArrayItem.cjs"),d=require("./object/getObjectKey.cjs"),m=require("./object/findObjectKey.cjs"),q=require("./object/objectForEach.cjs"),b=require("./object/omitObjectProperties.cjs"),l=require("./object/omitUndefinedObjectValues.cjs"),p=require("./function/noop.cjs"),j=require("./function/debounce.cjs"),g=require("./function/throttle.cjs"),f=require("./async/asyncify.cjs"),O=require("./async/asyncResult.cjs"),I=require("./async/sleep.cjs");exports.capitalize=e.capitalize;exports.secureString=r.secureString;exports.randomString=t.randomString;exports.round=n.round;exports.clamp=o.clamp;exports.randomInt=c.randomInt;exports.uniquify=i.uniquify;exports.subtractArrays=s.subtractArrays;exports.arraysIntersection=u.arraysIntersection;exports.groupArrayItems=a.groupArrayItems;exports.randomArrayItem=y.randomArrayItem;exports.getObjectKey=d.getObjectKey;exports.findObjectKey=m.findObjectKey;exports.objectForEach=q.objectForEach;exports.omitObjectProperties=b.omitObjectProperties;exports.omitUndefinedObjectValues=l.omitUndefinedObjectValues;exports.noop=p.noop;exports.debounce=j.debounce;exports.throttle=g.throttle;exports.asyncify=f.asyncify;exports.asyncResult=O.asyncResult;exports.sleep=I.sleep;
package/dist/index.js CHANGED
@@ -2,39 +2,45 @@ import { capitalize as e } from "./string/capitalize.js";
2
2
  import { secureString as m } from "./string/secureString.js";
3
3
  import { randomString as f } from "./string/randomString.js";
4
4
  import { round as n } from "./number/round.js";
5
- import { randomInt as i } from "./number/randomInt.js";
6
- import { uniquify as s } from "./array/uniquify.js";
7
- import { subtractArrays as u } from "./array/subtractArrays.js";
8
- import { arraysIntersection as b } from "./array/arraysIntersection.js";
9
- import { groupArrayItems as g } from "./array/groupArrayItems.js";
10
- import { randomArrayItem as I } from "./array/randomArrayItem.js";
11
- import { getObjectKey as A } from "./object/getObjectKey.js";
12
- import { findObjectKey as S } from "./object/findObjectKey.js";
13
- import { objectForEach as q } from "./object/objectForEach.js";
14
- import { omitObjectProperties as E } from "./object/omitObjectProperties.js";
15
- import { omitUndefinedObjectValues as P } from "./object/omitUndefinedObjectValues.js";
16
- import { noop as U } from "./function/noop.js";
17
- import { asyncify as k } from "./async/asyncify.js";
18
- import { asyncResult as w } from "./async/asyncResult.js";
19
- import { sleep as C } from "./async/sleep.js";
5
+ import { clamp as c } from "./number/clamp.js";
6
+ import { randomInt as s } from "./number/randomInt.js";
7
+ import { uniquify as u } from "./array/uniquify.js";
8
+ import { subtractArrays as b } from "./array/subtractArrays.js";
9
+ import { arraysIntersection as j } from "./array/arraysIntersection.js";
10
+ import { groupArrayItems as I } from "./array/groupArrayItems.js";
11
+ import { randomArrayItem as A } from "./array/randomArrayItem.js";
12
+ import { getObjectKey as K } from "./object/getObjectKey.js";
13
+ import { findObjectKey as q } from "./object/findObjectKey.js";
14
+ import { objectForEach as E } from "./object/objectForEach.js";
15
+ import { omitObjectProperties as P } from "./object/omitObjectProperties.js";
16
+ import { omitUndefinedObjectValues as U } from "./object/omitUndefinedObjectValues.js";
17
+ import { noop as k } from "./function/noop.js";
18
+ import { debounce as w } from "./function/debounce.js";
19
+ import { throttle as C } from "./function/throttle.js";
20
+ import { asyncify as G } from "./async/asyncify.js";
21
+ import { asyncResult as J } from "./async/asyncResult.js";
22
+ import { sleep as M } from "./async/sleep.js";
20
23
  export {
21
- b as arraysIntersection,
22
- w as asyncResult,
23
- k as asyncify,
24
+ j as arraysIntersection,
25
+ J as asyncResult,
26
+ G as asyncify,
24
27
  e as capitalize,
25
- S as findObjectKey,
26
- A as getObjectKey,
27
- g as groupArrayItems,
28
- U as noop,
29
- q as objectForEach,
30
- E as omitObjectProperties,
31
- P as omitUndefinedObjectValues,
32
- I as randomArrayItem,
33
- i as randomInt,
28
+ c as clamp,
29
+ w as debounce,
30
+ q as findObjectKey,
31
+ K as getObjectKey,
32
+ I as groupArrayItems,
33
+ k as noop,
34
+ E as objectForEach,
35
+ P as omitObjectProperties,
36
+ U as omitUndefinedObjectValues,
37
+ A as randomArrayItem,
38
+ s as randomInt,
34
39
  f as randomString,
35
40
  n as round,
36
41
  m as secureString,
37
- C as sleep,
38
- u as subtractArrays,
39
- s as uniquify
42
+ M as sleep,
43
+ b as subtractArrays,
44
+ C as throttle,
45
+ u as uniquify
40
46
  };
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=(t,e,a)=>Math.min(Math.max(t,e),a);exports.clamp=r;
@@ -0,0 +1 @@
1
+ export declare const clamp: (value: number, min: number, max: number) => number;
@@ -0,0 +1,4 @@
1
+ const n = (t, a, m) => Math.min(Math.max(t, a), m);
2
+ export {
3
+ n as clamp
4
+ };
@@ -1,2 +1,3 @@
1
1
  export * from './round';
2
+ export * from './clamp';
2
3
  export * from './randomInt';
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=(t=0,n=999999)=>Math.floor(Math.random()*(n-t+1))+t;exports.randomInt=o;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=(t,r={})=>{const{precision:o=0}=r;return Math.round(t*Math.pow(10,o))/Math.pow(10,o)};exports.round=e;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=(t,n)=>{var e;return(e=Object.entries(t).find(([,i])=>n(i)))==null?void 0:e[0]};exports.findObjectKey=r;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./findObjectKey.cjs"),n=(e,t)=>r.findObjectKey(e,c=>c===t);exports.getObjectKey=n;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=(e,c)=>{Object.keys(e).forEach(t=>{const o=e[t];c({key:t,value:o})})};exports.objectForEach=r;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const n=require("./objectForEach.cjs"),s=(o,t)=>{const e={};return n.objectForEach(o,({key:r,value:i})=>{(Array.isArray(t)?t.some(c=>String(c)===r):String(t)===r)||(e[r]=i)}),e};exports.omitObjectProperties=s;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./objectForEach.cjs"),i=o=>{const e={};return r.objectForEach(o,({key:c,value:t})=>{t!==void 0&&(e[c]=t)}),e};exports.omitUndefinedObjectValues=i;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=e=>e.charAt(0).toUpperCase()+e.slice(1);exports.capitalize=t;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=require("../number/randomInt.cjs"),u=require("../array/randomArrayItem.cjs"),d=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"],i=["A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"],m=["0","1","2","3","4","5","6","7","8","9"],l=["!","#","@","~","$","^",".",",","-","+","%","?","*","="],p={1:d,2:i,3:m,4:l},g=(o={})=>{const{length:n=16}=o;let r="";for(let t=0;t<n;t++){const e=a.randomInt(1,4),s=p[e],c=u.randomArrayItem(s);r+=c}return r};exports.randomString=g;
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const a=(e,n={})=>{const{maxInsecureChars:r=4}=n,t="****";if(!e)return e;const c=Math.min(Math.floor(e.length/2),r),s=e.slice(e.length-c);return`${t}${s}`};exports.secureString=a;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliexme/js-utils",
3
- "version": "2.0.0",
3
+ "version": "2.1.1",
4
4
  "description": "Common JavaScript utilities",
5
5
  "private": false,
6
6
  "author": "Alexander Smirnov <al.smirnov996@gmail.com>",
@@ -15,8 +15,15 @@
15
15
  "files": [
16
16
  "dist"
17
17
  ],
18
- "main": "./dist/index.js",
19
- "typings": "./dist/index.d.ts",
18
+ "main": "./dist/index.cjs",
19
+ "module": "./dist/index.js",
20
+ "types": "./dist/index.d.ts",
21
+ "exports": {
22
+ ".": {
23
+ "import": "./dist/index.js",
24
+ "require": "./dist/index.cjs"
25
+ }
26
+ },
20
27
  "sideEffects": false,
21
28
  "publishConfig": {
22
29
  "access": "public"
@@ -25,5 +32,5 @@
25
32
  "build": "vite build",
26
33
  "ts:check": "tsc --noEmit"
27
34
  },
28
- "gitHead": "0ccacb743bc09c4cdaf216d69109bb269bd75cc5"
35
+ "gitHead": "1b5e61f9119616217f31eaf85d7fafafb197f964"
29
36
  }