Giter VIP home page Giter VIP logo

flat-transform's Introduction

flat-transform

A small library transforming your flat objects into rich ones using declarative rules.

var transform = require("flat-transform").transform.filter

var object = {
  username: "John Doe",
  phone_country_code: "+420",
  phone_number: "123 456 789"
}

var rules = {
  username: "username",
  phone: {
    country_code: "phone_country_code",
    number: "phone_number"
  }
}

var output = transform(rules, object)

// {
//   username: "John Doe",
//   phone: {
//     country_code: "+420",
//     number: "123 456 789"
//   }
// }

Installation

npm install flat-transform --save

Use cases

Relational databases usually return flat row objects. The goal of an arbitrary API is to present data in a structured form.

This library's goal is to transform this flat relation into a structured object simply by declaring the output structure with matching property names of the original relation.

Function can also be used for creating new properties. The arguments of the function should correspond with the properties of the original object.

API

transform.filter(rules[, exceptions], object)

Transforms original object into the new one, which is defined by rules. It ommits original fields (the ones not in the rules object) in the new object.

The array of exceptions can be used, each item in the exceptions array is a function.

transform.preserve(rules[, exceptions], object)

Transforms original object into the new one, which is defined by rules. It preserves original fields (the ones not in the rules object) in the new object.

The array of exceptions can be used, each item in the exceptions array is a function.

Declaration of the rules

The rules object is the declaration of the output and its connection to the original object.

var object = { username: "John Doe" }  

var rules = {
  user: {
    name: "username"
  }
}

var output = transform(rules, object)

// { user: { name: "John Doe" } }

Each key of the rules object is the declaration, each value is the connection.

The values of the rules object can be either strings, functions or objects.

Any String value of the rules object is the connection to the key of the original object.

var object = { username: "John Doe" }

var rules = {
  is_john_doe: function (username) { return username == "John Doe" ? true : false }
}

var output = transform(rules, object)

// { is_john_doe: true }

Any Function value of the rules object is evaluated and arguments of the function are the connection to the matching keys of the original object.

Using the exceptions array

Often, the existence of certain keys on the outputting object may depend on the content of the original object.

var object = {
  username: "John Doe",
  email: null
}

var rules = {
  user: {
    username: "username",
    email: "email"
  }
}

var exceptions = [ function (email) { return email == null ? "email" : null } ]

var output = transform(rules, exceptions, object)

// { 
//   user: {
//     username: "John Doe"  
//   }
// }

Each Function in the exceptions array is evaluated and arguments of the function are the connection to the matching keys of the original object. The output is expected to be either String or null. The String output is defining which key in the rules object is to be ignored.

Applying the transformation on the array of objects

You can use map to apply the transformation function. The rules (and exceptions if needed) can be bound by the bind function (partial application).

var objects = [
  { username: "John Doe" },
  { username: "Brad Pitt" },
  { username: "Angelina Jolie" }
]

var rules = {
  is_john_doe: function (username) { return username == "John Doe" ? true : false }
}

var output = objects.map(transform.bind(this, rules))

// [
//   { is_john_doe: true },
//   { is_john_doe: false },
//   { is_john_doe: false }
// ]

Examples

var flat = require("flat-transform")

var obj = {
  message: "Hello",
  username: "John Doe",
  user_id: 1,
  phone_country_code: "+420",
  phone_number: "123 456 789",
  swag: true
}

var rules = {
  user: {
    id: "user_id",
    username: "username",
    phone: {
      country_code: "phone_country_code",
      number: { stringified: "phone_number" },
    },
    wot: function (user_id, swag) { return user_id && swag ? "such swag" : ":(" }
  },
  phone1: "phone_number",
  phone2: "phone_number",
  phone3: "phone_number",
  phone4: "phone_number"
}

var transformed = flat.transform.filter(rules, obj)

/*
{
  user: { 
    id: 1,
    username: "John Doe",
    phone: { 
      country_code: "+420" 
      number: { stringified: "123 456 789" }
    },
    wot: "such swag"
  },
  phone1: "123 456 789",
  phone2: "123 456 789",
  phone3: "123 456 789",
  phone4: "123 456 789"
}
*/

var transformed = flat.transform.preserve(rules, obj)

/*
{
  user: { 
    id: 1,
    username: "John Doe",
    phone: { 
      country_code: "+420" 
      number: { stringified: "123 456 789" }
    },
    wot: "such swag"
  },
  phone1: "123 456 789",
  phone2: "123 456 789",
  phone3: "123 456 789",
  phone4: "123 456 789",
  message: "Hello"
}
*/

License

MIT

flat-transform's People

Contributors

acro avatar janzal avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

janzal sinabakh

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.