Better organization of tests & other files
This commit is contained in:
18
src/decorators/exclude.ts
Normal file
18
src/decorators/exclude.ts
Normal 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
2
src/decorators/index.ts
Normal file
@@ -0,0 +1,2 @@
|
||||
export * from './exclude';
|
||||
export * from './pluck';
|
||||
18
src/decorators/pluck.ts
Normal file
18
src/decorators/pluck.ts
Normal 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 */
|
||||
43
src/serde.ts
43
src/serde.ts
@@ -1,9 +1,4 @@
|
||||
import "reflect-metadata";
|
||||
|
||||
|
||||
/* tslint:disable:comment-type */
|
||||
export const PLUCK_PROPERTIES_KEY = 'serde:pluck_properties';
|
||||
export const EXCLUDED_PROPERTIES_KEY = 'serde:excluded_properties';
|
||||
import { EXCLUDED_PROPERTIES_KEY, PLUCK_PROPERTIES_KEY } from './decorators';
|
||||
|
||||
export abstract class Serde<S> extends Object {
|
||||
protected removeableProperty(key: string): boolean {
|
||||
@@ -62,38 +57,4 @@ export abstract class Serde<S> extends Object {
|
||||
}, {} as Partial<S>);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* 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 */
|
||||
}
|
||||
@@ -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> {
|
||||
name: string;
|
||||
@@ -1,28 +1,27 @@
|
||||
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"target": "es6",
|
||||
"noImplicitAny": false,
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"importHelpers": true,
|
||||
"types": ["jest"],
|
||||
"typeRoots": [
|
||||
"node_modules/@types"
|
||||
],
|
||||
"lib": [
|
||||
"es2017",
|
||||
"dom"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*"
|
||||
]
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"experimentalDecorators": true,
|
||||
"emitDecoratorMetadata": true,
|
||||
"target": "es6",
|
||||
"noImplicitAny": false,
|
||||
"moduleResolution": "node",
|
||||
"sourceMap": true,
|
||||
"outDir": "dist",
|
||||
"baseUrl": ".",
|
||||
"importHelpers": true,
|
||||
"types": ["jest"],
|
||||
"typeRoots": [
|
||||
"node_modules/@types"
|
||||
],
|
||||
"lib": [
|
||||
"es2017",
|
||||
"dom"
|
||||
]
|
||||
},
|
||||
"include": [
|
||||
"src/**/*", "test/**/*"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user