https://github.com/aigoncharov/metadata-utils
Convenience utils for reflected metadata
https://github.com/aigoncharov/metadata-utils
metadata reflect reflection typescript
Last synced: 4 months ago
JSON representation
Convenience utils for reflected metadata
- Host: GitHub
- URL: https://github.com/aigoncharov/metadata-utils
- Owner: aigoncharov
- License: mit
- Created: 2019-03-30T13:10:49.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-03-30T14:42:21.000Z (over 7 years ago)
- Last Synced: 2025-02-18T09:07:16.768Z (over 1 year ago)
- Topics: metadata, reflect, reflection, typescript
- Language: TypeScript
- Homepage:
- Size: 62.5 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# metadata-utils [](https://travis-ci.org/keenondrums/metadata-utils) [](https://coveralls.io/github/keenondrums/metadata-utils?branch=master) [](https://twitter.com/intent/tweet?text=Convenience%20utils%20for%20reflected%20metadata&url=https://github.com/keenondrums/metadata-utils&hashtags=javascript,typescript,metadata,reflection)
Convenience utils for reflected metadata.
- [Installation](#installation)
- [API](#api)
- [getMetadataAndCache](#getmetadataandcache)
## Installation
1. Run
```sh
npm i @keenondrums/metadata-utils reflect-metadata
```
1. Add `import 'reflect-metadata'` to the root of your application
## API
### getMetadataAndCache
Gets metadata from the prototype (using [Relect.getMetadata](https://github.com/rbuckton/reflect-metadata#api)) and sets it as own metadata. Useful to enhance performance for retrieving metadata from ancestors. Has the same signature as [Relect.getMetadata](https://github.com/rbuckton/reflect-metadata#api).
```ts
import 'reflect-metadata'
import { getMetadataAndCache } from '@keenondrums/metadata-utils'
class Parent {}
Reflect.defineMetadata('key', 'value', Parent)
Reflect.getOwnMetadata('key', Parent) // returns 'value'
getMetadataAndCache('key', Parent) // returns 'value', basically calls Reflect.getOwnMetadata under the hood and does nothing else in this case
class Child extends Parent {}
Reflect.getOwnMetadata('key', Child) // returns `undefined` as Child doesn't have any own metadata for 'key'
getMetadataAndCache('key', Child) // returns 'value' from Parent and set it as own metadata
Reflect.getOwnMetadata('key', Child) // now it returns 'value'
```