strind
Version:
Partition strings based on character indices
76 lines (57 loc) • 1.4 kB
Markdown
[![NPM][npm]][npm-url]
[![Build][build]][build-badge]
[![Coverage][codecov-shield]][codecov]
> Partition strings based on character indices.
```bash
yarn add strind
```
```js
import strind from 'strind';
const result = strind('abcd', [[1, 1], [2, 6]]);
console.log(result);
/**
* {
matched: ['b', 'cd'],
unmatched: [
{
chars: 'a',
index: 0
}
]
}
*
*/
```
An optional callback function can be passed as the third argument.
The function is called with the substring `chars` and boolean `matches` if the substring matches the array indices.
```js
import strind from 'strind';
const result = strind('abcd', [[1, 1], [2, 6]], ({ chars, matches }) => {
return {
text: chars,
isHighlighted: matches
};
});
console.log(result);
/**
* [
{ isHighlighted: false, text: 'a' },
{ isHighlighted: true, text: 'b' },
{ isHighlighted: true, text: 'cd' }
]
*
*/
```
[](LICENSE)
[]: https://img.shields.io/npm/v/strind.svg?color=blue
[]: https://npmjs.com/package/strind
[]: https://travis-ci.com/metonym/strind.svg?branch=master
[]: https://travis-ci.com/metonym/strind
[]: https://codecov.io/gh/metonym/strind
[]: https://img.shields.io/codecov/c/github/metonym/strind.svg