Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-collection-native-array-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/parser": patch
---

fix: support native array methods on collections
6 changes: 6 additions & 0 deletions packages/parser/src/models/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ export abstract class Collection<T extends BaseModel = BaseModel, M extends Reco
super(...collections);
}

// Native Array transforms (map/filter/slice) construct results via species.
// Return Array so those methods do not call Collection with a length number.
static get [Symbol.species]() {
return Array;
}

abstract get(id: string): T | undefined;

has(id: string): boolean {
Expand Down
45 changes: 45 additions & 0 deletions packages/parser/test/models/collection.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,49 @@ describe('Collection model', function() {
expect(d.filterBy(filter)).toEqual([]);
});
});

describe('native Array transforms', function() {
it('should return a plain array from .map()', function() {
const item1 = new ItemModel({ name: 'name1' });
const item2 = new ItemModel({ name: 'name2' });
const d = new Model([item1, item2], { pointer: '/items' });

const mapped = d.map(item => item.name());

expect(mapped).toEqual(['name1', 'name2']);
expect(Array.isArray(mapped)).toEqual(true);
expect(mapped).not.toBeInstanceOf(Collection);
expect(mapped).not.toBeInstanceOf(Model);
expect(d.meta('pointer')).toEqual('/items');
expect(d.all()).toEqual([item1, item2]);
});

it('should return a plain array from .filter()', function() {
const item1 = new ItemModel({ name: 'keep' });
const item2 = new ItemModel({ name: 'drop' });
const d = new Model([item1, item2]);

const filtered = d.filter(item => item.name() === 'keep');

expect(filtered).toEqual([item1]);
expect(Array.isArray(filtered)).toEqual(true);
expect(filtered).not.toBeInstanceOf(Collection);
expect(filtered).not.toBeInstanceOf(Model);
});

it('should return a plain array from .slice() without mutating the source', function() {
const item1 = new ItemModel({ name: 'name1' });
const item2 = new ItemModel({ name: 'name2' });
const d = new Model([item1, item2], { pointer: '/items' });

const sliced = d.slice(0, 1);

expect(sliced).toEqual([item1]);
expect(Array.isArray(sliced)).toEqual(true);
expect(sliced).not.toBeInstanceOf(Collection);
expect(sliced).not.toBeInstanceOf(Model);
expect(d.length).toEqual(2);
expect(d.meta('pointer')).toEqual('/items');
});
});
});
11 changes: 11 additions & 0 deletions packages/parser/test/models/v2/security-requirements.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ describe('SecurityRequirements model', function () {
expect(requirements.has('anotherId')).toEqual(false);
});
});

describe('native Array transforms', function () {
it('should support .map() without throwing', function () {
const requirements = new SecurityRequirements([requirementItem]);
const mapped = requirements.map(requirement => requirement.meta('id' as any));

expect(mapped).toEqual(['test']);
expect(Array.isArray(mapped)).toEqual(true);
expect(mapped).not.toBeInstanceOf(SecurityRequirements);
});
});
});
Loading