Skip to content

Commit

Permalink
feat(compound): support two level transforms, close #8
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jul 21, 2017
1 parent 141c651 commit 25f829b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...`
Expand Down
10 changes: 10 additions & 0 deletions src/change-by-example-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
14 changes: 13 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)) {
Expand Down

0 comments on commit 25f829b

Please sign in to comment.