change-by-example

1.7.2 • Public • Published

change-by-example

Finds a function that transforms a given object into another given object.

NPM

Build status semantic-release js-standard-style next-update-travis badge renovate-app badge

Install

Requires Node version 6 or above.

npm install --save change-by-example

Use

Imagine we have two objects: a source object and a destination object. If we want to transform the source into destination, we have to write the transform function ourselves. We could use shortcuts like Ramda.evolve or use one of the libs helping us write the tranform (like map-factory, xform, mapper.js. transformo). All this is complicated.

Why can't the computer compute the transform that from source -> destination pair? Well, change-by-example is an attempt to do this.

Give it two objects and get back and transform function

const change = require('change-by-example')
const source = {
  name: 'john',
  age: '42',
  occupation: 'mechanic'
}
const destination = {
  name: 'John',
  age: 42
}
const f = change(source, destination)

If you call this transform function with original source object, you get back destination again

console.log(f(source))
// {name: "John", age: 42}

But if you give it an object of the same shape as source, it will transform it the same way.

console.log(f({
  name: 'mary',
  age: '30',
  occupation: 'engineer'
}))
// {name: 'Mary', age: 30}

Advanced

You can nest source and destination objects and even pass additional unary value transform functions to try. See spec.

Examples

Supported

  • generates transformation function string representation #1
  • property delete spec
  • property rename spec
  • simple string transforms from Ramda and Lodash
  • deep source paths #3
  • deep destination paths #7
  • compound transforms like trim + toLower #8
  • extracting from arrays #9 spec
  • passing custom transforms to use #10 spec
  • combining values like .fullName = .first + .last

Debugging

Run with environment variable DEBUG=change-by-example ...

Human text

You can see the human-friendly transformation by printing the returned transform's function. It shows for each destination property the transform and the source property. For above example it would print

const source = {
  name: 'john',
  age: '42',
  occupation: 'mechanic'
}
const destination = {
  name: 'John',
  age: 42
}
const change = require('change-by-example')
const t = change(source, destination)
console.log(t.toString())
/*
name: _.capitalize(name)
age: _.parseInt(age)
*/

Similarly, deep paths will be shown using dots

const input = {
  name: 'foo',
  other: {
    info: {
      age: '42'
    }
  }
}
const output = {
  name: {
    first: 'foo'
  },
  age: 42
}
const t = change(input, output)
console.log(t.toString())
/*
name.first: R.identity(name)
age: _.parseInt(other.info.age)
*/

Related

This is much simpler problem that general value to value mapping like I am trying to do in Rambo

Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2017

License: MIT - do anything with the code, but don't blame me if it does not work.

Support: if you find any problems with this module, email / tweet / open issue on Github

MIT License

Copyright (c) 2017 Gleb Bahmutov <gleb.bahmutov@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i change-by-example

Weekly Downloads

0

Version

1.7.2

License

MIT

Unpacked Size

14.5 kB

Total Files

5

Last publish

Collaborators

  • bahmutov