Giter Club home page Giter Club logo

flaty.js's Introduction

Flaty

Build Status

Flaty is a method for condensing a multi-dimensional JSON like object into a single-dimensional object.

Example:

obj =
  name: 'foo'
  val: 'bar'
  key1: 'test'
  ary: ['a', 'b', 'c', {x: 1, y: 3}, [1, 2, 3]]
  obj:
    t: 'a'
    z: 'x'
    nest: [1, 2, 3, 4]
    us:
      texas:
        austin: 'Weird'
        dallas: 'Cowboy'

console.log(flaty.flatten(obj))

flat =
  'ary@0': "a"
  'ary@1': "b"
  'ary@2': "c"
  '[email protected]': 1
  '[email protected]': 3
  'ary@4@0': 1
  'ary@4@1': 2
  'ary@4@2': 3
  'key1': "test"
  'name': "foo"
  'obj.nest@0': 1
  'obj.nest@1': 2
  'obj.nest@2': 3
  'obj.nest@3': 4
  'obj.t': "a"
  'obj.us.texas.austin': "Weird"
  'obj.us.texas.dallas': "Cowboy"
  'obj.z': "x"
  'val': "bar"

console.log(flaty.fatten(flat))

Why would you want to do this?

Often times you need to represent deeply nested objects as flat ones. For example, in HTML forms:

<form action="POST">
  <input type="text" name="address.street"/>
  <input type="text" name="address.city"/>
  <input type="text" name="address.zip"/>
  <hr/>
  <input type="text" name="contacts@0"/>
  <input type="text" name="contacts@1"/>
  <input type="text" name="contacts@2"/>
</form>

This also makes it easier to mix AJAX with forms:

$ = jQuery

// Load form from AJAX get
var data = flaty.flatten(json_response);

// Set form values
for ( k in data ) {
  $('form input[name="' + k + '"]').val(data[k]);
}

// Submit form with AJAX post
var form_data = {};
$('input, select, textarea').each(function() {
  var field = $(this);
  form_data[field.attr('name')] = field.val()
})

var json_data = flaty.fatten(form_data);

Flaty is also convenient for URL queries:

var urlParams = function (q) {
    var params = {};
    var e,
        a = /\+/g,
        r = /([^&=]+)=?([^&]*)/g,
        d = function (s) { return decodeURIComponent(s.replace(a, " ")); };

    while (e = r.exec(q))
       params[d(e[1])] = d(e[2]);
    return params;
};

var url = 'http://example.com?address.street=123+Road+Drive&address.city=Austin&name.first=Bob&name.last=Smith';

var params = urlParams(url.split('?')[1]);

// params will be:
/*
{
  'address.street': '123+Road+Drive',
  'address.city': 'Austin',
  'name.first': 'Bob',
  'name.last': 'Smith'
}
*/

var data = flaty.fatten(param);

// data will be:
/*
{
  'address': {
    'street': '123+Road+Drive',
    'city': 'Austin'
  },
  'name': {
    'first': 'Bob',
    'last': 'Smith'
  }
}
*/

Development and Testing

Install node modules:

npm install .

Run tests:

npm test

Building uses flour which requires node.js >=0.8.7:

cake build

flaty.js's People

Contributors

six8 avatar

Stargazers

node-migrator-bot avatar  avatar

Watchers

 avatar James Cloos avatar

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.