better-substring

Improved substring method to avoid splitting mid word

Usage no npm install needed!

<script type="module">
  import betterSubstring from 'https://cdn.skypack.dev/better-substring';
</script>

README

Better Substring

Build Status Size Dependencies

✂ 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:

npm install --save better-substring

Yarn:

yarn add better-substring

Examples

Basic example, will try to split at index 3.

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.

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

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