@aiszlab/relax 1.2.19 → 1.2.22

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
+ export declare const contains: (root: Node | null | undefined, n?: Node | null) => boolean;
@@ -0,0 +1,20 @@
1
+ const contains = (root, n) => {
2
+ if (!root) {
3
+ return false;
4
+ }
5
+ // Use native if support
6
+ if (root.contains) {
7
+ return root.contains(n !== null && n !== void 0 ? n : null);
8
+ }
9
+ // `document.contains` not support with IE11
10
+ let node = n;
11
+ while (node) {
12
+ if (node === root) {
13
+ return true;
14
+ }
15
+ node = node.parentNode;
16
+ }
17
+ return false;
18
+ };
19
+
20
+ export { contains };
@@ -1,2 +1,3 @@
1
1
  import { scrollTo } from './scroll-to';
2
- export { scrollTo };
2
+ import { contains } from './contains';
3
+ export { scrollTo, contains };
@@ -1 +1,2 @@
1
1
  export { scrollTo } from './scroll-to.mjs';
2
+ export { contains } from './contains.mjs';
@@ -1,14 +1,34 @@
1
1
  /// <reference types="react" />
2
+ interface Props {
3
+ /**
4
+ * @description
5
+ * initial value
6
+ */
7
+ initialValue?: number;
8
+ /**
9
+ * @description
10
+ * max
11
+ */
12
+ max?: number;
13
+ /**
14
+ * @description
15
+ * min
16
+ */
17
+ min?: number;
18
+ }
2
19
  /**
3
20
  * @author murukal
4
21
  *
5
22
  * @description
6
23
  * a number counter with some useful apis
7
24
  */
8
- export declare const useCounter: () => {
25
+ export declare const useCounter: ({ max, min, ...props }?: Props) => {
9
26
  count: number;
10
- next: () => void;
11
- prev: () => void;
27
+ next: (step?: number) => void;
28
+ prev: (step?: number) => void;
12
29
  first: () => void;
30
+ last: () => void;
31
+ reset: () => void;
13
32
  setCount: import("react").Dispatch<import("react").SetStateAction<number>>;
14
33
  };
34
+ export {};
@@ -1,4 +1,6 @@
1
- import { useState, useCallback } from 'react';
1
+ import { __rest } from '../node_modules/tslib/tslib.es6.mjs';
2
+ import { useMemo, useState, useCallback } from 'react';
3
+ import { clamp } from '../utils/clamp.mjs';
2
4
 
3
5
  /**
4
6
  * @author murukal
@@ -6,22 +8,32 @@ import { useState, useCallback } from 'react';
6
8
  * @description
7
9
  * a number counter with some useful apis
8
10
  */
9
- const useCounter = () => {
10
- const [count, setCount] = useState(0);
11
- const next = useCallback(() => {
12
- setCount((prev) => prev + 1);
13
- }, []);
14
- const prev = useCallback(() => {
15
- setCount((prev) => prev - 1);
16
- }, []);
11
+ const useCounter = (_a = { initialValue: 0, max: Infinity, min: 0 }) => {
12
+ var { max = Infinity, min = 0 } = _a, props = __rest(_a, ["max", "min"]);
13
+ const initialValue = useMemo(() => { var _a; return clamp((_a = props.initialValue) !== null && _a !== void 0 ? _a : 0, min, max); }, [max, min, props.initialValue]);
14
+ const [count, setCount] = useState(initialValue);
15
+ const next = useCallback((step = 1) => {
16
+ setCount((prev) => Math.min(max, prev + step));
17
+ }, [max]);
18
+ const prev = useCallback((step = 1) => {
19
+ setCount((prev) => Math.max(prev - step));
20
+ }, [min]);
17
21
  const first = useCallback(() => {
18
- setCount(0);
19
- }, []);
22
+ setCount(min);
23
+ }, [min]);
24
+ const last = useCallback(() => {
25
+ setCount(max);
26
+ }, [max]);
27
+ const reset = useCallback(() => {
28
+ setCount(initialValue);
29
+ }, [initialValue]);
20
30
  return {
21
31
  count,
22
32
  next,
23
33
  prev,
24
34
  first,
35
+ last,
36
+ reset,
25
37
  setCount
26
38
  };
27
39
  };
@@ -1,17 +1,23 @@
1
+ import { useEvent } from '../hooks/use-event';
1
2
  interface Options {
2
3
  /**
4
+ * @description
3
5
  * The delay time (in milliseconds) until the debounce function is called.
4
6
  * default 1000
7
+ *
8
+ * @example
9
+ * 2000
5
10
  */
6
11
  readonly delay: number;
7
12
  }
13
+ type Callable<T, R> = ReturnType<typeof useEvent<[T], R>>;
8
14
  /**
9
15
  * @author murukal
10
16
  *
11
17
  * @description
12
18
  * debounce callback
13
19
  */
14
- export declare const useDebounceCallback: <T>(callable: Function, { delay }?: Options) => {
20
+ export declare const useDebounceCallback: <T, R>(_callable: (args_0: T) => R, options?: Options) => {
15
21
  next: (value: T) => void;
16
22
  complete: () => void;
17
23
  cancel: () => void;
@@ -1,5 +1,6 @@
1
1
  import { useRef, useCallback, useEffect } from 'react';
2
2
  import { Observable, debounceTime } from 'rxjs';
3
+ import { useEvent } from './use-event.mjs';
3
4
 
4
5
  /**
5
6
  * @author murukal
@@ -7,54 +8,45 @@ import { Observable, debounceTime } from 'rxjs';
7
8
  * @description
8
9
  * debounce callback
9
10
  */
10
- const useDebounceCallback = (callable, { delay = 1000 } = {
11
- delay: 1000
12
- }) => {
13
- // trigger
14
- const trigger = useRef();
15
- // listener
16
- const listener = useRef();
17
- /// initialze listener function for debouce
18
- const initialize = useCallback(() => {
19
- listener.current = new Observable((subscriber) => {
11
+ const useDebounceCallback = (_callable, options) => {
12
+ var _a;
13
+ const trigger = useRef(null);
14
+ const listener = useRef(null);
15
+ const delay = (_a = options === null || options === void 0 ? void 0 : options.delay) !== null && _a !== void 0 ? _a : 1000;
16
+ const callable = useEvent(_callable);
17
+ const listen = useCallback(() => {
18
+ const listened = new Observable((subscriber) => {
20
19
  trigger.current = subscriber;
21
20
  })
22
21
  .pipe(debounceTime(delay))
23
22
  .subscribe({
24
- next: (value) => {
25
- callable(value);
26
- },
27
- complete: () => {
28
- initialize();
29
- }
23
+ next: callable,
24
+ complete: listen
30
25
  });
31
- }, [callable, delay]);
32
- /// initialize debounce function
33
- /// when delay / callable changed, need reinitialize
26
+ listener.current = listened;
27
+ return listened;
28
+ }, [delay]);
34
29
  useEffect(() => {
35
- initialize();
30
+ const listened = listen();
36
31
  // dispose
37
32
  return () => {
38
- var _a;
39
- (_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
33
+ listened.unsubscribe();
34
+ listener.current = null;
35
+ trigger.current = null;
40
36
  };
41
- }, [initialize]);
42
- /// next function has been debounced for hooks user
43
- const next = useCallback((value) => {
37
+ }, [listen]);
38
+ const next = useEvent((value) => {
44
39
  var _a;
45
40
  (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
46
- }, []);
47
- /// flush the debounce
48
- const complete = useCallback(() => {
41
+ });
42
+ const complete = useEvent(() => {
49
43
  var _a;
50
44
  (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
51
- }, []);
52
- /// cancel only valid in debounce time
53
- /// if the callback has been called, it can not be canceled
54
- const cancel = useCallback(() => {
45
+ });
46
+ const cancel = useEvent(() => {
55
47
  var _a;
56
48
  (_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
57
- }, []);
49
+ });
58
50
  return {
59
51
  next,
60
52
  complete,
@@ -1,3 +1,3 @@
1
- type Callback<U extends Array<unknown>, R> = (...args: U) => R;
2
- export declare const useEvent: <U extends unknown[], R>(callback: Callback<U, R>) => Callback<U, R>;
1
+ type Callable<U extends Array<unknown>, R> = (...args: U) => R;
2
+ export declare const useEvent: <U extends unknown[], R>(callable: Callable<U, R>) => Callable<U, R>;
3
3
  export {};
@@ -1,8 +1,8 @@
1
1
  import { useRef, useCallback } from 'react';
2
2
 
3
- const useEvent = (callback) => {
3
+ const useEvent = (callable) => {
4
4
  const ref = useRef();
5
- ref.current = callback;
5
+ ref.current = callable;
6
6
  return useCallback((...args) => ref.current(...args), []);
7
7
  };
8
8
 
@@ -1,4 +1,5 @@
1
1
  import { type MutableRefObject, type RefCallback } from 'react';
2
+ import type { Nullable } from '../utils/null-able';
2
3
  type Refable<T> = RefCallback<T> | MutableRefObject<T>;
3
- export declare const useRefs: <T>(...refs: Refable<T>[]) => (trigger: T) => void;
4
+ export declare const useRefs: <T>(...refs: Nullable<Refable<Nullable<T>>>[]) => (trigger: T) => void;
4
5
  export {};
@@ -12,6 +12,8 @@ const useRefs = (...refs) => {
12
12
  return useMemo(() => {
13
13
  return (trigger) => {
14
14
  refs.forEach((ref) => {
15
+ if (!ref)
16
+ return;
15
17
  mount(ref, trigger);
16
18
  });
17
19
  };
@@ -1,17 +1,23 @@
1
+ import { useEvent } from '../hooks/use-event';
1
2
  interface Options {
2
3
  /**
4
+ * @description
3
5
  * The delay time (in milliseconds) until the throttle function is called.
4
6
  * default 1000
7
+ *
8
+ * @example
9
+ * 2000
5
10
  */
6
11
  readonly duration: number;
7
12
  }
13
+ type Callable<T, R> = ReturnType<typeof useEvent<[T], R>>;
8
14
  /**
9
15
  * @author murukal
10
16
  *
11
17
  * @description
12
18
  * throttle callback
13
19
  */
14
- export declare const useThrottleCallback: <T>(callable: Function, { duration }?: Options) => {
20
+ export declare const useThrottleCallback: <T, R>(_callable: (args_0: T) => R, options?: Options) => {
15
21
  next: (value: T) => void;
16
22
  complete: () => void;
17
23
  cancel: () => void;
@@ -1,5 +1,6 @@
1
1
  import { useRef, useCallback, useEffect } from 'react';
2
2
  import { Observable, throttleTime } from 'rxjs';
3
+ import { useEvent } from './use-event.mjs';
3
4
 
4
5
  /**
5
6
  * @author murukal
@@ -7,54 +8,45 @@ import { Observable, throttleTime } from 'rxjs';
7
8
  * @description
8
9
  * throttle callback
9
10
  */
10
- const useThrottleCallback = (callable, { duration = 1000 } = {
11
- duration: 1000
12
- }) => {
13
- // runner
14
- const runner = useRef();
15
- // listener
16
- const listener = useRef();
17
- /// initialze listener function for debouce
18
- const initialize = useCallback(() => {
19
- listener.current = new Observable((subscriber) => {
20
- runner.current = subscriber;
11
+ const useThrottleCallback = (_callable, options) => {
12
+ var _a;
13
+ const trigger = useRef(null);
14
+ const listener = useRef(null);
15
+ const duration = (_a = options === null || options === void 0 ? void 0 : options.duration) !== null && _a !== void 0 ? _a : 1000;
16
+ const callable = useEvent(_callable);
17
+ const listen = useCallback(() => {
18
+ const listened = new Observable((subscriber) => {
19
+ trigger.current = subscriber;
21
20
  })
22
21
  .pipe(throttleTime(duration))
23
22
  .subscribe({
24
- next: (value) => {
25
- callable(value);
26
- },
27
- complete: () => {
28
- initialize();
29
- }
23
+ next: callable,
24
+ complete: listen
30
25
  });
31
- }, [callable, duration]);
32
- /// initialize debounce function
33
- /// when delay / callable changed, need reinitialize
26
+ listener.current = listened;
27
+ return listened;
28
+ }, [duration]);
34
29
  useEffect(() => {
35
- initialize();
30
+ const listened = listen();
36
31
  // dispose
37
32
  return () => {
38
- var _a;
39
- (_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
33
+ listened.unsubscribe();
34
+ listener.current = null;
35
+ trigger.current = null;
40
36
  };
41
- }, [initialize]);
42
- /// next function has been debounced for hooks user
43
- const next = useCallback((value) => {
37
+ }, [listen]);
38
+ const next = useEvent((value) => {
44
39
  var _a;
45
- (_a = runner.current) === null || _a === void 0 ? void 0 : _a.next(value);
46
- }, []);
47
- /// flush the debounce
48
- const complete = useCallback(() => {
40
+ (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.next(value);
41
+ });
42
+ const complete = useEvent(() => {
49
43
  var _a;
50
- (_a = runner.current) === null || _a === void 0 ? void 0 : _a.complete();
51
- }, []);
52
- /// cancel only valid in debounce time
53
- /// if the callback has been called, it can not be canceled
54
- const cancel = useCallback(() => {
44
+ (_a = trigger.current) === null || _a === void 0 ? void 0 : _a.complete();
45
+ });
46
+ const cancel = useEvent(() => {
55
47
  var _a;
56
48
  (_a = listener.current) === null || _a === void 0 ? void 0 : _a.unsubscribe();
57
- }, []);
49
+ });
58
50
  return {
59
51
  next,
60
52
  complete,
package/dist/index.d.ts CHANGED
@@ -47,3 +47,5 @@ export { type RequiredIn } from './utils/required-in';
47
47
  export { type ThenableEffectCallback, callAsEffect } from './utils/thenable-effect-callback';
48
48
  export { unique, uniqueBy } from './utils/unique';
49
49
  export { range } from './utils/range';
50
+ export { clamp } from './utils/clamp';
51
+ export { chain } from './utils/chain';
package/dist/index.mjs CHANGED
@@ -32,3 +32,5 @@ export { isThenable } from './is/is-thenable.mjs';
32
32
  export { callAsEffect } from './utils/thenable-effect-callback.mjs';
33
33
  export { unique, uniqueBy } from './utils/unique.mjs';
34
34
  export { range } from './utils/range.mjs';
35
+ export { clamp } from './utils/clamp.mjs';
36
+ export { chain } from './utils/chain.mjs';
@@ -0,0 +1,5 @@
1
+ /**
2
+ * @description
3
+ * if pointer event is usable
4
+ */
5
+ export declare const isPointerUsable: () => boolean;
@@ -0,0 +1,4 @@
1
+ import type { Partialable } from '../utils/partial-able';
2
+ type Callable<R> = (...args: R[]) => void;
3
+ export declare const chain: <S>(...callbacks: Partialable<Callable<S>>[]) => Callable<S>;
4
+ export {};
@@ -0,0 +1,9 @@
1
+ const chain = (...callbacks) => {
2
+ return (...args) => {
3
+ callbacks.forEach((callback) => {
4
+ callback === null || callback === void 0 ? void 0 : callback(...args);
5
+ });
6
+ };
7
+ };
8
+
9
+ export { chain };
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @description
3
+ * get mid value
4
+ * @param value number
5
+ * @param min number
6
+ * @param max number
7
+ * @returns number
8
+ */
9
+ export declare const clamp: (value: number, min: number, max: number) => number;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @description
3
+ * get mid value
4
+ * @param value number
5
+ * @param min number
6
+ * @param max number
7
+ * @returns number
8
+ */
9
+ const clamp = (value, min, max) => {
10
+ return Math.min(Math.max(value, min), max);
11
+ };
12
+
13
+ export { clamp };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aiszlab/relax",
3
- "version": "1.2.19",
3
+ "version": "1.2.22",
4
4
  "description": "react utils collection",
5
5
  "type": "module",
6
6
  "exports": {