-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from abraham/files
Move methods to index
- Loading branch information
Showing
3 changed files
with
100 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,104 @@ | ||
import { decorate, defineMetadata, getMetadata, hasOwnMetadata, metadata } from './methods'; | ||
|
||
export const Reflection = Object.assign(Reflect, { | ||
export const Reflection = Object.assign({}, Reflect, { | ||
decorate, | ||
defineMetadata, | ||
getMetadata, | ||
hasOwnMetadata, | ||
metadata, | ||
}); | ||
|
||
export type Decorator = ClassDecorator | MemberDecorator; | ||
export type MemberDecorator = <T>(target: Target, propertyKey: PropertyKey, descriptor?: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void; | ||
export type MetadataKey = string; | ||
export type MetadataValue = Function; | ||
export type PropertyKey = string | symbol; | ||
export type Target = object | Function; | ||
|
||
const Metadata = new WeakMap(); | ||
|
||
export function defineMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey) { | ||
return ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); | ||
} | ||
|
||
export function decorate(decorators: ClassDecorator[], target: Function): Function | ||
export function decorate(decorators: MemberDecorator[], target: object, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): PropertyDescriptor | undefined | ||
export function decorate(decorators: Decorator[], target: Target, propertyKey?: PropertyKey, attributes?: PropertyDescriptor): Function | PropertyDescriptor | undefined { | ||
if (decorators.length === 0) throw new TypeError(); | ||
|
||
if (typeof target === 'function') { | ||
return decorateConstructor(decorators as ClassDecorator[], target); | ||
} else if (propertyKey !== undefined) { | ||
return decorateProperty(decorators as MemberDecorator[], target, propertyKey, attributes); | ||
} | ||
return; | ||
} | ||
|
||
export function metadata(metadataKey: MetadataKey, metadataValue: MetadataValue) { | ||
return function decorator(target: Function, propertyKey?: PropertyKey) { | ||
ordinaryDefineOwnMetadata(metadataKey, metadataValue, target, propertyKey); | ||
}; | ||
} | ||
|
||
export function getMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey) { | ||
return ordinaryGetMetadata(metadataKey, target, propertyKey); | ||
} | ||
|
||
export function hasOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): boolean { | ||
return ordinaryHasOwnMetadata(metadataKey, target, propertyKey); | ||
} | ||
|
||
function decorateConstructor(decorators: ClassDecorator[], target: Function): Function { | ||
decorators.reverse().forEach((decorator: ClassDecorator) => { | ||
const decorated = decorator(target); | ||
if (decorated) { | ||
target = decorated; | ||
} | ||
}); | ||
return target; | ||
} | ||
|
||
function decorateProperty(decorators: MemberDecorator[], target: Target, propertyKey: PropertyKey, descriptor?: PropertyDescriptor): PropertyDescriptor | undefined { | ||
decorators.reverse().forEach((decorator: MemberDecorator) => { | ||
const decorated = decorator(target, propertyKey, descriptor); | ||
if (decorated) { | ||
descriptor = decorated; | ||
} | ||
}); | ||
return descriptor; | ||
} | ||
|
||
function ordinaryDefineOwnMetadata(metadataKey: MetadataKey, metadataValue: MetadataValue, target: Target, propertyKey?: PropertyKey): void { | ||
if (propertyKey && !['string', 'symbol'].includes(typeof propertyKey)) throw new TypeError(); | ||
|
||
createMetadataMap(target, propertyKey) | ||
.set(metadataKey, metadataValue); | ||
} | ||
|
||
function ordinaryGetMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined { | ||
return ordinaryHasOwnMetadata(metadataKey, target, propertyKey) | ||
? ordinaryGetOwnMetadata(metadataKey, target, propertyKey) | ||
: Object.getPrototypeOf(target) | ||
? ordinaryGetMetadata(metadataKey, Object.getPrototypeOf(target), propertyKey) | ||
: undefined; | ||
} | ||
|
||
function ordinaryHasOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): boolean { | ||
const metadataMap = getMetadataMap(target, propertyKey); | ||
return !!metadataMap && !!metadataMap.has(metadataKey); | ||
} | ||
|
||
function ordinaryGetOwnMetadata(metadataKey: MetadataKey, target: Target, propertyKey?: PropertyKey): Function | undefined { | ||
const metadataMap = getMetadataMap(target, propertyKey); | ||
return metadataMap && metadataMap.get(metadataKey); | ||
} | ||
|
||
function getMetadataMap(target: Target, propertyKey?: PropertyKey): Map<MetadataKey, MetadataValue> | undefined { | ||
return Metadata.get(target) && Metadata.get(target).get(propertyKey); | ||
} | ||
|
||
export function createMetadataMap(target: Target, propertyKey?: PropertyKey): Map<MetadataKey, MetadataValue> { | ||
const targetMetadata = new Map<PropertyKey | undefined, Map<MetadataKey, MetadataValue>>(); | ||
Metadata.set(target, targetMetadata); | ||
const metadataMap = new Map<MetadataKey, MetadataValue>(); | ||
targetMetadata.set(propertyKey, metadataMap); | ||
return metadataMap; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters