Skip to content

Commit

Permalink
feat(dest): support deep nested properties in destination, close #7
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Jul 21, 2017
1 parent 120ecde commit 122e24b
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ 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
* [ ] deep destination paths #7
* [x] deep source paths bahmutov/change-by-example#3
* [x] deep destination paths bahmutov/change-by-example#7
* [ ] compound transforms like `trim + toLower`
* [ ] combining values like `.fullName = .first` + `.last`

Expand Down
2 changes: 1 addition & 1 deletion src/change-by-example-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('change-by-example', () => {
la(R.equals(output, expected), diff(expected, output).text)
})

it.skip('should generate non-flat object', () => {
it('should generate non-flat object', () => {
const source = {
name: {
first: 'joe',
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const la = require('lazy-ass')
const R = require('ramda')
const debug = require('debug')('change-by-example')
const pluralize = require('pluralize')
const { findTransform } = require('./utils')
const { allPaths, findTransform } = require('./utils')

function o2o (source, destination) {
la(is.object(source), 'expected an object', source)
Expand All @@ -14,11 +14,14 @@ function o2o (source, destination) {
// look at each destination property
const destinationPropertyTransforms = []

R.keys(destination).forEach(key => {
const destinationValue = destination[key]
const paths = allPaths(destination)
la(Array.isArray(paths), 'could not compute list of paths from', destination)

paths.forEach(path => {
const destinationValue = R.view(R.lensPath(path), destination)
const readTransform = findTransformTo(destinationValue)
if (readTransform) {
const write = R.set(R.lensProp(key))
const write = R.set(R.lensPath(path))
const readAndWrite = (from, to) => write(readTransform(from), to)
destinationPropertyTransforms.push(readAndWrite)
}
Expand Down
1 change: 0 additions & 1 deletion src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const findTransform = source => value => {
const paths = allPaths(source)
la(Array.isArray(paths), 'could not compute list of paths from', source)

// R.keys(source).some(key => {
paths.some(path => {
const sourceValue = R.view(R.lensPath(path), source)
const foundTransform = transforms.some(t => {
Expand Down

0 comments on commit 122e24b

Please sign in to comment.