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-utils

v1.0.7

Published

A small library which exposes some helpful string-replacement functions, including exact types

Downloads

27

Readme

String Replace Utils

A small library which exposes some helpful string-replacement functions, including exact types. Types used here come from rolling-ts-utils.

⚠️ Pay attention to the use of as const throughout this document. Not using as const after string and array initialisation will result in generic types being used, or unexpected behaviour!

Installation

npm install --save string-replace-utils

Basic Usage

import { replace } from "string-replace-utils";

const str = "This is an example {noun}" as const;

const sentence = replace(str, "{noun}", "dog");
//    ^? typeof sentence = "This is an example dog"

API Reference

replace(str, key, value)

  • str: The original string
  • key: The substring to be replaced
  • value: The string to be inserted in place of the first occurance of key
  • If no match is found, the original string is returned
const str = "This is an example {noun}" as const;

const sentence = replace(str, "{noun}", "dog");
//    ^? typeof sentence = "This is an example dog"

replaceGlobal(str, key, value)

  • str: The original string
  • key: The substring to be replaced
  • value: The string to be inserted in place of ALL occurances of key
  • If no match is found, the original string is returned
const str = "This is {word} {word} {thing}" as const;

const sentence = replaceGlobal(str, "{word}", "dog");
//    ^? typeof sentence = "This is dog dog {thing}"

replaceOrdered(str, values)

  • str: The original string
  • values: An array containing the strings to be inserted in place of, in order, each occurance of {string}, where string is any string
  • If no {string} is found, the original string is returned
const str = "This is {article} {adjective} {noun}." as const;

const sentence = replaceOrdered(str, ["a", "sneaky", "cat"] as const);
//    ^? typeof sentence = "This is a sneaky cat."

replaceMultiple(str, keys, values)

  • str: The original string
  • keys: An array containing the substrings to be replaced
  • values: An array of strings to be inserted in place of the key at the same position in the Keys array
  • If no match is found, the key-value pair has no impact
const str = "This is {article} {adjective} {noun}." as const;

const sentence = replaceMultiple(
  str,
  ["{article}", "{adjective}", "{noun}"] as const,
  ["a", "sneaky", "cat"] as const
);
//    ^? typeof sentence = "This is a sneaky cat."

replaceAll(str, value)

  • str: The original string
  • value: The string to be inserted in place of ALL occurences of {string}
const str = "This is {article} {adjective} {noun}." as const;

const sentence = replaceAll(str, "cat");
//    ^? typeof sentence = "This is a cat cat cat."

Weak Functions

A weak version of replaceMultiple is provided in weak.ts. This function does not support exact types, but allows key-value pairs to be passed as an array of {key, value} objects.

This was more useful in the past when the replaceMultiple function had the same functionality as the current replaceOrdered function (thus this was the only way to replace specified substrings):

const str = "This is {article} {adjective} {noun}." as const;

const sentence = weakReplaceMultiple(str, [
  { key: "{article}", value: "a" },
  { key: "{adjective}", value: "sneaky" },
  { key: "{noun}", value: "cat" },
]);
//    ^? "This is a sneaky cat."
//    typeof sentence = string

Replaceable Class

String Replace Utils also provides a class, Replaceable, containing all of the methods provided by the pure functions, to allow for chaining methods.

As the class extends String, it can be used in the same way as a normal string (though asserting as string, or widening accepted parameter-types to include Replaceable<string> may be necessary to prevent Type errors on functions which take string parameters). Alternatively, use .valueOf() to get the raw string, as in the examples below.

⚠️ Some methods do not share the same name as the pure functions:

  • replace becomes extReplace to avoid conflict with String.prototype.replace()
  • replaceAll becomes extReplaceAll to avoid conflict with String.prototype.replaceAll()
const str = new Replaceable("This is {article} {adjective} {noun}." as const);

const sentence = str
  .extReplace("{article}", "a")
  .extReplace("{adjective}", "sneaky")
  .extReplace("{noun}", "cat")
  .valueOf();
//    ^? typeof sentence = "This is a sneaky cat."
const str = new Replaceable("This is {article} {adjective} {noun}." as const);

const sentence = str
  .replaceMultiple(
    ["{article}", "{adjective}", "{noun}"] as const,
    ["a", "pesky", "goose"] as const
  )
  .valueOf();
//    ^? typeof sentence = "This is a pesky goose."