All Projects → bahmutov → Change By Example

bahmutov / Change By Example

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

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Change By Example

derivejs
DeriveJS is a reactive ODM - Object Document Mapper - framework, a "wrapper" around a database, that removes all the hassle of data-persistence by handling it transparently in the background, in a DRY manner.
Stars: ✭ 54 (+68.75%)
Mutual labels:  object, mapper
Mapper
A simple and easy go tools for auto mapper map to struct, struct to map, struct to struct, slice to slice, map to slice, map to json.
Stars: ✭ 175 (+446.88%)
Mutual labels:  mapper, map
Ngx Dynamic Form Builder
FormBuilder + class-transformer + class-validator = dynamic form group builder for Angular10+
Stars: ✭ 93 (+190.63%)
Mutual labels:  object, transform
Excel2Object
excel convert to .NET Object | Excel与.NET 对象进行转换,支持公式、多Sheet等功能
Stars: ✭ 35 (+9.38%)
Mutual labels:  object, mapper
awesome-maps-ukraine
A curated list of maps of Ukraine, ukrainian mappers and tools that they use or develop for creating and publish maps
Stars: ✭ 35 (+9.38%)
Mutual labels:  map, mapper
Kind Of
Get the native JavaScript type of a value, fast. Used by superstruct, micromatch and many others!
Stars: ✭ 268 (+737.5%)
Mutual labels:  object, map
UltraMapper
A fast and lightweight object-to-object .NET mapper.
Stars: ✭ 28 (-12.5%)
Mutual labels:  map, mapper
Morphism
⚡ Type-safe data transformer for JavaScript, TypeScript & Node.js.
Stars: ✭ 336 (+950%)
Mutual labels:  object, mapper
Lpfmpoints
Evolution of LPFM Stations
Stars: ✭ 19 (-40.62%)
Mutual labels:  map
Fingereyes Xr
FingerEyes-Xr for HTML5, A JavaScript library for building professional GIS System.
Stars: ✭ 9 (-71.87%)
Mutual labels:  map
Hashmap
A Golang lock-free thread-safe HashMap optimized for fastest read access.
Stars: ✭ 899 (+2709.38%)
Mutual labels:  map
Unesco Map
Show all the monuments of the UNESCO on an interactive map
Stars: ✭ 23 (-28.12%)
Mutual labels:  map
Reconstruct
👷 Iterate over properties and merge them into a new object.
Stars: ✭ 11 (-65.62%)
Mutual labels:  object
Vuex Mappers
Component agnostic Vuex mappers
Stars: ✭ 19 (-40.62%)
Mutual labels:  mapper
Hkr Map
车辆历史轨迹展示、百度地图
Stars: ✭ 12 (-62.5%)
Mutual labels:  map
Maps
A Mapbox GL react native module for creating custom maps
Stars: ✭ 893 (+2690.63%)
Mutual labels:  map
Mappinggenerator
🔄 "AutoMapper" like, Roslyn based, code fix provider that allows to generate mapping code in design time.
Stars: ✭ 831 (+2496.88%)
Mutual labels:  mapper
Open Redistricting
A collaborative, open legislative redistricting tool, built atop GitHub.
Stars: ✭ 21 (-34.37%)
Mutual labels:  map
Luftdaten irceline viewer
interactive Air-Quality map showing Irceline and luftdaten data
Stars: ✭ 12 (-62.5%)
Mutual labels:  map
Performance
In diesem Repository befinden sich Projekte rund um das Thema Performanz.
Stars: ✭ 7 (-78.12%)
Mutual labels:  mapper

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

  • [x] generates transformation function string representation #1
  • [x] property delete spec
  • [x] property rename spec
  • [x] simple string transforms from Ramda and Lodash
  • [x] deep source paths #3
  • [x] deep destination paths #7
  • [x] compound transforms like trim + toLower #8
  • [x] extracting from arrays #9 spec
  • [x] 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 <[email protected]> © 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 <[email protected]>

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].