Advertisement
Anubis_Black

JS Library

Mar 27th, 2013
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. String.format = function () {
  2.     var formattedString = arguments[0];
  3.  
  4.     for (var index = 0; index < arguments.length - 1; index++) {
  5.         var regex = new RegExp("\\{" + index + "\\}", "gm");
  6.         formattedString = formattedString.replace(regex, arguments[index + 1]);
  7.     }
  8.  
  9.     return formattedString;
  10. };
  11.  
  12. var validateNumber = function (input) {
  13.     var number = new Object();
  14.     number.error = false;
  15.  
  16.     // In these two cases parseFloat returns MAX_INT and MIN_INT respectively, which is not expected behaviour.
  17.     if (input != "9007199254740993" && input != "-9007199254740993") {
  18.         number.value = parseFloat(input);
  19.  
  20.         if (isNaN(number.value)) {
  21.             number.error = true;
  22.             number.message = "\x27" + input + "\x27 is not a number.";
  23.         }
  24.     }
  25.     else {
  26.         number.value = input;
  27.         number.error = true;
  28.         number.message = input + " cannot be represented exactly with double- precision arithmetic.";
  29.     }
  30.  
  31.     return number;
  32. };
  33.  
  34. var validateInteger = function (input) {
  35.     var number = validateNumber(input);
  36.    
  37.     if (!number.error) {
  38.         if (number.value - Math.floor(number.value) != 0) {
  39.             number.error = true;
  40.             number.message = input + " is not an integer.";
  41.         }
  42.         else if (number.value > Number.MAX_INT || number.value < Number.MIN_INT) {
  43.             number.error = true;
  44.             number.message = String.format("Integer overflow. Integers must lie in the range [{0}, {1}].", Number.MIN_INT, Number.MAX_INT);
  45.         }
  46.     }
  47.  
  48.     return number;
  49. };
  50.  
  51. // Rounds input to decimals decimal places. The built- in function only rounds integers and the function
  52. // for fixed decimal places always puts zeroes at the end of the number to fill up to the fixed position.
  53. // This version works much like the string format #.### where numbers are shown without the zeroes at the end
  54. var round = function (input, decimals) {
  55.     // Constants
  56.     var MIN_DECIMAL_PLACES = 0;
  57.     var MAX_DECIMAL_PLACES = 308;
  58.  
  59.     var number = new Object();
  60.     number.value = input;
  61.     number.error = false;
  62.  
  63.     if (decimals < MIN_DECIMAL_PLACES || decimals > MAX_DECIMAL_PLACES) {
  64.         number.error = true;
  65.         number.message = String.format("Cannot round to {0} decimal places. Restrictions are [{1}, {2}].", decimals, MIN_DECIMAL_PLACES, MAX_DECIMAL_PLACES);
  66.     }
  67.     else {
  68.         var power = 1;
  69.         for (var iterations = 0; iterations < decimals; iterations++) {
  70.             power *= 10;
  71.         }
  72.  
  73.         if (number.value * power >= Number.MAX_VALUE) {
  74.             number.error = true;
  75.             number.message = "Overflow. Choose a lower value for decimal places.";
  76.         }
  77.         else {
  78.             number.value = Math.round(number.value * power) / power;
  79.         }
  80.     }
  81.  
  82.     return number;
  83. };
  84.  
  85. /*var clearScreen = function () {
  86.     document.body.innerHTML = String.empty;
  87. }*/
  88.  
  89. // Useful constants that should have been built- in
  90. String.empty = "";
  91. Number.MAX_INT = Math.pow(2, 53);
  92. Number.MIN_INT = -Number.MAX_INT;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement