Skip to content

Commit 2fad381

Browse files
committed
add removeAttribute method
1 parent c5ab0b2 commit 2fad381

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

packages/core/src/scope.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,25 @@ export class Scope {
356356
return this.setAttributes({ [key]: value });
357357
}
358358

359+
/**
360+
* Removes the attribute with the given key from the scope.
361+
*
362+
* @param key - The attribute key.
363+
*
364+
* @example
365+
* ```typescript
366+
* scope.removeAttribute('is_admin');
367+
* ```
368+
*/
369+
public removeAttribute(key: string): this {
370+
if (key in this._attributes) {
371+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
372+
delete this._attributes[key];
373+
this._notifyScopeListeners();
374+
}
375+
return this;
376+
}
377+
359378
/**
360379
* Set an object that will be merged into existing extra on the scope,
361380
* and will be sent as extra data with the event.

packages/core/test/lib/scope.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,15 @@ describe('Scope', () => {
244244
intArray: { value: [1, 2, 3], type: 'integer[]', unit: 'ms' },
245245
});
246246
});
247+
248+
it('notifies scope listeners once per call', () => {
249+
const scope = new Scope();
250+
const listener = vi.fn();
251+
scope.addScopeListener(listener);
252+
scope.setAttribute('str', 'b');
253+
scope.setAttribute('int', 1);
254+
expect(listener).toHaveBeenCalledTimes(2);
255+
});
247256
});
248257

249258
describe('setAttributes', () => {
@@ -299,6 +308,42 @@ describe('Scope', () => {
299308
intArray: { type: 'integer[]', value: [1, 2, 3], unit: 'ms' },
300309
});
301310
});
311+
312+
it('notifies scope listeners once per call', () => {
313+
const scope = new Scope();
314+
const listener = vi.fn();
315+
scope.addScopeListener(listener);
316+
scope.setAttributes({ str: 'b', int: 1 });
317+
scope.setAttributes({ bool: true });
318+
expect(listener).toHaveBeenCalledTimes(2);
319+
});
320+
});
321+
322+
describe('removeAttribute', () => {
323+
it('removes an attribute', () => {
324+
const scope = new Scope();
325+
scope.setAttribute('str', 'b');
326+
scope.setAttribute('int', 1);
327+
scope.removeAttribute('str');
328+
expect(scope['_attributes']).toEqual({ int: { type: 'integer', value: 1 } });
329+
});
330+
331+
it('notifies scope listeners after deletion', () => {
332+
const scope = new Scope();
333+
const listener = vi.fn();
334+
scope.addScopeListener(listener);
335+
});
336+
337+
it('does nothing if the attribute does not exist', () => {
338+
const scope = new Scope();
339+
const listener = vi.fn();
340+
341+
scope.addScopeListener(listener);
342+
scope.removeAttribute('str');
343+
344+
expect(scope['_attributes']).toEqual({});
345+
expect(listener).not.toHaveBeenCalled();
346+
});
302347
});
303348

304349
test('setUser', () => {

0 commit comments

Comments
 (0)