Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
object-treeify
Advanced tools
The object-treeify npm package is designed to convert JavaScript objects into tree-like string representations. This can be particularly useful for visualizing hierarchical data structures, debugging, or logging purposes. It allows developers to easily generate a textual representation of an object's structure, making it more readable and understandable.
Basic tree generation
This feature allows you to generate a basic tree structure from a JavaScript object. The code sample demonstrates how to convert a nested object into a tree-like string representation, categorizing items under 'fruits' and 'vegetables'.
const treeify = require('object-treeify');
const data = {
fruits: {
apple: {},
banana: {},
cherry: {}
},
vegetables: {
tomato: {},
cucumber: {},
carrot: {}
}
};
console.log(treeify(data));
Customizing options
This feature demonstrates how to customize the appearance of the tree structure using various options. The code sample shows how to adjust the spacing, joining character, and the prefixes used for keys to alter how the tree is displayed.
const treeify = require('object-treeify');
const data = {
animals: {
mammals: {
dog: {},
cat: {}
},
birds: {
parrot: {},
sparrow: {}
}
}
};
const options = {
join: '\n',
spacerNoNeighbour: ' ',
spacerNeighbour: '│ ',
keyNoNeighbour: '└─ ',
keyNeighbour: '├─ '
};
console.log(treeify(data, options));
Archy is a package that also allows for the visualization of hierarchical data as a tree structure in the console. Compared to object-treeify, archy focuses more on simplicity and ease of use but might not offer the same level of customization for the tree representation.
Treeify is another npm package that converts objects into tree structures. While it shares a similar purpose with object-treeify, its approach and customization options may differ, offering users an alternative way to visualize their object hierarchies.
Stringify Object as tree structure
{
oranges: {
'mandarin': { ├─ oranges
clementine: null, │ └─ mandarin
tangerine: 'so cheap and juicy!' -=> │ ├─ clementine
} │ └─ tangerine: so cheap and juicy!
}, └─ apples
apples: { ├─ gala
'gala': null, └─ pink lady
'pink lady': null
}
}
Project was inspired by treeify and works almost identical. However the algorithm is much shorter and faster, works without recursion and is very memory efficient. Furthermore the output can be sorted using a custom comparator function.
$ npm install --save object-treeify
const treeify = require('object-treeify');
treeify({
oranges: {
mandarin: {
clementine: null,
tangerine: 'so cheap and juicy!'
}
},
apples: {
gala: null,
'pink lady': null
}
}, {/* options */});
// =>
// ├─ oranges
// │ └─ mandarin
// │ ├─ clementine
// │ └─ tangerine: so cheap and juicy!
// └─ apples
// ├─ gala
// └─ pink lady
Type: boolean
Default: true
By default a single string is returned. Can be set to false
to instead return an array containing lines.
Type: string
Default:
Prefix for depth level when no further neighbour is present.
Type: string
Default: │
Prefix for depth level when a further neighbour is present.
Type: string
Default: └─
Prefix for key when no further neighbour is present.
Type: string
Default: ├─
Prefix for key when a further neighbour is present.
Type: string
Default: :
Used to separate node key from node value.
Type: function
Default: (node) => (['boolean', 'string', 'number'].includes(typeof node) ? node : undefined)
Can be used to overwrite the node rendering logic. Node is rendered if result is not equal undefined
.
Type: function
Default: null
Function that defines the key sort order. Defaults to ordering of Object.keys(...)
, which is typically insertion order.
Type: string
or null
Default: (circular ref.)
When string
, circular references are broken with that string, at a minor performance cost.
More examples can be found in the tests.
FAQs
Stringify Object as tree structure
The npm package object-treeify receives a total of 1,093,769 weekly downloads. As such, object-treeify popularity was classified as popular.
We found that object-treeify demonstrated a not healthy version release cadence and project activity because the last version was released 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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.