Have numericLerp work in fixed point based on the longest operand.
[string-lerp.git] / string-lerp.js
index 607abc1..46a55bf 100644 (file)
         return patch(partial, target);
     }
 
-    var NUMBERS = /(-?\d+(?:\.\d+)?)/g;
+    var NUMBERS = /(-?\d{1,20}(?:\.\d{1,20})?)/g;
 
     function areNumericTwins(source, target) {
         /** Check if a and b differ only in numerals */
             numerals gives undefined results.
         */
 
-        // TODO: Try to preserve precision of the original numbers.
         var sourceParts = source.split(NUMBERS);
         var targetParts = target.split(NUMBERS);
         var destParts = targetParts;
             var part = nlerp(+sourcePart, +targetPart, amount);
             var sourcePoint = sourcePart.indexOf(".");
             var targetPoint = targetPart.indexOf(".");
-            if (sourcePoint === -1 && targetPoint === -1)
-                part = Math.round(part);
-            targetParts[i] = part.toString();
+            var point = Math.max(
+                sourcePoint >= 0 ? (sourcePart.length - 1) - sourcePoint : 0,
+                targetPoint >= 0 ? (targetPart.length - 1) - targetPoint : 0);
+            targetParts[i] = part.toFixed(point);
         }
         return targetParts.join("");
     }