Are you happy with your logging solution? Would you help us out by taking a 30-second survey? Click here

esrever

A Unicode-aware string reverser written in JavaScript.

Subscribe to updates I use esrever


Statistics on esrever

Number of watchers on Github 545
Number of open issues 7
Average time to close an issue 3 months
Main language JavaScript
Open pull requests 1+
Closed pull requests 1+
Last commit about 2 years ago
Repo Created almost 7 years ago
Repo Last Updated about 2 years ago
Size 47 KB
Homepage https://git.io/es...
Organization / Authormathiasbynens
Contributors2
Page Updated
Do you use esrever? Leave a review!
View open issues (7)
View esrever activity
View on github
Fresh, new opensource launches 🚀🚀🚀
Trendy new open source projects in your inbox! View examples

Subscribe to our mailing list

Evaluating esrever for your project? Score Explanation
Commits Score (?)
Issues & PR Score (?)

Esrever Build status Code coverage status Dependency status

Esrever is a Unicode-aware string reverser written in JavaScript. It allows you to easily reverse any string of Unicode symbols, while handling combining marks and astral symbols just fine. Heres an online demo.

Why not just use string.split('').reverse().join('')?

The following code snippet is commonly used to reverse a string in JavaScript:

// Dont use this!
var naiveReverse = function(string) {
  return string.split('').reverse().join('');
};

However, there are some problems with this solution. For example:

naiveReverse('foo  bar');
//  'rab  oof'
// Where did the `` symbol go? Whoops!

If youre wondering why this happens, read up on JavaScripts internal character encoding.

But theres more:

naiveReverse('maana manana');
//  'ananam anaam'
// Wait, so now the tilde is applied to the `a` instead of the `n`? WAT.

In order to correctly reverse any given string, Esrever implements an algorithm that was originally developed by Missy Misdemeanor Elliot in September 2002:

I put my thang down, flip it, and reverse it. I put my thang down, flip it, and reverse it.

And indeed: by swapping the position of any combining marks with the symbol they belong to, as well as reversing any surrogate pairs before further processing the string, the above issues are avoided successfully. Thanks, Missy!

Installation

Via npm:

npm install esrever

Via Bower:

bower install esrever

Via Component:

component install mathiasbynens/esrever

In a browser:

<script src="esrever.js"></script>

In Narwhal, Node.js, and RingoJS:

var esrever = require('esrever');

In Rhino:

load('esrever.js');

Using an AMD loader like RequireJS:

require(
  {
    'paths': {
      'esrever': 'path/to/esrever'
    }
  },
  ['esrever'],
  function(esrever) {
    console.log(esrever);
  }
);

API

esrever.version

A string representing the semantic version number.

esrever.reverse(string)

This function takes a string and returns the reversed version of that string, correctly accounting for Unicode combining marks and astral symbols.

Usage example

var input = 'Lorem ipsum  dolor sit amet.';
var reversed = esrever.reverse(input);

console.log(reversed);
//  '.tema tis rolod  muspi meroL'

esrever.reverse(reversed) == input;
//  true

Using the esrever binary

To use the esrever binary in your shell, simply install Esrever globally using npm:

npm install -g esrever

After that you will be able to reverse strings from the command line:

$ esrever 'I put my thang down, flip it, and reverse it.'
.ti esrever dna ,ti pilf ,nwod gnaht ym tup I

$ esrever 'HE COMH'
HMOC EH

$ cat foo.txt
These are the contents of `foo.txt`.
This is line two.

$ esrever -f foo.txt
.owt enil si sihT
.`txt.oof` fo stnetnoc eht era esehT

$ esrever -l foo.txt
.`txt.oof` fo stnetnoc eht era esehT
.owt enil si sihT

Why not just use the good old rev command instead? Glad you asked. rev doesnt account for Unicode combining marks:

$ rev <<< 'maana manana'
ananam anaam

On the other hand, the esrever binary returns the expected result:

$ esrever 'maana manana'
ananam anaam

See esrever --help for the full list of options.

Support

Esrever has been tested in at least Chrome 27-29, Firefox 3-22, Safari 4-6, Opera 10-12, IE 6-10, Node.js v0.10.0, io.js v1.0.0, Narwhal 0.3.2, RingoJS 0.8-0.11, PhantomJS 1.9.0, and Rhino 1.7RC4.

Unit tests & code coverage

After cloning this repository, run npm install to install the dependencies needed for Esrever development and testing. You may want to install Istanbul globally using npm install istanbul -g.

Once thats done, you can run the unit tests in Node using npm test or node tests/tests.js. To run the tests in Rhino, Ringo, Narwhal, and web browsers as well, use grunt test.

To generate the code coverage report, use grunt cover.

Author

twitter/mathias
Mathias Bynens

License

Esrever is available under the MIT license.

esrever open issues Ask a question     (View All Issues)
  • over 4 years Support Unicode 9 prepended concatenation marks
  • over 4 years Myanmar vowel signs
  • almost 6 years Support for Hebrew diacritics and other grapheme extenders
  • almost 7 years Should Esrever support ligatures?
  • almost 7 years binary: Support `-l` for non-files too
esrever open pull requests (View All Pulls)
  • Build up result in array instead of strings
esrever list of languages used
Other projects in JavaScript