Skip to content

Commit

Permalink
feat: setting comments and adding non-existing entries
Browse files Browse the repository at this point in the history
  • Loading branch information
alexzarbn committed Sep 11, 2022
1 parent b2a65b7 commit 8161720
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# .env File Entries Manager

This package allows you to easily manage `.env` file entries in a type-safe manner.

## Usage

```ts

let dotEnv = new DotEnv(`EXAMPLE_NUMBER_FIELD=123`);

// retrieving
const exampleNumberEntry = dotEnv.get<number>('EXAMPLE_NUMBER_FIELD');

console.log(exampleNumberEntry.key); // string ("EXAMPLE_NUMBER_FIELD")
console.log(exampleNumberEntry.value); // number (123)
console.log(exampleNumberEntry.comment); // undefined

// setting values and comments
dotEnv.set('EXAMPLE_NUMBER_FIELD', 456);
dotEnv.setComment('EXAMPLE_NUMBER_FIELD', 'Example comment');

dotEnv.set('NEW_FIELD', 'test');

// formatting entries to string
console.log(dotEnv.toString()); // "EXAMPLE_NUMBER_FIELD=456 # Example comment\nNEWFIELD=test"
```
20 changes: 18 additions & 2 deletions src/dotEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,30 @@ export class DotEnv {
return this.entries.find((entry) => entry.key === key) as DotEnvEntry<T>;
}

public set<T extends DotEnvEntryValueType>(key: string, value: DotEnvEntryValueType): DotEnvEntry<T> {
public set<T extends DotEnvEntryValueType>(key: string, value: T, comment?: string): DotEnvEntry<T> {
let entryIndex = this.entries.findIndex((entry) => entry.key === key);

if (entryIndex < 0) {
const entry = new DotEnvEntry(key, value, comment);

this.entries.push(entry);

return entry;
} else {
this.entries[entryIndex].value = value;

return this.entries[entryIndex] as DotEnvEntry<T>;
}
}

public setComment<T extends DotEnvEntryValueType>(key: string, comment: string | undefined): DotEnvEntry<T> {
let entryIndex = this.entries.findIndex((entry) => entry.key === key);

if (entryIndex < 0) {
throw new Error(`Unable to find entry with the given key: ${key}`);
}

this.entries[entryIndex].value = value;
this.entries[entryIndex].comment = comment;

return this.entries[entryIndex] as DotEnvEntry<T>;
}
Expand Down
5 changes: 2 additions & 3 deletions src/dotEnvEntry.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import {DotEnvEntryValueType} from "./dotEnvEntryValueType";

export class DotEnvEntry<T = DotEnvEntryValueType> {
public key?: string;
public value?: T;
public comment?: string;
constructor(public key?: string, public value?: T, public comment?: string) {
}

public toString(): string {
let formattedEntry = '';
Expand Down
25 changes: 25 additions & 0 deletions tests/dotEnv.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,31 @@ describe('DotEnv tests', () => {
expect(updatedEntry?.value).toBe('updated');
});

test('setting value and comment of a non-existing entry', () => {
const dotEnv = new DotEnv(`keyA=a`);

const addedEntry = dotEnv.set('NON_EXISTING', 'test-value', 'test-comment');

const entry = dotEnv.get('NON_EXISTING');

expect(entry?.value).toBe('test-value');
expect(addedEntry?.value).toBe('test-value');

expect(entry?.comment).toBe('test-comment');
expect(addedEntry?.comment).toBe('test-comment');
});

test('setting entry comment', () => {
const dotEnv = new DotEnv(`keyA=a`);

const updatedEntry = dotEnv.setComment('keyA', 'example comment');

const entry = dotEnv.get('keyA');

expect(entry?.comment).toBe('example comment');
expect(updatedEntry?.comment).toBe('example comment');
});

test('removing an entry', () => {
const dotEnv = new DotEnv(`keyA=a`);

Expand Down

0 comments on commit 8161720

Please sign in to comment.