Skip to content

mojule/transform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

27 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

transform

Declarative object transformation

Useful for when you want to write simple models, but something in your code requires more complex models. Particularly good for turning a minimal amount of data into something that has more boiler plate. One use case we have is turning simple data models into view models. Great when you have to write lots of models by hand and don't want to write them out in full when a tool can do that for you.

Usage

npm install @mojule/transform

const transform = require( '@mojule/transform' )
const declaration = require( './path/to/declaration.json' )

const transformed = transform( model, declaration )

Examples

Append a property

Input:

const model = {
  "a": "Hello"
}

const transform = {
  "b": "World"
}

Output:

{
  "a": "Hello",
  "b": "World"
}

Copy a property

Input:

const model = {
  "a": "Hello"
}

const transform = {
  "b": {
    "$value": "a"
  }
}

Output:

{
  "a": "Hello",
  "b": "Hello"
}

Delete a property

Input:

const model = {
  "a": "Hello"
}

const transform = {
  "a": "$delete"
}

Output:

{}

Rename a property

Input:

const model = {
  "a": "Hello"
}

const transform = {
  "a": "$delete",
  "b": {
    "$value": "a"
  }
}

Output:

{
  "b": "Hello"
}

Conditional

Input:

const model = {
  "a": "Hello"
}

const transform = {
  "b": {
    "$if": [
      { "$value": "a" },
      true
    ]
  },
  "c": {
    "$if": [
      { "$value": "d" },
      true
    ]
  }
}

Output:

{
  "a": "Hello",
  "b": true
}

About

Declarative object transformation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published