Skip to content

Commit

Permalink
feat: optimized read-array & read-number-array helper & enhanced docs
Browse files Browse the repository at this point in the history
  • Loading branch information
tada5hi committed Feb 9, 2024
1 parent 8b08f6c commit 6aed04b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
33 changes: 33 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ npm install envix --save
## Usage

### Write
The write method makes it possible to set an environment variable retrospectively for later accesses.

```typescript
import { write } from 'envix';
Expand All @@ -32,6 +33,9 @@ write('foo', 'bar');
```

### Read
The read method accepts the key of the environment variable as the first argument and an
alternative value as the second argument, which is returned if the variable does not exist.
If no argument is passed, an object with all environment variables is returned.

```typescript
import { read, write } from 'envix';
Expand All @@ -46,53 +50,82 @@ read('bar', 'baz'); // string
```

### Read Array
The readArray method makes it possible to read an environment variable as a string array.
A fallback value can be defined as the second argument.
```typescript
import { readArray, write } from 'envix';

write('foo', 'bar,baz');

readArray('foo'); // string[] | undefined
// ['bar', 'baz']

readArray('bar', ['foo']); // string[]
// ['foo']
```

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

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

write('foo', 'true');

readBool('foo'); // boolean | undefined
// true

readBool('bar', false); // boolean
// false
```

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

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

write('foo', '1.0');

readInt('foo'); // number | undefined
// 1

readInt('bar', 2); // number
// 2
```

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

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

write('foo', '1.0');

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

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

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

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

write('foo', '1.0,2.1');

readNumberArray('foo'); // number[] | undefined
// [1.0,2.1]

readNumberArray('bar', [2,3]); // number[]
// [2,3]
```

## Contributing
Expand Down
8 changes: 5 additions & 3 deletions src/read-array.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { read } from './read';

export function readArray(key: string) : string[] | undefined {
export function readArray(key: string) : string[] | undefined;
export function readArray(key: string, alt: string[]) : string[];
export function readArray(key: string, alt?: string[]): any {
const value = read(key);
if (typeof value === 'undefined') {
return undefined;
return alt;
}

return value.split(',');
return value.split(',').filter(Boolean);
}
5 changes: 4 additions & 1 deletion src/read-number-array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ export function readNumberArray(key: string) : number[] | undefined {
return undefined;
}

const nums = value.split(',').map((el) => toNumber(el));
const nums = value.split(',')
.filter(Boolean)
.map((el) => toNumber(el));

if (nums.some((el) => typeof el === 'undefined')) {
return undefined;
}
Expand Down

0 comments on commit 6aed04b

Please sign in to comment.