Skip to content

Commit

Permalink
Implemented delete and ownKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
thephoenixofthevoid committed Jun 10, 2021
1 parent dd368f1 commit 8cff503
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
43 changes: 36 additions & 7 deletions Reflect.MIT.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,19 @@ export function hasOwnMetadata(
return !!ordinaryGetOwnMetadata(metadataKey, target, propertyKey);
}

export function deleteMetadata(
metadataKey: MetadataKey,
target?: Target,
propertyKey?: PropertyKey,
): boolean {
if (target === undefined) {
throw new TypeError();
}
const metadataMap = getMetadataMap<unknown>(target, propertyKey);
if (!metadataMap) return false;
return metadataMap.delete(metadataKey);
}

export function hasMetadata(
metadataKey: MetadataKey,
target: Target,
Expand All @@ -195,12 +208,28 @@ export function defineMetadata<MetadataValue>(
ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey);
}

function notImplemented<T extends any[]>() {
return (..._args: T) => {
throw new Error("notImplemented")
}
export function getMetadataKeys(
target: Target,
propertyKey?: PropertyKey,
): any[] {
target
propertyKey
return []
}

export function getOwnMetadataKeys(
target: Target,
propertyKey?: PropertyKey,
): any[] {
if (target === undefined) {
throw new TypeError();
}
const metadataMap = getMetadataMap<unknown>(target, propertyKey);
if (!metadataMap) return [];
return Array.from(metadataMap.keys());
}


export const Reflection = {
decorate,
defineMetadata,
Expand All @@ -210,9 +239,9 @@ export const Reflection = {
hasOwnMetadata,
metadata,

deleteMetadata: notImplemented<any[]>(),
getMetadataKeys: notImplemented<any[]>(),
getOwnMetadataKeys: notImplemented<any[]>(),
deleteMetadata,
getMetadataKeys,
getOwnMetadataKeys
};

declare global {
Expand Down
6 changes: 4 additions & 2 deletions Reflect.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ describe("Reflect.defineMetadata", () => {

// 4.1.10 Reflect.deleteMetadata ( metadataKey, target [, propertyKey] )
// https://rbuckton.github.io/reflect-metadata/#reflect.deletemetadata
describe.skip("Reflect.deleteMetadata", () => {
describe("Reflect.deleteMetadata", () => {
it("InvalidTarget", () => {
expect(() => Reflect.deleteMetadata("key", undefined, undefined)).toThrow(TypeError);
});
Expand Down Expand Up @@ -285,6 +285,7 @@ describe.skip("Reflect.deleteMetadata", () => {
describe.skip("Reflect.getMetadataKeys", () => {
it("KeysInvalidTarget", () => {
// 1. If Type(target) is not Object, throw a TypeError exception.
// @ts-expect-error
expect(() => Reflect.getMetadataKeys(undefined)).toThrow(TypeError);
});

Expand Down Expand Up @@ -430,9 +431,10 @@ describe("Reflect.getMetadata", () => {

// 4.1.9 Reflect.getOwnMetadataKeysKeys ( target [, propertyKey] )
// https://rbuckton.github.io/reflect-metadata/#reflect.getownmetadatakeys
describe.skip("Reflect.deleteMetadata", () => {
describe("Reflect.getOwnMetadataKeysKeys", () => {
it("KeysKeysInvalidTarget", () => {
// 1. If Type(target) is not Object, throw a TypeError exception.
// @ts-expect-error
expect(() => Reflect.getOwnMetadataKeys(undefined, undefined)).toThrow(TypeError);
});

Expand Down

0 comments on commit 8cff503

Please sign in to comment.