1.0.2 • Published 7 years ago

min-repr v1.0.2

Weekly downloads
1
License
MIT
Repository
github
Last release
7 years ago

min-repr.js

Minimal representation

Compute the start index of the minimal representation(also called Lexicographically minimal string rotation) of a string. This is an implementation of the Booth's O(n) algorithm(Lexicographically least circular substrings)

Usage

The minRepr function supports four kinds of parameters:

  • Plain string
  • Plain string with comparer
  • Array (includes comparable elements)
  • Array with comparer

The input parameter comparer is a comparison function, where comparer(a, b) === 0 means equal relationship, any number <0 means less than, and >0 means larger than.

Example

var a = 'acabc',
    b = [5, 0, 9, 7, 0];

minRepr(a);                     // 2
minRepr(a, (x, y) => {
    if (x > y) {
        return -1;
    } else if (x < y) {
        return 1;
    } else {
        return 0;
    }
});                             // 4
minRepr(b);                     // 4
minRepr(b, (x, y) => y - x);    // 2