Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
update logic for transform to pass in config
Browse files Browse the repository at this point in the history
  • Loading branch information
Patrick Camacho committed Jan 2, 2017
1 parent 57f2a7c commit 139f11b
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,10 @@ A property is comprised of the following keyed values:
```js
(configObject) => configObject.differentKey
```
* **transform** (*optional function*) - a function that accepts a found value and maps it to a new value
* **transform** (*optional function*) - a function that accepts a found value and maps it to a new value (it recieves the config as a secondary argument)

```js
(value) => parseInt(value, 10)
(value, config) => parseInt(value, 10)
```

### Reserved Keys
Expand Down
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"name": "pulpo",
"version": "1.3.0",
"version": "1.3.1",
"description": "Configuration mechanism",
"author": "Patrick Camacho <[email protected]>",
"license": "Apache",
"license": "Apache-2.0",
"keywords": [
"configuration",
"config",
Expand Down Expand Up @@ -39,7 +39,9 @@
"typescript": "1.8.10"
},
"jest": {
"scriptPreprocessor": "<rootDir>/preprocessor.js",
"transform": {
".*": "<rootDir>/preprocessor.js"
},
"moduleFileExtensions": [
"ts",
"tsx",
Expand All @@ -49,7 +51,11 @@
},
"dependencies": {
"dotty": "0.0.2",
"jest-cli": "18.1.0",
"minimist": "1.2.0",
"typecast": "0.0.1"
"next-update": "1.5.1",
"tslint": "4.2.0-dev.0",
"typecast": "0.0.1",
"typescript": "2.2.0-dev.20170101"
}
}
6 changes: 3 additions & 3 deletions src/property.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export interface PropertyDefinition {
env?: string;
argv?: string;
resolve?(config: Object): any;
transform?(value: any): any;
transform?(value: any, config: Object): any;
}

function isDefined(value: any): boolean {
Expand Down Expand Up @@ -106,8 +106,8 @@ export default class Property {
}

transform(value: any, rawConfig: Object): any {
if (!isDefined(value)) return value;
return this.definition.transform ? this.definition.transform(value) : value;
if (!(isDefined(value) && this.definition.transform)) return value;
return this.definition.transform(value, rawConfig);
}

validate(value: any): void {
Expand Down
2 changes: 1 addition & 1 deletion src/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default class Type {
if (typeof definition.validate !== 'function') {
throw new Error(`Type ${name} does not have validate method`);
}

this.validate = definition.validate;
}

Expand Down
23 changes: 22 additions & 1 deletion test/integration/transform.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,26 @@ describe('Transform', () => {

expect(bound).not.toThrow();
expect(bound()).toEqual({ origin: 'localhost:8888' });
})
});

it('accepts a raw config object as a second argument', () => {
const schema = new Schema({
foo: {
description: 'description',
type: 'string',
transform: (value, config) => [value, config.bar].join(':')
},
bar: {
description: 'description',
type: 'string',
required: true,
transform: (value) => value.trim()
}
});

expect(schema.hydrate({ bar: ' baz ', foo: 'qux' })).toEqual({
foo: 'qux:baz',
bar: 'baz'
});
});
});

0 comments on commit 139f11b

Please sign in to comment.