https://github.com/pandasekh/better-substring
✂ Improved substring method to avoid splitting mid word
https://github.com/pandasekh/better-substring
split string-manipulation substring trim
Last synced: 12 months ago
JSON representation
✂ Improved substring method to avoid splitting mid word
- Host: GitHub
- URL: https://github.com/pandasekh/better-substring
- Owner: PandaSekh
- Created: 2021-06-29T10:01:51.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-06-29T14:59:41.000Z (over 4 years ago)
- Last Synced: 2025-01-15T00:32:33.405Z (about 1 year ago)
- Topics: split, string-manipulation, substring, trim
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/better-substring
- Size: 106 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Better Substring
✂ Improved substring method to avoid splitting mid word
better-substring is a *lightweight* (**~180 bytes**) tool that enhances the default substring method. No more words split in half!
* * *
## Install
npm:
```bash
npm install --save better-substring
```
Yarn:
```bash
yarn add better-substring
```
## Examples
Basic example, will try to split at index 3.
```js
import betterSubstring from "better-substring";
const sentence = "Hello World :D";
const subs = betterSubstring(sentence, 0, false, 3, true);
console.log(subs); // "Hello"
```
Instead of going forward until the word is finished, with `false` we go back.
```js
import betterSubstring from "better-substring";
const sentence = "Hello World :D";
const subs = betterSubstring(sentence, 0, false, 8, false);
console.log(subs); // "Hello"
```
We can also define a starting point
```js
import betterSubstring from "better-substring";
const sentence = "Hello World :D";
const subs = betterSubstring(sentence, 6, true, 8, true);
console.log(subs); // "World"
```
## API
### `substring(sentence: string, init: number, initForward = false, end?: number, endForward = true) => string`
Returns a substring without splitting words.
- `sentence: string` the sentence/string to work with.
- `init: string` index where to start the substring. 0 to start from the beginning.
- `initForward = false` (optional) in case the split will occur mid-word, shall we go forward (true) or back (false)?.
- `end: number` (optional) index where you want the split to occur.
- `endForward = true` (optional) in case the split will occur mid-word, shall we go forward (true) or back (false)?.
## License
MIT © [PandaSekh](https://github.com/PandaSekh)