string-remove-duplicate-heads-tails4.0.2

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

§ Quick Take

import { strict as assert } from "assert";
import removeDuplicateHeadsTails from "string-remove-duplicate-heads-tails";

assert.equal(
  removeDuplicateHeadsTails(
    "{{ Hi {{ first_name }}! }}",
    {
      heads: "{{ ",
      tails: " }}",
    }
  ),
  "Hi {{ first_name }}!"
);

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

:exclamation: {{ 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

removeDuplicateHeadsTails(
  str,
  [opts]
)

In other words, it's a function which takes two input arguments, second-one optional.

§ API - Input

Input argumentTypeObligatory?Description
strStringyesSource string upon which to perform the operation
optsPlain objectnoOptional Options Object, see below for its API

If input string is not given, it will throw.

§ An Optional Options Object

Optional Options Object's keyType of its valueDefaultDescription
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.
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 curlies are default for Nunjucks opens in a new tab/Jinja and many other templating languages. Nunjucks is my favourite.

§ API - Output

Returns a string

§ Changelog

See it in the monorepo opens in a new tab, on Sourcehut.

§ Licence

MIT opens in a new tab

Copyright © 2010–2020 Roy Revelt and other contributors

Related packages:

📦 detergent 6.1.1
Extracts, cleans and encodes text
📦 string-character-is-astral-surrogate 1.11.0
Tells, is given character a part of astral character, specifically, a high and low surrogate
📦 string-remove-thousand-separators 4.0.3
Detects and removes thousand separators (dot/comma/quote/space) from string-type digits
📦 string-apostrophes 1.3.2
Comprehensive, HTML-entities-aware tool to typographically-correct the apostrophes and single/double quotes
📦 string-range-expander 1.12.0
Expands string index ranges within whitespace boundaries until letters are met
📦 string-strip-html 7.0.3
Strips HTML tags from strings. No parser, accepts mixed sources
📦 string-remove-widows 1.7.3
Helps to prevent widow words in a text