From 25f829b7adc348dd49f0505bc8a5951bf13f6a52 Mon Sep 17 00:00:00 2001 From: Gleb Bahmutov Date: Thu, 20 Jul 2017 22:48:19 -0400 Subject: [PATCH] feat(compound): support two level transforms, close #8 --- README.md | 10 +++++++--- src/change-by-example-spec.js | 10 ++++++++++ src/utils.js | 14 +++++++++++++- 3 files changed, 30 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 1eb439c..a431851 100644 --- a/README.md +++ b/README.md @@ -71,11 +71,15 @@ console.log(f({ * [x] property rename * [x] simple string transforms from [Ramda](http://ramdajs.com/docs/) and [Lodash](https://lodash.com/docs/) -* [x] deep source paths [#3](https://github.com/bahmutov/change-by-example/issues/3) -* [x] deep destination paths [#7](https://github.com/bahmutov/change-by-example/issues/7) -* [ ] compound transforms like `trim + toLower` +* [x] deep source paths [#3][3] +* [x] deep destination paths [#7][7] +* [x] compound transforms like `trim + toLower` [#8][8] * [ ] combining values like `.fullName = .first` + `.last` +[3]: https://github.com/bahmutov/change-by-example/issues/3 +[7]: https://github.com/bahmutov/change-by-example/issues/7 +[8]: https://github.com/bahmutov/change-by-example/issues/8 + ## Debugging Run with environment variable `DEBUG=change-by-example ...` diff --git a/src/change-by-example-spec.js b/src/change-by-example-spec.js index 0da267f..4b471cf 100644 --- a/src/change-by-example-spec.js +++ b/src/change-by-example-spec.js @@ -59,6 +59,16 @@ describe('change-by-example', () => { finds(source, destination) }) + it('finds 2 level transforms', () => { + const source = { + name: ' joe ' + } + const destination = { + name: 'JOE' + } + finds(source, destination) + }) + it('works with nested objects', () => { const source = { age: 42, diff --git a/src/utils.js b/src/utils.js index 1a013c0..ee871c9 100644 --- a/src/utils.js +++ b/src/utils.js @@ -14,6 +14,18 @@ const otherTransforms = [ const transforms = otherTransforms.concat(stringTransforms) +// combinations of two transforms +const combine = (t, s) => { + const combined = { + f: R.compose(t.f, s.f), + name: `${t.name} * ${s.name}` + } + return combined +} +const transforms2 = R.flatten( + transforms.map(t => transforms.map(s => combine(t, s))) +) + // returns transform function that is Transform(get view lens) const findTransform = source => value => { // given source object and desired value @@ -27,7 +39,7 @@ const findTransform = source => value => { paths.some(path => { const sourceValue = R.view(R.lensPath(path), source) - const foundTransform = transforms.some(t => { + const foundTransform = transforms2.some(t => { try { const out = t.f(sourceValue) if (R.equals(value, out)) {