Skip to content

Commit caa95da

Browse files
authored
fix(bundle): create array of certain size upfront (#71)
1 parent c52431d commit caa95da

2 files changed

Lines changed: 68 additions & 40 deletions

File tree

src/__tests__/bundle.spec.ts

Lines changed: 67 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { cloneDeep, get } from 'lodash';
1+
import { cloneDeep } from 'lodash';
22

33
import { BUNDLE_ROOT, bundleTarget } from '../bundle';
4-
import { pointerToPath } from '../pointerToPath';
54
import { safeStringify } from '../safeStringify';
65

76
describe('bundleTargetPath()', () => {
@@ -360,7 +359,7 @@ describe('bundleTargetPath()', () => {
360359
},
361360
[BUNDLE_ROOT]: {
362361
parameters: [
363-
undefined,
362+
null,
364363
{
365364
schema: {
366365
name: 'param',
@@ -562,6 +561,17 @@ describe('bundleTargetPath()', () => {
562561
const document = {
563562
components: {
564563
schemas: {
564+
bar: {
565+
type: 'array',
566+
items: [
567+
{
568+
type: 'string',
569+
},
570+
{
571+
type: 'number',
572+
},
573+
],
574+
},
565575
foo: {
566576
allOf: [
567577
{},
@@ -575,37 +585,70 @@ describe('bundleTargetPath()', () => {
575585
},
576586
],
577587
},
578-
foo_again: {
579-
allOf: [
580-
{
581-
$ref: '#/components/schemas/foo/allOf/0',
582-
},
583-
{
584-
type: 'object',
585-
properties: {
586-
attributes: {
587-
allOf: [
588-
{},
589-
{
590-
$ref: '#/components/schemas/foo/allOf/1/properties/attributes/allOf/1',
591-
},
592-
],
588+
baz: {
589+
type: 'object',
590+
properties: {
591+
attributes: {
592+
anyOf: [
593+
{
594+
$ref: '#/components/schemas/bar/items/1',
593595
},
594-
},
596+
{
597+
$ref: '#/components/schemas/foo/allOf/1/properties/attributes/allOf/1',
598+
},
599+
],
595600
},
596-
],
601+
},
597602
},
598603
},
599604
},
600605
};
601606

602607
const result = bundleTarget({
603608
document,
604-
path: '#/components/schemas/foo_again',
609+
path: '#/components/schemas/baz',
605610
});
606611

607-
expect(
608-
'0' in get(result, pointerToPath('#/__bundled__/components/schemas/foo/allOf/1/properties/attributes/allOf')),
609-
).toBe(true);
612+
expect(result).toStrictEqual({
613+
type: 'object',
614+
properties: {
615+
attributes: {
616+
anyOf: [
617+
{
618+
$ref: '#/__bundled__/components/schemas/bar/items/1',
619+
},
620+
{
621+
$ref: '#/__bundled__/components/schemas/foo/allOf/1/properties/attributes/allOf/1',
622+
},
623+
],
624+
},
625+
},
626+
__bundled__: {
627+
components: {
628+
schemas: {
629+
bar: {
630+
items: [
631+
null,
632+
{
633+
type: 'number',
634+
},
635+
],
636+
},
637+
foo: {
638+
allOf: [
639+
null,
640+
{
641+
properties: {
642+
attributes: {
643+
allOf: [null, {}],
644+
},
645+
},
646+
},
647+
],
648+
},
649+
},
650+
},
651+
},
652+
});
610653
});
611654
});

src/bundle.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,18 +67,13 @@ const _bundle = (
6767

6868
const target = get(document, pathProcessed);
6969
if (Array.isArray(target)) {
70-
set(bundledObj, inventoryPathProcessed, []);
70+
set(bundledObj, inventoryPathProcessed, new Array(target.length).fill(null));
7171
} else if (typeof target === 'object') {
7272
set(bundledObj, inventoryPathProcessed, {});
7373
}
7474
}
7575

7676
set(bundledObj, inventoryPath, bundled$Ref);
77-
const parentObj =
78-
inventoryPath.length === 1 ? bundledObj : get(bundledObj, inventoryPath.slice(0, inventoryPath.length - 1));
79-
if (Array.isArray(parentObj)) {
80-
ensureNoSparseArray(parentObj);
81-
}
8277

8378
if (!stack[$ref]) {
8479
stack[$ref] = true;
@@ -97,13 +92,3 @@ const _bundle = (
9792

9893
return objectToBundle;
9994
};
100-
101-
function ensureNoSparseArray(arr: unknown[]) {
102-
for (let i = 0; i < arr.length; i++) {
103-
if (!(i in arr)) {
104-
arr[i] = null;
105-
}
106-
}
107-
108-
return arr;
109-
}

0 commit comments

Comments
 (0)