Skip to content

Commit

Permalink
feat: add read-float helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 9, 2024
1 parent 6aed04b commit 12594c9
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 3 deletions.
28 changes: 26 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ This library simplifies reading, transforming, and requiring environment variabl
**Table of Contents**
- [Installation](#installation)
- [Usage](#usage)
- [write](#write)
- [read](#read)
- [readArray](#read-array)
- [readBool](#read-bool)
- [readFloat](#read-float)
- [readInt](#read-int)
- [readNumber](#read-number)
- [readNumberArray](#read-number-array)
- [Contributing](#contributing)
- [License](#license)

Expand Down Expand Up @@ -80,8 +88,24 @@ readBool('bar', false); // boolean
// false
```

### Read Float
The readFloat method makes it possible to read an environment variable as a float.
A fallback value can be defined as the second argument.

```typescript
import { readFloat, write } from 'envix';

write('foo', '1');

readFloat('foo'); // number | undefined
// 1.0

readFloat('bar', 2.0); // number
// 2.0
```

### Read Int
The readBool method makes it possible to read an environment variable as a boolean.
The readInt method makes it possible to read an environment variable as a integer.
A fallback value can be defined as the second argument.

```typescript
Expand All @@ -97,7 +121,7 @@ readInt('bar', 2); // number
```

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

```typescript
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export * from './read';
export * from './read-array';
export * from './read-bool';
export * from './read-int';
export * from './read-float';
export * from './read-number';
export * from './read-number-array';
export * from './write';
13 changes: 13 additions & 0 deletions src/read-float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { read } from './read';
import { toFloat } from './utils';

export function readFloat(key: string) : number | undefined;
export function readFloat<T>(key: string, alt: T) : T | number;
export function readFloat<T>(key: string, alt?: T) : any {
const value = read(key) as any;
if (typeof value !== 'undefined') {
return toFloat(value) ?? alt;
}

return alt;
}
8 changes: 8 additions & 0 deletions src/utils/float.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function toFloat(value: any) : number | undefined {
const num = Number.parseFloat(value);
if (Number.isNaN(num) || Number.isNaN(value)) {
return undefined;
}

return num;
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './bool';
export * from './float';
export * from './int';
export * from './number';
23 changes: 23 additions & 0 deletions test/unit/read-float.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { readFloat, write } from '../../src';

describe('src/read-float.ts', () => {
it('should read float env', () => {
write('foo', '1');

let result = readFloat('foo');
expect(result).toEqual(1.0);

write('foo', 'foo');

result = readFloat('foo');
expect(result).toBeUndefined();

write('foo', undefined);

result = readFloat('foo', 2.0);
expect(result).toEqual(2.0);

result = readFloat('foo');
expect(result).toBeUndefined();
});
});
2 changes: 1 addition & 1 deletion test/unit/read-int.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { readInt, write } from '../../src';

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

let result = readInt('foo');
Expand Down

0 comments on commit 12594c9

Please sign in to comment.