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 -S string-replace-slices-array
const replaceSlicesArr = require('string-replace-slices-array')
Idea
You compile an array of string slices, feed it to this library, and it deletes/replaces them for you.
First, make sure you found exact boundaries of the slice - preview it using String.slice
:
console.log(someString.slice(sliceFrom, sliceTo)) // <--- you want exactly this range to be deleted
Now that you have "from" index, sliceFrom
and "to" index sliceTo
, put them into an array and push them into another array. You can push many such "from"-"to" arrays into it.
For replacement, set the new value as a third element in the ranges array: [sliceFrom, sliceTo, replaceWith]
That's it. Feed that array of ranges into this package, together with your source string and your deletion/replacement will be done for you.
API
stringReplaceSlicesArray(inputString, rangesArray[, opts])
Returns a string.
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',
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
All contributions are welcome. Please stick to Standard JavaScript notation and supplement the test.js
with new unit tests covering your feature(s).
If you see anything incorrect whatsoever, do raise an issue. If you file a pull request, I'll do my best to help you to get it merged as soon as possible. If you have any comments on the code, including ideas how to improve something, don't hesitate to contact me by email.
Licence
MIT License (MIT)
Copyright (c) 2017 Codsen Ltd, Roy Revelt
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.