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

preserve-case-replace

v1.0.2

Published

Replaces substring in a text while preserving case and grammatical number (plural/singular)

Downloads

281

Readme

Build Status Coverage Status

preserve-case-replace

Allows you to replace compound words or phrases in the input text with different phrases while preserving the case and also the plural/singular form of the original text.

Probably the most useful usage of this library would be generating code based on some predefined templates. Template in this case would be just a normal file with a code that doesnt include any tags or special marks to indicate where the modification should occur. Instead replacement of names/identifiers inside the file would happen automatically. This has a huge benefit in that the code in the template file would still be valid code which would make the templates easy to test and maintain. Replacement is language agnostic, so it should work for any programming language or just general text files.

Typescript declarations are included.

Examples

Let's say you want to refactor user photo to member file All you have to do is to call replace(inputText, 'user photo', 'member file')

All of these replacements (and more) would be performed:

'user photo' => 'member file'
'userPhoto' => 'memberFile'
'UserPhoto' => 'memberFile'
'user-photo' => 'member-file'
'USER_PHOTO' => 'MEMBER_FILE'

Also plural/singular forms would be preserved:

'user photos' => 'member files'
'userPhotos' => 'memberFiles'
'UserPhotos' => 'memberFiles'
'user-photos' => 'member-files'
'USER_PHOTOS' => 'MEMBER_FILES'

It works even for irregular words:

replace(inputText, 'user photo', 'small puppy')
// 'userPhotos' => 'smallPuppies'

replace(inputText, 'user photo', 'big wolf')
// 'user-photos' => 'big-wolves'

Install

npm install preserve-case-replace --save

API

replace(inputText, searchFor, replaceWith)

Replaces all occurrences of searchFor with replaceWith

  • inputText - Text to be processed
  • searchFor - String containing the phrase that should get replaced. You can use any case for this parameter as well as any of plural or singular forms. For example al of these are valid and identical in terms of how replacement works: userPhoto, user photos, USER_PHOTO
  • replaceWith - Phrase that should replace all occurrences of searchFor phrase.

Returns text with all variants of searchFor (all cases and plural/singular forms) replaced with replaceWith, while preserving the original case and plural/singular form.

import replace from 'preserve-case-replace'

const inputText = `
# User Photo
The file user-photo.ts Exports the UserPhoto class along with the instance userPhoto.
All data is stored in user_photos db table.`

const output = replace(inputText, 'userPhoto', 'dogOwner')

// output will contain:
//
// # Dog Owner
// The file dog-owner.ts Exports the DogOwner class along with the instance dogOwner.
// All data is stored in dog_owners db table.