@baklavabg/common-ts 1.0.8 → 1.0.10

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
@@ -6,7 +6,6 @@ export { loadFile } from "./utils/file.utils.js";
6
6
  export { tryCatch } from "./utils/tryCatchDecorator.js";
7
7
  export { TryCatch } from "./utils/tryCatchSafeDecorator.js";
8
8
  export { equals, greaterThan, isEmpty, isEven, isNegative, isNotEmpty, isNull, isNullOrUndefined, isOdd, isPositive, isUndefined, isZero, lessThan, notEquals, } from "./safeExit/conditionHelpers.js";
9
- export type { ConditionalResult } from "./safeExit/types.js";
10
9
  export { createFolder } from "./utils/file.utils.js";
11
10
  export { formatDate, capitalize, randomString } from "./utils/tmp.js";
12
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,UAAU,EACV,UAAU,EACV,MAAM,EACN,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,MAAM,gCAAgC,CAAC;AACxC,YAAY,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAE7D,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,YAAY,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAC;AAErD,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,UAAU,EACV,UAAU,EACV,MAAM,EACN,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -5,6 +5,7 @@ export { loadFile } from "./utils/file.utils.js";
5
5
  export { tryCatch } from "./utils/tryCatchDecorator.js";
6
6
  export { TryCatch } from "./utils/tryCatchSafeDecorator.js";
7
7
  export { equals, greaterThan, isEmpty, isEven, isNegative, isNotEmpty, isNull, isNullOrUndefined, isOdd, isPositive, isUndefined, isZero, lessThan, notEquals, } from "./safeExit/conditionHelpers.js";
8
+ // export type { ConditionalResult } from "./safeExit/types.js";
8
9
  export { createFolder } from "./utils/file.utils.js";
9
10
  export { formatDate, capitalize, randomString } from "./utils/tmp.js";
10
11
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,UAAU,EACV,UAAU,EACV,MAAM,EACN,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,MAAM,gCAAgC,CAAC;AAGxC,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AAGpC,OAAO,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAEvD,OAAO,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAE7C,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,QAAQ,EAAE,MAAM,8BAA8B,CAAC;AACxD,OAAO,EAAE,QAAQ,EAAE,MAAM,kCAAkC,CAAC;AAE5D,OAAO,EACH,MAAM,EACN,WAAW,EACX,OAAO,EACP,MAAM,EACN,UAAU,EACV,UAAU,EACV,MAAM,EACN,iBAAiB,EACjB,KAAK,EACL,UAAU,EACV,WAAW,EACX,MAAM,EACN,QAAQ,EACR,SAAS,GACZ,MAAM,gCAAgC,CAAC;AACxC,gEAAgE;AAEhE,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AAErD,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC"}
@@ -1,3 +1,4 @@
1
1
  export { returnIf, returnResultIf } from "./reurnIf.js";
2
2
  export { safe } from "./safe.js";
3
+ export { ConditionalResult } from "./types.js";
3
4
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/safeExit/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/safeExit/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
@@ -1,3 +1,4 @@
1
1
  export { returnIf, returnResultIf } from "./reurnIf.js";
2
2
  export { safe } from "./safe.js";
3
+ export { ConditionalResult } from "./types.js";
3
4
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/safeExit/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/safeExit/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACxD,OAAO,EAAE,IAAI,EAAE,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC"}
@@ -1,32 +1,16 @@
1
- import { type ConditionalResult } from "./types.js";
1
+ import { ConditionalResult } from "./types.js";
2
2
  /**
3
- * Returns early from a function if the condition passes
4
- * @param condition Function that tests the value
5
- * @param value Value to test
6
- * @param logMessage Optional message to log if condition passes
7
- * @param returnValue Value to return if condition passes (default: null)
8
- */
9
- export declare function returnIf<T, R = null>(condition: (value: T) => boolean, value: T, logMessage?: string, returnValue?: R): void;
10
- /**
11
- * Returns early from a function if the condition expression is true
12
- * @param condition Boolean expression to evaluate
3
+ * Returns early from a function if the condition is true
4
+ * @param condition Boolean or function that returns boolean
13
5
  * @param logMessage Optional message to log if condition is true
14
- * @param returnValue Value to return if condition is true (default: null)
15
- */
16
- export declare function returnIf<R = null>(condition: boolean, logMessage?: string, returnValue?: R): void;
17
- /**
18
- * Returns a result object if the condition passes
19
- * @param condition Function that tests the value
20
- * @param value Value to test
21
- * @param returnValue Value to include in result if condition passes
22
- * @param logMessage Optional message to log and include in result if condition passes
6
+ * @param returnValue Value to return if condition is true
23
7
  */
24
- export declare function returnResultIf<T, R = any>(condition: (value: T) => boolean, value: T, returnValue?: R, logMessage?: string): ConditionalResult<R>;
8
+ export declare function returnIf(condition: boolean | (() => boolean), logMessage?: string, returnValue?: any): void;
25
9
  /**
26
- * Returns a result object if the condition expression is true
27
- * @param condition Boolean expression to evaluate
10
+ * Returns a result object if the condition is true
11
+ * @param condition Boolean or function that returns boolean
28
12
  * @param returnValue Value to include in result if condition is true
29
13
  * @param logMessage Optional message to log and include in result if condition is true
30
14
  */
31
- export declare function returnResultIf<R = any>(condition: boolean, returnValue?: R, logMessage?: string): ConditionalResult<R>;
15
+ export declare function returnResultIf(condition: boolean | (() => boolean), returnValue?: any, logMessage?: string): ConditionalResult<any>;
32
16
  //# sourceMappingURL=reurnIf.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"reurnIf.d.ts","sourceRoot":"","sources":["../../src/saf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;;;;;GAKG;AACH,wBAAgB,cAAc,CAC1B,SAAS,EAAE,OAAO,GAAG,CAAC,MAAM,OAAO,CAAC,EACpC,WAAW,CAAC,EAAE,GAAG,EACjB,UAAU,CAAC,EAAE,MAAM,GACpB,iBAAiB,CAAC,GAAG,CAAC,CAWxB"}
@@ -2,92 +2,65 @@
2
2
  * Purpose:
3
3
  * This file provides utility functions to help you exit early from a function or return a result object if a certain condition is met.
4
4
  *
5
- * Overloads:
6
- * Both `returnIf` and `returnResultIf` are overloaded functions. This means you can call them in two different ways:
7
- * 1. With a function as the condition and a value to test.
8
- * 2. With a simple boolean condition.
9
- *
10
5
  * Functions:
11
6
  * - `returnIf`: Throws a special error to exit the current function early if a condition is true, optionally logging a message and returning a value.
12
7
  * - `returnResultIf`: Returns a result object with a value and message if a condition is true, otherwise returns an object with nulls.
13
8
  *
14
- * Example usages for each overload:
9
+ * Example usages:
15
10
  *
16
- * // --- returnIf overloads ---
17
- * // 1. Using a function as the condition
18
- * function processNumber(n: number) {
19
- * returnIf((x) => x < 0, n, "Negative number not allowed", "NEGATIVE");
11
+ * // --- returnIf examples ---
12
+ * // Using a boolean condition
13
+ * function doSomething(flag: boolean) {
14
+ * returnIf(flag, "Flag was true, exiting early", 123);
20
15
  * // ...rest of function
21
16
  * }
22
17
  *
23
- * // 2. Using a boolean as the condition
24
- * function doSomething(flag: boolean) {
25
- * returnIf(flag, "Flag was true, exiting early", 123);
18
+ * // Using a function condition (now combined with value)
19
+ * function processNumber(n: number) {
20
+ * returnIf(() => n < 0, "Negative number not allowed", "NEGATIVE");
26
21
  * // ...rest of function
27
22
  * }
28
23
  *
29
- * // --- returnResultIf overloads ---
30
- * // 1. Using a function as the condition
31
- * const result = returnResultIf((s) => s === "", "", "EMPTY", "String is empty");
32
- * // result: { value: "EMPTY", message: "String is empty" }
24
+ * // --- returnResultIf examples ---
25
+ * // Using a boolean condition
26
+ * const result1 = returnResultIf(false, "OK", "All good");
27
+ * // result1: { value: null, message: null }
33
28
  *
34
- * // 2. Using a boolean as the condition
35
- * const result2 = returnResultIf(false, "OK", "All good");
36
- * // result2: { value: null, message: null }
29
+ * // Using a function condition
30
+ * const result2 = returnResultIf(() => "" === "", "EMPTY", "String is empty");
31
+ * // result2: { value: "EMPTY", message: "String is empty" }
37
32
  */
38
33
  import { print } from "../utils/log.utils.js";
39
- import { EarlyReturnError } from "./types.js";
40
- export function returnIf(conditionOrValue, valueOrLogMessage, logMessageOrReturnValue, returnValue) {
41
- let shouldReturn;
42
- let actualReturnValue;
43
- let actualLogMessage;
44
- if (typeof conditionOrValue === "boolean") {
45
- // Simple boolean condition overload
46
- shouldReturn = conditionOrValue;
47
- actualLogMessage = valueOrLogMessage;
48
- actualReturnValue = logMessageOrReturnValue ?? null;
49
- }
50
- else {
51
- // Function condition overload
52
- shouldReturn = conditionOrValue(valueOrLogMessage);
53
- actualLogMessage = logMessageOrReturnValue;
54
- actualReturnValue = returnValue ?? null;
55
- }
34
+ import { ConditionalResult, EarlyReturnError } from "./types.js";
35
+ /**
36
+ * Returns early from a function if the condition is true
37
+ * @param condition Boolean or function that returns boolean
38
+ * @param logMessage Optional message to log if condition is true
39
+ * @param returnValue Value to return if condition is true
40
+ */
41
+ export function returnIf(condition, logMessage, returnValue) {
42
+ const shouldReturn = typeof condition === "boolean" ? condition : condition();
56
43
  if (shouldReturn) {
57
- if (actualLogMessage) {
58
- print.yellow(actualLogMessage);
44
+ if (logMessage) {
45
+ print.yellow(logMessage);
59
46
  }
60
- throw new EarlyReturnError(actualReturnValue, actualLogMessage);
47
+ throw new EarlyReturnError(returnValue ?? null, logMessage);
61
48
  }
62
49
  }
63
- export function returnResultIf(conditionOrValue, valueOrReturnValue, returnValueOrLogMessage, logMessage) {
64
- let shouldReturn;
65
- let actualReturnValue;
66
- let actualLogMessage;
67
- if (typeof conditionOrValue === "boolean") {
68
- // Simple boolean condition overload
69
- shouldReturn = conditionOrValue;
70
- actualReturnValue = valueOrReturnValue;
71
- actualLogMessage = returnValueOrLogMessage;
72
- }
73
- else {
74
- // Function condition overload
75
- shouldReturn = conditionOrValue(valueOrReturnValue);
76
- actualReturnValue = returnValueOrLogMessage;
77
- actualLogMessage = logMessage;
78
- }
50
+ /**
51
+ * Returns a result object if the condition is true
52
+ * @param condition Boolean or function that returns boolean
53
+ * @param returnValue Value to include in result if condition is true
54
+ * @param logMessage Optional message to log and include in result if condition is true
55
+ */
56
+ export function returnResultIf(condition, returnValue, logMessage) {
57
+ const shouldReturn = typeof condition === "boolean" ? condition : condition();
79
58
  if (shouldReturn) {
80
- if (actualLogMessage) {
81
- print.yellow(actualLogMessage);
59
+ if (logMessage) {
60
+ print.yellow(logMessage);
82
61
  }
83
- return {
84
- value: actualReturnValue ?? null,
85
- message: actualLogMessage ?? null,
86
- };
62
+ return new ConditionalResult(returnValue ?? null, logMessage ?? null);
87
63
  }
88
- return {
89
- value: null,
90
- message: null,
91
- };
64
+ return new ConditionalResult(null, null);
92
65
  }
93
66
  //# sourceMappingURL=reurnIf.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"reurnIf.js","sourceRoot":"","sources":["../../src/safeExit/reurnIf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoCG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,gBAAgB,EAA0B,MAAM,YAAY,CAAC;AAiBtE,MAAM,UAAU,QAAQ,CACpB,gBAAmD,EACnD,iBAA8B,EAC9B,uBAAoC,EACpC,WAAe;IAEf,IAAI,YAAqB,CAAC;IAC1B,IAAI,iBAA2B,CAAC;IAChC,IAAI,gBAAoC,CAAC;IAEzC,IAAI,OAAO,gBAAgB,KAAK,SAAS,EAAE,CAAC;QACxC,oCAAoC;QACpC,YAAY,GAAG,gBAAgB,CAAC;QAChC,gBAAgB,GAAG,iBAA2B,CAAC;QAC/C,iBAAiB,GAAI,uBAA6B,IAAI,IAAI,CAAC;IAC/D,CAAC;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sourceRoot":"","sources":["../../src/safeExit/reurnIf.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+BG;AACH,OAAO,EAAE,KAAK,EAAE,MAAM,uBAAuB,CAAC;AAC9C,OAAO,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEjE;;;;;GAKG;AACH,MAAM,UAAU,QAAQ,CAAC,SAAoC,EAAE,UAAmB,EAAE,WAAiB;IACjG,MAAM,YAAY,GAAG,OAAO,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAE9E,IAAI,YAAY,EAAE,CAAC;QACf,IAAI,UAAU,EAAE,CAAC;YACb,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC7B,CAAC;QACD,MAAM,IAAI,gBAAgB,CAAC,WAAW,IAAI,IAAI,EAAE,UAAU,CAAC,CAAC;IAChE,CAAC;AACL,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,cAAc,CAC1B,SAAoC,EACpC,WAAiB,EACjB,UAAmB;IAEnB,MAAM,YAAY,GAAG,OAAO,SAAS,KAAK,SAAS,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;IAE9E,IAAI,YAAY,EAAE,CAAC;QACf,IAAI,UAAU,EAAE,CAAC;YACb,KAAK,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;QAC7B,CAAC;QACD,OAAO,IAAI,iBAAiB,CAAC,WAAW,IAAI,IAAI,EAAE,UAAU,IAAI,IAAI,CAAC,CAAC;IAC1E,CAAC;IAED,OAAO,IAAI,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC7C,CAAC"}
@@ -0,0 +1,32 @@
1
+ import { ConditionalResult } from "./types.js";
2
+ /**
3
+ * Returns early from a function if the condition passes
4
+ * @param condition Function that tests the value
5
+ * @param value Value to test
6
+ * @param logMessage Optional message to log if condition passes
7
+ * @param returnValue Value to return if condition passes (default: null)
8
+ */
9
+ export declare function returnIf<T, R = null>(condition: (value: T) => boolean, value: T, logMessage?: string, returnValue?: R): void;
10
+ /**
11
+ * Returns early from a function if the condition expression is true
12
+ * @param condition Boolean expression to evaluate
13
+ * @param logMessage Optional message to log if condition is true
14
+ * @param returnValue Value to return if condition is true (default: null)
15
+ */
16
+ export declare function returnIf<R = null>(condition: boolean, logMessage?: string, returnValue?: R): void;
17
+ /**
18
+ * Returns a result object if the condition passes
19
+ * @param condition Function that tests the value
20
+ * @param value Value to test
21
+ * @param returnValue Value to include in result if condition passes
22
+ * @param logMessage Optional message to log and include in result if condition passes
23
+ */
24
+ export declare function returnResultIf<T, R = any>(condition: (value: T) => boolean, value: T, returnValue?: R, logMessage?: string): ConditionalResult<R>;
25
+ /**
26
+ * Returns a result object if the condition expression is true
27
+ * @param condition Boolean expression to evaluate
28
+ * @param returnValue Value to include in result if condition is true
29
+ * @param logMessage Optional message to log and include in result if condition is true
30
+ */
31
+ export declare function returnResultIf<R = any>(condition: boolean, returnValue?: R, logMessage?: string): ConditionalResult<R>;
32
+ //# sourceMappingURL=reurnIf_prev.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reurnIf_prev.d.ts","sourceRoot":"","sources":["../../src/safeExit/reurnIf_prev.ts"],"names":[],"mappings":"AAsCA,OAAO,EAAE,iBAAiB,EAAoB,MAAM,YAAY,CAAC;AAEjE;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,CAAC,EAAE,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAAE,KAAK,EAAE,CAAC,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;AAC9H;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,CAAC,GAAG,IAAI,EAAE,SAAS,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC;AA+BnG;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,CAAC,EAAE,CAAC,GAAG,GAAG,EACrC,SAAS,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,EAChC,KAAK,EAAE,CAAC,EACR,WAAW,CAAC,EAAE,CAAC,EACf,UAAU,CAAC,EAAE,MAAM,GACpB,iBAAiB,CAAC,CAAC,CAAC,CAAC;AACxB;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,CAAC,GAAG,GAAG,EAAE,SAAS,EAAE,OAAO,EAAE,WAAW,CAAC,EAAE,CAAC,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,iBAAiB,CAAC,CAAC,CAAC,CAAC"}
@@ -0,0 +1,87 @@
1
+ /**
2
+ * Purpose:
3
+ * This file provides utility functions to help you exit early from a function or return a result object if a certain condition is met.
4
+ *
5
+ * Overloads:
6
+ * Both `returnIf` and `returnResultIf` are overloaded functions. This means you can call them in two different ways:
7
+ * 1. With a function as the condition and a value to test.
8
+ * 2. With a simple boolean condition.
9
+ *
10
+ * Functions:
11
+ * - `returnIf`: Throws a special error to exit the current function early if a condition is true, optionally logging a message and returning a value.
12
+ * - `returnResultIf`: Returns a result object with a value and message if a condition is true, otherwise returns an object with nulls.
13
+ *
14
+ * Example usages for each overload:
15
+ *
16
+ * // --- returnIf overloads ---
17
+ * // 1. Using a function as the condition
18
+ * function processNumber(n: number) {
19
+ * returnIf((x) => x < 0, n, "Negative number not allowed", "NEGATIVE");
20
+ * // ...rest of function
21
+ * }
22
+ *
23
+ * // 2. Using a boolean as the condition
24
+ * function doSomething(flag: boolean) {
25
+ * returnIf(flag, "Flag was true, exiting early", 123);
26
+ * // ...rest of function
27
+ * }
28
+ *
29
+ * // --- returnResultIf overloads ---
30
+ * // 1. Using a function as the condition
31
+ * const result = returnResultIf((s) => s === "", "", "EMPTY", "String is empty");
32
+ * // result: { value: "EMPTY", message: "String is empty" }
33
+ *
34
+ * // 2. Using a boolean as the condition
35
+ * const result2 = returnResultIf(false, "OK", "All good");
36
+ * // result2: { value: null, message: null }
37
+ */
38
+ import { print } from "../utils/log.utils.js";
39
+ import { ConditionalResult, EarlyReturnError } from "./types.js";
40
+ export function returnIf(conditionOrValue, valueOrLogMessage, logMessageOrReturnValue, returnValue) {
41
+ let shouldReturn;
42
+ let actualReturnValue;
43
+ let actualLogMessage;
44
+ if (typeof conditionOrValue === "boolean") {
45
+ // Simple boolean condition overload
46
+ shouldReturn = conditionOrValue;
47
+ actualLogMessage = valueOrLogMessage;
48
+ actualReturnValue = logMessageOrReturnValue ?? null;
49
+ }
50
+ else {
51
+ // Function condition overload
52
+ shouldReturn = conditionOrValue(valueOrLogMessage);
53
+ actualLogMessage = logMessageOrReturnValue;
54
+ actualReturnValue = returnValue ?? null;
55
+ }
56
+ if (shouldReturn) {
57
+ if (actualLogMessage) {
58
+ print.yellow(actualLogMessage);
59
+ }
60
+ throw new EarlyReturnError(actualReturnValue, actualLogMessage);
61
+ }
62
+ }
63
+ export function returnResultIf(conditionOrValue, valueOrReturnValue, returnValueOrLogMessage, logMessage) {
64
+ let shouldReturn;
65
+ let finalReturnValue;
66
+ let finalLogMessage;
67
+ if (typeof conditionOrValue === "boolean") {
68
+ // Simple boolean condition overload
69
+ shouldReturn = conditionOrValue;
70
+ finalReturnValue = valueOrReturnValue;
71
+ finalLogMessage = returnValueOrLogMessage;
72
+ }
73
+ else {
74
+ // Function condition overload
75
+ shouldReturn = conditionOrValue(valueOrReturnValue);
76
+ finalReturnValue = returnValueOrLogMessage;
77
+ finalLogMessage = logMessage;
78
+ }
79
+ if (shouldReturn) {
80
+ if (finalLogMessage) {
81
+ print.yellow(finalLogMessage);
82
+ }
83
+ return new ConditionalResult(finalReturnValue ?? null, finalLogMessage ?? null);
84
+ }
85
+ return new ConditionalResult(null, null);
86
+ }
87
+ //# sourceMappingURL=reurnIf_prev.js.map
@@ -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oCAAoC;QACpC,YAAY,GAAG,gBAAgB,CAAC;QAChC,gBAAgB,GAAG,kBAAuB,CAAC;QAC3C,eAAe,GAAG,uBAAiC,CAAC;IACxD,CAAC;SAAM,CAAC;QACJ,8BAA8B;QAC9B,YAAY,GAAG,gBAAgB,CAAC,kBAAuB,CAAC,CAAC;QACzD,gBAAgB,GAAG,uBAA4B,CAAC;QAChD,eAAe,GAAG,UAAU,CAAC;IACjC,CAAC;IAED,IAAI,YAAY,EAAE,CAAC;QACf,IAAI,eAAe,EAAE,CAAC;YAClB,KAAK,CAAC,MAAM,CAAC,eAAe,CAAC,CAAC;QAClC,CAAC;QACD,OAAO,IAAI,iBAAiB,CAAC,gBAAgB,IAAI,IAAI,EAAE,eAAe,IAAI,IAAI,CAAC,CAAC;IACpF,CAAC;IAED,OAAO,IAAI,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;AAC7C,CAAC"}
@@ -1,6 +1,11 @@
1
- export interface ConditionalResult<T = any> {
2
- value: T | null;
3
- message: string | null;
1
+ export declare class ConditionalResult<T = any> {
2
+ private _value;
3
+ private _message;
4
+ constructor(value: T | null, message: string | null);
5
+ get value(): T | null;
6
+ get message(): string | null;
7
+ log(): T | null;
8
+ logAppend(externalLog: string[]): T | null;
4
9
  }
5
10
  export declare class EarlyReturnError extends Error {
6
11
  returnValue: any;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/safeExit/types.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,iBAAiB,CAAC,CAAC,GAAG,GAAG;IACtC,KAAK,EAAE,CAAC,GAAG,IAAI,CAAA;IACf,OAAO,EAAE,MAAM,GAAG,IAAI,CAAA;CACzB;AAGD,qBAAa,gBAAiB,SAAQ,KAAK;IACpB,WAAW,EAAE,GAAG;gBAAhB,WAAW,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,MAAM;CAIxD"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/safeExit/types.ts"],"names":[],"mappings":"AAAA,qBAAa,iBAAiB,CAAC,CAAC,GAAG,GAAG;IAClC,OAAO,CAAC,MAAM,CAAW;IACzB,OAAO,CAAC,QAAQ,CAAgB;gBAEpB,KAAK,EAAE,CAAC,GAAG,IAAI,EAAE,OAAO,EAAE,MAAM,GAAG,IAAI;IAKnD,IAAI,KAAK,IAAI,CAAC,GAAG,IAAI,CAEpB;IAED,IAAI,OAAO,IAAI,MAAM,GAAG,IAAI,CAE3B;IAED,GAAG,IAAI,CAAC,GAAG,IAAI;IAOf,SAAS,CAAC,WAAW,EAAE,MAAM,EAAE,GAAG,CAAC,GAAG,IAAI;CAM7C;AAGD,qBAAa,gBAAiB,SAAQ,KAAK;IAE5B,WAAW,EAAE,GAAG;gBAAhB,WAAW,EAAE,GAAG,EACvB,OAAO,CAAC,EAAE,MAAM;CAKvB"}
@@ -1,3 +1,29 @@
1
+ export class ConditionalResult {
2
+ _value;
3
+ _message;
4
+ constructor(value, message) {
5
+ this._value = value;
6
+ this._message = message;
7
+ }
8
+ get value() {
9
+ return this._value;
10
+ }
11
+ get message() {
12
+ return this._message;
13
+ }
14
+ log() {
15
+ if (this._message) {
16
+ console.log(this._message);
17
+ }
18
+ return this._value;
19
+ }
20
+ logAppend(externalLog) {
21
+ if (this.message) {
22
+ externalLog.push(this.message);
23
+ }
24
+ return this._value;
25
+ }
26
+ }
1
27
  // Custom error class for early returns
2
28
  export class EarlyReturnError extends Error {
3
29
  returnValue;
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/safeExit/types.ts"],"names":[],"mappings":"AAKA,uCAAuC;AACvC,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IACpB;IAAnB,YAAmB,WAAgB,EAAE,OAAgB;QACjD,KAAK,CAAC,OAAO,IAAI,cAAc,CAAC,CAAA;QADjB,gBAAW,GAAX,WAAW,CAAK;QAE/B,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAA;IAClC,CAAC;CACJ"}
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/safeExit/types.ts"],"names":[],"mappings":"AAAA,MAAM,OAAO,iBAAiB;IAClB,MAAM,CAAW;IACjB,QAAQ,CAAgB;IAEhC,YAAY,KAAe,EAAE,OAAsB;QAC/C,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,IAAI,CAAC,QAAQ,GAAG,OAAO,CAAC;IAC5B,CAAC;IAED,IAAI,KAAK;QACL,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,IAAI,OAAO;QACP,OAAO,IAAI,CAAC,QAAQ,CAAC;IACzB,CAAC;IAED,GAAG;QACC,IAAI,IAAI,CAAC,QAAQ,EAAE,CAAC;YAChB,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAC/B,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;IAED,SAAS,CAAC,WAAqB;QAC3B,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;YACf,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACnC,CAAC;QACD,OAAO,IAAI,CAAC,MAAM,CAAC;IACvB,CAAC;CACJ;AAED,uCAAuC;AACvC,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAE5B;IADX,YACW,WAAgB,EACvB,OAAgB;QAEhB,KAAK,CAAC,OAAO,IAAI,cAAc,CAAC,CAAC;QAH1B,gBAAW,GAAX,WAAW,CAAK;QAIvB,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;IACnC,CAAC;CACJ"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baklavabg/common-ts",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "type": "module",
5
5
  "description": "A comprehensive TypeScript library with common utility functions and custom types",
6
6
  "main": "./dist/index.js",