Skip to content

Commit

Permalink
[REFACTOR] Update Readme and documentaion
Browse files Browse the repository at this point in the history
  • Loading branch information
andbul committed Oct 14, 2019
1 parent 5c2daf9 commit 4762aa8
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
18 changes: 8 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,21 @@ Decorator accepts two arguments:
- Mappings - array of rules how to convert one field to another

```typescript
export class MappingRules {
source!: string; // Field name from the source object which would be mapped
target!: string; // Field name from the target object where to map
default?: any; // Default value if source field is null
expr?: (x: any) => any; // Expression for manual converting or preparing field
export class MappingRules<SourceField, TargetField, SourceFieldType, TargetFieldType> {
source!: SourceField; // String Field name from the source object which would be mapped
target!: TargetField; // String Field name from the target object where to map
default?: TargetFieldType; // Default value if source field is null
isCollection?: Boolean = false; // Flag that enables Array.map converting for this field
converter?: Constructor<Converter<any, any>>; // Converter constructor for nested objects
expr?: (x: SourceFieldType) => TargetFieldType; // Expression for manual converting or preparing field
converter?: Constructor<Converter<SourceFieldType, TargetFieldType>>; // Converter constructor for nested objects
}
```

Options in mapping rules have priority when evaluating:
Options in mapping rules have choice when evaluating:

1. Exec converting by `expr`
1. Exec converting by `expr` OR
2. Exec converting by `converter.convert`

If one of this options empty, another will be successfully performed.

Additionally:

- If source field is null then default value will be set and none `expr` or `converter` called
Expand Down
8 changes: 3 additions & 5 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import { type } from "os";
* source - name of the source object field
* source - name of the target object field
* default - set default value if field is null
* expr - helps to pre evaluate field value
* isCollection - transform field value with Array.map
* expr - helps to convert value instead implementing converter
* converter - convert field value with converter
*
* Mapping has the next order:
* - Setup default in field if null
* - Evaluate expr
* - Evaluate converter
* - Evaluate expr or Evaluate converter
*
* If isCollection is true then for each value in collection will be performed:
* - Evaluate expr
* - Evaluate converter
* - Evaluate expr or Evaluate converter
*/
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
Expand Down
3 changes: 2 additions & 1 deletion src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function setProperty<T, K extends keyof T>(obj: T, key: K, value: T[K]) {

/**
* Generated mapping function
*
* @param sourceObject - object that contains values
* @param targetObject - object that will be filled according mapping
* @param mappings - from => to mapping declaration with meta information
Expand Down Expand Up @@ -84,7 +85,7 @@ export function mapObject<

/**
* Describes order of converting.
* Current realisation is: expr => converter.convert
*
* @param expr - expression that will be evaluated
* @param Converter - converter constructor
*/
Expand Down

0 comments on commit 4762aa8

Please sign in to comment.