Socket
Socket
Sign inDemoInstall

preserve-case

Package Overview
Dependencies
1
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    preserve-case

case-preserving string.replace


Version published
Weekly downloads
39
increased by56%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

preserve-case

Build Status Coverage Status semantic-release Commitizen friendly

case-preserving string.replace

Usage

npm install --save preserve-case
import replace from 'preserve-case'

console.log(replace.all('foo bar FOO_BAR foo-bar fooBar foo-Bar', 'foo bar', 'baz qux'))
// baz qux BAZ_QUX baz-qux bazQux baz-Qux

API

replace(str, query, replacement, [options])

replace(
  str: string,
  query: string | RegExp,
  replacement: string | Function,
  options?: {
    caseTypes?: Array<string>
  }
): string`

Works exactly like str.replace(query, replacement), except that:

  • it preserves the case of what it replaces (using the marvelous case package)
  • if query is a string, it matches query in any case. (Under the hood, it creates and uses a RegExp with all caseTypes of query joined together, e.g. str.replace(/foo bar|FOO BAR|fooBar|FooBar|.../, ...))

options.caseTypes defaults to all types built into the case package. This may be more than you want, so look into it. For instance, the other cases of 'foo bar' include, but may not be limited to:

'Foo Bar'
'Foo bar'
'FOO BAR'
'fooBar'
'FooBar'
'foo-bar'
'Foo-Bar'
'foo_bar'
'FOO_BAR'

replace.all(str, query, replacement, [options])

Unlike replace, this replaces all occurrences of query, not just the first one, even if query is a string or a RegExp without the g (global) flag.

Acknowledgements

Thanks to Nathan Bubna for his case package, which powers preserve-case!

replace.createSearchRegExp(query, [options])

Creates a RegExp that matches for any case of the given query string. Like the other functions, options.caseTypes defaults to all types built into the case package.

You may also pass a flags option for the RegExp flags (e.g. flags: 'g')

Keywords

FAQs

Last updated on 13 Aug 2018

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc