Let's try mozilla's fixedCharAt function
This commit is contained in:
parent
7dff6e190b
commit
0e923aa4b4
@ -262,14 +262,14 @@ function design5(input) {
|
||||
|
||||
if (dist > radius - 1 && dist < radius + 1) { //&& dist > radius - 1
|
||||
lineHasInput = true;
|
||||
outputLine += "" + input.charCodeAt(charIterate(input)); //zero-width space
|
||||
outputLine += "" + input.fixedCharAt(charIterate(input)); //zero-width space
|
||||
} else {
|
||||
outputLine += " ";
|
||||
}
|
||||
}
|
||||
} else {
|
||||
lineHasInput = true;
|
||||
outputLine += input.charCodeAt(charIterate(input)) + space + input + space + spaceOffset + input.charCodeAt(charIterate(input));
|
||||
outputLine += input.fixedCharAt(charIterate(input)) + space + input + space + spaceOffset + input.fixedCharAt(charIterate(input));
|
||||
}
|
||||
|
||||
//console.log(i, j, outputLine);
|
||||
@ -280,5 +280,38 @@ function design5(input) {
|
||||
}
|
||||
return outputString;
|
||||
}
|
||||
|
||||
// Code from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charAt
|
||||
|
||||
function fixedCharAt(str, idx) {
|
||||
str = String(str);
|
||||
|
||||
const surrogatePairs = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g;
|
||||
while (surrogatePairs.exec(str) !== null) {
|
||||
const lastIdx = surrogatePairs.lastIndex;
|
||||
if (lastIdx - 2 < idx) {
|
||||
idx++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (idx >= str.length || idx < 0) {
|
||||
return "";
|
||||
}
|
||||
|
||||
let ret = str.charAt(idx);
|
||||
|
||||
if (
|
||||
/[\uD800-\uDBFF]/.test(ret) &&
|
||||
/[\uDC00-\uDFFF]/.test(str.charAt(idx + 1))
|
||||
) {
|
||||
// Go one further, since one of the "characters" is part of a surrogate pair
|
||||
ret += str.charAt(idx + 1);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//console.log(design4("super long test string super long test string"));
|
||||
//"super long test string super long test string"
|
Loading…
Reference in New Issue
Block a user