var TEST = function () {
/**
* i = index
* ┌ len=1 => (i , i ) ┐ // origin, odd
* └ len=2 => (i , i+1) ┘ // go forward, even
* ┌ len=3 => (i-1, i+1) ┐ // go backward, odd
* └ len=4 => (i-1, i+2) ┘ // go forward, even
* ┌ len=5 => (i-2, i+2) ┐ // go backward, odd
* └ len=6 => (i-2, i+3) ┘ // go forward, even
*/
let _strLen = NaN;
let _rec = { start: NaN, len: NaN };
const _getMatchLen = (str, si, ei) => {
while (si >= 0 && ei < _strLen && str[si] === str[ei]) {
si--;
ei++;
}
return ei - si - 1; // (ei-1) - (si+1) + 1;
}
const _update = (i, len) => {
if (len > _rec.len) {
_rec.len = len;
_rec.start = i - Math.ceil(len / 2) + 1;
return true;
}
}
return {
exec: function (str) {
if (!str) { return ''; }
_strLen = str.length;
_rec.start = 0;
_rec.len = 1;
for (let i = 0; i < _strLen; ++i) {
if (!str[i + Math.ceil(_rec.len / 2)]) { break; }
if (_update(i, _getMatchLen(str, i, i + 1))) {
continue;
}
_update(i, _getMatchLen(str, i, i));
}
return str.substr(_rec.start, _rec.len);
}
};
}();