npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

flat-transform

v5.0.0

Published

flat-transform is a small library tranforming your flat objects into rich ones using declarative rules

Downloads

9

Readme

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