3.0.1-a • Published 8 months ago

@dormammuuuuu/json-helper v3.0.1-a

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

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
acornacorn-walkansi-escapesansi-regexansi-stylesanymatchargargparseasyncbabel-jestbabel-plugin-istanbulbabel-plugin-jest-hoistbabel-preset-current-node-syntaxbabel-preset-jestbalanced-matchbrace-expansionbracesbrowserslistbs-loggerbserbuffer-fromcallsitescamelcasecaniuse-litechalkchar-regexci-infocjs-module-lexercliuicocollect-v8-coveragecolor-convertcolor-nameconcat-mapconvert-source-mapcreate-jestcreate-requirecross-spawndebugdedentdeepmergedetect-newlinediffdiff-sequencesejselectron-to-chromiumemitteryemoji-regexerror-exesbuildescaladeescape-string-regexpesprimaexecaexitexpectfast-json-stable-stringifyfb-watchmanfilelistfill-rangefind-upfs.realpathfunction-bindgensyncget-caller-fileget-package-typeget-streamglobglobalsgraceful-fshas-flaghasownhtml-escaperhuman-signalsimport-localimurmurhashinflightinheritsis-arrayishis-core-moduleis-fullwidth-code-pointis-generator-fnis-numberis-streamisexeistanbul-lib-coverageistanbul-lib-instrumentistanbul-lib-reportistanbul-lib-source-mapsistanbul-reportsjakejest-changed-filesjest-circusjest-clijest-configjest-diffjest-docblockjest-eachjest-environment-nodejest-get-typejest-haste-mapjest-leak-detectorjest-matcher-utilsjest-message-utiljest-mockjest-pnp-resolverjest-regex-utiljest-resolvejest-resolve-dependenciesjest-runnerjest-runtimejest-snapshotjest-utiljest-validatejest-watcherjest-workerjs-tokensjs-yamljsescjson-parse-even-better-errorsjson5kleurlevenlines-and-columnslocate-pathlodash.memoizelru-cachemake-dirmake-errormakeerrormerge-streammicromatchmimic-fnminimatchmsnanoidnatural-comparenode-int64node-releasesnormalize-pathnpm-run-pathonceonetimep-limitp-locatep-tryparse-jsonpath-existspath-is-absolutepath-keypath-parsepicocolorspicomatchpiratespkg-dirpostcsspretty-formatpromptspure-randreact-isrequire-directoryresolveresolve-cwdresolve-fromresolve.exportsrollupsemvershebang-commandshebang-regexsignal-exitsisteransislashsource-mapsource-map-jssource-map-supportsprintf-jsstack-utilsstring-lengthstring-widthstrip-ansistrip-bomstrip-final-newlinestrip-json-commentssupports-colorsupports-preserve-symlinks-flagtest-excludetmplto-regex-rangetype-detecttype-festundici-typesupdate-browserslist-dbv8-compile-cache-libv8-to-istanbulwalkerwhichwrap-ansiwrappywrite-file-atomicy18nyallistyargsyargs-parserynyocto-queue
3.0.1-a

8 months ago

3.0.1

8 months ago

3.0.0

8 months ago