Coder Social home page Coder Social logo

dot-object's Introduction

Build Status

Dot-Object

Dot-Object makes it possible to transform javascript objects using dot notation.

Move a property within one object to another location

var DJ = require('dot-object');

var dj = new DJ();

var obj = {
  'first_name': 'John',
  'last_name': 'Doe'
};

dj.move('first_name', 'contact.firstname', obj);
dj.move('last_name', 'contact.lastname', obj);

console.log(obj);

{
  contact: {
    firstname: 'John',
    lastname: 'Doe'
  }
}

Transform an object

var DJ = require('dot-object');

var dj = new DJ();

var row = {
  'id': 2,
  'contact.name.first': 'John',
  'contact.name.last': 'Doe',
  'contact.email': '[email protected]',
  'contact.info.about.me': 'classified'
};

dj.object(row);

console.log(row);

{
  "id": 2,
  "contact": {
    "name": {
      "first": "John",
      "last": "Doe"
    },
    "email": "[email protected]",
    "info": {
      "about": {
      "me": "classified"
      }
    }
  }
}

To convert manually per string use:

var DJ = require('dot-object');

var dj = new DJ();

var obj = { val: 'test' };
dj.str('this.is.my.string', 'value', obj);

console.log(obj);

{
  "val": "test",
  "this": {
    "is": {
      "my": {
        "string": "value"
      }
    }
  }
}

Pick a value using dot notation:

var obj = {
 some: {
   nested: {
     value: 'Hi there!'
   }
 }
};

var val = dj.pick('some.nested.key', obj);
console.log(val);

Hi there!

Using modifiers

You can use modifiers to translate values on the fly.

This example uses the underscore.string library.

var DJ = require('dot-object');

var dj = new DJ();

var _s = require('underscore.string');

var row = {
  'nr': 200,
  'doc.name': '    My Document   '
};

var mods = {
  "doc.name": [_s.trim, _s.underscored],
};

dj.object(row, mods);

console.log(row);
{
  "nr": 200,
  "doc": {
    "name": "my_document"
  }
}

Or using .str() directy:

var DJ = require('dot-object');
var _s = require('underscore.string');
var obj = { id: 100 };

var dj = new DJ();

// use one modifier
dj.str('my.title', 'this is my title', obj, _s.slugify);

// multiple modifiers
dj.str('my.title', '   this is my title  ', obj, [_s.trim, _s.slugify]);

console.log(obj);

Result:

{
  "id": 100,
  "my": {
    "title": "this-is-my-title"
  }
}

Using a different seperator

If you do not like dot notation, you are free to specify a different seperator.

var DJ = require('dot-object');

var dj = new DJ('->');

var _s = require('underscore.string');

var row = {
  'nr': 200,
  'doc->name': '    My Document   '
};

var mods = {
  "doc->name": [_s.trim, _s.underscored],
};

dj.object(row, mods);

console.log(row);
{
  "nr": 200,
  "doc": {
    "name": "my_document"
  }
}

Transforming SQL results to JSON

SQL translation on the fly:

 // TODO

Copyright © 2013 Rob Halff, released under the MIT license

dot-object's People

Contributors

rhalff avatar liverbool 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.