home *** CD-ROM | disk | FTP | other *** search
/ Freelog 100 / FreelogNo100-NovembreDecembre2010.iso / Graphisme / GoogleSketchUp / GoogleSketchUpWFR.exe / GoogleSketchUp8.msi / SketchUpMeta.cab / converter.js.78D17A5F_0E0A_44D2_877D_2C56D45D16B7 < prev    next >
Encoding:
Text File  |  2010-08-26  |  20.1 KB  |  698 lines

  1. //  Copyright 2008 Google Inc.
  2. //  All Rights Reserved.
  3.  
  4. /**
  5.  * @fileoverview DynamicComponent-specific unit conversion support functions.
  6.  * The data structures and methods here are used in both the configure and
  7.  * manager dialogs to convert units.
  8.  *
  9.  * This file is used inside SketchUp as a local javascript library. On SketchUp
  10.  * for the Mac, it runs inside Safari and on SketchUp for PC it runs inside IE.
  11.  * These are the only browsers this is ever used in.
  12.  */
  13.  
  14. // Declare su namespace.
  15. var su = window.su;
  16.  
  17. // Declare skp namespace.
  18. var skp = window.skp;
  19.  
  20. // Declare mgr namespace.
  21. var mgr = window.mgr;
  22.  
  23. // Define a "conv" object where we can hang our functionality.
  24. var conv = {};
  25.  
  26. /**
  27.  * Default number of decimal places to show at format time.
  28.  * @type {number}
  29.  */
  30. conv.DEFAULT_FORMAT_DECIMAL_PLACES = 3;
  31.  
  32. /**
  33.  * Array of objects that describe the supported units.
  34.  * @type {array}
  35.  */
  36. conv.units = [
  37.   {
  38.     name: 'DEFAULT',
  39.     label: 'User\'s default template units',
  40.     group: 'LENGTH',
  41.     configOnly: true
  42.   },
  43.   {
  44.     name: 'INTEGER',
  45.     label: 'Whole Number, no units',
  46.     group: 'INTEGER',
  47.     configOnly: true,
  48.     defaultFormulaUnit: 'FLOAT'
  49.   },
  50.   {
  51.     name: 'FLOAT',
  52.     label: 'Decimal Number, no units',
  53.     group: 'FLOAT'
  54.   },
  55.   {
  56.     name: 'PERCENT',
  57.     label: 'Percentage',
  58.     group: 'FLOAT',
  59.     test: '^[\\d\\.,\\-\\s]+\\%[^\\w]*$',
  60.     template: '{value}%',
  61.     conversion: 100,
  62.     configOnly: true,
  63.     defaultFormulaUnit: 'FLOAT'
  64.   },
  65.   {
  66.     name: 'BOOLEAN',
  67.     label: 'True/False',
  68.     group: 'BOOLEAN',
  69.     test: '^(true|false)$',
  70.     configOnly: true,
  71.     defaultFormulaUnit: 'FLOAT'
  72.   },
  73.   {
  74.     name: 'STRING',
  75.     label: 'Arbitrary Text, no units',
  76.     group: 'STRING'
  77.   },
  78.   {
  79.     name: 'INCHES',
  80.     label: 'Inches',
  81.     template: '{value}"',
  82.     group: 'LENGTH',
  83.     test: '^\d*\s*[^\\\']\s*\\d*\\s*\\"'
  84.   },
  85.   {
  86.     name: 'FEET',
  87.     label: 'Decimal Feet',
  88.     template: "{value}'",
  89.     group: 'LENGTH',
  90.     conversion: 0.0833333333333,
  91.     test: '^[\\d\\.,\\-\\s]+\\\'[\\d\\.,\\-\\s\\\\/"]*$',
  92.     configOnly: true,
  93.     defaultFormulaUnit: 'INCHES'
  94.   },
  95.   {
  96.     name: 'MILLIMETERS',
  97.     label: 'Millimeters',
  98.     template: '{value} mm',
  99.     group: 'LENGTH',
  100.     conversion: 25.4,
  101.     test: '^[\\d\\.,\\-\\s]+mm[^\\w]*$',
  102.     configOnly: true,
  103.     defaultFormulaUnit: 'CENTIMETERS'
  104.   },
  105.   {
  106.     name: 'CENTIMETERS',
  107.     label: 'Centimeters',
  108.     template: '{value} cm',
  109.     conversion: 2.54,
  110.     group: 'LENGTH',
  111.     test: '^[\\d\\.,\\-\\s]+cm[^\\w]*$'
  112.   },
  113.   {
  114.     name: 'METERS',
  115.     label: 'Meters',
  116.     template: '{value} m',
  117.     group: 'LENGTH',
  118.     conversion: .0254,
  119.     test: '^[\\d\\.,\\-\\s]+m[^\\w]*$',
  120.     configOnly: true,
  121.     defaultFormulaUnit: 'CENTIMETERS'
  122.   },
  123.   {
  124.     name: 'DEGREES',
  125.     label: 'Degrees',
  126.     template: '{value}°',
  127.     group: 'ANGLE',
  128.     configOnly: true,
  129.     defaultFormulaUnit: 'FLOAT'
  130.   },
  131.   {
  132.     name: 'DOLLARS',
  133.     label: 'Dollars',
  134.     group: 'CURRENCY',
  135.     test: '\\$\\d',
  136.     configOnly: true,
  137.     defaultFormulaUnit: 'FLOAT'
  138.   },
  139.   {
  140.     name: 'EUROS',
  141.     label: 'Euros',
  142.     group: 'CURRENCY',
  143.     configOnly: true,
  144.     defaultFormulaUnit: 'FLOAT'
  145.   },
  146.   {
  147.     name: 'YEN',
  148.     label: 'Yen',
  149.     group: 'currency',
  150.     configOnly: true,
  151.     defaultFormulaUnit: 'FLOAT'
  152.   },
  153.   {
  154.     name: 'POUNDS',
  155.     label: 'Pounds (weight)',
  156.     template: '{value} lbs',
  157.     group: 'WEIGHT',
  158.     test: '\\d\\s*lbs',
  159.     configOnly: true,
  160.     defaultFormulaUnit: 'FLOAT'
  161.   },
  162.   {
  163.     name: 'KILOGRAMS',
  164.     label: 'Kilograms',
  165.     template: '{value} kg',
  166.     group: 'WEIGHT',
  167.     conversion: 0.45359237,
  168.     test: '\\d\\s*kg',
  169.     configOnly: true,
  170.     defaultFormulaUnit: 'FLOAT'
  171.   }
  172. ];
  173.  
  174. /**
  175.  * A friendly hash index into our conv.units, to allow us to use easy lookups
  176.  * like conv.unitsHash['CENTIMETERS'].
  177.  * @type {Object}
  178.  */
  179. conv.unitsHash = {
  180.  'DEFAULT': conv.units[0],
  181.  'INTEGER': conv.units[1],
  182.  'FLOAT': conv.units[2],
  183.  'PERCENT': conv.units[3],
  184.  'BOOLEAN': conv.units[4],
  185.  'STRING': conv.units[5],
  186.  'INCHES': conv.units[6],
  187.  'FEET': conv.units[7],
  188.  'MILLIMETERS': conv.units[8],
  189.  'CENTIMETERS': conv.units[9],
  190.  'METERS': conv.units[10],
  191.  'DEGREES': conv.units[11],
  192.  'DOLLARS': conv.units[12],
  193.  'EUROS': conv.units[13],
  194.  'YEN': conv.units[14],
  195.  'POUNDS': conv.units[15],
  196.  'KILOGRAMS': conv.units[16]
  197. };
  198.  
  199. /**
  200.  * Array of objects that describe the supported unit groups.
  201.  * @type {Array.<Object>}
  202.  */
  203. conv.groups = [
  204.  {name: 'STRING', label: 'Text', base: 'STRING'},
  205.  {name: 'LENGTH', label: 'Length', base: 'INCHES'},
  206.  {name: 'INTEGER', label: 'Whole Number', base: 'INTEGER'},
  207.  {name: 'FLOAT', label: 'Decimal Number', base: 'FLOAT'},
  208.  {name: 'ANGLE', label: 'Angle', base: 'DEGREES'},
  209.  {name: 'CURRENCY', label: 'Currency', base: 'DOLLARS'},
  210.  {name: 'WEIGHT', label: 'Weight', base: 'POUNDS'}
  211. ];
  212.  
  213. /**
  214.  * A friendly hash index into our conv.groups, to allow us to use easy lookups
  215.  * like conv.groupsHash['LENGTH'].
  216.  * @type {Object}
  217.  */
  218. conv.groupsHash = {
  219.  'STRING': conv.groups[0],
  220.  'LENGTH': conv.groups[1],
  221.  'INTEGER': conv.groups[2],
  222.  'FLOAT': conv.groups[3],
  223.  'ANGLE': conv.groups[4],
  224.  'CURRENCY': conv.groups[5],
  225.  'WEIGHT': conv.groups[6]
  226. };
  227.  
  228. /**
  229.  * Tests a user-entered string for any matches against out list of unit tests.
  230.  * For example, if a user entered a value like "16'", this would return "FEET".
  231.  * @param {string} string The value to test.
  232.  * @param {boolean} opt_defaultIfConfigOnly If true, then configOnly matches
  233.  *     will default down to their approriate defaultFormulaUnit.
  234.  * @return {string} The matching unit type.
  235.  */
  236. conv.recognizeUnits = function(string, opt_defaultIfConfigOnly) {
  237.   var unit;
  238.   var testExpression;
  239.   var foundUnit;
  240.  
  241.   // Force conversion to string object.
  242.   var stringToTest = string + '';
  243.  
  244.   // Loop across our units to run our tests.
  245.   for (var i = 0; i < conv.units.length; i++) {
  246.     unit = conv.units[i];
  247.     if (su.isValid(unit.test)) {
  248.       testExpression = new RegExp(unit.test, 'i');
  249.       if (testExpression.test(stringToTest)) {
  250.         foundUnit = unit.name
  251.         break;
  252.       }
  253.     }
  254.   }
  255.  
  256.   // Let's convert configOnly units down to the appropriate formulaUnit match
  257.   // if opt_defaultIfConfigOnly is true.
  258.   if (opt_defaultIfConfigOnly == true && su.notEmpty(foundUnit)) {
  259.     foundUnit = su.ifEmpty(conv.unitsHash[foundUnit].defaultFormulaUnit,
  260.         foundUnit)
  261.   }
  262.  
  263.   // If there are no matches, then it counts as a string.
  264.   return su.ifEmpty(foundUnit, 'STRING');
  265. };
  266.  
  267. /**
  268.  * Takes a string or number value and converts it into the appropriate base
  269.  * units. For example, a call of parseIntoBase(25.4, 'MILLIMETERS') would
  270.  * return 1.0, since inches are the base unit for millimeters.
  271.  * @param {string|number} value The value to convert.
  272.  * @param {string} units The units name to convert from.
  273.  * @return {string|number} The converted value, or '' if that value is empty.
  274.  */
  275. conv.toBase = function(value, units) {
  276.   if (su.isEmpty(value)) {
  277.     return '';
  278.   }
  279.  
  280.   var unit = conv.unitsHash[units];
  281.   if (su.notValid(unit) || units == 'STRING') {
  282.     return value;
  283.   }
  284.  
  285.   // See if there is a toBase function. If so, call that.
  286.   var functionName = units.toLowerCase() + 'ToBase';
  287.   if (su.canCall(conv, functionName)) {
  288.     return conv[functionName](value);
  289.   }
  290.  
  291.   var numberValue = conv.toNumber(value);
  292.   if (su.notValid(unit.conversion)) {
  293.     return numberValue;
  294.   }
  295.   return numberValue / unit.conversion;
  296. };
  297.  
  298. /**
  299.  * Takes a string or number value and converts it into the passed units from
  300.  * the appropriate base.
  301.  * For example, a call of fromBase(1.0, 'MILLIMETERS') would return 25.4,
  302.  * since inches are the base unit for millimeters.
  303.  * @param {string|number} value The value to convert.
  304.  * @param {string} units The units name to convert into.
  305.  * @return {string|number} The converted value.
  306.  */
  307. conv.fromBase = function(value, units) {
  308.   var unit = conv.unitsHash[units];
  309.   if (su.notValid(unit) || units == 'STRING') {
  310.     return value;
  311.   }
  312.   var numberValue = conv.toNumber(value);
  313.   if (su.notValid(unit.conversion)) {
  314.     return value;
  315.   }
  316.   return numberValue * unit.conversion;
  317. };
  318.  
  319. /**
  320.  * Takes a value and displays it in the passed unit in a pretty format.
  321.  * For example, a call of format(2.53, 'CENTIMETERS', 1, true) would return
  322.  * 2.5cm or 2,5cm (depending on user's decimal delimiter).
  323.  * @param {string|number} value The value to format.
  324.  * @param {string} units The units name to convert to.
  325.  * @param {number} opt_decimalPlaces Number of decimal places to round to.
  326.  * @param {boolean} opt_useTemplate Whether to apply the unit template,
  327.  *     defaults to true.
  328.  * @param {string} opt_decimalDelimiter A string containing the decimal
  329.  *     delimiter to format with, defaults to '.'.
  330.  * @return {string|number} The converted value.
  331.  */
  332. conv.format = function(value, units, opt_decimalPlaces, opt_useTemplate,
  333.     opt_decimalDelimiter) {
  334.   var decimalPlaces = su.ifEmpty(opt_decimalPlaces,
  335.       conv.DEFAULT_FORMAT_DECIMAL_PLACES);
  336.   var decimalDelimiter = su.ifEmpty(opt_decimalDelimiter, '.');
  337.  
  338.   // It's bad practice to treat params as vars, so reassign and adjust to
  339.   // handle user template default unit choice.
  340.   var unit = (units == 'DEFAULT') ? skp.units() : units;
  341.  
  342.   var displayValue = value;
  343.  
  344.   if (unit != 'STRING') {
  345.     displayValue = conv.roundDecimalPoints(displayValue, decimalPlaces);
  346.     displayValue = displayValue + '';
  347.     displayValue = displayValue.replace(/\./, decimalDelimiter);
  348.   }
  349.  
  350.   if (opt_useTemplate === false || unit == 'STRING') {
  351.     return displayValue;
  352.   } else {
  353.     // See if there is a formatting function. If so, call that.
  354.     var functionName = 'format' + units.substr(0, 1).toUpperCase() +
  355.         units.substr(1, units.length).toLowerCase();
  356.     if (su.canCall(conv, functionName)) {
  357.       displayValue = conv[functionName](value);
  358.       displayValue = displayValue.replace(/\./, decimalDelimiter);
  359.       return displayValue;
  360.     }
  361.  
  362.     var template = su.ifEmpty(conv.unitsHash[units].template, '{value}');
  363.     displayValue = template.replace(/{value}/gi, displayValue);
  364.     displayValue = displayValue.replace(/\./, decimalDelimiter);
  365.     return displayValue;
  366.   }
  367. };
  368.  
  369. /**
  370.  * Takes a user entered string and converts it to a given unit. This has some
  371.  * extra smarts around only doing conversion if the two units are from the
  372.  * same group. If they're not, then no conversion is done. (If you type 6cm
  373.  * into a weight field, we just parse it down to 6.)
  374.  * @param {string} value The value to convert.
  375.  * @param {string} targetUnits The unit we want the value in.
  376.  * @param {string} opt_decimalDelimiter Optional decimal delimiter to parse
  377.  *     in context of. For example, if ',' is passed, then the value 5,5 will be
  378.  *     correctly interpreted as 5.5.
  379.  * @return {string|number} The converted value, unformatted.
  380.  */
  381. conv.parseTo = function(value, targetUnits, opt_decimalDelimiter) {
  382.   if (su.isEmpty(value)) {
  383.     return '';
  384.   }
  385.   var cleanValue = value + '';
  386.  
  387.   // If our value is being parsed down to a number, clean up the user-entered
  388.   // string to use periods for decimal delimiters so JS can understand it.
  389.   if (targetUnits != 'STRING') {
  390.     if (opt_decimalDelimiter == ',') {
  391.       cleanValue = cleanValue.replace(/\./, '');
  392.       cleanValue = cleanValue.replace(/\,/, '.');
  393.     }
  394.     if (targetUnits == 'DOLLARS' || targetUnits == 'YEN' ||
  395.       targetUnits == 'EUROS') {
  396.       // Strip off leading non-digit characters, to allow for entry with
  397.       // currency amounts at the front.
  398.       cleanValue = cleanValue.replace(/^[^\d\-]/, '');
  399.     }
  400.   }
  401.   // Figure out the units involved.
  402.   var valueUnits = conv.recognizeUnits(cleanValue);
  403.  
  404.   // If there were no specific unit match based on the string entered, then
  405.   // best choice is to assume they entered the target units.
  406.   if (valueUnits == 'STRING') {
  407.     valueUnits = targetUnits;
  408.   }
  409.   var valueInBase = conv.toBase(cleanValue, valueUnits);
  410.   var valueGroup = conv.unitsHash[valueUnits].group;
  411.   var targetGroup = conv.unitsHash[targetUnits].group;
  412.  
  413.   // If these two units are in the same group, then we know how to convert.
  414.   // If they're not, then we'll just return a clean (untemplated) value in
  415.   // the original units.
  416.   if (valueGroup == targetGroup) {
  417.     return conv.fromBase(valueInBase, targetUnits);
  418.   } else if (targetUnits == 'STRING') {
  419.     return value;
  420.   } else {
  421.     return conv.toNumber(cleanValue);
  422.   }
  423. };
  424.  
  425. /**
  426.  * Rounds the fractional portion of a value to the desired number of digits.
  427.  * This method differs from toFixed in that it returns a string and explicitly
  428.  * chops off trailing zeroes. So if you pass it "75.0", as Ruby is wont to do,
  429.  * it will return a nice "75".
  430.  * @param {string|number} value The value to process.
  431.  * @param {number} digits The number of fractional digits to preserve.
  432.  * @return {string} The newly processed value.
  433.  */
  434. conv.roundDecimalPoints = function(value, digits) {
  435.   if (su.isNumber(value)) {
  436.     value = conv.toNumber(value);
  437.     var returnVal = value.toFixed(digits);
  438.     if (digits > 0) {
  439.       // Strip off trailing zeroes and trailing decimal.
  440.       returnVal = returnVal.replace(/0+$/, '');
  441.       returnVal = returnVal.replace(/\.$/, '');
  442.     }
  443.     return returnVal;
  444.   }
  445.  
  446.   if (su.isString(value)) {
  447.     // Note that we will only ever hit this point of making a recursive call
  448.     // if the original passed value is not a number. So by running toNumber
  449.     // here we're guaranteeing that we won't have an infinite loop.
  450.     return conv.roundDecimalPoints(conv.toNumber(value), digits);
  451.   } else {
  452.     return 0;
  453.   }
  454. };
  455.  
  456. /**
  457.  * Processes anything into a number.
  458.  * @param {string|number} value The value to process.
  459.  * @return {number} The newly processed value.
  460.  */
  461. conv.toNumber = function(value) {
  462.   var strippedValue = value + '';
  463.   strippedValue = strippedValue.replace(/\,/gi, '.');
  464.   var returnValue = parseFloat(strippedValue);
  465.   if (isNaN(returnValue)) {
  466.     returnValue = 0;
  467.   }
  468.   return returnValue;
  469. };
  470.  
  471. /**
  472.  * Formats a currency value. NOTE that no currency conversion is performed.
  473.  * @param {string|number} value The currency value to format.
  474.  * @param {string} opt_prefix The currency character to start with.
  475.  * @param {string} opt_suffix The currency character to end with.
  476.  * @param {string} opt_dividerChar The character to split long strings with,
  477.  *     typically ','.
  478.  * @param {string} opt_decimalDelimiter The character to use for the decimal
  479.  *     char typically '.'.
  480.  * @return {string} The formatted value, or '' if the value is empty.
  481.  */
  482. conv.formatCurrency = function(value, opt_prefix, opt_suffix, opt_dividerChar,
  483.     opt_decimalDelimiter) {
  484.   if (su.isEmpty(value)) {
  485.     return '';
  486.   }
  487.   var prefix = su.ifEmpty(opt_prefix, '');
  488.   var suffix = su.ifEmpty(opt_suffix, '');
  489.   var decimal = su.ifEmpty(opt_decimalDelimiter, skp.decimalDelimiter());
  490.  
  491.   // Get our numeric values.
  492.   var val = conv.toNumber(value);
  493.  
  494.   var whole = Math.floor(val);
  495.   // Compute fraction, 0-padding if < 10.
  496.   var fraction = Math.round((val - whole) * 100);
  497.   if (fraction == 100) {
  498.     fraction = 0;
  499.     whole += 1;
  500.   }
  501.   fraction = (fraction < 10) ? '0' + fraction : fraction;
  502.  
  503.   if (!su.isEmpty(opt_dividerChar)) {
  504.     whole = whole + '';
  505.     var formattedWhole = '';
  506.     var count = 0;
  507.     for (var i = whole.length - 1; i >= 0; i--) {
  508.       if (count == 3) {
  509.         formattedWhole = opt_dividerChar + formattedWhole;
  510.         count = 0;
  511.       }
  512.       count++;
  513.       formattedWhole = whole.charAt(i) + formattedWhole;
  514.     }
  515.   } else {
  516.     formattedWhole = whole;
  517.   }
  518.   if (whole > 999 && fraction == '00') {
  519.     return prefix + formattedWhole + suffix;
  520.   } else {
  521.     return prefix + formattedWhole + decimal + fraction + suffix;
  522.   }
  523. };
  524.  
  525. /**
  526.  * Does a floating-point aware assessment of whether two values are identical
  527.  * enough to count as equal. This must be smart enough to compare a string to
  528.  * a number and return true if the string contains a number that is close
  529.  * enough to pass.
  530.  * @param {Object} value1 The 1st value to compare.
  531.  * @param {Object} value2 The 2nd value to compare.
  532.  * @return {boolean} Whether they are equal within a reasonable tolerance.
  533.  */
  534. conv.isEqual = function(value1, value2) {
  535.   if (value1 == value2) {
  536.     return true;
  537.   } else if (conv.containsNumber(value1) && conv.containsNumber(value2)) {
  538.     if (Math.abs(conv.toNumber(value1) - conv.toNumber(value2)) < .0000001) {
  539.       return true;
  540.     }
  541.   }
  542.   return false;
  543. };
  544.  
  545. /**
  546.  * Checks a string to see if it contains a number. For example, '1.234'
  547.  * returns true, whereas 'Google' returns false.
  548.  * @param {string} value The value to check.
  549.  * @return {boolean} Whether it contains a number.
  550.  */
  551. conv.containsNumber = function(value) {
  552.   var stringValue = value + '';
  553.   return stringValue.match(/^\d+\.*\d*$/);
  554. };
  555.  
  556. /**
  557.  * Takes a user-entered string and attempts to return its value
  558.  * as a float, parsing for fractions such as 4 5/8 or 4-5/8. Note that this
  559.  * method will raise an error if a division by zero is attempted.
  560.  * @param {string} value The units string, such as "INCHES".
  561.  * @return {float} The parsed value.
  562.  */
  563. conv.parseFraction = function(value) {
  564.   var wholeNumber = 0;
  565.  
  566.   // Look for a slash to indicate a fraction.
  567.   if (value.match(/\//)) {
  568.     var splitValues = value.split(/\//);
  569.     var dividend = splitValues[0];
  570.     var divisor = splitValues[1];
  571.  
  572.     // Clean leading spaces from the dividend to ease regular expression
  573.     // matching below.
  574.     dividend = dividend.replace(/^\s*/, '');
  575.  
  576.     // Check the dividend for white space. If we find these then
  577.     // we must have a compound fraction like "5 5/16".
  578.     if (dividend.match(/\d[\s\-]*\d/)) {
  579.       splitValues = dividend.split(/\b[\s\-](?=\d)/);
  580.       wholeNumber = splitValues[0];
  581.       dividend = splitValues[1];
  582.     }
  583.     var returnValue;
  584.     var wholeNumber = parseFloat(wholeNumber);
  585.     var fraction = parseFloat(dividend) / parseFloat(divisor);
  586.     if (wholeNumber < 0) {
  587.       returnValue = wholeNumber - fraction;
  588.     } else {
  589.       returnValue = wholeNumber + fraction;
  590.     }
  591.     return returnValue;
  592.   } else {
  593.     return parseFloat(value);
  594.   }
  595. };
  596.  
  597. /**
  598.  *  Unit-specific formatting functions.
  599.  */
  600.  
  601. /**
  602.  * Returns nicely formatted string for our INTEGER unit.
  603.  * @param {number} value The value to format.
  604.  * @return {string} The newly formatted value.
  605.  */
  606. conv.formatInteger = function(value) {
  607.   if (su.isEmpty(value)) {
  608.     return '';
  609.   } else {
  610.     return Math.floor(conv.toNumber(value), 0) + '';
  611.   }
  612. };
  613.  
  614. /**
  615.  * Returns nicely formatted string for our EUROS unit.
  616.  * @param {number} value The value to format.
  617.  * @return {string} The newly formatted value.
  618.  */
  619. conv.formatBoolean = function(value) {
  620.   if (conv.toNumber(value) > 0) {
  621.     return su.translateString('TRUE');
  622.   } else {
  623.     return su.translateString('FALSE');
  624.   }
  625. };
  626.  
  627. /**
  628.  * Returns nicely formatted string for our DOLLARS unit.
  629.  * @param {number} value The value to format.
  630.  * @return {string} The newly formatted value.
  631.  */
  632. conv.formatDollars = function(value) {
  633.   return conv.formatCurrency(value, '$');
  634. };
  635.  
  636. /**
  637.  * Returns nicely formatted string for our YEN unit.
  638.  * @param {number} value The value to format.
  639.  * @return {string} The newly formatted value.
  640.  */
  641. conv.formatYen = function(value) {
  642.   return conv.formatCurrency(value, '¥');
  643. };
  644.  
  645. /**
  646.  * Returns nicely formatted string for our EUROS unit.
  647.  * @param {number} value The value to format.
  648.  * @return {string} The newly formatted value.
  649.  */
  650. conv.formatEuros = function(value) {
  651.   return conv.formatCurrency(value, '€');
  652. };
  653.  
  654. /**
  655.  *  Unit-specific toBase functions.
  656.  */
  657.  
  658. /**
  659.  * Converts an entered value to the appropriate base units.
  660.  * @param {string|number} value The value to format.
  661.  * @return {string} The newly formatted value.
  662.  */
  663. conv.booleanToBase = function(value) {
  664.   var stringValue = value + '';
  665.   if (stringValue.toLowerCase() == 'true') {
  666.     return 1;
  667.   } else if (stringValue.toLowerCase() == 'false') {
  668.     return 0;
  669.   } else {
  670.     var numberValue = conv.toNumber(value);
  671.     if (numberValue > 0) {
  672.       return 1;
  673.     } else {
  674.       return 0;
  675.     }
  676.   }
  677. };
  678.  
  679. /**
  680.  * Converts an entered value to the appropriate base units.
  681.  * @param {string|number} value The value to format.
  682.  * @return {string} The newly formatted value.
  683.  */
  684. conv.feetToBase = function(value) {
  685.   var stringValue = value + '';
  686.   var splitValues = stringValue.split(/\'/);
  687.   var feet = su.ifEmpty(splitValues[0], '0');
  688.   var inches = su.ifEmpty(splitValues[1], '0');
  689.   // Parse the remaining strings for fractions.
  690.   feet = conv.parseFraction(feet);
  691.   inches = conv.parseFraction(inches);
  692.   if (feet < 0) {
  693.     return feet * 12 - inches;
  694.   } else {
  695.     return feet * 12 + inches;
  696.   }
  697. };
  698.