Skip to content

Commit

Permalink
update set type when removing a value (#312)
Browse files Browse the repository at this point in the history
  • Loading branch information
lorem--ipsum committed May 9, 2024
1 parent a4a101e commit 442164c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/datatypes/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,14 @@ export class Set implements Instance<SetValue, SetJS> {
if (!this.contains(value)) return this;
const keyFn = this.keyFn;
const key = keyFn(value);

const newElements = this.elements.filter(element => keyFn(element) !== key);

return new Set({
setType: this.setType, // There must be a set type since at least the value is there
elements: this.elements.filter(element => keyFn(element) !== key),
// In the case where we removed a string and all that's left is null, we need
// to change the setType accordingly
setType: getValueType(newElements.length ? newElements[0] : null),
elements: newElements,
});
}

Expand Down
5 changes: 5 additions & 0 deletions test/datatypes/set.mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ describe('Set', () => {
setType: 'STRING',
elements: ['A'],
});

expect(Set.fromJS([null, 'A']).remove('A').toJS()).to.deep.equal({
setType: 'NULL',
elements: [null],
});
});
});

Expand Down

0 comments on commit 442164c

Please sign in to comment.