Skip to content

Commit 282b834

Browse files
committed
feat: add read-float helper
1 parent 6dde86f commit 282b834

File tree

7 files changed

+73
-3
lines changed

7 files changed

+73
-3
lines changed

README.MD

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ This library simplifies reading, transforming, and requiring environment variabl
1212
**Table of Contents**
1313
- [Installation](#installation)
1414
- [Usage](#usage)
15+
- [write](#write)
16+
- [read](#read)
17+
- [readArray](#read-array)
18+
- [readBool](#read-bool)
19+
- [readFloat](#read-float)
20+
- [readInt](#read-int)
21+
- [readNumber](#read-number)
22+
- [readNumberArray](#read-number-array)
1523
- [Contributing](#contributing)
1624
- [License](#license)
1725

@@ -80,8 +88,24 @@ readBool('bar', false); // boolean
8088
// false
8189
```
8290

91+
### Read Float
92+
The readFloat method makes it possible to read an environment variable as a float.
93+
A fallback value can be defined as the second argument.
94+
95+
```typescript
96+
import { readFloat, write } from 'envix';
97+
98+
write('foo', '1');
99+
100+
readFloat('foo'); // number | undefined
101+
// 1.0
102+
103+
readFloat('bar', 2.0); // number
104+
// 2.0
105+
```
106+
83107
### Read Int
84-
The readBool method makes it possible to read an environment variable as a boolean.
108+
The readInt method makes it possible to read an environment variable as a integer.
85109
A fallback value can be defined as the second argument.
86110

87111
```typescript
@@ -97,7 +121,7 @@ readInt('bar', 2); // number
97121
```
98122

99123
### Read Number
100-
The readBool method makes it possible to read an environment variable as a number.
124+
The readNumber method makes it possible to read an environment variable as a number.
101125
A fallback value can be defined as the second argument.
102126

103127
```typescript

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export * from './read';
55
export * from './read-array';
66
export * from './read-bool';
77
export * from './read-int';
8+
export * from './read-float';
89
export * from './read-number';
910
export * from './read-number-array';
1011
export * from './write';

src/read-float.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { read } from './read';
2+
import { toFloat } from './utils';
3+
4+
export function readFloat(key: string) : number | undefined;
5+
export function readFloat<T>(key: string, alt: T) : T | number;
6+
export function readFloat<T>(key: string, alt?: T) : any {
7+
const value = read(key) as any;
8+
if (typeof value !== 'undefined') {
9+
return toFloat(value) ?? alt;
10+
}
11+
12+
return alt;
13+
}

src/utils/float.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function toFloat(value: any) : number | undefined {
2+
const num = Number.parseFloat(value);
3+
if (Number.isNaN(num) || Number.isNaN(value)) {
4+
return undefined;
5+
}
6+
7+
return num;
8+
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './bool';
2+
export * from './float';
23
export * from './int';
34
export * from './number';

test/unit/read-float.spec.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { readFloat, write } from '../../src';
2+
3+
describe('src/read-float.ts', () => {
4+
it('should read float env', () => {
5+
write('foo', '1');
6+
7+
let result = readFloat('foo');
8+
expect(result).toEqual(1.0);
9+
10+
write('foo', 'foo');
11+
12+
result = readFloat('foo');
13+
expect(result).toBeUndefined();
14+
15+
write('foo', undefined);
16+
17+
result = readFloat('foo', 2.0);
18+
expect(result).toEqual(2.0);
19+
20+
result = readFloat('foo');
21+
expect(result).toBeUndefined();
22+
});
23+
});

test/unit/read-int.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { readInt, write } from '../../src';
22

33
describe('src/read-int.ts', () => {
4-
it('should read number env', () => {
4+
it('should read int env', () => {
55
write('foo', '1.0');
66

77
let result = readInt('foo');

0 commit comments

Comments
 (0)