Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
dot-object
Advanced tools
dot-object makes it possible to transform and read (JSON) objects using dot notation.
The dot-object npm package is a utility for transforming and manipulating JavaScript objects using dot notation. It allows you to easily convert between nested objects and flat objects, set and get values using dot notation, and remove properties from objects.
Flattening Objects
This feature allows you to convert a nested object into a flat object using dot notation. The code sample demonstrates how to flatten a nested object.
const Dot = require('dot-object');
const dot = new Dot();
const nested = { a: { b: { c: 1 } } };
const flattened = dot.dot(nested);
console.log(flattened); // { 'a.b.c': 1 }
Unflattening Objects
This feature allows you to convert a flat object with dot notation keys back into a nested object. The code sample demonstrates how to unflatten a flat object.
const Dot = require('dot-object');
const dot = new Dot();
const flattened = { 'a.b.c': 1 };
const nested = dot.object(flattened);
console.log(nested); // { a: { b: { c: 1 } } }
Setting Values
This feature allows you to set a value in an object using dot notation. The code sample demonstrates how to set a nested value in an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = {};
dot.set('a.b.c', 1, obj);
console.log(obj); // { a: { b: { c: 1 } } }
Getting Values
This feature allows you to get a value from an object using dot notation. The code sample demonstrates how to retrieve a nested value from an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = { a: { b: { c: 1 } } };
const value = dot.pick('a.b.c', obj);
console.log(value); // 1
Removing Properties
This feature allows you to remove a property from an object using dot notation. The code sample demonstrates how to delete a nested property from an object.
const Dot = require('dot-object');
const dot = new Dot();
const obj = { a: { b: { c: 1 } } };
dot.del('a.b.c', obj);
console.log(obj); // { a: { b: {} } }
The 'flat' package provides similar functionality for flattening and unflattening objects. It is simple to use and focuses on converting nested objects to flat objects and vice versa. Compared to dot-object, 'flat' is more specialized and may be easier to use for basic flattening and unflattening tasks.
Lodash is a utility library that provides a wide range of functions for manipulating objects, arrays, and other data structures. It includes methods for deep setting, getting, and removing properties using dot notation. While Lodash offers more comprehensive functionality beyond object manipulation, it can be more complex to use compared to dot-object.
The 'object-path' package provides utilities for accessing and manipulating deep properties of objects using dot notation. It offers similar features to dot-object, such as setting, getting, and deleting properties. 'object-path' is focused on deep property manipulation and is a good alternative to dot-object for these specific tasks.
Dot-Object makes it possible to transform javascript objects using dot notation.
Install from npm:
npm install dot-object --save
Install from bower:
bower install dot-object --save
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'
}
}
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
}
}
}
}
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}}}
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"
}
}
}
}
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!
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 indexesvar 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);
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"
}
}
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"
}
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"
}
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"]
}
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"
}
}
SQL translation on the fly:
// TODO
Copyright © 2013 Rob Halff, released under the MIT license
2024-04-19 Version 2.1.5
a23221d367408
] - Add LICENSE property to package.json (Fixed by denniskim #54)FAQs
dot-object makes it possible to transform and read (JSON) objects using dot notation.
The npm package dot-object receives a total of 209,471 weekly downloads. As such, dot-object popularity was classified as popular.
We found that dot-object demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.