@axi-engine/utils 0.1.3 → 0.1.4
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.mts +25 -1
- package/dist/index.d.ts +25 -1
- package/dist/index.js +24 -16
- package/dist/index.mjs +23 -16
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -83,8 +83,32 @@ declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
|
83
83
|
* Throws an error if the condition is true.
|
|
84
84
|
* @param conditionForThrow - If true, an error will be thrown.
|
|
85
85
|
* @param exceptionMessage - The message for the error.
|
|
86
|
+
* @throws {Error} if the value is true
|
|
86
87
|
*/
|
|
87
88
|
declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
|
|
89
|
+
/**
|
|
90
|
+
* Throws an error if the value is null, undefined, or an empty array.
|
|
91
|
+
*
|
|
92
|
+
* @template T The type of the value being checked.
|
|
93
|
+
* @param value The value to check.
|
|
94
|
+
* @param exceptionMessage The message for the error.
|
|
95
|
+
* @throws {Error} if the value is null, undefined, or an empty array.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // Example with a potentially undefined variable
|
|
99
|
+
* const user: { name: string } | undefined = findUser();
|
|
100
|
+
* throwIfEmpty(user, 'User not found');
|
|
101
|
+
* // From here, TypeScript knows `user` is not undefined.
|
|
102
|
+
* console.log(user.name);
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Example with an array
|
|
106
|
+
* const items: string[] = getItems();
|
|
107
|
+
* throwIfEmpty(items, 'Items array cannot be empty');
|
|
108
|
+
* // From here, you can safely access items[0] without checking for an empty array again.
|
|
109
|
+
* console.log('First item:', items[0]);
|
|
110
|
+
*/
|
|
111
|
+
declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
|
|
88
112
|
|
|
89
113
|
interface AxiEngineConfig {
|
|
90
114
|
pathSeparator: string;
|
|
@@ -155,4 +179,4 @@ declare function randInt(min: number, max: number): number;
|
|
|
155
179
|
*/
|
|
156
180
|
declare function randId(): string;
|
|
157
181
|
|
|
158
|
-
export { type AxiEngineConfig, type PathType, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNullOrUndefined, isNumber, isPercentageString, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, unique };
|
|
182
|
+
export { type AxiEngineConfig, type PathType, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNullOrUndefined, isNumber, isPercentageString, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, throwIfEmpty, unique };
|
package/dist/index.d.ts
CHANGED
|
@@ -83,8 +83,32 @@ declare function getRandomElement<T>(array: T[]): T | undefined;
|
|
|
83
83
|
* Throws an error if the condition is true.
|
|
84
84
|
* @param conditionForThrow - If true, an error will be thrown.
|
|
85
85
|
* @param exceptionMessage - The message for the error.
|
|
86
|
+
* @throws {Error} if the value is true
|
|
86
87
|
*/
|
|
87
88
|
declare function throwIf(conditionForThrow: boolean, exceptionMessage: string): void | never;
|
|
89
|
+
/**
|
|
90
|
+
* Throws an error if the value is null, undefined, or an empty array.
|
|
91
|
+
*
|
|
92
|
+
* @template T The type of the value being checked.
|
|
93
|
+
* @param value The value to check.
|
|
94
|
+
* @param exceptionMessage The message for the error.
|
|
95
|
+
* @throws {Error} if the value is null, undefined, or an empty array.
|
|
96
|
+
*
|
|
97
|
+
* @example
|
|
98
|
+
* // Example with a potentially undefined variable
|
|
99
|
+
* const user: { name: string } | undefined = findUser();
|
|
100
|
+
* throwIfEmpty(user, 'User not found');
|
|
101
|
+
* // From here, TypeScript knows `user` is not undefined.
|
|
102
|
+
* console.log(user.name);
|
|
103
|
+
*
|
|
104
|
+
* @example
|
|
105
|
+
* // Example with an array
|
|
106
|
+
* const items: string[] = getItems();
|
|
107
|
+
* throwIfEmpty(items, 'Items array cannot be empty');
|
|
108
|
+
* // From here, you can safely access items[0] without checking for an empty array again.
|
|
109
|
+
* console.log('First item:', items[0]);
|
|
110
|
+
*/
|
|
111
|
+
declare function throwIfEmpty<T>(value: T, exceptionMessage: string): asserts value is NonNullable<T>;
|
|
88
112
|
|
|
89
113
|
interface AxiEngineConfig {
|
|
90
114
|
pathSeparator: string;
|
|
@@ -155,4 +179,4 @@ declare function randInt(min: number, max: number): number;
|
|
|
155
179
|
*/
|
|
156
180
|
declare function randId(): string;
|
|
157
181
|
|
|
158
|
-
export { type AxiEngineConfig, type PathType, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNullOrUndefined, isNumber, isPercentageString, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, unique };
|
|
182
|
+
export { type AxiEngineConfig, type PathType, areArraysEqual, axiSettings, clampNumber, configure, ensurePathArray, ensurePathString, firstKeyOf, genArray, getPercentOf, getRandomElement, haveSameElements, isBoolean, isNullOrUndefined, isNumber, isPercentageString, isSequentialStart, isString, isUndefined, last, randId, randInt, shuffleArray, throwIf, throwIfEmpty, unique };
|
package/dist/index.js
CHANGED
|
@@ -43,6 +43,7 @@ __export(index_exports, {
|
|
|
43
43
|
randInt: () => randInt,
|
|
44
44
|
shuffleArray: () => shuffleArray,
|
|
45
45
|
throwIf: () => throwIf,
|
|
46
|
+
throwIfEmpty: () => throwIfEmpty,
|
|
46
47
|
unique: () => unique
|
|
47
48
|
});
|
|
48
49
|
module.exports = __toCommonJS(index_exports);
|
|
@@ -93,22 +94,6 @@ function getRandomElement(array) {
|
|
|
93
94
|
return array[index];
|
|
94
95
|
}
|
|
95
96
|
|
|
96
|
-
// src/assertion.ts
|
|
97
|
-
function throwIf(conditionForThrow, exceptionMessage) {
|
|
98
|
-
if (conditionForThrow) {
|
|
99
|
-
throw new Error(exceptionMessage);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
// src/config.ts
|
|
104
|
-
var defaultConfig = {
|
|
105
|
-
pathSeparator: "/"
|
|
106
|
-
};
|
|
107
|
-
var axiSettings = { ...defaultConfig };
|
|
108
|
-
function configure(newConfig) {
|
|
109
|
-
Object.assign(axiSettings, newConfig);
|
|
110
|
-
}
|
|
111
|
-
|
|
112
97
|
// src/guards.ts
|
|
113
98
|
function isNullOrUndefined(val) {
|
|
114
99
|
return val === void 0 || val === null;
|
|
@@ -129,6 +114,28 @@ function isPercentageString(val) {
|
|
|
129
114
|
return typeof val === "string" && val.endsWith("%");
|
|
130
115
|
}
|
|
131
116
|
|
|
117
|
+
// src/assertion.ts
|
|
118
|
+
function throwIf(conditionForThrow, exceptionMessage) {
|
|
119
|
+
if (conditionForThrow) {
|
|
120
|
+
throw new Error(exceptionMessage);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function throwIfEmpty(value, exceptionMessage) {
|
|
124
|
+
const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
|
|
125
|
+
if (isNullOrUndefined(value) || isArrayAndEmpty) {
|
|
126
|
+
throw new Error(exceptionMessage);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
// src/config.ts
|
|
131
|
+
var defaultConfig = {
|
|
132
|
+
pathSeparator: "/"
|
|
133
|
+
};
|
|
134
|
+
var axiSettings = { ...defaultConfig };
|
|
135
|
+
function configure(newConfig) {
|
|
136
|
+
Object.assign(axiSettings, newConfig);
|
|
137
|
+
}
|
|
138
|
+
|
|
132
139
|
// src/math.ts
|
|
133
140
|
function clampNumber(val, min, max) {
|
|
134
141
|
if (!isNullOrUndefined(min)) val = Math.max(val, min);
|
|
@@ -187,5 +194,6 @@ function randId() {
|
|
|
187
194
|
randInt,
|
|
188
195
|
shuffleArray,
|
|
189
196
|
throwIf,
|
|
197
|
+
throwIfEmpty,
|
|
190
198
|
unique
|
|
191
199
|
});
|
package/dist/index.mjs
CHANGED
|
@@ -44,22 +44,6 @@ function getRandomElement(array) {
|
|
|
44
44
|
return array[index];
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
-
// src/assertion.ts
|
|
48
|
-
function throwIf(conditionForThrow, exceptionMessage) {
|
|
49
|
-
if (conditionForThrow) {
|
|
50
|
-
throw new Error(exceptionMessage);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// src/config.ts
|
|
55
|
-
var defaultConfig = {
|
|
56
|
-
pathSeparator: "/"
|
|
57
|
-
};
|
|
58
|
-
var axiSettings = { ...defaultConfig };
|
|
59
|
-
function configure(newConfig) {
|
|
60
|
-
Object.assign(axiSettings, newConfig);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
47
|
// src/guards.ts
|
|
64
48
|
function isNullOrUndefined(val) {
|
|
65
49
|
return val === void 0 || val === null;
|
|
@@ -80,6 +64,28 @@ function isPercentageString(val) {
|
|
|
80
64
|
return typeof val === "string" && val.endsWith("%");
|
|
81
65
|
}
|
|
82
66
|
|
|
67
|
+
// src/assertion.ts
|
|
68
|
+
function throwIf(conditionForThrow, exceptionMessage) {
|
|
69
|
+
if (conditionForThrow) {
|
|
70
|
+
throw new Error(exceptionMessage);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
function throwIfEmpty(value, exceptionMessage) {
|
|
74
|
+
const isArrayAndEmpty = Array.isArray(value) && value.length === 0;
|
|
75
|
+
if (isNullOrUndefined(value) || isArrayAndEmpty) {
|
|
76
|
+
throw new Error(exceptionMessage);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/config.ts
|
|
81
|
+
var defaultConfig = {
|
|
82
|
+
pathSeparator: "/"
|
|
83
|
+
};
|
|
84
|
+
var axiSettings = { ...defaultConfig };
|
|
85
|
+
function configure(newConfig) {
|
|
86
|
+
Object.assign(axiSettings, newConfig);
|
|
87
|
+
}
|
|
88
|
+
|
|
83
89
|
// src/math.ts
|
|
84
90
|
function clampNumber(val, min, max) {
|
|
85
91
|
if (!isNullOrUndefined(min)) val = Math.max(val, min);
|
|
@@ -137,5 +143,6 @@ export {
|
|
|
137
143
|
randInt,
|
|
138
144
|
shuffleArray,
|
|
139
145
|
throwIf,
|
|
146
|
+
throwIfEmpty,
|
|
140
147
|
unique
|
|
141
148
|
};
|