Skip to content

Instantly share code, notes, and snippets.

@robinduckett
Created June 2, 2010 14:04
Show Gist options
  • Select an option

  • Save robinduckett/422404 to your computer and use it in GitHub Desktop.

Select an option

Save robinduckett/422404 to your computer and use it in GitHub Desktop.

Revisions

  1. robinduckett created this gist Jun 2, 2010.
    18 changes: 18 additions & 0 deletions closest.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    String.prototype.closest = function(find, pos) {
    var found = -1;

    var cur1,cur2; cur1 = cur2 = pos;

    while (cur1 > 0 || cur2 <= this.length) {
    is = this.substr(cur1, find.length);
    it = this.substr(cur2, find.length);

    if (is == find) return cur1;
    if (it == find) return cur2;

    if (cur1 > 0) cur1--;
    if (cur2 <= this.length) cur2++;
    }

    return -1;
    };