Socket
Book a DemoInstallSign in
Socket

@dormammuuuuu/json-helper

Package Overview
Dependencies
Maintainers
0
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dormammuuuuu/json-helper

A library that provides utility functions for traversing and manipulating JSON data.

3.0.1-a
latest
Source
npmnpm
Version published
Weekly downloads
32
-11.11%
Maintainers
0
Weekly downloads
 
Created
Source

JSONHelper

JSONHelper is a utility for efficiently traversing and manipulating nested JSON objects. It helps users search for nodes, retrieve parents, find siblings, and more, in hierarchical or tree-structured data. This is the modernization of dabeng/json-digger.

Features

  • findNodeById: Retrieve a node by its unique ID.
  • findOneNode: Find the first node that satisfies specified conditions.
  • findNodes: Retrieve all nodes that match the provided conditions.
  • findParent: Get the parent node of a given node by its ID.
  • findSiblings: Retrieve the sibling nodes of a given node.
  • findAncestors: Get the ancestors of a node.
  • addChildren: Add child nodes to a given node.
  • addSiblings: Add sibling nodes to a given node.
  • addRoot: Replace the root node.
  • updateNode: Update a single node's information.
  • updateNodes: Update multiple nodes' information.
  • removeNode: Remove a node by its ID.
  • removeNodes: Remove multiple nodes by their IDs or conditions.

Installation

npm install json-helper

Usage

import { JSONHelper } from 'json-helper';

const datasource = {
  pk: '1',
  name: 'Lao Lao',
  title: 'general manager',
  inferiors: [/* Nested children */]
};

const helper = JSONHelper({
  datasource,
  idProp: 'pk',
  childrenProp: 'inferiors'
});

const node = helper.findNodeById('1');  // Find node by ID

Methods

findNodeById(id)

Find a node by its ID.

const node = helper.findNodeById('1');

findOneNode(conditions)

Find the first node that matches a set of conditions.

const node = helper.findOneNode({ name: 'Xiang Xiang' });

findNodes(conditions)

Find all nodes matching the conditions.

const nodes = helper.findNodes({ title: /engineer/i });

findParent(id)

Find the parent of a node by its ID.

const parent = helper.findParent('2');

findSiblings(id)

Find the siblings of a node by its ID.

const siblings = helper.findSiblings('2');

findAncestors(id)

Get the ancestors of a node.

const ancestors = helper.findAncestors('6');

addChildren(id, data)

Add child nodes to a given node.

helper.addChildren('2', { pk: '11', name: 'New Child' });

addSiblings(id, data)

Add sibling nodes to a given node.

helper.addSiblings('2', { pk: '11', name: 'New Sibling' });

addRoot(data)

Replace the root node with new data.

helper.addRoot({ pk: '11', name: 'New Root' });

updateNode(data)

Update the properties of a node.

helper.updateNode({ pk: '1', name: 'Updated Name' });

updateNodes(ids, data)

Update multiple nodes' properties.

helper.updateNodes(['1', '2'], { title: 'New Title' });

removeNode(id)

Remove a node by its ID.

helper.removeNode('2');

removeNodes(param)

Remove multiple nodes based on IDs or conditions.

helper.removeNodes(['2', '3']);

Running Tests

Run tests using the following command:

npm run test

Keywords

json

FAQs

Package last updated on 24 Oct 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.