Skip to Content
  • Website
Codsen
  • Home
  • Open Source
  • Articles
  • About

prevOpen Source→string-overlap-one-on-anothernext

string-overlap-one-on-another

Lay one string on top of another, with an optional offset

Downloads per monthMIT Licenselibera manifesto
  • the top
  • IDEA
  • API — OVERLAP()
  • API — DEFAULTS
  • API — VERSION
  • EDGE CASES

Idea

In essence,

//           aaa
//      +  bbb      (negative offset of 2 means it's pushed to the left by 2 places)
//         -----
//      =  bbbaa

API — overlap()

The main function overlap() is imported like this:

It’s a function which takes three input arguments:

Input argumentTypeObligatoryDescription
str1
Type: String
Obligatory: yes
str1StringyesThe string which will be put “under” str2
str2
Type: String
Obligatory: yes
str2StringyesThe string which will be put “over” str1
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object.

The Optional Options Object has the following shape:

KeyTypeDefaultDescription
offset
Type: Positive or negative integer or zero
Default: 0
offsetPositive or negative integer or zero0It instructs to offset the top string by this many characters to the right (if a positive number) or to the left (if a negative number). The default value is zero.
offsetFillerCharacter
Type: String
Default: " " (a single space)
offsetFillerCharacterString" " (a single space)If the offset value (character amount to push left) pushes the str2 outside the boundaries of str1 and not even there’s no overlap, but there is a gap, this gap is formed out of these characters. The default is a single space.

Here are all defaults in one place for copying:

The function will return a string.

API — defaults

You can import defaults:

It's a plain object:

The main function calculates the options to be used by merging the options you passed with these defaults.

API — version

You can import version:

Edge cases

The algorithm is the following:

  1. If one and only one of two input strings is zero-long, the other string is returned as a result.
  2. If both input strings are empty, an empty string is returned.
  3. If both input strings are non-empty, the result is second string overlaid on the first, considering the offset.
↑ back to top
prev next

Copyright

All rights reserved © Roy Revelt 2026
All our open source packages are under MIT licenceopens in a new tab

Activities

🐛 See a bug? Raise an issueopens in a new tab
💘 Check out the Indiewebopens in a new tab and Libera manifestoopens in a new tab