Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/artentica/string-replace-enhanced

A package to go further with string replace
https://github.com/artentica/string-replace-enhanced

javascript npm-package regex standalone string string-manipulation utility

Last synced: 4 days ago
JSON representation

A package to go further with string replace

Awesome Lists containing this project

README

        

📝✏️string-replace-enhanced


A package to go further with string replace




MIT licenses


snyk


NPM version


NPM dependencies


Travis


codecov


Codacy


bundlephobia


semantic-release


PRs Welcome

**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:

```javascript
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:

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

In a browser:

```html

/* stringReplaceEnhanced is available here */
/* in object style */

new StringReplaceEnhanced.default(options) //constructor

/* standalone functions */

StringReplaceEnhanced.replace(...args) //function

```

## 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.

```javascript
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: ' ',
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 !
```

### Functions

For the standalone functions you will need to specify every arguments

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

console.log(replaceBefore('Hello world !', ' ', ' '))
// Hello world !
```

## Options

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

- All options are documented in [here](https://artentica.github.io/string-replace-enhanced/interfaces/_interfaces_.isettings.html)

- The default object implementation's settings is [here](https://artentica.github.io/string-replace-enhanced/modules/_defaultsetttings_.html)

| name | type | default | description |
| --------- | ------------------------------- | --------------------------- | ------------------------------------------------------------------------------------ |
| after | ReadonlyArray or string | `['«']` | Array of char in need of potential need of replacement after itself |
| around | ReadonlyArray or string | `[]` | Array of char in need of potential need of replacement around itself |
| before | ReadonlyArray or string | `['!', '?', ':', ';', '»']` | Array of char in need of potential need of replacement before itself |
| regex | ReadonlyArray or RegExp | `[]` | Array of regex, replace match by the replacer |
| replacer | string | `' '` | Replacer of matching `toReplace` |
| toReplace | string | `' '` | 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](https://artentica.github.io/string-replace-enhanced/modules/_core_.html)

## Documentation

To see more about the functions or the configuration you can access it in the latest documentation [here](https://artentica.github.io/string-replace-enhanced/) or one in the forked project:

npm i && npm run build:doc

The documentation will be available in the `docs` file.