Skip to content

Commit

Permalink
fix: has method respects falsy values (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch authored Feb 18, 2023
1 parent 8211e0b commit 69f06e7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/lib/cls.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,14 @@ describe('ClsService', () => {
expect(service.has('b')).toEqual(false);
});
});

it('checks key presence if falsy (string key)', () => {
service.run(() => {
service.set('c', false);
expect(service.has('c')).toEqual(true);
});
});

it('checks key presence (symbol key)', () => {
const sym = Symbol('sym');
service.run(() => {
Expand All @@ -116,6 +124,14 @@ describe('ClsService', () => {
expect(service.has(sym)).toEqual(false);
});
});

it('checks key presence if falsy (symbol key)', () => {
const sym = Symbol('sym');
service.run(() => {
service.set(sym, '');
expect(service.has(sym)).toEqual(true);
});
});
});

describe('edge cases', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/cls.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ export class ClsService<S extends ClsStore = ClsStore> {
has(key: string | symbol): boolean {
const store = this.als.getStore();
if (typeof key === 'symbol') {
return !!store[key];
return store[key] !== undefined;
}
return !!getValueFromPath(store as S, key as any);
return getValueFromPath(store as S, key as any) !== undefined;
}

/**
Expand Down

0 comments on commit 69f06e7

Please sign in to comment.