Improved substring method to avoid splitting mid word
npm install 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!
*
npm:
``bash`
npm install --save better-substring
Yarn:
`bash`
yarn add better-substring
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
$3
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)?. MIT © PandaSekh