UNPKG

@phensley/cldr-core

Version:
108 lines 4.66 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; }; return function (d, b) { extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); var numbers_1 = require("../../types/numbers"); var isInteger = (function (n) { return typeof n === 'number' && isFinite(n) && Math.floor(n) === n; }); var NumberingSystem = /** @class */ (function () { function NumberingSystem(name, symbols, minimumGroupingDigits, primaryGroupingSize, secondaryGroupingSize) { this.name = name; this.symbols = symbols; this.minimumGroupingDigits = minimumGroupingDigits; this.primaryGroupingSize = primaryGroupingSize; this.secondaryGroupingSize = secondaryGroupingSize; } return NumberingSystem; }()); exports.NumberingSystem = NumberingSystem; var DecimalNumberingSystem = /** @class */ (function (_super) { __extends(DecimalNumberingSystem, _super); function DecimalNumberingSystem(name, digits, symbols, minimumGroupingDigits, primaryGroupingSize, secondaryGroupingSize) { var _this = _super.call(this, name, symbols, minimumGroupingDigits, primaryGroupingSize, secondaryGroupingSize) || this; _this.digits = digits; return _this; } DecimalNumberingSystem.prototype.formatString = function (n, groupDigits, minInt) { if (minInt === void 0) { minInt = 1; } if (!groupDigits && isInteger(n)) { return this._fastFormatDecimal(String(n), minInt); } return this._formatDecimal(new numbers_1.StringDecimalFormatter(), n, groupDigits, minInt); }; // TODO: future merging of internal number formatting code into this module // format<R>(formatter: NumberFormatter<R>, n: DecimalArg, groupDigits: boolean = false, minInt: number = 1): R { // const f = formatter.formatter(this.symbols.decimal, groupDigits ? this.symbols.group : ''); // return this._formatDecimal(f, n, groupDigits, minInt); // } // formatPattern<R>(formatter: NumberFormatter<R>, pattern: NumberPattern, n: DecimalArg, // groupDigits: boolean, currencySymbol: string, percentSymbol: string, minInt: number): R { // return {} as R; // } DecimalNumberingSystem.prototype._formatDecimal = function (f, n, groupDigits, minInt) { if (minInt === void 0) { minInt = 1; } var d = numbers_1.coerceDecimal(n); var group = groupDigits ? this.symbols.group : ''; d.format(f, this.symbols.decimal || '.', group, minInt, this.minimumGroupingDigits, this.primaryGroupingSize, this.secondaryGroupingSize, this.digits); return f.render(); }; DecimalNumberingSystem.prototype._fastFormatDecimal = function (n, minInt) { var r = ''; var dg = this.digits; var len = n.length; for (var i = 0; i < len; i++) { var c = n.charCodeAt(i); switch (c) { case 48 /* DIGIT0 */: case 49 /* DIGIT1 */: case 50 /* DIGIT2 */: case 51 /* DIGIT3 */: case 52 /* DIGIT4 */: case 53 /* DIGIT5 */: case 54 /* DIGIT6 */: case 55 /* DIGIT7 */: case 56 /* DIGIT8 */: case 57 /* DIGIT9 */: r += dg[c - 48 /* DIGIT0 */]; break; } } // Left pad zeros if minimum integer digits > formatted length var diff = minInt - r.length; if (diff > 0) { var p = ''; while (diff-- > 0) { p += dg[0]; } return p + r; } return r; }; return DecimalNumberingSystem; }(NumberingSystem)); exports.DecimalNumberingSystem = DecimalNumberingSystem; var INTERNAL_SYMBOLS = { decimal: '.', group: ',', list: ';', percentSign: '%', plusSign: '+', minusSign: '-', exponential: 'E', currencyDecimal: '.', currencyGroup: ',', superscriptingExponent: '×', perMille: '‰', infinity: '∞', nan: 'NaN', timeSeparator: ':' }; exports.INTERNAL_NUMBERING = new DecimalNumberingSystem('internal', '0123456789'.split(''), INTERNAL_SYMBOLS, 1, 3, 3); //# sourceMappingURL=decimal.js.map