Détail du package

dot-object

rhalff1.8mMIT2.1.5

dot-object makes it possible to transform and read (JSON) objects using dot notation.

json, object, filter, transform

readme

Build Status

Dot-Object

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

Installation

Install from npm:

  npm install dot-object --save

Install from bower:

  bower install dot-object --save

Download

Usage

Move a property within one object to another location

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

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

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

console.log(obj);

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

Copy property from one object to another

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

var src = {
  name: 'John',
  stuff: {
    phone: {
      brand: 'iphone',
      version: 6
    }
  }
};

var tgt = {name: 'Brandon'};

dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt);

console.log(tgt);

{
  name: 'Brandon',
  wanna: {
    haves: {
      phone: {
        brand: 'iphone',
        version: 6
      }
    }
  }
}

Transfer property from one object to another

Does the same as copy but removes the value from the source object:

dot.transfer('stuff.phone', 'wanna.haves.phone', src, tgt);

// src: {"name":"John","stuff":{}}
// tgt: {"name":"Brandon","wanna":{"haves":{"phone":{"brand":"iphone","version":6}}}

Expand to an object

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

var row = {
  'id': 2,
  'contact.name.first': 'John',
  'contact.name.last': 'Doe',
  'contact.email': 'example@gmail.com',
  'contact.info.about.me': 'classified',
  'devices[0]': 'mobile',
  'devices[1]': 'laptop',
  'some.other.things.0': 'this',
  'some.other.things.1': 'that'
};

dot.object(row);

console.log(row);

{
  "id": 2,
  "contact": {
    "name": {
      "first": "John",
      "last": "Doe"
    },
    "email": "example@gmail.com",
    "info": {
      "about": {
        "me": "classified"
      }
    }
  },
  "devices": [
    "mobile",
    "laptop"
  ],
  "some": {
    "other": {
      "things": [
        "this",
        "that"
      ]
    }
  }
}

To convert manually per string use:

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

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

console.log(tgt);

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

Pick a value using dot notation:

Picks a value from the object without removing it.

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

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

var val = dot.pick('some.nested.value', obj);
console.log(val);

Hi there!

Delete/Remove a value using dot notation:

Remove and delete mostly behave the same, but in case of a path addressing array items:

  • delete will re-index the array.
  • remove will retain array indexes
var dot = require('dot-object');

var obj = {
 a: 'Hi There!',
 nested: {
   array: [ 
     'Veni',
     'Vidi',
     'Vici',
   ]
 }
};

var val = dot.delete('a', obj);
console.log(val);

Hi There!

// To remove an item and directly update any array indexes use:
var val = dot.delete('nested.array[1]', obj);
console.log(val);

Vidi

// Remove a value but retain array indexes. 
var val = dot.remove('nested.array[1]', obj);

// To remove multiple paths at once:
var val = dot.remove(['nested.array[0]', 'nested.array[2]'], obj);

Using modifiers

You can use modifiers to translate values on the fly.

This example uses the underscore.string library.

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

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

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

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

dot.object(row, mods);

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

Or using .str() directy:


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

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

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

console.log(obj);

Result:

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

Transform object

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

var source = {
  "id": 1,
  "contact": {
    "firstName": "John",
    "lastName": "Doe",
    "email": "example@gmail.com",
  }
}

var recipe = {
  'id': 'nr',
  'contact.firstName': 'name.first',
  'contact.lastName': 'name.last',
  'contact.email': 'email'
};

var tgt = {}
dot.transform(recipe, source, tgt);

// OR

var tgt = dot.transform(recipe, source);

console.log(tgt);
{
  "nr": 1,
  "name": {
    "first": "John",
    "last": "Doe"
  },
  "email": "example@gmail.com"
}

Convert object to dotted-key/value pair

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

var obj = {
  id: 'my-id',
  nes: { ted: { value: true } },
  other: { nested: { stuff: 5 } },
  some: { array: ['A', 'B'] }
};

var tgt = dot.dot(obj);

// or

var tgt = {};
dot.dot(obj, tgt);

console.log(tgt);

Result:

{
  "id": "my-id",
  "nes.ted.value": true,
  "other.nested.stuff": 5,
  "some.array[0]": "A",
  "some.array[1]": "B"
}

Keep array

Set keepArray to true.

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

var obj = {
  id: 'my-id',
  other: [1, 2, 3],
  some: { array: ['A', 'B'] }
};

dot.keepArray = true;
var tgt = dot.dot(obj);

console.log(tgt);

Result:

{
  "id": "my-id",
  "other": [1, 2, 3],
  "some.array": ["A", "B"]
}

Using a different separator

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

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

var dot = new Dot('->');

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

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

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

dot.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

changelog

ChangeLog

2024-04-19 Version 2.1.5

  • [a23221d367408] - Add LICENSE property to package.json (Fixed by denniskim #54)
  • 2020-09-10 Version 2.1.4

  • [94b9eb8a2d] - Fix parsing of array paths for non standard separators (Fixed by boidolr #58)

2020-02-16 Version 2.1.3

  • fix possible pollution of prototype for paths containing proto

2019-11-02 Version 2.1.1

  • fix undefined key with root level array.

2019-11-02 Version 2.1.1

  • Wrong array conversion when brackets are used (Reported by vladshcherbin #27)

2019-11-02 Version 2.1.0

  • fix delete function not being wrapped. (Reported by murphyke #40)

2019-11-02 Version 2.0.0

  • [2cb41bbd1b] - Add useBrackets option for the .dot() method (by z1m1n #42)
  • dot() now writes brackets by default (possibly breaking change). Use Dot.useBrackets = false to keep the old behavior

2019-07-29 Version 1.9.0

  • allows allow to process root level property using dot.object

2019-07-18 Version 1.8.1

  • always allow to set root level property using dot.str

2019-07-18 Version 1.8.0

  • [cdc691424b] - Options to remove array elements and reindex the array. (Fixed by MechJosh0 #36)

2018-10-26 Version 1.7.1

  • [e1bb99c83e] - Fix isIndex numeric key matching. (Fixed by mrdivyansh #31)

2017-09-20 Version 1.7.0

  • let .dot and .object understand empty objects / arrays

2017-07-27 Version 1.6.0

  • implemented keepArray

2016-09-29 Version 1.5.4

  • update dist

2016-09-29, Version 1.5.3

  • Fix override bug in str()

2016-08-04, Version 1.5.0

  • [a7e948f2fa] - Ensure we only process true Arrays and Objects. (Fixed by MechJosh0 #15)

2016-02-14, Version 1.4.0

  • add transform() method
  • use standard style

2015-11-17, Version 1.3.1

  • [e46da6ffc0] - Fix deep array bug. (Reported by ami44 #10)

2015-10-01, Version 1.3.0

  • [baa42022bf] - Adds a parameter (useArray) to allow converting object without using arrays. (Thomas Moiron)

2015-09-07, Version 1.2.0

  • allow override even when target is non-empty object
  • also return the object when using object() or str()

2015-08-08, Version 1.1.0

  • Also let .object() understand array notation.

2015-08-03, Version 1.0.0

  • Convert object to dotted-key/value pairs

2015-04-15, Version 0.11.0

  • Add support for bracket notation

2015-03-22, Version 0.10.0

  • Make return value optional for Object/Array modifier(s)
  • Add modifier option to move(), transfer() & copy()

2015-03-21, Version 0.9.0

  • support multiple replacements/deletions (cli)
  • add del/remove method
  • improve bower build

2015-03-09, Version 0.8.1

2015-03-06, Version 0.8.0

2015-03-06, Version 0.7.0

  • [c4658c386f] - Look for properties down in the prototype chain. (Fer Uría)
  • [a45c4a7982] - Fix picking a null property with a dotted value. (Fer Uría)