Skip to content

Commit de756c5

Browse files
committed
fix: remove getIntegerThrowInvalid
1 parent 26c0bc5 commit de756c5

4 files changed

Lines changed: 14 additions & 63 deletions

File tree

README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ import {
8686
getValidatedThrow,
8787
getDefaultIfInvalid,
8888

89-
getIntegerThrowInvalid, // equivalent to get
9089
getIntegerDefault,
9190
getIntegerThrow,
9291
} from "envprocessor";
@@ -235,7 +234,6 @@ import {
235234
getValidatedThrow,
236235
getDefaultIfInvalid,
237236

238-
getIntegerThrowInvalid, // equivalent to get
239237
getIntegerDefault,
240238
getIntegerThrow,
241239
} from "envprocessor";
@@ -251,7 +249,6 @@ const {
251249
getValidatedThrow,
252250
getDefaultIfInvalid,
253251

254-
getIntegerThrowInvalid, // equivalent to get
255252
getIntegerDefault,
256253
getIntegerThrow,
257254
} = require("envprocessor");
@@ -268,7 +265,6 @@ console.log(`get('USER') >${get("USER")}`);
268265
- `getTrimmedThrow(key)`: Retrieves, trims, and throws an error if the variable is missing or empty after trimming.
269266
- `getValidatedThrow(key, validator)`: Retrieves and validates using a `RegExp` or a custom function. The validator function should return an error message `string` on failure, or `null`/`undefined` on success. It can also throw an error directly.
270267
- `getDefaultIfInvalid(key, defaultValue, validator)`: Retrieves and validates using a `RegExp` or a custom function. If validation fails (regex mismatch, function returns anything other than `null`/`undefined`, or function throws) or the variable is not found, it returns the specified `defaultValue`.
271-
- `getIntegerThrowInvalid(key)`: Retrieves and converts to an integer. Throws if it exists but is not a valid integer.
272268
- `getIntegerDefault(key, defaultValue)`: Retrieves as an integer, or returns `defaultValue` if not found or invalid.
273269
- `getIntegerThrow(key)`: Retrieves as an integer, throws if missing or invalid.
274270

examples/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
get,
3535
getDefault,
3636
getThrow,
37-
getIntegerThrowInvalid,
3837
getIntegerDefault,
3938
getIntegerThrow,
4039
} from "env";

src/source/env.ts

Lines changed: 13 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -173,36 +173,6 @@ export function getDefaultIfInvalid(
173173

174174
const intTest = /^-?\d+$/;
175175

176-
/**
177-
* Retrieves an environment variable and converts it to an integer.
178-
*
179-
* @param key - The name of the environment variable.
180-
* @returns The integer value if it exists and is valid, otherwise undefined.
181-
* @throws Will throw an error if the variable exists but cannot be converted to a valid integer.
182-
*/
183-
export function getIntegerThrowInvalid(key: string): number | undefined {
184-
if (has(key)) {
185-
// We know the value exists because has(key) returned true
186-
const value = get(key) as string;
187-
188-
if (!intTest.test(value)) {
189-
throw th(`env var ${key} is not a number. value >${value}<, doesn't match regex >${intTest}<`);
190-
}
191-
192-
const int = parseInt(value, 10);
193-
194-
const strint = String(int);
195-
196-
if (!intTest.test(strint)) {
197-
throw th(`parseInt(${value}, 10) returned ${strint}, doesn't match regex >${intTest}<`);
198-
}
199-
200-
return int;
201-
}
202-
203-
return undefined;
204-
}
205-
206176
/**
207177
* Retrieves an environment variable as an integer, or returns a default value if not found or invalid.
208178
*
@@ -212,11 +182,7 @@ export function getIntegerThrowInvalid(key: string): number | undefined {
212182
*/
213183
export function getIntegerDefault(key: string, defaultValue: number): number {
214184
try {
215-
const val = getIntegerThrowInvalid(key);
216-
217-
if (typeof val === "number") {
218-
return val;
219-
}
185+
return getIntegerThrow(key);
220186
} catch (e) {}
221187

222188
return defaultValue;
@@ -230,11 +196,19 @@ export function getIntegerDefault(key: string, defaultValue: number): number {
230196
* @throws Will throw an error if the variable is not defined or cannot be converted to a valid integer.
231197
*/
232198
export function getIntegerThrow(key: string): number {
233-
const val = getIntegerThrowInvalid(key);
199+
const value = getThrow(key);
200+
201+
if (!intTest.test(value)) {
202+
throw th(`env var ${key} is not a number. value >${value}<, doesn't match regex >${intTest}<`);
203+
}
204+
205+
const int = parseInt(value, 10);
206+
207+
const strint = String(int);
234208

235-
if (typeof val === "number") {
236-
return val;
209+
if (!intTest.test(strint)) {
210+
throw th(`parseInt(${value}, 10) returned ${strint}, doesn't match regex >${intTest}<`);
237211
}
238212

239-
throw th(`env var ${key} is not defined or is not a number`);
213+
return int;
240214
}

tests/env.unit.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
mockEnv,
55
get,
66
getDefault,
7-
getIntegerThrowInvalid,
87
getIntegerDefault,
98
getIntegerThrow,
109
getThrow,
@@ -45,23 +44,6 @@ it("getThrow - ABC -> DEF", async () => {
4544
expect(() => getThrow("GHI")).toThrowError("env.js: env var GHI is not defined");
4645
});
4746

48-
it("getIntegerThrowInvalid - ABC -> 123", async () => {
49-
mockEnv({
50-
ABC: "123",
51-
ZZZ: "not a number",
52-
BIG: "90071992547409919007199254740991",
53-
});
54-
55-
expect(getIntegerThrowInvalid("ABC")).toEqual(123);
56-
expect(getIntegerThrowInvalid("GHI")).toEqual(undefined);
57-
expect(() => getIntegerThrowInvalid("ZZZ")).toThrowError(
58-
"env.js: env var ZZZ is not a number. value >not a number<, doesn't match regex >/^-?\\d+$/<",
59-
);
60-
expect(() => getIntegerThrowInvalid("BIG")).toThrowError(
61-
"env.js: parseInt(90071992547409919007199254740991, 10) returned 9.007199254740992e+31, doesn't match regex >/^-?\\d+$/<",
62-
);
63-
});
64-
6547
it("getIntegerDefault - ABC -> 123", async () => {
6648
mockEnv({
6749
ABC: "123",
@@ -96,7 +78,7 @@ test("getIntegerThrow", async () => {
9678
}
9779

9880
expect(data).toEqual({
99-
throw: "env.js: env var GHI is not defined or is not a number",
81+
throw: "env.js: env var GHI is not defined",
10082
throw2: "env.js: env var ZZZ is not a number. value >not a number<, doesn't match regex >/^-?\\d+$/<",
10183
});
10284
});

0 commit comments

Comments
 (0)