Skip to content

Commit

Permalink
Merge pull request #4 from abraham/get-own-metadata
Browse files Browse the repository at this point in the history
Add getOwnMetadata
  • Loading branch information
abraham authored Jul 24, 2018
2 parents 8daaad5 + 11e23c0 commit e3a2a4f
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import '@abraham/reflection/dist/reflect';
Reflect.defineMetadata(metadataKey, metadataValue, target);
```


API
----

Expand All @@ -47,6 +46,7 @@ Reflection does not currently cover the complete API surface of reflect-metadata
Reflect.decorate(...);
Reflect.defineMetadata(...);
Reflect.getMetadata(...);
Reflect.getOwnMetadata(...);
Reflect.hasOwnMetadata(...);
Reflect.metadata(...);
```
41 changes: 41 additions & 0 deletions src/get-own-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Reflection as Reflect } from './index';

const prototype = {};
const metadataKey = 'key';
const metadataValue = 'value';
const target = {};
const propertyKey = 'name';

test('invalid target', () => {
expect(() => Reflect.getOwnMetadata(metadataKey)).toThrow(TypeError);
});

test('not defined with target', () => {
expect(Reflect.getOwnMetadata(metadataKey, target)).toBeUndefined();
});

test('defined', () => {
Reflect.defineMetadata(metadataKey, metadataValue, target);
expect(Reflect.getOwnMetadata(metadataKey, target)).toEqual(metadataValue);
});

test('defined on prototype', () => {
const target = Object.create(prototype);
Reflect.defineMetadata(metadataKey, metadataValue, prototype);
expect(Reflect.getOwnMetadata(metadataKey, target)).toBeUndefined();
});

test('not defined with property key', () => {
expect(Reflect.getOwnMetadata(metadataKey, target, propertyKey)).toBeUndefined();
});

test('defined with property key', () => {
Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
expect(Reflect.getOwnMetadata(metadataKey, target, propertyKey)).toEqual(metadataValue);
});

test('defined on prototype with property key', () => {
const target = Object.create(prototype);
Reflect.defineMetadata(metadataKey, metadataValue, prototype, propertyKey);
expect(Reflect.getOwnMetadata(metadataKey, target, propertyKey)).toBeUndefined();
});
6 changes: 6 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export const Reflection = Object.assign({}, Reflect, {
decorate,
defineMetadata,
getMetadata,
getOwnMetadata,
hasOwnMetadata,
metadata,
});
Expand Down Expand Up @@ -42,6 +43,10 @@ export function getMetadata(metadataKey: MetadataKey, target: Target, propertyKe
return ordinaryGetMetadata(metadataKey, target, propertyKey);
}

export function getOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey) {
return ordinaryGetOwnMetadata(metadataKey, target, propertyKey);
}

export function hasOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): boolean {
return ordinaryHasOwnMetadata(metadataKey, target, propertyKey);
}
Expand Down Expand Up @@ -87,6 +92,7 @@ function ordinaryHasOwnMetadata(metadataKey: MetadataKey, target: Target, proper
}

function ordinaryGetOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined {
if (target === undefined) throw new TypeError();
const metadataMap = getMetadataMap(target, propertyKey);
return metadataMap && metadataMap.get(metadataKey);
}
Expand Down
4 changes: 4 additions & 0 deletions src/reflect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@ test('metadata', () => {
test('hasOwnMetadata', () => {
expect(Reflect.hasOwnMetadata).toBeDefined();
});

test('getOwnMetadata', () => {
expect(Reflect.getOwnMetadata).toBeDefined();
});

0 comments on commit e3a2a4f

Please sign in to comment.