string-mod

Tools for string modification. Includes pad, fill, repeat, insert

Usage no npm install needed!

<script type="module">
  import stringMod from 'https://cdn.skypack.dev/string-mod';
</script>

README

string-mod package structure

Methods

repeat(obj, count) - repeats given obj as string

Returns new string with obj being repeated count times

Returns empty string if count was less that 0

Converts any obj to type string

Example:

// Repeating strings
str_mod.repeat('X',  4); // Output: XXXXX
str_mod.repeat('*-', 4); // Output: *-*-*-*-

// Repeating other types
str_mod.repeat(5, 4); // Output: 5555
srt_mod.repeat(true, 4); // Output: truetruetruetrue
str_mod.repeat({}), 4); // Output: [object Object][object Object][object Object][object Object]

fill(obj, size) - creates new string with given size using obj(cyclic)

Returns new string with size length, filled by obj(cyclic)

Returns empty string if size is less than 0

Converts any obj to type string

Example:

// Filling with characters
str_mod.fill('X', 5); // Output: XXXXX

// Filling with strings
str_mod.fill('*-', 5); // Output: *-*-*

Pad methods

pad(text, filler, length) - adds filler to match target length

Returns text with added filler to match given length

If length is less than 0 filler will be added to the right of text

Example:

// Left pad
str_mod.pad('xxx', '-', 5); // Output: --xxx
str_mod.pad('xxx', '-', 2); // Output: xxx

// Right pad
str_mod.pad('xxx', '-', -5); // Output: xxx--
str_mod.pad('xxx', '-', -2); // Output: xxx

padLeft(text, filler, length) - adds leading filler to match target length

Returns text with leading filler to match given length

Example:

// Left pad
str_mod.padLeft('xxx', '-',  5); // Output: --xxx
str_mod.padLeft('xxx', '-',  2); // Output: xxx
str_mod.padLeft('xxx', '-', -5); // Output: xxx

padRight(text, filler, length) - adds terminating filler to match target length

Returns text with terminating filler to match given length

Example:

// Right pad
str_mod.padRight('xxx', '-',  5); // Output: xxx--
str_mod.padRight('xxx', '-',  2); // Output: xxx
str_mod.padRight('xxx', '-', -5); // Output: xxx

padCenter(text, filler, length) - surrounds text with filler to match target length

Returns text with added filler around it to match given length

Returns text if length is less than text.length

Example:

// Odd length
str_mod.padCenter('xxx', '-', 7); // Outputs: --xxx--
str_mod.padCenter('xxx', '-', 1); // Outputs: xxx

// Even length
str_mod.padCenter('xxx', '-', 6); // Outputs: -xxx--
str_mod.padCenter('xxx', '-', 4); // Outputs: xxx-

// Length is less than text.length
str_mod.padCenter('xxx', '-', 1); // Ouputs: xxx

Insert methods

insertAt(text, sti, position) - insert sti at given position to text

Returns new string from text with sti inserted before given position

If position is negative inserts after given position, going from right to left

Throws error if position is out of bounds

Example:

// Inserting at positive position
str_mod.insertAt('xxxx', '-', 2); // Outputs: xx-xx
str_mod.insertAt('xxxx', '-', 3); // Outputs: xxx-x

// Inserting at negative position
str_mod.insertAt('xxxx', '-', -1); // Outputs: xxxx-
str_mod.insertAt('xxxx', '-', -3); // Outpus: xx-xx

// Bad insers
str_mod.insertAt('xxxx', '-', 8); // Throws error. 8 is greater than text length
str_mod.insertAt('xxxx', '-', -9); // Throws error. -9 is less than negative text length

insertStep(text, sti, step) - inserts sti after each step symbols into text

Returns new string with sti inserted in text after each step symbols. Does not insert sti at the end

Returns new string with sti inserted in text after each step symbols from right to left if step is negative

Returns text if step is more than text.length

Throws error if step is 0

Example:

// Inserting with positive step
str_mod.insertStep('thetststr:)', '-', 3); // Outputs: the-tst-str-:)
str_mod.insertStep('D:thetststr', '-', 3); // Outputs: D:t-het-sts-tr

// Insert with negative step
str_mod.insertStep('thetststr:)', '-', -3); // Outputs: th-ets-tst-r:)
str_mod.insertStep('D:thetststr', '-', -3); // Outputs: D:-the-tst-str

// Inserting without end
str_mod.insertStep('thekey', '-', 3); // Outputs: the-key

// Bad insert
str_mod.insertStep('thekey', '-', 0); // Throws error. Step can not be 0