No reason to split into sourceParts, use the regex to extract matches directly.
[string-lerp.git] / string-lerp.js
index 114bebd..bb799a4 100644 (file)
             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
             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(".");
                 targetPoint >= 0 ? (targetPart.length - 1) - targetPoint : 0,
                 amount));
             targetParts[i] = part.toFixed(point);
+            i += 2;
         }
         return targetParts.join("");
     }