Skip to content

Commit 6fe4b81

Browse files
committed
add removeAttribute method
1 parent c94bcd6 commit 6fe4b81

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
@@ -358,6 +358,25 @@ export class Scope {
358358
return this.setAttributes({ [key]: value });
359359
}
360360

361+
/**
362+
* Removes the attribute with the given key from the scope.
363+
*
364+
* @param key - The attribute key.
365+
*
366+
* @example
367+
* ```typescript
368+
* scope.removeAttribute('is_admin');
369+
* ```
370+
*/
371+
public removeAttribute(key: string): this {
372+
if (key in this._attributes) {
373+
// eslint-disable-next-line @typescript-eslint/no-dynamic-delete
374+
delete this._attributes[key];
375+
this._notifyScopeListeners();
376+
}
377+
return this;
378+
}
379+
361380
/**
362381
* Set an object that will be merged into existing extra on the scope,
363382
* 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
@@ -213,6 +213,15 @@ describe('Scope', () => {
213213
intArray: { value: [1, 2, 3], type: 'integer[]', unit: 'ms' },
214214
});
215215
});
216+
217+
it('notifies scope listeners once per call', () => {
218+
const scope = new Scope();
219+
const listener = vi.fn();
220+
scope.addScopeListener(listener);
221+
scope.setAttribute('str', 'b');
222+
scope.setAttribute('int', 1);
223+
expect(listener).toHaveBeenCalledTimes(2);
224+
});
216225
});
217226

218227
describe('setAttributes', () => {
@@ -268,6 +277,42 @@ describe('Scope', () => {
268277
intArray: { type: 'integer[]', value: [1, 2, 3], unit: 'ms' },
269278
});
270279
});
280+
281+
it('notifies scope listeners once per call', () => {
282+
const scope = new Scope();
283+
const listener = vi.fn();
284+
scope.addScopeListener(listener);
285+
scope.setAttributes({ str: 'b', int: 1 });
286+
scope.setAttributes({ bool: true });
287+
expect(listener).toHaveBeenCalledTimes(2);
288+
});
289+
});
290+
291+
describe('removeAttribute', () => {
292+
it('removes an attribute', () => {
293+
const scope = new Scope();
294+
scope.setAttribute('str', 'b');
295+
scope.setAttribute('int', 1);
296+
scope.removeAttribute('str');
297+
expect(scope['_attributes']).toEqual({ int: { type: 'integer', value: 1 } });
298+
});
299+
300+
it('notifies scope listeners after deletion', () => {
301+
const scope = new Scope();
302+
const listener = vi.fn();
303+
scope.addScopeListener(listener);
304+
});
305+
306+
it('does nothing if the attribute does not exist', () => {
307+
const scope = new Scope();
308+
const listener = vi.fn();
309+
310+
scope.addScopeListener(listener);
311+
scope.removeAttribute('str');
312+
313+
expect(scope['_attributes']).toEqual({});
314+
expect(listener).not.toHaveBeenCalled();
315+
});
271316
});
272317

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

0 commit comments

Comments
 (0)