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

strfnr

v0.1.0

Published

Painless string find and replace.

Downloads

22

Readme

strfnr

Painless string find and replace. Find search patterns in a string and:

  • replace it
  • remove it
  • add text before it
  • add text after it
  • find its character offset within the string

When should you use this?

JavaScript developers have a tendency to reinvent the wheel. strfnr should only used when you want to find and replace:

  • using multiple criteria
  • one pattern, or fall back to another pattern if the first finds nothing

You should NOT use strfnr if:

  • you want to replace only one string (use String.prototype.replace() instead)
  • you want to replace different search patterns with different replacement patterns

Installation

This package works with bost CommonJS and ESM projects. No need to worry about pesky module errors! But watch out: this package has no default exports.

npm install strfnr

TypeScript types are bundled with the package.

Usage

To match the first occurrence of a search pattern, use stringMatch.

import { stringMatch } from 'strfnr';
// Or for CommonJS,
// const { stringMatch } = require('strfnr');

const str = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?';
const match = stringMatch(str, 'wood');

To match all occurrences of a search pattern, use stringMatches.

import { stringMatches } from 'strfnr';
// Or for CommonJS,
// const { stringMatches } = require('strfnr');

const str = 'How much wood would a woodchuck chuck if a woodchuck could chuck wood?';
const matches = stringMatches(str, 'wood');

You can then:

  • remove(): this – Remove that occurrence of the string
  • replace( text: string ): this – Replace that occurrence of the string with a new string
  • before( text: string ): this – Prepend the matched string(s) with a new string
  • after(): this – Append the matched string(s) with a new string
  • final(): string – Get the final string. Equal to toString().

All functions except final() (and toString()) return the match object, so you can chain complex actions, such as appending and prepending text, in one statement.

Example

Picking up from the above:

console.log(matches.replace("cat").final());
// How much cat would a catchuck chuck if a catchuck could chuck cat?

// Direct-to-string conversion also works.
console.log(`${matches.after("dog")}`);
// How much catdog would a catdogchuck chuck if a catdogchuck could chuck catdog?

console.log(`${matches.before("mouse")}`);
// How much mousecatdog would a mousecatdogchuck chuck if a mousecatdogchuck could chuck mousecatdog?

console.log(`${matches.remove()}`);
// How much mousedog would a mousedogchuck chuck if a mousedogchuck could chuck mousedog?
// Note how anything added by `before` or `after` is not removed.

Offsets

You can get the offsets using match.offset. For single matches, this will return a single offset. For multiple matches, this will return an array of offsets. Each offset has the following properties:

  • start: number – The character offset of the start of the match
  • end: number – The character offset of the end of the match
  • filter: string | RegExp | RegExpLike – The search pattern that matched that offset
import { stringMatches } from 'strfnr';

const str = "The sixth sick sheik's sixth sick sheep";
const matches = stringMatches(str, 'sixth');

console.log(matches.offset);
// [
//    { start: 4, end: 9, filter: 'sixth' },
//    { start: 23, end: 28, filter: 'sixth' }
// ]

Any modification done with the functions defined above will accordingly update the offsets.

RegExpLike

RegExpLike refers to any object with a source and, if applicable, flags property. This is useful when you want to use a filter that comes from a JSON object or other serialized format.

Edge cases

Invalid filters get skipped. Zero-matches can occur if this is the case. Zero matches have an offset of null (for single matches) or [] (for multiple matches). Attempting to mutate it with any function does nothing. The string will remain unmodified.

Passing anything that isn't a string as the first variable will return null. Unlike the case where the filters are invalid, this will cause access attempts (i.e. match.replace()) to throw errors.

Development

Just clone, install packages, and test away.

git clone https://github.com/ChlodAlejandro/strfnr.git
npm i

GitHub Actions will test every pull request and master branch commit. Any coverage less than 100% will cause a status check error.

License

3-Clause BSD License.