@aliexme/js-utils 1.2.0-alpha.1 → 2.1.0

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.
@@ -1,2 +1,3 @@
1
1
  export * from './asyncify';
2
2
  export * from './asyncResult';
3
+ export * from './sleep';
@@ -0,0 +1 @@
1
+ export declare const sleep: (ms: number) => Promise<void>;
@@ -0,0 +1,4 @@
1
+ const t = async (e) => new Promise((s) => setTimeout(s, e));
2
+ export {
3
+ t as sleep
4
+ };
@@ -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,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.js CHANGED
@@ -1,38 +1,46 @@
1
1
  import { capitalize as e } from "./string/capitalize.js";
2
2
  import { secureString as m } from "./string/secureString.js";
3
- import { randomString as p } from "./string/randomString.js";
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 O } 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";
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";
19
23
  export {
20
- b as arraysIntersection,
21
- w as asyncResult,
22
- k as asyncify,
24
+ j as arraysIntersection,
25
+ J as asyncResult,
26
+ G as asyncify,
23
27
  e as capitalize,
24
- S as findObjectKey,
25
- A as getObjectKey,
26
- g as groupArrayItems,
27
- U as noop,
28
- q as objectForEach,
29
- E as omitObjectProperties,
30
- P as omitUndefinedObjectValues,
31
- O as randomArrayItem,
32
- i as randomInt,
33
- p as randomString,
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,
39
+ f as randomString,
34
40
  n as round,
35
41
  m as secureString,
36
- u as subtractArrays,
37
- s as uniquify
42
+ M as sleep,
43
+ b as subtractArrays,
44
+ C as throttle,
45
+ u as uniquify
38
46
  };
@@ -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';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aliexme/js-utils",
3
- "version": "1.2.0-alpha.1",
3
+ "version": "2.1.0",
4
4
  "description": "Common JavaScript utilities",
5
5
  "private": false,
6
6
  "author": "Alexander Smirnov <al.smirnov996@gmail.com>",
@@ -25,5 +25,5 @@
25
25
  "build": "vite build",
26
26
  "ts:check": "tsc --noEmit"
27
27
  },
28
- "gitHead": "2106876d0d328dd81f7ff58c6e558465d2ca8804"
28
+ "gitHead": "e00bfc1c4f1f64875f0e2b9c6b8119c5725fc77f"
29
29
  }