npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@dormammuuuuu/json-helper

v3.0.1-a

Published

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

Downloads

237

Readme

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