polyfill_Intl_NumberFormat.js.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>JSDoc: Source: polyfill/Intl/NumberFormat.js</title>
  6. <script src="scripts/prettify/prettify.js"> </script>
  7. <script src="scripts/prettify/lang-css.js"> </script>
  8. <!--[if lt IE 9]>
  9. <script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
  10. <![endif]-->
  11. <link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
  12. <link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
  13. </head>
  14. <body>
  15. <div id="main">
  16. <h1 class="page-title">Source: polyfill/Intl/NumberFormat.js</h1>
  17. <section>
  18. <article>
  19. <pre class="prettyprint source linenums"><code>// Licensed Materials - Property of IBM
  20. //
  21. // IBM Watson Analytics
  22. //
  23. // (C) Copyright IBM Corp. 2018
  24. //
  25. // US Government Users Restricted Rights - Use, duplication or
  26. // disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  27. module.exports = ( function( Format )
  28. {
  29. "use strict";
  30. /**
  31. * These classes are NOT a polyfill, and are not meant to be!
  32. * They provide a poor man's fallback for the Intl formatters, for non-compliant environments.
  33. */
  34. var reCurrencyCode = /^[A-Z]{3}$/;
  35. var nbsp = String.fromCharCode( 160 );
  36. /**
  37. * Object that parses an Options object and normalizes it to options.
  38. * @class module:barejs/polyfill/Intl.NumberFormat~NumberFormatOptions
  39. */
  40. function NumberFormatOptions( _options )
  41. {
  42. // MinimumFractionDigits is usually 0
  43. this.minimumFractionDigits = 0;
  44. this.maximumFractionDigits = 3;
  45. this.minimumIntegerDigits = 1;
  46. this.numberingSystem = "latn";
  47. this.useGrouping = true;
  48. switch ( "style" in _options ? _options.style : "decimal" )
  49. {
  50. case "percent":
  51. this.style = "percent";
  52. this.maximumFractionDigits = Math.max( 0, this.minimumFractionDigits );
  53. break;
  54. case "currency":
  55. this.style = "currency";
  56. if ( !_options.currency )
  57. throw new TypeError( "Currency code is required with currency style." );
  58. if ( ( typeof _options.currency !== "string" ) || !reCurrencyCode.test( _options.currency ) )
  59. throw new TypeError( "Invalid currency code:" + _options.currency );
  60. this.currency = _options.currency;
  61. this.currencyDisplay = "code"; // This is all the fallback supports!
  62. this.maximumFractionDigits = Math.max( 2, this.minimumFractionDigits );
  63. break;
  64. case "decimal":
  65. this.style = "decimal";
  66. break;
  67. default:
  68. throw new RangeError( "Value " + _options.style + " out of range for numberformat options property style" );
  69. }
  70. }
  71. /**
  72. * Provides number formatting
  73. * @class module:barejs/polyfill/Intl.NumberFormat
  74. * @extends module:barejs/polyfill/Intl~Format
  75. */
  76. function NumberFormat( _locales, _options )
  77. {
  78. Format.call( this, _locales, new NumberFormatOptions( Object( _options ) ) );
  79. }
  80. NumberFormat.prototype = Object.create( Format.prototype,
  81. /** @lends module:barejs/polyfill/Intl.NumberFormat# */
  82. {
  83. constructor: { writable: true, value: NumberFormat },
  84. format:
  85. {
  86. enumerable: true,
  87. value: function format( _value )
  88. {
  89. if ( _value === null || typeof _value === "undefined" )
  90. return "";
  91. var value = +_value;
  92. var prefix = "";
  93. var suffix = "";
  94. if ( isNaN( value ) )
  95. return "NaN";
  96. switch( this._options.style )
  97. {
  98. case "percent":
  99. value *= 100;
  100. suffix = "%";
  101. break;
  102. case "currency":
  103. prefix = this._options.currency + nbsp;
  104. break;
  105. }
  106. // We're currently ignoring min/max decimal places (which is bad).
  107. return prefix + value.toLocaleString() + suffix;
  108. }
  109. }
  110. } );
  111. return NumberFormat;
  112. }( require( "./Format" ) ) );
  113. </code></pre>
  114. </article>
  115. </section>
  116. </div>
  117. <nav>
  118. <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-barejs.html">barejs</a></li><li><a href="module-barejs_polyfill.html">barejs/polyfill</a></li><li><a href="module-barejs_polyfill_Intl.html">barejs/polyfill/Intl</a></li></ul><h3>Classes</h3><ul><li><a href="module-barejs.decl.html">decl</a></li><li><a href="module-barejs.decl-Enum.html">Enum</a></li><li><a href="module-barejs.decl-Interface.html">Interface</a></li><li><a href="module-barejs.decl-SpecialType.html">SpecialType</a></li><li><a href="module-barejs.Destroyable.html">Destroyable</a></li><li><a href="module-barejs.EventArgs.html">EventArgs</a></li><li><a href="module-barejs.Evented.html">Evented</a></li><li><a href="module-barejs.Evented-EventedHandle.html">EventedHandle</a></li><li><a href="module-barejs.Exception.html">Exception</a></li><li><a href="module-barejs_polyfill.Array.html">Array</a></li><li><a href="module-barejs_polyfill.Date.html">Date</a></li><li><a href="module-barejs_polyfill.EntryStore.html">EntryStore</a></li><li><a href="module-barejs_polyfill.EntryStore.Iterator.html">Iterator</a></li><li><a href="module-barejs_polyfill.Function.html">Function</a></li><li><a href="module-barejs_polyfill.Map.html">Map</a></li><li><a href="module-barejs_polyfill.Map-MapIterator.html">MapIterator</a></li><li><a href="module-barejs_polyfill.Math.html">Math</a></li><li><a href="module-barejs_polyfill.Number.html">Number</a></li><li><a href="module-barejs_polyfill.Object.html">Object</a></li><li><a href="module-barejs_polyfill.Promise.html">Promise</a></li><li><a href="module-barejs_polyfill.Set.html">Set</a></li><li><a href="module-barejs_polyfill.Set-SetIterator.html">SetIterator</a></li><li><a href="module-barejs_polyfill.String.html">String</a></li><li><a href="module-barejs_polyfill.Symbol.html">Symbol</a></li><li><a href="module-barejs_polyfill.WeakMap.html">WeakMap</a></li><li><a href="module-barejs_polyfill.WeakSet.html">WeakSet</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat.html">DateTimeFormat</a></li><li><a href="module-barejs_polyfill_Intl.DateTimeFormat-DateTimeFormatOptions.html">DateTimeFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat.html">NumberFormat</a></li><li><a href="module-barejs_polyfill_Intl.NumberFormat-NumberFormatOptions.html">NumberFormatOptions</a></li><li><a href="module-barejs_polyfill_Intl-Format.html">Format</a></li></ul>
  119. </nav>
  120. <br class="clear">
  121. <footer>
  122. Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a> on Wed Oct 03 2018 15:59:34 GMT+0200 (W. Europe Daylight Time)
  123. </footer>
  124. <script> prettyPrint(); </script>
  125. <script src="scripts/linenumber.js"> </script>
  126. </body>
  127. </html>