X-Git-Url: https://git.yukkurigames.com/?p=string-lerp.git;a=blobdiff_plain;f=string-lerp.js;h=bb799a4743e828658c7eef1c3d3d5df8716b981d;hp=114bebd67c6ba4097f18ba25b0627a81b686a938;hb=bf06839ad7c1952c807da031f82d83e61280bc04;hpb=eb2b2d4d7a57095b17f17eb6cfc084cbaa37ea90 diff --git a/string-lerp.js b/string-lerp.js index 114bebd..bb799a4 100644 --- a/string-lerp.js +++ b/string-lerp.js @@ -182,10 +182,10 @@ Numbers may have a leading "-" and a single "." to mark the decimal point, but something must be after the ".". No other floating point syntax (e.g. 1e6) is supported. - If both of the numbers in a pair are integers, the result - is clamped to an integer. + They are treated as fixed-point values, with the point's + position itself interpolating. - For example, numericLerp("0.0", "100", 0.123) === "12.3" + For example, numericLerp("0.0", "100".0, 0.123) === "12.3" because the "." in "0.0" is interpreted as a decimal point. But numericLerp("0.", "100.", 0.123) === "12." because the strings are interpreted as integers followed @@ -195,11 +195,11 @@ numerals gives undefined results. */ - var sourceParts = source.split(NUMBERS); var targetParts = target.split(NUMBERS); - var destParts = targetParts; - for (var i = 1; i < sourceParts.length; i += 2) { - var sourcePart = sourceParts[i]; + var match; + var i = 1; + while ((match = NUMBERS.exec(source))) { + var sourcePart = match[0]; var targetPart = targetParts[i]; var part = nlerp(+sourcePart, +targetPart, amount); var sourcePoint = sourcePart.indexOf("."); @@ -209,6 +209,7 @@ targetPoint >= 0 ? (targetPart.length - 1) - targetPoint : 0, amount)); targetParts[i] = part.toFixed(point); + i += 2; } return targetParts.join(""); }