From 139f11ba20a84bb1a988e7778069d1ce0b177849 Mon Sep 17 00:00:00 2001 From: Patrick Camacho Date: Sun, 1 Jan 2017 17:55:44 -0800 Subject: [PATCH] update logic for transform to pass in config --- README.md | 4 ++-- package.json | 14 ++++++++++---- src/property.ts | 6 +++--- src/type.ts | 2 +- test/integration/transform.spec.ts | 23 ++++++++++++++++++++++- 5 files changed, 38 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 010b26a..04aa6df 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/package.json b/package.json index debc3c5..036d8b3 100644 --- a/package.json +++ b/package.json @@ -1,9 +1,9 @@ { "name": "pulpo", - "version": "1.3.0", + "version": "1.3.1", "description": "Configuration mechanism", "author": "Patrick Camacho ", - "license": "Apache", + "license": "Apache-2.0", "keywords": [ "configuration", "config", @@ -39,7 +39,9 @@ "typescript": "1.8.10" }, "jest": { - "scriptPreprocessor": "/preprocessor.js", + "transform": { + ".*": "/preprocessor.js" + }, "moduleFileExtensions": [ "ts", "tsx", @@ -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" } } diff --git a/src/property.ts b/src/property.ts index 2aab81b..97bd459 100644 --- a/src/property.ts +++ b/src/property.ts @@ -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 { @@ -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 { diff --git a/src/type.ts b/src/type.ts index 417e0ca..f4bcdca 100644 --- a/src/type.ts +++ b/src/type.ts @@ -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; } diff --git a/test/integration/transform.spec.ts b/test/integration/transform.spec.ts index 8522648..0c2ed6c 100644 --- a/test/integration/transform.spec.ts +++ b/test/integration/transform.spec.ts @@ -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' + }); + }); });