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

string-replace-balanced

v1.0.0

Published

Replace simple custom tags

Downloads

1

Readme

workflow Jest coverage

string-replace-balanced is a CommonJS module exporting one class that encapsulates

  • a pair of start / end tokens,
  • a transform function

and contains a method to replace each ${start}${_}${end} fragment in a given string with the corresponding this.transform(_) value.

Installation

npm install string-replace-balanced

Usage

const TagProcessor = require ('string-replace-balanced')

const 
  template = '<*~one*> and <*~two*> makes 3.',
  val = {one: 1, two: 2},

// (1) bag of options

const processor = new TagProcessor ({
  start     : '<*~',
  end       :  '*>',
  transform : name => val [name]
})

// (2.1) subclass, setting properties via constructor

const processor = new (class extends TagProcessor {
  constructor () {
    super ()
    this.start = '<*~'
    this.end   =  '*>'
  }
  transform (_) {return val [_]}
})

// (2.2) subclass, implementing getters

const processor = new (class extends TagProcessor {
  get start  () {return '<*~'}
  get end    () {return '*>'}
  transform (_) {return val [_]}
})

// ...anyway,

console.log (processor.process (template))

// result: '1 and 2 makes 3.'

API

Constructor

The constructor takes a bag of options and copies its start, end and transform properties into 'this' instance. Neither option is mandatory, the argument may me omitted at all.

Properties

|Name | Type | Description |-|-|- |start | String | The opening bracket of tags to replace |end | String | The closing bracket of tags to replace

NOTE: It's the caller' responsibility to set start and end as non empty strings, an infinite loop may occur otherwise.

Methods

transform

This method, not implemented by default (but settable at instantiation via the bag of options) is called for each ${start}${_}${end} fragment with the _ value and must return the replacement string.

process

This method does the main job: takes a string and returns its copy with all ${start}${_}${end} fragments replaced.

Notes

The same functionality is available via the standard String.prototype.replaceAll() method, but only with regular expressions which cause a certain performance overhead, avoidable by using string-replace-balanced instead.

The original purpose of this module is to scan HTML for pictures given as Data URLs ("data:image ... ") and to store them separately overriding the corresponding links.

Limitations

No character escaping is supported: start and end are always considered tag delimiters and cannot be isolated with /*...*/, <![CDATA[...]]> nor whatever alike.

Nested tags are not allowed: each start is expected to be closed with the nearest end.

The balance is not enforced though. For example, multiple end tokens may occur in a row � they will be copied into the result as is without reporting an error.