Skip to content
This repository has been archived by the owner on Dec 10, 2022. It is now read-only.

Commit

Permalink
fix(preset): Store metadata directly on the object
Browse files Browse the repository at this point in the history
As TS will fold it later into it's prototype
  • Loading branch information
gund committed Jan 23, 2018
1 parent bc402a3 commit 601d659
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
22 changes: 9 additions & 13 deletions src/preset/metadata/preset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,52 +5,48 @@ import {
} from './preset';

describe('setPresetMetadataOn() function', () => {
it('should set metadata on type`s constructor under `PRESET_METADATA_KEY`', () => {
it('should set metadata on type`s prototype under `PRESET_METADATA_KEY`', () => {
const metadata = { __medatadta: true };
const t = getFakeType();
const ctor = t.constructor;

setPresetMetadataOn(t, metadata as any);

expect(ctor[PRESET_METADATA_KEY]).toEqual([metadata]);
expect(t[PRESET_METADATA_KEY]).toEqual([metadata]);
});

it('should set array of metadata on type`s constructor under `PRESET_METADATA_KEY`', () => {
it('should set array of metadata on type`s prototype under `PRESET_METADATA_KEY`', () => {
const metadata = { __medatadta: true };
const metadata2 = { __medatadta2: true };
const t = getFakeType();
const ctor = t.constructor;

setPresetMetadataOn(t, [metadata, metadata2] as any);

expect(ctor[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]);
expect(t[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]);
});

it('should preserve previously set metadata on type`s constructor under `PRESET_METADATA_KEY`', () => {
it('should preserve previously set metadata on type`s prototype under `PRESET_METADATA_KEY`', () => {
const metadata = { __medatadta: true };
const metadata2 = { __medatadta2: true };
const t = getFakeType();
const ctor = t.constructor;

setPresetMetadataOn(t, metadata as any);
expect(ctor[PRESET_METADATA_KEY]).toEqual([metadata]);
expect(t[PRESET_METADATA_KEY]).toEqual([metadata]);

setPresetMetadataOn(t, metadata2 as any);
expect(ctor[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]);
expect(t[PRESET_METADATA_KEY]).toEqual([metadata, metadata2]);
});
});

describe('getPresetMetadataFrom() function', () => {
it('should return metadata from type under `PRESET_METADATA_KEY`', () => {
const metadata = { __metadata: true };
const t = getFakeType();
const ctor = t.constructor;
setPresetMetadataOn(t, metadata as any);

expect(getPresetMetadataFrom(ctor)).toEqual([metadata]);
expect(getPresetMetadataFrom({ prototype: t })).toEqual([metadata]);
});
});

function getFakeType(): any {
return { constructor: {} };
return class {};
}
8 changes: 4 additions & 4 deletions src/preset/metadata/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export function setPresetMetadataOn(
metadata = [metadata];
}

const ctor = type.constructor;
const existingMetadata = ctor[PRESET_METADATA_KEY] || [];
ctor[PRESET_METADATA_KEY] = [...existingMetadata, ...metadata];
const existingMetadata = type[PRESET_METADATA_KEY] || [];
type[PRESET_METADATA_KEY] = [...existingMetadata, ...metadata];
}

export function getPresetMetadataFrom(type: any): PresetMetadata[] {
return type[PRESET_METADATA_KEY] || [];
const proto = type.prototype || (type.prototype = {});
return proto[PRESET_METADATA_KEY] || [];
}

0 comments on commit 601d659

Please sign in to comment.