diff --git a/src/decorators/exclude.ts b/src/decorators/exclude.ts new file mode 100644 index 0000000..bf54ffb --- /dev/null +++ b/src/decorators/exclude.ts @@ -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 + ); + }; + } \ No newline at end of file diff --git a/src/decorators/index.ts b/src/decorators/index.ts new file mode 100644 index 0000000..3b2d1ae --- /dev/null +++ b/src/decorators/index.ts @@ -0,0 +1,2 @@ +export * from './exclude'; +export * from './pluck'; \ No newline at end of file diff --git a/src/decorators/pluck.ts b/src/decorators/pluck.ts new file mode 100644 index 0000000..c01b493 --- /dev/null +++ b/src/decorators/pluck.ts @@ -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 */ \ No newline at end of file diff --git a/src/serde.ts b/src/serde.ts index 101590b..914041a 100644 --- a/src/serde.ts +++ b/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 extends Object { protected removeableProperty(key: string): boolean { @@ -62,38 +57,4 @@ export abstract class Serde extends Object { }, {} as Partial); } -} - -/* 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 */ \ No newline at end of file +} \ No newline at end of file diff --git a/src/serde.spec.ts b/test/serde.spec.ts similarity index 97% rename from src/serde.spec.ts rename to test/serde.spec.ts index 6ae32b2..e1a4193 100644 --- a/src/serde.spec.ts +++ b/test/serde.spec.ts @@ -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 { name: string; diff --git a/tsconfig.json b/tsconfig.json index ef8f864..9d40c18 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -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/**/*" + ] } \ No newline at end of file