1.0.3 • Published 5 years ago

string-replace-enhanced v1.0.3

Weekly downloads
4
License
MIT
Repository
github
Last release
5 years ago

string-replace-enhanced allow you to go futher with the command replace for string in javascript. You have two options, use the object and implement his settings or use the functions in a standalone way.

Installation

Using npm:

npm install -S string-replace-enhanced

... and then:

import stringReplaceEnhanced from 'string-replace-enhanced'
//  or
import {replaceBefore, replaceAfter, replaceAround, replace, replaceRegex} from 'string-replace-enhanced'
//  or
const {default: stringReplaceEnhanced} = require('string-replace-enhanced')

In NodeJS:

const {default : stringReplaceEnhanced} = require('string-replace-enhanced')
//  or
import {replaceBefore, replaceAfter, replaceAround, replace, replaceRegex} from 'string-replace-enhanced'

In a browser:

<script src="https://unpkg.com/string-replace-enhanced@latest/lib/sre.min.js"></script>
<script>
  /* stringReplaceEnhanced is available here */
  /* in object style */

  new StringReplaceEnhanced.default(options) //constructor

  /* standalone functions */

  StringReplaceEnhanced.replace(...args) //function
</script>

Usage

There is two possibilities, use the object ou the separated functions.

Object

With the object it will be possible to reuse a preset configuration or use the default one.

import stringReplaceEnhanced from 'string-replace-enhanced'

// If an option is missing, it will be given a default value
const options =  {
  after: ['«'],
  around: [],
  before: ['!', '?', ':', ';', '»'],
  regex: [],
  replacer: '&nbsp;',
  toReplace: ' ',
};

// Create a new stringReplaceEnhanced object
const sre = new stringReplaceEnhanced(options);

// Use its methods simply giving the string to work on
console.log(sre.replaceBefore('Hello world !'));
// Hello world&nbsp;!

Functions

For the standalone functions you will need to specify every arguments

import { replaceBefore } from 'string-replace-enhanced'

console.log(replaceBefore('Hello world !', ' ', '&nbsp;'))
// Hello world&nbsp;!

Options

Check the links below to have the up to date version:

  • All options are documented in here

  • The default object implementation's settings is here

nametypedefaultdescription
afterReadonlyArray or string['«']Array of char in need of potential need of replacement after itself
aroundReadonlyArray or string[]Array of char in need of potential need of replacement around itself
beforeReadonlyArray or string['!', '?', ':', ';', '»']Array of char in need of potential need of replacement before itself
regexReadonlyArray or RegExp[]Array of regex, replace match by the replacer
replacerstring'&nbsp;'Replacer of matching toReplace
toReplacestring' 'String to replace, in the function replace toReplace can be an array of string

Properties and methods

To see more about the methods and properties you can access to the documentation here

Documentation

To see more about the functions or the configuration you can access it in the latest documentation here or one in the forked project:

npm i && npm run build:doc

The documentation will be available in the docs file.