1
0

Better organization of tests & other files

This commit is contained in:
2020-03-06 12:00:39 -05:00
parent d0eac56e50
commit 6c101bb7b2
6 changed files with 67 additions and 68 deletions

18
src/decorators/exclude.ts Normal file
View File

@@ -0,0 +1,18 @@
export const EXCLUDED_PROPERTIES_KEY = 'serde:excluded_properties';
/* tslint:disable:variable-name only-arrow-functions */
/**
* Adding this decorator prevents the property from being included in the object built by Serde.serialize()
*/
export function Exclude(): Function {
return function (target: any, key: string): void {
Reflect.defineMetadata(
EXCLUDED_PROPERTIES_KEY,
[
...Reflect.getMetadata(EXCLUDED_PROPERTIES_KEY, target) || [],
key
],
target
);
};
}

2
src/decorators/index.ts Normal file
View File

@@ -0,0 +1,2 @@
export * from './exclude';
export * from './pluck';

18
src/decorators/pluck.ts Normal file
View File

@@ -0,0 +1,18 @@
export const PLUCK_PROPERTIES_KEY = 'serde:pluck_properties';
/**
* Adding this decorator allows for plucking out an T[] or {T: v}[]
*
* @param field - use 'fieldName' when creating string|number[],
* use ['fieldName'] when creating {[fieldName]: value}[]
*/
export function Pluck(field: string | string[]): Function {
return function (target: any, key: string): void {
Reflect.defineMetadata(PLUCK_PROPERTIES_KEY, {
...Reflect.getMetadata(PLUCK_PROPERTIES_KEY, target) || {},
...{ [key]: field }
}, target);
};
}
/* tslint:enable:variable-name */

View File

@@ -1,9 +1,4 @@
import "reflect-metadata"; import { EXCLUDED_PROPERTIES_KEY, PLUCK_PROPERTIES_KEY } from './decorators';
/* tslint:disable:comment-type */
export const PLUCK_PROPERTIES_KEY = 'serde:pluck_properties';
export const EXCLUDED_PROPERTIES_KEY = 'serde:excluded_properties';
export abstract class Serde<S> extends Object { export abstract class Serde<S> extends Object {
protected removeableProperty(key: string): boolean { protected removeableProperty(key: string): boolean {
@@ -63,37 +58,3 @@ export abstract class Serde<S> extends Object {
} }
} }
/* tslint:disable:variable-name only-arrow-functions */
/**
* Adding this decorator prevents the property from being included in the object built by Serde.serialize()
*/
export function Exclude(): Function {
return function (target: any, key: string): void {
Reflect.defineMetadata(
EXCLUDED_PROPERTIES_KEY,
[
...Reflect.getMetadata(EXCLUDED_PROPERTIES_KEY, target) || [],
key
],
target
);
};
}
/**
* Adding this decorator allows for plucking out an T[] or {T: v}[]
*
* @param field - use 'fieldName' when creating string|number[],
* use ['fieldName'] when creating {[fieldName]: value}[]
*/
export function Pluck(field: string | string[]): Function {
return function (target: any, key: string): void {
Reflect.defineMetadata(PLUCK_PROPERTIES_KEY, {
...Reflect.getMetadata(PLUCK_PROPERTIES_KEY, target) || {},
...{ [key]: field }
}, target);
};
}
/* tslint:enable:variable-name */

View File

@@ -1,4 +1,5 @@
import { Exclude, Pluck, Serde } from './serde'; import { Serde } from 'src/serde';
import { Exclude, Pluck } from 'src/decorators';
class ExcludeTestModel extends Serde<ExcludeTestModel> { class ExcludeTestModel extends Serde<ExcludeTestModel> {
name: string; name: string;

View File

@@ -1,28 +1,27 @@
{ {
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"esModuleInterop": true, "esModuleInterop": true,
"allowSyntheticDefaultImports": true, "allowSyntheticDefaultImports": true,
"experimentalDecorators": true, "experimentalDecorators": true,
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,
"target": "es6", "target": "es6",
"noImplicitAny": false, "noImplicitAny": false,
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": true, "sourceMap": true,
"outDir": "dist", "outDir": "dist",
"baseUrl": ".", "baseUrl": ".",
"importHelpers": true, "importHelpers": true,
"types": ["jest"], "types": ["jest"],
"typeRoots": [ "typeRoots": [
"node_modules/@types" "node_modules/@types"
], ],
"lib": [ "lib": [
"es2017", "es2017",
"dom" "dom"
] ]
}, },
"include": [ "include": [
"src/**/*" "src/**/*", "test/**/*"
] ]
} }