Skip to content

Commit

Permalink
Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jul 23, 2018
0 parents commit 3b11d10
Show file tree
Hide file tree
Showing 15 changed files with 670 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
package-lock.json
9 changes: 9 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language: node_js
node_js:
- node
cache:
directories:
- node_modules
script:
- npm test
- npm run build
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2018 Abraham Williams

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[![Version Status](https://img.shields.io/npm/v/@abraham/reflection.svg?style=flat&label=version&colorB=4bc524)](https://npmjs.com/package/@abraham/reflection)
[![Build Status](https://img.shields.io/travis/abraham/reflection.svg?style=flat)](https://travis-ci.org/abraham/reflection)
[![Dependency Status](https://david-dm.org/abraham/reflection.svg?style=flat)](https://david-dm.org/abraham/reflection)

Reflection
====

Lightweight ES Module implementation of [reflect-metadata](https://github.com/rbuckton/reflect-metadata/) to work with TypeScript's [experimental decorator support](https://www.typescriptlang.org/docs/handbook/decorators.html).

Why?
----

The main reason for this library is to provide a much smaller implementation that can be included as a module.

- `reflect-metadata` is 52 K without compression while `reflection` is about 3 K
- `reflection` can be loaded with `<script type="module" src="..."></script>`

Install
----

```sh
npm install @abraham/reflection
```

Usage
-----

```ts
import { Reflection as Reflect } from '@abraham/reflection';
Reflect.defineMetadata(metadataKey, metadataValue, target);
```

If a globally available version you can use the following.

```ts
import '@abraham/reflection/dist/reflect';
Reflect.defineMetadata(metadataKey, metadataValue, target);
```


API
----

Reflection does not cover the complete API surface of reflect-metadata. Currently the following are available.

```ts
Reflect.decorate(...);
Reflect.defineMetadata(...);
Reflect.getMetadata(...);
Reflect.hasOwnMetadata(...);
Reflect.metadata(...);
```
63 changes: 63 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@abraham/reflection",
"version": "0.1.0",
"description": "Lightweight ES Module implementation of reflect-metadata",
"main": "dist/index.js",
"module": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "tsc",
"clean": "rimraf dist",
"prebuild": "npm run clean",
"prepare": "npm run build",
"test": "jest"
},
"repository": {
"type": "git",
"url": "git+https://github.com/abraham/reflection.git"
},
"keywords": [
"typescript",
"reflect",
"reflect-metadata",
"metadata",
"lightweight",
"micro",
"library"
],
"author": "Abraham Williams <4braham@gmail.com>",
"license": "MIT",
"bugs": {
"url": "https://github.com/abraham/reflection/issues"
},
"homepage": "https://github.com/abraham/reflection#readme",
"devDependencies": {
"@types/jest": "23.3.0",
"jest": "23.4.1",
"rimraf": "2.6.2",
"ts-jest": "23.0.1",
"typescript": "2.9.2"
},
"jest": {
"roots": [
"<rootDir>/src"
],
"transform": {
"^.+\\.tsx?$": "ts-jest"
},
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$",
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"jsx",
"json",
"node"
]
},
"renovate": {
"extends": [
"config:base"
]
}
}
222 changes: 222 additions & 0 deletions src/decorate.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
import { Reflection as Reflect } from './index';

test('with invalid decorators and target', () => {
const decorators = undefined;
const target = () => {};
expect(() => Reflect.decorate(decorators, target)).toThrow(TypeError);
});

test('with no decorators', () => {
const decorators = [];
const target = () => {};
expect(() => Reflect.decorate(decorators, target)).toThrow(TypeError);
});

test('with property and invalid decorators', () => {
const decorators = undefined;
const target = {};
const property = 'name';
expect(() => Reflect.decorate(decorators, target, property)).toThrow(TypeError);
});

test('with property and invalid decorators', () => {
const decorators = [];
const target = 1;
const property = 'name';
expect(() => Reflect.decorate(decorators, target, property)).toThrow(TypeError);
});

test('with property and descriptor and invalid decorators', () => {
const decorators = undefined;
const target = {};
const property = 'name';
const descriptor = {};
expect(() => Reflect.decorate(decorators, target, property, descriptor)).toThrow(TypeError);
});

test('with decorators, property, and descriptor and invalid target ', () => {
const decorators = [];
const target = 1;
const property = 'name';
const descriptor = {};
expect(() => Reflect.decorate(decorators, target, property, descriptor)).toThrow(TypeError);
});

test('executes decorators in reverse order for function', () => {
const order: number[] = [];
const decorators = [
() => { order.push(0); },
() => { order.push(1); },
];
const target = () => {};
Reflect.decorate(decorators, target);
expect(order[0]).toEqual(1);
expect(order[1]).toEqual(0);
});

test('executes decorators in reverse order for property', () => {
const order: number[] = [];
const decorators = [
() => { order.push(0); },
() => { order.push(1); },
];
const target = {};
const property = 'name';
Reflect.decorate(decorators, target, property);
expect(order[0]).toEqual(1);
expect(order[1]).toEqual(0);
});

test('executes decorators in reverse order for property with descriptor', () => {
const order: number[] = [];
const decorators = [
() => { order.push(0); },
() => { order.push(1); },
];
const target = {};
const property = 'name';
const descriptor = {};
Reflect.decorate(decorators, target, property, descriptor);
expect(order[0]).toEqual(1);
expect(order[1]).toEqual(0);
});

test('something function', () => {
const a = function a() {};
const b = function b() {};
const decorators = [
() => {},
() => a,
() => b,
];
const target = () => {};
const result = Reflect.decorate(decorators, target);
expect(result).toStrictEqual(a);
});

test('something property', () => {
const a = function a() {};
const b = function b() {};
const decorators = [
() => {},
() => a,
() => b,
];
const target = {};
const property = 'name';
const result = Reflect.decorate(decorators, target, property);
expect(result).toStrictEqual(a);
});

test('something property with descriptor', () => {
const a = function a() {};
const b = function b() {};
const decorators = [
() => {},
() => a,
() => b,
];
const target = {};
const property = 'name';
const descriptor = function c() { };
const result = Reflect.decorate(decorators, target, property, descriptor);
expect(result).toStrictEqual(a);
});

test('decorate correct target for function', () => {
const sent: Function[] = [];
const a = function a() {};
const b = function b() {};
const decorators = [
(target: Function) => { sent.push(target); },
(target: Function) => { sent.push(target); },
(target: Function) => { sent.push(target); return a; },
(target: Function) => { sent.push(target); return b; },
];
const target = () => {};
Reflect.decorate(decorators, target);
expect(sent[0]).toStrictEqual(target);
expect(sent[1]).toStrictEqual(b);
expect(sent[2]).toStrictEqual(a);
expect(sent[3]).toStrictEqual(a);
});

test('decorate correct target for function', () => {
const sent: object[] = [];
const decorators = [
(target: object) => { sent.push(target); },
(target: object) => { sent.push(target); },
(target: object) => { sent.push(target); },
(target: object) => { sent.push(target); },
];
const target = {};
const property = 'name';
Reflect.decorate(decorators, target, property);
expect(sent).toStrictEqual([target, target, target, target]);
});

// DecoratorCorrectNameInPipelineForPropertyOverload
test('decorate correct target for function', () => {
const sent: string[] = [];
const decorators = [
(_target: object, name: string) => { sent.push(name); },
(_target: object, name: string) => { sent.push(name); },
(_target: object, name: string) => { sent.push(name); },
(_target: object, name: string) => { sent.push(name); },
];
const target = {};
const property = 'name';
Reflect.decorate(decorators, target, property);
expect(sent).toStrictEqual([property, property, property, property]);
});

test('decorate correct target for function', () => {
const sent: object[] = [];
const a = {};
const b = {}
const decorators = [
(target: object) => { sent.push(target); },
(target: object) => { sent.push(target); },
(target: object) => { sent.push(target); return a; },
(target: object) => { sent.push(target); return b; },
];
const target = {};
const property = 'name';
const descriptor = {};
Reflect.decorate(decorators, target, property, descriptor);
expect(sent).toStrictEqual([target, target, target, target]);
});

test('decorate correct target for function', () => {
const sent: string[] = [];
const a = {};
const b = {}
const decorators = [
(_target: object, name: string) => { sent.push(name); },
(_target: object, name: string) => { sent.push(name); },
(_target: object, name: string) => { sent.push(name); return a; },
(_target: object, name: string) => { sent.push(name); return b; },
];
const target = {};
const property = 'name';
const descriptor = {};
Reflect.decorate(decorators, target, property, descriptor);
expect(sent).toStrictEqual([property, property, property, property]);
});

test('decorate correct target for function', () => {
const sent: PropertyDescriptor[] = [];
const a = {};
const b = {}
const decorators = [
(_target: object, _name: string, descriptor: PropertyDescriptor) => { sent.push(descriptor); },
(_target: object, _name: string, descriptor: PropertyDescriptor) => { sent.push(descriptor); },
(_target: object, _name: string, descriptor: PropertyDescriptor) => { sent.push(descriptor); return a; },
(_target: object, _name: string, descriptor: PropertyDescriptor) => { sent.push(descriptor); return b; },
];
const target = {};
const property = 'name';
const descriptor = {};
Reflect.decorate(decorators, target, property, descriptor);
expect(sent).toStrictEqual([descriptor, b, a, a]);
});
22 changes: 22 additions & 0 deletions src/define-metadata.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Reflection as Reflect } from './index';

test('with invalid target', () => {
const metadataKey = 'key';
const metadataValue = 'value';
expect(() => Reflect.defineMetadata(metadataKey, metadataValue)).toThrow(TypeError);
});

test('with target but no property key', () => {
const metadataKey = 'key';
const metadataValue = 'value';
const target = {};
expect(() => Reflect.defineMetadata(metadataKey, metadataValue, target)).not.toThrow();
});

test('with target and property key', () => {
const metadataKey = 'key';
const metadataValue = 'value';
const target = {};
const propertyKey = 'name';
expect(() => Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey)).not.toThrow();
});
Loading

0 comments on commit 3b11d10

Please sign in to comment.