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

json-boundary

v2.0.0

Published

A simple NPM package to get boundary indexes from JSON string or normal string easily and faster.

Downloads

140

Readme

A simple NPM package to get boundary indexes from JSON string or normal string easily and faster.

What is Boundary

Here boundary indecates spacial area which need to track. Generally in JSON string there may have Objects, Arrays or internal strings. If we need to track their start or end indexes, we can use JB [Json Boundary ]

When we need this

  • When we work with json string we may need to apply regex on it. but we don't want to capture anything from a boundary area who has a special starting & ending char. Then we can get their indexes , can do more complex operation and can apply regex with own logic easily.
  • We can find how many strings, objects , arrays or boundaries in our string and can find their size form the start & end indexes.
  • When we need to simplify our complex json string .

Installation

npm install json-boundary

Implementation

import { JsonBoundary } from "json-boundary";
const JB = new JsonBoundary();

const string = `(string= !"(a='4' && b=6)") && id=50 && tag = [["abc\'d","(xyz)"],{'abc':true},1,2]`;
console.log(JB.getBoundaries(string)); 

// output: [ { start: 10, end: 25 }, { start: 46, end: 81 } ]

/************************************************************** get simple version of our string */
const { str: simpleVersion, id } = JB.getSimple(string, "###");
console.log(simpleVersion);

// output: (string= !###) && id=50 && tag = ###

/************************************************************** now easily apply regex or any other operation to our simple string */
const modified = simpleVersion.replace(
  /(string)(=)(.+###)/g,
  (m, g1, g2, g3) => {
    return `"${g1}":{"equals":${g3.trim()}}`; // replace match with "string":{"equals":!###}"
  }
);
console.log(JB.replaceOriginal(modified, id)); 

// output: ("string":{"equals":!"(a='4' && b=6)") && id=50 && tag = [["abc'd","(xyz)"],{'abc':true},1,2]}

Explanation

Here default setup is: 
setup={
  mileStones: "!:=@<->^?*$#",
  ignoreCharSet: ";&|",
  boundaries: {
    "[": "]",
    "{": "}",
    '"': '"',
    "'": "'",
  },
}
Where ,
  mileStones    ==> Mile stones tell to start finding boundaries.
  ignoreCharSet ==> Ignore Char inspire to find new mileStone again to start the finding operation.
  boundaries    ==> Boundaries tell to find boundaries between these characters.
  
NB: We can chagange this setup by passing an object during instant creation.

Change the default setup

Suppose our new milestons= "=<>!" our string is: (string= !"(a='4' && b=6)") && (tag : [["abc\'d","(xyz)"],{'abc':true},1,2]) here we just replace "tag =" to "tag :" Now let see the result

import { JsonBoundary } from "json-boundary";
const JB = new JsonBoundary({
  mileStones: "=<>!", // you can change other options also
});

const str = `(string= !"(a='4' && b=6)") && (tag : [["abc\'d","(xyz)"],{'abc':true},1,2])`;
const boundaries = JB.getBoundaries(str);

console.log(boundaries); 


//  [ { start: 10, end: 25 } ] 
//  maShaAllah it capture only string boundary

Features

  • getBoundaries ⇒ ok // @param originalString, callback(startIndex, currentIndex, flag) ⇒ this call ones for every charerter's index of the string if provided ; @return all boundary indexes Here, startIndex = last boundary start point, end= current char index of the original string. flag | -1= outside the boundary | 1 = boundary area is running | 0 = boundary area is end. Here, startIndex = last boundary start point, end= current char index of the original string. flag | -1= outside the boundary | 1 = boundary area is running | 0 = boundary area is end.
  • getSimple ⇒ ok // @param originalString, identifier ; @return a simplify version of original string and a tracking id to perform operation/s
  • replaceOriginal ⇒ ok // @param modifiedString, trackingId; @return replace the original boundary content to our modified string then return it.
  • getBoundaryMap ⇒ may be in Progress if needed // @param originalString; @return all boundaries with nasted operation
  • others may be included if needed InshaAllah.

Contributing

Contributions are warmly welcomed! Feel free to submit a Pull Request or open an Issue with any suggestions or improvements.