Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/metonym/strind
Partition strings based on character indices
https://github.com/metonym/strind
partition string string-manipulation
Last synced: about 2 months ago
JSON representation
Partition strings based on character indices
- Host: GitHub
- URL: https://github.com/metonym/strind
- Owner: metonym
- License: mit
- Created: 2019-07-10T02:53:15.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2022-03-24T07:15:29.000Z (almost 3 years ago)
- Last Synced: 2024-11-14T15:45:18.216Z (2 months ago)
- Topics: partition, string, string-manipulation
- Language: TypeScript
- Homepage:
- Size: 329 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# strind
[![NPM][npm]][npm-url]
[![Build][build]][build-badge]> Partition strings based on character indices.
## Install
```bash
yarn add strind
```## Usage
Required arguments are the string and an array of tuples that denote the start and end parse indices.
```js
import strind from "strind";const result = strind("abcd", [
[1, 1],
[2, 6],
]);console.log(result);
/**
* {
matched: ['b', 'cd'],
unmatched: [
{
chars: 'a',
index: 0
}
]
}
*
*/
```### Callback
The module accepts an optional callback 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";// signature
// strind(string, Array, [function])const result = strind(
"abcd",
[
[1, 1],
[2, 6],
],
({ chars, matches }) => ({
isHighlighted: matches,
text: chars,
})
);console.log(result);
/**
* [
{ isHighlighted: false, text: 'a' },
{ isHighlighted: true, text: 'b' },
{ isHighlighted: true, text: 'cd' }
]
*
*/
```## [Changelog](CHANGELOG.md)
## License
[MIT](LICENSE)
[npm]: https://img.shields.io/npm/v/strind.svg?color=blue
[npm-url]: https://npmjs.com/package/strind
[build]: https://travis-ci.com/metonym/strind.svg?branch=master
[build-badge]: https://travis-ci.com/metonym/strind