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

prevOpen Source→string-remove-duplicate-heads-tailsnext

string-remove-duplicate-heads-tails7.2.1

Detect and (recursively) remove head and tail wrappings around the input string

Downloads per monthChangelogMIT Licenselibera manifesto
  • the top
  • Installation
  • Quick Take
  • Examples
  • THE PURPOSE
  • API — REMDUP()
  • API — DEFAULTS
  • API — VERSION
  • API — TYPES
  • Changelog

Installation

Quick Take

Examples

  • Remove a duplicate outer layer with the default markers
  • Remove mixed outer wrappers while preserving inner variables
  • Remove empty marker pairs around meaningful text
  • Leave content intact unless both outer markers match

The Purpose

Let’s say, you know that variables are wrapped with heads, for example, {{ and tails, }}.

For example:

"Hi {{ first_name }}!";

This library detects and deletes redundant heads and tails wrapped around the whole input string:

"{{ Hi {{ first_name }}! }}" => "Hi {{ first_name }}!"

But it’s also smart and detects legit heads and tails at the edges of string: {{ first_name }} {{ last_name }} is not turned into first_name }} {{ last_name.

That’s what this library is mainly about — being able to detect, are outer heads and tails currently wrapping a single chunk of text, or are those heads and tails from separate chunks.

Also, this lib removes the leading/trailing empty clumps of empty heads/tails, with or without empty space:

// without whitespace:
`{{}}{{}} {{}}{{}} {{}}{{}} a {{}}{{}}` => `a`
// with whitespace:
`{{   \n   }}   \t   a   \n\n {{ \n\n \n\n   }}   \t\t` => `a`

You can configure heads and tails to be whatever you like, single string or array of them. Also, the length of the different heads in your given set can be different.

API — remDup()

The main function remDup() is imported like this:

It’s a function which takes three input arguments:

Input argumentTypeObligatoryDescription
str
Type: String
Obligatory: yes
strStringyesSource string upon which to perform the operation
opts
Type: Plain object
Obligatory: no
optsPlain objectnoOptional Options Object, see below for its API

The Optional Options Object has the following shape:

KeyTypeDefaultDescription
heads
Type: Non-empty string or array of one or more non-empty strings
Default: ['{{']
headsNon-empty string or array of one or more non-empty strings['{{']Put here the way you mark the beginnings of your variables in a string.
tails
Type: Non-empty string or array of one or more non-empty strings
Default: ['}}']
tailsNon-empty string or array of one or more non-empty strings['}}']Put here the way you mark the endings of your variables in a string.

These double curly braces are default for Nunjucksopens in a new tab/Jinja and many other templating languages.

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:

API — types

This package is written in TypeScript and exports the following types:

TypeDescription
Opts
Type: Opts
OptsThe resolved Options Object, where heads and tails are always arrays of strings.
LenientOpts
Type: LenientOpts
LenientOptsThe looser shape remDup() actually accepts, where heads and tails can each be a single string.
import type { LenientOpts, Opts } from "string-remove-duplicate-heads-tails";

Changelog

Open Changelog
↑ 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