Package Exports
- string-replace-slices-array
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (string-replace-slices-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
string-replace-slices-array
Delete or replace an array of slices in string
Table of Contents
Install
npm i string-replace-slices-array
// consume as CommonJS require:
const replaceSlicesArr = require('string-replace-slices-array')
// or as ES Module:
import replaceSlicesArr from 'string-replace-slices-array'
Here's what you'll get:
Type | Key in package.json |
Path | Size |
---|---|---|---|
Main export - CommonJS version, transpiled to ES5, contains require and module.exports |
main |
dist/string-replace-slices-array.cjs.js |
3 KB |
ES module build that Webpack/Rollup understands. Untranspiled ES6 code with import /export . |
module |
dist/string-replace-slices-array.esm.js |
3 KB |
UMD build for browsers, transpiled, minified, containing iife 's and has all dependencies baked-in |
browser |
dist/string-replace-slices-array.umd.js |
3 KB |
Idea
Let's say you want to delete bunch of characters from a string and also to replace some. Technically, this means you need to mark the indexes of the characters where you start deletion and where you end. That's two numbers to put into an array. They mark a slice of string. Let's add a third element into that array - what to put instead. If it's blank, nothing will be added (it becomes a deletion operation), if it's a non-empty string, it will be inserted insted of the deleted characters (it becomes a replacement operation).
[
[10, 15], // <-- delete this string slice range
[18, 20, 'replace with this'] // <-- delete from 18th to 20th, then insert string there
]
Now what happens when you have a few slices? You put them into a parent array.
This library consumes such parent array and does the actual job crunching your string according to the list of slices.
Now, let's do it practically.
First, make sure you found the exact boundaries of the slice - preview each using String.slice
:
console.log('>>>' + someString.slice(sliceFrom, sliceTo) + '<<<') // <--- make sure what you see is exactly what you want deleted/replaced or the place where it starts is exactly where you want string inserted
PSST. Check out string-slices-array-push which helps to manage the rangesArray
. It has methods to add and retrieve the slices. Also, it helps in cases where slices overlap and helps to maintain the order of index ranges (it always goes from smallest to largest index, everywhere).
API
stringReplaceSlicesArray(inputString, rangesArray) // options will come in later releases
Returns a string with requested slices deleted/replaced.
inputString
Type: string
- the string we want to work on.
rangesArray
Type: array
- the array of zero or more arrays containing a range and an optional replacement string.
For example,
[
[10, 15], // <-- deletion
[18, 20, 'replace with this'] // <-- replacement
]
Usage
const repl = require('string-replace-slices-array')
let str = 'aaa delete me bbb and me too ccc'
// we preview the slice #1 - we're happy to replace it:
console.log('slice 1: >>>' + str.slice(4, 13) + '<<<')
// slice #2 will be replaced too:
console.log('slice 2: >>>' + str.slice(18, 28) + '<<<\n')
//
str = repl(
str,
[
[4, 13, 'zzz'],
[18, 28, 'yyy']
]
)
console.log('str = ' + str)
// => 'aaa zzz bbb yyy ccc',
To insert a piece of string into a string pass the index where you want the string inserted as both "from" and "to" values:
const repl = require('string-replace-slices-array')
let str = 'aaa ccc'
//
str = repl(
str,
[
[4, 4, 'bbb']
]
)
console.log('str = ' + str)
// 'aaa bbb ccc'
The algorithm
The plan is simple - we array.reduce
your given ranges array, slicing the input string accordingly.
The main thing is unit tests and edge case scenarios. Also, fancy optional features (upcoming) like using character enumeration counting emoji as one character.
In my case
Originally this library was part of email-remove-unused-css, where I traversed HTML as a string and compiled an array of things to delete or replace later, in one go. The performance was important, so it was not a good idea to delete/replace things on the spot because each deletion slowed down the process. Instead, I traversed the string, compiled this to-do array, then did the deletion/replacement on the whole thing, once. This appears to be the fastest way.
I'm going to use this library in all my HTML processing libraries who work on HTML as on string, without parsing it.
Contributing
Hi! 99% of people in the society are passive - consumers. They wait for others to take action, they prefer to blend in. The remaining 1% are proactive citizens who will do something rather than wait. If you are one of that 1%, you're in luck because I am the same and together we can make something happen.
If you want a new feature in this package or you would like to change some of its functionality, raise an issue on this repo. Also, you can email me. Just let it out.
If you tried to use this library but it misbehaves, or you need an advice setting it up, and its readme doesn't make sense, just document it and raise an issue on this repo. Alternatively, you can email me.
If you don't like the code in here and would like to give advice about how something could be done better, please do. Same drill - GitHub issues or email, your choice.
If you would like to add or change some features, just fork it, hack away, and file a pull request. I'll do my best to merge it quickly. Code style is
airbnb-base
, only without semicolons. If you use a good code editor, it will pick up the established ESLint setup.
Licence
MIT License (MIT)
Copyright © 2018 Codsen Ltd, Roy Revelt