-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from marquesVF/feature/build-reducer
Feature/build reducer
- Loading branch information
Showing
11 changed files
with
129 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Metadata } from '../core/metadata' | ||
import { Reducer, ReductionMetadata } from '../types/reduction' | ||
|
||
export function BuildReduction<T>(reducer: Reducer<T>): PropertyDecorator { | ||
return function (target: Object, propertyKey: string | symbol) { | ||
const className = target.constructor.name | ||
|
||
const metadata: ReductionMetadata<T> = { | ||
propertyKey: propertyKey.toString(), | ||
reducer | ||
} | ||
|
||
Metadata.getInstance() | ||
.registerBuildReduction(className, metadata) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type ReductionFn<T> = (obj: object) => T | ||
|
||
export interface Reducer<T> { | ||
reduce: ReductionFn<T> | ||
} | ||
|
||
export interface ReductionMetadata<T> { | ||
propertyKey: string | ||
reducer: Reducer<T> | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Property } from '../../lib' | ||
import { BuildReduction } from '../../lib/decorators/build-reduction' | ||
|
||
export class ReducibleObject { | ||
|
||
@Property() | ||
@BuildReduction({ | ||
reduce: (obj: any) => `${obj.firstProp} ${obj.secondProp}` | ||
}) | ||
combinedProp: string | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { buildObject } from '../../lib' | ||
import { ReducibleObject } from '../fixtures/reductionable' | ||
|
||
describe('object reduction', () => { | ||
const jsonObject = { | ||
firstProp: 'hello', | ||
secondProp: 'world' | ||
} | ||
const expectedObj: ReducibleObject = { | ||
combinedProp: 'hello world' | ||
} | ||
|
||
it('should use both properties in the transformation', () => { | ||
const result = buildObject(ReducibleObject, jsonObject) | ||
|
||
expect(result).toEqual(expectedObj) | ||
}) | ||
}) |