Skip to content

Commit

Permalink
feat: return entries instead of values in toJSON method (#9345)
Browse files Browse the repository at this point in the history
* feat(collection): return entries instead of values in toJSON method

* test: adjust test

---------

Co-authored-by: Jiralite <33201955+Jiralite@users.noreply.github.com>
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 8, 2023
1 parent 67a2538 commit defeee5
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
9 changes: 7 additions & 2 deletions packages/collection/__tests__/collection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -828,9 +828,14 @@ describe('tap() tests', () => {
});

describe('toJSON() tests', () => {
test('it returns the values as an array', () => {
test('it returns the entries of the collection', () => {
const c = createTestCollection();
expect(c.toJSON()).toStrictEqual([1, 2, 3]);

expect(c.toJSON()).toStrictEqual([
['a', 1],
['b', 2],
['c', 3],
]);
});
});

Expand Down
2 changes: 1 addition & 1 deletion packages/collection/src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ export class Collection<K, V> extends Map<K, V> {

public toJSON() {
// toJSON is called recursively by JSON.stringify.
return [...this.values()];
return [...this.entries()];
}

private static defaultSort<V>(firstValue: V, secondValue: V): number {
Expand Down

0 comments on commit defeee5

Please sign in to comment.