@nemigo/helpers 0.8.0 → 0.8.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.
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import type { CanBeArray, CanBeReadonly } from "./types.js";
1
+ import type { CanBeArray, CanBePromise, CanBeReadonly } from "./types.js";
2
2
  /**
3
3
  * Добавляет ведущие нули к числу для достижения указанной длины
4
4
  *
@@ -52,3 +52,8 @@ export declare const useConditionGuard: (condition: unknown, ...args: any[]) =>
52
52
  * Сортирует ключи объекта в алфавитном порядке и возвращает новый объект с отсортированными ключами
53
53
  */
54
54
  export declare function sortObjectByKeys<T>(obj: T): T;
55
+ export type BoundaryFallback<E> = E | ((err: unknown) => CanBePromise<E>);
56
+ /**
57
+ * Приведение коллбэка к промису с отловом ошибок и fallback-ом
58
+ */
59
+ export declare const boundary: <R = undefined, E = R>(call: () => CanBePromise<R>, fallback?: BoundaryFallback<E>) => Promise<R | E>;
package/dist/index.js CHANGED
@@ -67,3 +67,19 @@ export function sortObjectByKeys(obj) {
67
67
  result[key] = obj[key];
68
68
  return result;
69
69
  }
70
+ /**
71
+ * Приведение коллбэка к промису с отловом ошибок и fallback-ом
72
+ */
73
+ export const boundary = async (call, fallback) => {
74
+ try {
75
+ return await call();
76
+ }
77
+ catch (e) {
78
+ console.error("boundary", e);
79
+ if (typeof fallback === "function") {
80
+ // @ts-expect-error <тут не типизировать>
81
+ return fallback(e);
82
+ }
83
+ return fallback;
84
+ }
85
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nemigo/helpers",
3
- "version": "0.8.0",
3
+ "version": "0.8.1",
4
4
  "private": false,
5
5
  "author": {
6
6
  "name": "Vlad Logvin",