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

 
  • the top
  • RANGE LIBRARI…
  • RANGES-APPLY
  • RANGES-PUSH
  • RANGES-MERGE
  • RANGES-SORT
  • RANGES-CROP
  • RANGES-REGEX
  • RANGES-ENT-D…
  • RANGES-INVERT
  • RANGES-OFFSE…
  • RANGES-IS-IND…
  • RANGES-ITERAT…
  • RANGES-PROCE…
  • ALTERNATIVES…

So-called Ranges

We invented the term, but it’s just a fancy way to describe arrays of “from” — “to” string index ranges:

[
  [3, 7],
  [10, 15, "replacement"],
  [4, 7],
];

[3, 7] means delete characters from index 3 to 7.

[10, 15, "replacement"] means replace characters from index 10 to 15 with "replacement".

A case of no ranges is marked by a falsy null (but if you pass a truthy empty array, it will work too).

That’s all there is.

Same indexes as in String.prototype.slice().

Ranges-based packages allow us to perform composable operations on a string: you gather what needs to be done, then, in the end, “render” the result. Ranges are also portable between packages and each amendment can be tweaked or discarded later.

As you know, strings in JavaScript are immutableopens in a new tab, which means, each time you amend a string, a new value is written to the memory and the old value is left for garbage collector to clean up. Now, if the string is larger, if there are many .replace() chained, it gets out of control very quick (both in terms of performance and in DX). That’s where Ranges step in.

Range Libraries

  • ranges-apply
  • ranges-push
  • ranges-merge
  • ranges-sort
  • ranges-crop
  • ranges-regex
  • ranges-ent-decode
  • ranges-invert
  • ranges-offset
  • ranges-is-index-within
  • ranges-iterate
  • ranges-process-outside

ranges-apply

It performs all the amendments described by the ranges (array or null) on a given string, returning a new string.

See the package

↑ back to top

ranges-push

When we want to gather ranges, instead of pushing them into an array, we can push them into this helper Class (for example, gatheredRanges below). That gives us automatic merging and sorting.

ranges-push also checks the types so it acts like a safeguard.

See the package

↑ back to top

ranges-merge

If, after sorting, any two ranges in the vicinity have the same edge value (like 2 below), merge them.

See the package

↑ back to top

ranges-sort

It sorts the ranges.

See the package

↑ back to top

ranges-crop

It crops the ranges, ensuring no range from an array goes beyond a given index.

Along the way, it will also merge and sort ranges.

See the package

↑ back to top

ranges-regex

Takes a string, matches the given regex on it and returns ranges which would do the same thing.

Similarly to String.prototype.match(), a no-results case will yield null (which ranges-merge and others would gladly accept).

See the package

↑ back to top

ranges-ent-decode

This is a wrapper on top of market-leading HTML entity decoder he.jsopens in a new tab decode() which returns ranges instead of string.

We tested the hell out of the code, directly and up-the-dependency-stream but as a cherry on top, all he.js unit tests were ported to our uvu-based test suite and they do pass.

See the package

↑ back to top

ranges-invert

Inverts ranges.

See the package

↑ back to top

ranges-offset

Offsets all indexes in every range.

See the package

↑ back to top

ranges-is-index-within

Tells, is a given natural number index within any of the ranges. It’s a wrapper on top of Array.prototype.find().

See the package

↑ back to top

ranges-iterate

It iterates all characters in a string, as if given ranges were already applied.

Sometimes certain operations on a string aren’t really composable — sometimes we want to traverse the string as if ranges were applied, as if we already had the final result.

See the package

↑ back to top

ranges-process-outside

Processes the string outside the given ranges. Each “gap” in the string between ranges will be fed into callback you supply — same like in Array.prototype.forEach().

This program makes the life easier because if you did it manually, you’d have to invert ranges and loop over each inverted chunk. Finally, you’d have to write unit tests of all that.

See the package

↑ back to top

Alternatives — magic-string

The magic-stringopens in a new tab by Rich Harris, the Rollup creator.

It is an all-in-one program to perform operations on strings in a controllable manner. It’s oriented at operations on code, and its produced sourcemaps are aimed at browsers.

Range libraries are best used for when you want to:

  • transfer string amendment instructions between programs
  • gather string amendment instructions and discard some, conditionally, or tweak them
  • when string processing is complex

magic-string is best used in programs similar to Rollup: you process code and generate sourcemaps for browsers.

In comparison:

magic-string method .overwrite — equivalent to ranges-push and ranges-apply.

Codsen the company

UK Companies House 🏢 assigned Codsen Ltd a number #9204532 back on Friday, September 5th, 2014. That makes Codsen 10 years, 8 months and 11 days old.

Cookies

We don’t use cookies. This website only tracks anonymised website traffic via Fathomopens in a new tab which is GDPR compliant and all its data is housed exclusively in EU. Our analytics are publicopens in a new tab, see our Privacy Policy.

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

Trivia

Built with 💿 Remixopens in a new tab framework. See more in Trivia section.
Page performance: TTFBopens in a new tab loading FCPopens in a new tab loading.

Copyright

All rights reserved © Codsen Ltd 2022
All our open source packages are under MIT licenceopens in a new tab