andrew4582

Javascript utilities

Mar 7th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * is_number
  3.  *  Return true if a value represents a number, else return false.
  4.  */
  5. function is_number(value)
  6. {
  7.     var str = value + "";
  8.     return str.match(/^-?\d*\.?\d+$/) ? true : false;
  9. }
  10. /*
  11.  * is_blank
  12.  *  Return true if value is a blank (i.e. "").
  13.  */
  14. function is_blank(value)
  15. {
  16.     var str = value + "";
  17.     return str.match(/^\s*$/) ? true : false;
  18. }
  19.  
  20.  
  21. /*
  22.  * trim_string
  23.  *  Remove leading and trailing blank spaces from a string.
  24.  */
  25. function trim_string(str)
  26. {
  27.     var trim = str + "";
  28.     trim = trim.replace(/^\s*/, "");
  29.     return trim.replace(/\s*$/, "");
  30. }
  31. /*
  32.  * are_values_equal()
  33.  *  Compare values of types boolean, string and number. The types may be different.
  34.  *  Returns true if values are equal.
  35.  */
  36. function are_values_equal(val1, val2)
  37. {
  38.     /* Make sure we can handle these values. */
  39.     switch (typeof(val1)) {
  40.     case 'boolean':
  41.     case 'string':
  42.     case 'number':
  43.         break;
  44.     default:
  45.         // alert("are_values_equal does not handle the type '" + typeof(val1) + "' of val1 '" + val1 + "'.");
  46.         return false;
  47.     }
  48.  
  49.     switch (typeof(val2)) {
  50.     case 'boolean':
  51.         switch (typeof(val1)) {
  52.         case 'boolean':
  53.             return (val1 == val2);
  54.         case 'string':
  55.             if (val2) {
  56.                 return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
  57.             } else {
  58.                 return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
  59.             }
  60.             break;
  61.         case 'number':
  62.             return (val1 == val2 * 1);
  63.         }
  64.         break;
  65.     case 'string':
  66.         switch (typeof(val1)) {
  67.         case 'boolean':
  68.             if (val1) {
  69.                 return (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on");
  70.             } else {
  71.                 return (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off");
  72.             }
  73.             break;
  74.         case 'string':
  75.             if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
  76.                 return (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on");
  77.             }
  78.             if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
  79.                 return (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off");
  80.             }
  81.             return (val2 == val1);
  82.         case 'number':
  83.             if (val2 == "1" || val2.toLowerCase() == "true" || val2.toLowerCase() == "on") {
  84.                 return (val1 == 1);
  85.             }
  86.             if (val2 == "0" || val2.toLowerCase() == "false" || val2.toLowerCase() == "off") {
  87.                 return (val1 === 0);
  88.             }
  89.             return (val2 == val1 + "");
  90.         }
  91.         break;
  92.     case 'number':
  93.         switch (typeof(val1)) {
  94.         case 'boolean':
  95.             return (val1 * 1 == val2);
  96.         case 'string':
  97.             if (val1 == "1" || val1.toLowerCase() == "true" || val1.toLowerCase() == "on") {
  98.                 return (val2 == 1);
  99.             }
  100.             if (val1 == "0" || val1.toLowerCase() == "false" || val1.toLowerCase() == "off") {
  101.                 return (val2 === 0);
  102.             }
  103.             return (val1 == val2 + "");
  104.         case 'number':
  105.             return (val2 == val1);
  106.         }
  107.         break;
  108.     default:
  109.         return false;
  110.     }
  111. }
  112. /*
  113.  * copy_select_options(from_select, to_select )
  114.  *  copy options from the given select element. Do not copy the first option which is the descriptive
  115.  *      label such as <option value="-1">Computer Name</option>
  116.  *
  117.  * NOTE: Attempts to preserve the current selection, or reverts to "-1" if the selection has disappeared.
  118.  */
  119. function copy_select_options(from_select, to_select)
  120. {
  121.         /*
  122.          * Disable the select element so that it does not conflict with any user-interaction
  123.          */
  124.         var saved_state = to_select.disabled;
  125.  
  126.         if (!saved_state) {
  127.             to_select.disabled = true;
  128.         }
  129.  
  130.         /*
  131.          * Remember the selection value of this pulldown, if any
  132.          */
  133.         var current_value_selection = null;
  134.         if (to_select.selectedIndex) {
  135.             var current_selected_index = to_select.selectedIndex;
  136.             current_value_selection = to_select.value;
  137.         } else {
  138.             current_value_selection = "-1";
  139.         }
  140.  
  141.         /*
  142.          * Clear the select options as we are now going to refresh the list
  143.          *  Keep the descriptive label
  144.          */
  145.         for (var k = to_select.options.length - 1; k > 0; k--) {
  146.             to_select.options[k] = null;
  147.         }
  148.  
  149.         /*
  150.          * Copy options from the new select
  151.          *  Don't touch the descriptive label, start from entry 1
  152.          *  opt.text = "Computer Name"; opt.value = "-1";
  153.          */
  154.         var selected_index = 0;
  155.         for (var entry = 1; entry < from_select.options.length; ++entry) {
  156.             to_select.options.add(new Option(from_select.options[entry].text, from_select.options[entry].value));
  157.  
  158.             /*
  159.              * If the previously selected value matches this entry then we remeber the
  160.              * index of it so that we can restore the selected position
  161.              */
  162.             if (from_select.options[entry].value == current_value_selection) {
  163.                 selected_index = entry;
  164.             }
  165.         }
  166.  
  167.         /*
  168.          * Restore the selected position
  169.          */
  170.         to_select.selectedIndex = selected_index;
  171.  
  172.         /*
  173.          * Re-enable the select element
  174.          */
  175.         to_select.disabled = saved_state;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment