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
- Development version: https://unpkg.com/dot-object/dist/dot-object.js Uncompressed with Comments
- Production version: https://unpkg.com/dot-object/dist/dot-object.min.js Minified
Usage
Move a property within one object to another location
var dot = ; var obj = 'first_name': 'John' 'last_name': 'Doe'; dot;dot; console; contact: firstname: 'John' lastname: 'Doe'
Copy property from one object to another
var dot = ; var src = name: 'John' stuff: phone: brand: 'iphone' version: 6 ; var tgt = name: 'Brandon'; dot; console; 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; // src: {"name":"John","stuff":{}}// tgt: {"name":"Brandon","wanna":{"haves":{"phone":{"brand":"iphone","version":6}}}
Expand to an object
var dot = ; 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'; dotobjectrow; console; "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 = ; var tgt = val: 'test' ;dot; console; "val": "test" "this": "is": "my": "string": "value"
Pick a value using dot notation:
Picks a value from the object without removing it.
var dot = ; var obj = some: nested: value: 'Hi there!' ; var val = dot;console; 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 = ; var obj = a: 'Hi There!' nested: array: 'Veni' 'Vidi' 'Vici' ; var val = dot;console; Hi There! // To remove an item and directly update any array indexes use:var val = dot;console; Vidi // Remove a value but retain array indexes. var val = dot; // To remove multiple paths at once:var val = dot;
Using modifiers
You can use modifiers to translate values on the fly.
This example uses the underscore.string library.
var dot = ; var _s = ; var row = 'nr': 200 'doc.name': ' My Document '; var mods = "doc.name": _strim _sunderscored; dotobjectrow mods; console;
{
"nr": 200,
"doc": {
"name": "my_document"
}
}
Or using .str() directy:
var dot = ;var _s = ;var obj = id: 100 ; // use one modifierdot; // multiple modifiersdot; console;
Result:
Transform object
var dot = ; 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; // OR var tgt = dot; console; "nr": 1 "name": "first": "John" "last": "Doe" "email": "example@gmail.com"
Convert object to dotted-key/value pair
var dot = ; var obj = id: 'my-id' nes: ted: value: true other: nested: stuff: 5 some: array: 'A' 'B' ; var tgt = dot; // or var tgt = {};dot; console;
Result:
Keep array
Set keepArray to true.
var dot = ; var obj = id: 'my-id' other: 1 2 3 some: array: 'A' 'B' ; dotkeepArray = true;var tgt = dot; console;
Result:
Using a different separator
If you do not like dot notation, you are free to specify a different separator.
var Dot = ; var dot = '->'; var _s = ; var row = 'nr': 200 'doc->name': ' My Document '; var mods = "doc->name": _strim _sunderscored; dotobjectrow mods; console;
{
"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